Page 1 of 1

Reading an image pixel by pixel

Posted: Mon Mar 12, 2007 12:03 am
by Killswitch
I've been looking into this for a while now, but I'm not really sure how I'd accomplish it. What I'd like to do is be able to open an image, find out its width and height and then examine the RGB value of each pixel.

I'm only really interested in a single image format at the moment (any of: .gif,.jpeg,.bmp,.png) but if anyone has a more general solution that would be rather welcome!

Thanks

Posted: Mon Mar 12, 2007 12:19 am
by Hydrate
You could load the image using purebasic's built in decoder as you would a normal image, and then use the Point(x,y) function to find the colour for the iamge for each pixel, and edit it on there and then.

Posted: Mon Mar 12, 2007 12:23 am
by Kaeru Gaman
on windows, you could also use GetDIBits (have a look at the MSDN)
Linux should have appropriate API-functions, too...

Posted: Mon Mar 12, 2007 1:02 pm
by Fluid Byte
Is this any help?

Code: Select all

CreateImage(0,320,240) 

hdc = StartDrawing(ImageOutput(0)) 

IW = ImageWidth(0) : IH = ImageHeight(0)

For i=0 To IH-1
 	colref = i * 100 / (IH-1)

	Box(0,i,IW,1,RGB(155 + colref,colref,0))
Next

lpvBits = AllocateMemory(IW * IH * 4) 

bmi.BITMAPINFO
bmi\bmiHeader\biSize = SizeOf(BITMAPINFOHEADER) 
bmi\bmiHeader\biWidth = IW 
bmi\bmiHeader\biHeight = IH
bmi\bmiHeader\biPlanes = 1 
bmi\bmiHeader\biBitCount = 32 
bmi\bmiHeader\biCompression = #BI_RGB 

GetDIBits_(hdc,ImageID(0),0,IH,lpvBits,bmi,#DIB_RGB_COLORS)     

*pxData.LONG  = lpvBits

For i=1 To (IW * IH) 
	R = *pxData\l & $FF 
	G = *pxData\l >> 8 & $FF 
	B = *pxData\l >> 16 
	
	Noise = 30
	Result = Noise - Random(Noise * 2)
	
	R + Result : G + Result : B + Result
	
	If Result < 0
		If R < 0 : R = 0 : EndIf
		If G < 0 : G = 0 : EndIf
		If B < 0 : B = 0 : EndIf
	Else
		If R > 255 : R = 255 : EndIf
		If g > 255 : G = 255 : EndIf
		If B > 255 : B = 255 : EndIf
	EndIf
	
	*pxData\l = R | G << 8 | B << 16                 
	
	*pxData + 4 
Next 

SetDIBits_(hdc,ImageID(0),0,IH,lpvBits,bmi,#DIB_RGB_COLORS) 

FreeMemory(lpvBits) 

StopDrawing()          

OpenWindow(0,0,0,320,240,"Bitmap Manipulation",#WS_SYSMENU | #WS_CAPTION | 1) 
CreateGadgetList(WindowID(0)) 
ImageGadget(0,0,0,0,0,ImageID(0)) 

While WaitWindowEvent() ! 16 : Wend

Posted: Mon Mar 12, 2007 2:31 pm
by Killswitch
Excellent, thanks guys.