Page 2 of 2

Posted: Sun Mar 09, 2008 1:39 am
by hellhound66
Removed.

Posted: Mon Mar 10, 2008 8:29 am
by dige
@otto: Wow!! :shock: I've got crazy 90ms .. thats amazing fast! Thank you very much! :D

Posted: Mon Mar 10, 2008 7:26 pm
by otto
@Michael Vogel: Thank you again!


@Dige

If the number of pixels is uneven the last pixel must be
processed separately. A bitmap with only one pixel is treated by
the routine as it would have two pixels.
In my test bitmaps created with createDIBSection() / createImage()
were curiously always aligned to a 65536 bytes boundary.

Code: Select all

    bmp.BITMAP

    For t=0 To 5000
        hbmp=CreateImage(t, 16,16, 4)
        n=GetObject_(hbmp, SizeOf(BITMAP), bmp)
        ; Debug bmp\bmBits
        If bmp\bmBits&$FFFF
            Debug bmp\bmBits&$FFFF ; output if it's not aligned to 65536 bytes
        EndIf
        If bmp\bmBits=0
            Debug "no bits"
        EndIf
    Next
    
    Debug "end"
    End

When a bitmap is not aligned to 8 bytes the first and
perhaps (pixelCount&1=0) the last pixel have to be done
individual.


@hellhound:

Which opcodes can be saved? When this routine is run under a
core2 at 2.4Ghz it consumes 34ms. Only writing to memory
(comment out the lines in the loop until movq [esi],xmm0) takes
27ms on the same machine. So there are only 7ms left for the
real code.
But it would be interesting to compare integer against floating
point calculation.

You would have to:
1. convert 8 bit ints to 32 bit ints
2. convert 32 bit ints to floats
3. mulps
...

Re: Is that the fastest way to image grayscale?

Posted: Tue Feb 27, 2018 5:25 pm
by firace
Sorry to dig out this old thread, but why is dige's initial code (which he was already complaining was slow in 2008) now taking around 2700ms to run on my new, reasonably fast laptop?

According to dige, it was taking 594ms on PB 4.20 at the time...
Just curious!

Code: Select all

Procedure.b GrayScaleImg ( ImgID.l ); Works only with 32 Bit Images!!
  Protected Grey.l
  
  #lumared   = 3
  #lumagreen = 6
  #lumablue  = 1
  
  If Not IsImage( ImgID ) Or Not GetObject_(ImageID(ImgID), SizeOf(BITMAP), bmp.BITMAP) : ProcedureReturn #False : EndIf
  
  *ptr.RGBQUAD = bmp\bmBits
  Size = *ptr + ImageWidth(ImgID) * ImageHeight(ImgID) * 4 - 4
  
  Repeat
    Grey = (#lumared * *ptr\rgbRed&$FF + #lumagreen * *ptr\rgbGreen&$FF + #lumablue * *ptr\rgbBlue&$FF) / 10
    *ptr\rgbRed   = Grey
    *ptr\rgbGreen = *ptr\rgbRed
    *ptr\rgbBlue  = *ptr\rgbRed
    *ptr + 4
  Until *ptr > Size
  
  ProcedureReturn #True
EndProcedure


If CreateImage( 1, 4000, 4000, 32)
  time = ElapsedMilliseconds()
  GrayScaleImg( 1 )
  MessageRequester("", Str(ElapsedMilliseconds() - time), 0)
EndIf 

Re: Is that the fastest way to image grayscale?

Posted: Tue Feb 27, 2018 5:35 pm
by Fred
Did you run with debugger off ?

Re: Is that the fastest way to image grayscale?

Posted: Tue Feb 27, 2018 6:19 pm
by firace
Fred wrote:Did you run with debugger off ?
:oops: Sorry - That was it indeed, thanks! 133 ms now :D

Re: Is that the fastest way to image grayscale?

Posted: Tue Feb 27, 2018 7:34 pm
by Michael Vogel
Ooh, my notebook needs 650ms (debugger is off), what a pitty...

..so I did some changes to speed everything up a little bit - it only needs half the time now (but I was to lazy to check if the conversion still works):

Code: Select all

Procedure.b GrayScaleImg ( ImgID.l ); Works only with 32 Bit Images!!

	#lumared   = 77
	#lumagreen = 153
	#lumablue  = 26

	If Not IsImage( ImgID ) Or Not GetObject_(ImageID(ImgID), SizeOf(BITMAP), bmp.BITMAP) : ProcedureReturn #False : EndIf

	Structure Pixel
		StructureUnion
			RGB.RGBQUAD
			Mem.Long
		EndStructureUnion
	EndStructure

	*ptr.Pixel = bmp\bmBits
	Size = *ptr + ImageWidth(ImgID) * ImageHeight(ImgID) * 4 - 4

	Repeat
		*ptr\Mem\l=((#lumared * *ptr\RGB\rgbRed&$FF + #lumagreen * *ptr\RGB\rgbGreen&$FF + #lumablue * *ptr\RGB\rgbBlue&$FF) >> 8)*$10101
		*ptr + 4
	Until *ptr > Size

	ProcedureReturn #True
EndProcedure

If CreateImage( 1, 4000, 4000, 32)
	time = ElapsedMilliseconds()
	GrayScaleImg( 1 )
	MessageRequester("", Str(ElapsedMilliseconds() - time), 0)
EndIf

Re: Is that the fastest way to image grayscale?

Posted: Tue Feb 27, 2018 7:43 pm
by wilbert
A few years ago I created a grayscale module.
http://www.purebasic.fr/english/viewtop ... 18#p482418
Don’t know how it compares to the other solutions.

Re: Is that the fastest way to image grayscale?

Posted: Tue Feb 27, 2018 10:51 pm
by Michael Vogel
wilbert wrote:I created a grayscale module [...] how it compares to the other solutions.
Did some quick tests now, wilbert - on my computer, the following timing can be seen:
Initial code of this thread: 650ms (100%)
my optimized code: 260ms (40%)
your code (high quality): 170ms (26%)
your code (high speed): 150ms (23%)

Re: Is that the fastest way to image grayscale?

Posted: Wed Feb 28, 2018 6:25 am
by wilbert
Michael Vogel wrote:Did some quick tests now, wilbert - on my computer, the following timing can be seen:
Thanks for the info :)