parse an image file

Blitz3D Forums/Blitz3D Beginners Area/parse an image file

Doggie(Posted 2010) [#1]
I need some help
I'm trying to get the color content from an image and convert it to
a larger grid where i will draw a rectangle of the correct color in the correct position. My image is 80wide by 70 high and my grid is 400by 350
with 10x10 grid.
Case btn002;PREV
         If Frame>1 Then Frame=Frame-1
         SendMessage( lbl002, "LM_SETTEXT", "Frame #:  " + Frame )
         TinyImage=LoadImage (imagename$ +(Frame) +".bmp")
         DrawBlock TinyImage,650,120
         Flip
         SetBuffer ImageBuffer(TinyImage)
         For x=0 To 80 Step 2
         For y=0 To 70 Step 2
          zit=zit+1
          GetColor x,y
          clr+(zit)=Color ColorRed(zit),ColorGreen(zit),ColorBlue(zit)
         Next
         Next

       
        SetBuffer ImageBuffer(TestImage)
For x=0 To 400 Step 10
For y=0 To 350 Step 10
Color ColorRed(clr+(zit)),ColorGreen(clr+(zit)),ColorBlue(clr+(zit))
        Rect x*10,y*10,10,10,0
        SetBuffer ImageBuffer(TinyImage)

Next
Next




obviously doesn't work but would like some input on the correct logic I need. Thanks.


Midimaster(Posted 2010) [#2]
I don't know, where to start. The whole code is a catastrophy:

You cannot use COLOR() like this. And also not COLORRED().

Color() returns no values. And COLORRED accept no parameters. The normal way is:

GetColor X,Y
ColorR=ColorRed()
ColorG=....


In your context I would suggest to add a Array Pixel(X,Y,C), where X is the X-coordinate, Y the Y-Coordinate and C the 3 Aspects of Color R, G and B

Now you can save the red part of a pixel at (15,20) in...

Pixel(15,20,R)=ColorRed()


So you can translate the whole source picture into an array

         For x=0 To 40 
         	For y=0 To 35 
          		GetColor x*2,y*2
          		Pixel(x,y,R)= ColorRed()
			Pixel(x,y,G)=ColorGreen()
			Pixel(x,y,B)=ColorBlue()
         	Next
         Next


To step only every second pixel, there is no need to do this in the FOR-statement, but do it in the GETCOLOR()-coordinates

The way back is now to set the color in the target-picture related to the array values:

        SetBuffer ImageBuffer(TestImage)
		For x=0 To 40
			For y=0 To 35
			    Color Pixel(x,y,R), Pixel(x,y,G), Pixel(x,y,B)
			    Rect x*10,y*10,10,10,1
			Next 
		Next


Also here, no need of a STEP parameter. The RECT-statement must have a "1"-flag at the end to get filled rectangles

And set the Buffer to BACKBUFFER() before you want to draw something on the screen.

Now here is the whole working code:
Graphics 800,600
TestImage=CreateImage(400,400)
Dim Pixel%(40,35,3)
R=1
G=2
B=3
         TinyImage=LoadImage ( ImageName + (Frame) +".bmp")
         SetBuffer BackBuffer()

         DrawImage TinyImage,650,120
         Flip
         SetBuffer ImageBuffer(TinyImage)
         For x=0 To 40 
         	For y=0 To 35 
          		GetColor x*2,y*2
          		Pixel(x,y,R)= ColorRed()
			Pixel(x,y,G)=ColorGreen()
			Pixel(x,y,B)=ColorBlue()
         	Next
         Next

       
        SetBuffer ImageBuffer(TestImage)
		For x=0 To 40
			For y=0 To 35
			    Color Pixel(x,y,R), Pixel(x,y,G), Pixel(x,y,B)
			    Rect x*10,y*10,10,10,1
			Next 
		Next
      SetBuffer BackBuffer()
      DrawImage TestImage,0,0
		Flip



Doggie(Posted 2010) [#3]
Thank you for the help. I really appreciate the time you took to explain some things for me. It helps a lot.

DOG


Midimaster(Posted 2010) [#4]
The difference between Basic an C is the fact, that you can test the code immediately in the moment you wrote it.

My suggestion:

Do not write too much code line, but test the code after each line and look, what happens.

Sometime it will not start with an error message, because you wrote the wrong syntax. But now you know the line, the error is in.

Sometimes it will run, but not do the thing you expected. In this case you should use DEBUGLOG to test the contect of a variable (wether it is as expected), etc...

Using this way you willl be sure, not to get a couple of errors, where you have no chance to find the reason for the behavior.

Do not write combinations of steps! Means: First try to write the loupe function in a stand-alone program. Reduce the necessary code to a minimum for this. no IFs, no SendMessage(), etc... At the end, when it is working perfect, integrate it into your main code


Doggie(Posted 2010) [#5]
excellent suggestions. I did that very thing when I added your code into my program since it just wouldn't work even tho it worked fine alone. Finally figured out that the variables "r" "g" and "b" were being used in other functions but didn't cause a crash error just skewed the results in the new code. A quick change of variable names got it working. Sometimes I get lucky and things work the first time I write 'em but what you said is more often the case.