Reading an image pixel by pixel

Just starting out? Need help? Post your questions and find answers here.
Killswitch
Enthusiast
Enthusiast
Posts: 731
Joined: Wed Apr 21, 2004 7:12 pm

Reading an image pixel by pixel

Post 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
~I see one problem with your reasoning: the fact is thats not a chicken~
Hydrate
Enthusiast
Enthusiast
Posts: 436
Joined: Mon May 16, 2005 9:37 pm
Contact:

Post 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.
.::Image::.
User avatar
Kaeru Gaman
Addict
Addict
Posts: 4826
Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany

Post by Kaeru Gaman »

on windows, you could also use GetDIBits (have a look at the MSDN)
Linux should have appropriate API-functions, too...
oh... and have a nice day.
User avatar
Fluid Byte
Addict
Addict
Posts: 2336
Joined: Fri Jul 21, 2006 4:41 am
Location: Berlin, Germany

Post 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
Windows 10 Pro, 64-Bit / Whose Hoff is it anyway?
Killswitch
Enthusiast
Enthusiast
Posts: 731
Joined: Wed Apr 21, 2004 7:12 pm

Post by Killswitch »

Excellent, thanks guys.
~I see one problem with your reasoning: the fact is thats not a chicken~
Post Reply