Is that the fastest way to image grayscale?

Everything else that doesn't fall into one of the other PB categories.
hellhound66
Enthusiast
Enthusiast
Posts: 119
Joined: Tue Feb 21, 2006 12:37 pm

Post by hellhound66 »

Removed.
Last edited by hellhound66 on Wed Mar 19, 2008 11:29 pm, edited 1 time in total.
dige
Addict
Addict
Posts: 1247
Joined: Wed Apr 30, 2003 8:15 am
Location: Germany
Contact:

Post by dige »

@otto: Wow!! :shock: I've got crazy 90ms .. thats amazing fast! Thank you very much! :D
otto
New User
New User
Posts: 5
Joined: Mon Sep 26, 2005 11:13 pm

Post 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
...
firace
Addict
Addict
Posts: 899
Joined: Wed Nov 09, 2011 8:58 am

Re: Is that the fastest way to image grayscale?

Post 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 
Fred
Administrator
Administrator
Posts: 16619
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: Is that the fastest way to image grayscale?

Post by Fred »

Did you run with debugger off ?
firace
Addict
Addict
Posts: 899
Joined: Wed Nov 09, 2011 8:58 am

Re: Is that the fastest way to image grayscale?

Post by firace »

Fred wrote:Did you run with debugger off ?
:oops: Sorry - That was it indeed, thanks! 133 ms now :D
User avatar
Michael Vogel
Addict
Addict
Posts: 2666
Joined: Thu Feb 09, 2006 11:27 pm
Contact:

Re: Is that the fastest way to image grayscale?

Post 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
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3870
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: Is that the fastest way to image grayscale?

Post 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.
Windows (x64)
Raspberry Pi OS (Arm64)
User avatar
Michael Vogel
Addict
Addict
Posts: 2666
Joined: Thu Feb 09, 2006 11:27 pm
Contact:

Re: Is that the fastest way to image grayscale?

Post 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%)
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3870
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: Is that the fastest way to image grayscale?

Post by wilbert »

Michael Vogel wrote:Did some quick tests now, wilbert - on my computer, the following timing can be seen:
Thanks for the info :)
Windows (x64)
Raspberry Pi OS (Arm64)
Post Reply