Is that the fastest way to image grayscale?
-
- Enthusiast
- Posts: 119
- Joined: Tue Feb 21, 2006 12:37 pm
@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.
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
...
@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
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?
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!
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?
Did you run with debugger off ?
Re: Is that the fastest way to image grayscale?
Sorry - That was it indeed, thanks! 133 ms nowFred wrote:Did you run with debugger off ?
- Michael Vogel
- Addict
- Posts: 2666
- Joined: Thu Feb 09, 2006 11:27 pm
- Contact:
Re: Is that the fastest way to image grayscale?
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):
..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?
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.
http://www.purebasic.fr/english/viewtop ... 18#p482418
Don’t know how it compares to the other solutions.
Windows (x64)
Raspberry Pi OS (Arm64)
Raspberry Pi OS (Arm64)
- Michael Vogel
- Addict
- Posts: 2666
- Joined: Thu Feb 09, 2006 11:27 pm
- Contact:
Re: Is that the fastest way to image grayscale?
Did some quick tests now, wilbert - on my computer, the following timing can be seen:wilbert wrote:I created a grayscale module [...] how it compares to the other solutions.
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?
Thanks for the infoMichael Vogel wrote:Did some quick tests now, wilbert - on my computer, the following timing can be seen:
Windows (x64)
Raspberry Pi OS (Arm64)
Raspberry Pi OS (Arm64)