Wow, what's wrong here ? (Source included)

BlitzMax Forums/BlitzMax Programming/Wow, what's wrong here ? (Source included)

dw817(Posted 2015) [#1]
This is NOT a good sign. I am getting =3= different images from loading the same image twice ! Is this a gross error on the part of Blitzmax ... or what the heck is it ?

Strict
Local i,j,r,g,b
Graphics 1024,768
SetBlend solidblend
Global pic:TPixmap=LoadPixmap("frame.png")
Global img:TImage=LoadImage("frame.png")
DrawImage img,0,0
DrawPixmap pic,48,0
img=CreateImage(96,48)
GrabImage img,0,0
pic=LockImage(img)
For i=0 To 47
  For j=0 To 95
    getpixel pic,j,i,r,g,b
    SetColor r,g,b
    DrawRect j*8,128+i*8,7,7
  Next
Next
UnlockImage(img)
Flip
WaitKey

Function getpixel(p:TPixmap,x,y,r Var,g Var,b Var)
  Local c=ReadPixel(p,x,y)
	r=(c&$ff0000)Shr 16
	g=(c&$ff00)Shr 8
	b=c&$ff
EndFunction


Find FRAME.PNG HERE:



Floyd(Posted 2015) [#2]
It's some kind of filtering/smoothing/whatever.

If the image is loaded with all flags zero then it looks like the pixmap, which is presumably loaded as "raw data".

Global img:TImage=LoadImage("frame.png", 0)



Derron(Posted 2015) [#3]
If you remove the "setblend solidblend" from the begin, results of loadimage(uri, defaultflag)and loadpixmap(uri) are the same (it is in "alphablend" mode then).

Nonetheless it is even wrong then ... which is visible in the darker green dots left and right of the center part.
Why? because of the "GrabImage"

Replace it with "pic = GrabPixmap(0,0,96,48)" and then the pixmap part/right side is correct). Now doing both: LoadImage(uri, 0) and GrabPixmap() will lead to a correct check.

Another option, is to have "CreateImage()" use the right flags :-)

your code adjusted:
Strict
Local i,j,r,g,b
Graphics 1024,768
SetBlend solidblend
Global pic:TPixmap=LoadPixmap("loadimage.png")
'appended flag = 0, to avoid smoothing
Global img:TImage=LoadImage("loadimage.png", 0)
DrawImage img,0,0
DrawPixmap pic,48,0

'a) use grabimage: (with flag = 0)
img=CreateImage(96,48, ,0)
GrabImage img,0,0
pic=LockImage(img)

'b) use grabpixmap
'pic = GrabPixmap(0,0,96,48)

For i=0 To 47
  For j=0 To 95
    getpixel pic,j,i,r,g,b
    SetColor r,g,b
    DrawRect j*8,128+i*8,7,7
  Next
Next
UnlockImage(img)
Flip
WaitKey

Function getpixel(p:TPixmap,x,y,r Var,g Var,b Var)
  Local c=ReadPixel(p,x,y)
	r=(c&$ff0000)Shr 16
	g=(c&$ff00)Shr 8
	b=c&$ff
EndFunction



bye
Ron


dw817(Posted 2015) [#4]
Floyd (CaptainPool):

* I thought zero was the default, I see now it apparently isn't. I'll have to ensure that I force a zero when I want to maintain perfect pixels for reading later.

For both commands, loadimage and createimage.

* Derron (Ron):

Trying out the code you wrote, it does run correctly now.

Good advice and code - all, thank you. But now this brings up another question. Are images dithered even set to scale 1,1 ?

Why on Earth would BlitzMAX do this ? At a scale of 1,1 no image should be dithered. Less than or greater than, yes, but not if one-pixel across and down truly is one-pixel across and down.

Even SolidBlend should be just that, a blend of solid pixels, at a scale of 1,1.

Now I noticed this does NOT happen with normal images, say one that has brown fade to green, etc. But it most definitely does occur when you go from solid red to another solid color.

I am using these as masks to build a complex frame.

Red appears black, white lightens the area beneath, green darkens the area beneath. Blue (were it present) is solid color chosen by configuration.

Drawpixmap does indeed render it correctly, but it also has the added disadvantage that it is no affected by scale, say scale 2,2.

Drawpixmap maintains its scale of 1,1 and does not double the pixels, either solid or dithered.

I also made this new routine to read pixel colors. Does anyone think it is better, faster, more accurate, worse, less accurate, or about the same as the one I used above ?

Another problem I face is that devil alpha. Is it ever recorded on the screen ? If so, is it ever less than 255 and greater than 0 ?

Function getpixel(p:TPixmap,x,y,r Var,g Var,b Var)
  Local c=(PixmapWidth(p)*y+x)*3
  r=p.pixels[c]
  g=p.pixels[c+1]
  b=p.pixels[c+2]
EndFunction


Reading pixels is very important when you are making graphic editing software. I still think BlitzMAX has an error, at a scale of 1,1. What is read in should truly display out with no artefacts.

Now, here is another puzzle, why does createpixmap() and grabpixmap() yield different results than just loadpixmap() ?

Strict
Local i,j,r,g,b
Graphics 1024,768
SetBlend solidblend
Global img:TImage=LoadImage("system\frame.png",0)
Global pic:TPixmap=LoadPixmap("system\frame.png"),pic2:TPixmap

DrawImage img,0,0
DrawPixmap pic,48,0
Flip
pic2=CreatePixmap(96,48,pf_rgb888)
pic2=GrabPixmap(0,0,96,48)
For i=0 To 47
  For j=0 To 95
    getpixel pic2,j,i,r,g,b
    SetColor r,g,b
    DrawRect j*8,i*8+128,7,7
  Next
Next
Flip
WaitKey

Function getpixel(p:TPixmap,x,y,r Var,g Var,b Var)
  Local c=(PixmapWidth(p)*y+x)*3
  r=p.pixels[c]
  g=p.pixels[c+1]
  b=p.pixels[c+2]
EndFunction


The results, garbage pixels are read ! I know that I can use readpixel(), but I'm curious to know why this doesn't work.

Because if you change the "for j=0 to 95" to "for j=0 to 47" and the "getpixel pic2,j,i,r,g,b" to "getpixel pic,j,i,r,g,b" you'll find that - it does work then.

What's going on HERE ?


Derron(Posted 2015) [#5]
You cannot read "alpha" from a grabpixmap()-created pixmap.

Your new getpixel does not return the same results than your old one.

When reading "rgb" you will have to take the original format into consideration (rgba versus argb ... and others).

Your code contains this:

pic2=CreatePixmap(96,48,pf_rgba8888)
pic2=GrabPixmap(0,0,96,48)


so the second line assigns a new pixmap to "pic2" - making the "createPixmap()" function call useless.


But more important: you "flip" before grabbing, which somehow results in grabbing the black screen.
Removing the first "flip" creates again a working result.


Edit: I cannot explain why the images are filtered even when having a scale of 1,1 - maybe it is something with OGL/DX ... because it is some "2D in 3D" scenery.

bye
Ron


dw817(Posted 2015) [#6]
Hi Ron:

I did not know that grabpixmap also did create. I think you need both create and grab with "timage" however.

* Here's a question for you. Is it possible to retrieve ALPHA from a screen with image capture ? Or is this a write-only value for dithering pixels ?

As for the dithering problem, I can make a routine that forces 0 display mode if the scale is set to 1,1 and change it to default display if scale is set to any other value.

A kludge, but it should work.


Derron(Posted 2015) [#7]
If you want to retrieve alpha, you will have to do a software render - or render 2 texture.


bye
Ron


dw817(Posted 2015) [#8]
Is that only for Blitz3D, Ron ? If not, could you please post a working example in BlitzMAX ?