Access to pixels image

New to FreeBASIC? Post your questions here.
VANYA
Posts: 1374
Joined: Oct 24, 2010 15:16
Location: Ярославль
Contact:

Access to pixels image

Postby VANYA » May 28, 2013 15:03

Why is the replacement pixels , two right square of the same color?

Code: Select all

#Include "fbgfx.bi"
Using FB

Screen 17,32

Dim As Any Ptr img = ImageCreate(200,200)
Dim As Ubyte ptr p
ImageInfo img,,,,,p

Line img,-(99,99),&hff,BF
Line img,(100,100)-(200,200),&hff00,BF
Line img,(100,0)-(200,99),&hffFF,BF
Line img,(0,100)-(99,200),&hff0000,BF

Put(0,0),img
Sleep(1000)

Do
  For y As Integer = 0 To 200*200*4-1 Step 4
   p[y] += 1
  Next 
  ScreenLock
  Cls
  Put(0,0),img
  ScreenUnLock
  Sleep(10)
Loop Until MultiKey(SC_ESCAPE)
I3I2UI/I0
Posts: 90
Joined: Jun 03, 2005 10:39
Location: Germany

Re: Access to pixels image

Postby I3I2UI/I0 » May 28, 2013 19:01

&hff00 -> p[y] += 1 -> &hff01 = green
&hffFF -> p[y] += 1 -> &hff00 = green, the same color.
counting_pine
Site Admin
Posts: 6225
Joined: Jul 05, 2005 17:32
Location: Manchester, Lancs

Re: Access to pixels image

Postby counting_pine » May 28, 2013 19:18

This may not help you, but I have a couple of things to say:
You're making some assertions about the pitch of the image. These may improve performance, but I think it would also be worth fetching the pitch with ImageInfo, and Asserting that it is what you expect it to be.
You should also free the image when you are done with it, by using ImageDestroy
VANYA
Posts: 1374
Joined: Oct 24, 2010 15:16
Location: Ярославль
Contact:

Re: Access to pixels image

Postby VANYA » May 28, 2013 19:36

I3I2UI/I0 wrote:&hff00 -> p[y] += 1 -> &hff01 = green
&hffFF -> p[y] += 1 -> &hff00 = green, the same color.


Why green?

y - Red
y+1 - Green
y+2 - Blue
y+3 - Alpha

Or i do not understand something?

P.S. Thanks for the answers
anonymous1337
Posts: 5494
Joined: Sep 12, 2005 20:06
Location: California

Re: Access to pixels image

Postby anonymous1337 » May 28, 2013 20:36

My guess is that they're not the same, just very close. When &FF gets one added, since it's a byte, it overflows and becomes &00. At this point, the other color value, which was &00, had one added, and should now be &01.

0xFF + 0x01 overflows into 0x00.
0x00 + 0x01 equals 0x01.

The colors will be off by 0x01 from thereon, except when an overflow occurs and the color values for that byte suddenly "reset".

Rinse and repeat.
Last edited by anonymous1337 on May 28, 2013 20:40, edited 1 time in total.
anonymous1337
Posts: 5494
Joined: Sep 12, 2005 20:06
Location: California

Re: Access to pixels image

Postby anonymous1337 » May 28, 2013 20:39

Looks like I was correct:

Code: Select all

#Include "fbgfx.bi"
Using FB

Screen 17,32

Dim As Any Ptr img = ImageCreate(200,200)
Dim As Ubyte ptr p
ImageInfo img,,,,,p

Line img,(100,100)-(200,200),&h0000ff00,BF
Line img,(100,0)-(200,99),&h0000ffff,BF

Put(0,0),img
Sleep

Do
  For y As Integer = 0 To 200*200*4-1 Step 4
   p[y] += 1
  Next
  Put(0,0),img, pset
 
  var color1 = point(150, 15, img)
  var color2 = point(150, 150, img)
  locate 1, 1
  print "Color1: " & color1
  print "Color2: " & color2
 
  Sleep(150)
Loop Until MultiKey(SC_ESCAPE)
anonymous1337
Posts: 5494
Joined: Sep 12, 2005 20:06
Location: California

Re: Access to pixels image

Postby anonymous1337 » May 28, 2013 20:47

VANYA wrote:
I3I2UI/I0 wrote:&hff00 -> p[y] += 1 -> &hff01 = green
&hffFF -> p[y] += 1 -> &hff00 = green, the same color.


Why green?

y - Red
y+1 - Green
y+2 - Blue
y+3 - Alpha

Or i do not understand something?

P.S. Thanks for the answers
Those are the channels. You're using a byte pointer and changing the value of that byte. If you did p += 1 (pointer addition) rather than p[y] += 1 (changing the value at the address the pointer points to), it'd be a different story.
gothon
Posts: 224
Joined: Apr 11, 2011 22:22

Re: Access to pixels image

Postby gothon » May 28, 2013 20:58

VANYA wrote:
I3I2UI/I0 wrote:&hff00 -> p[y] += 1 -> &hff01 = green
&hffFF -> p[y] += 1 -> &hff00 = green, the same color.


Why green?

y - Red
y+1 - Green
y+2 - Blue
y+3 - Alpha

Or i do not understand something?

P.S. Thanks for the answers


32bit color values in memory are layed out in groups of 4 bytes, each byte representing a different component of the color. Because you are using a 'Ubyte ptr' with a step size of 4, your loop is iterating over every 4th byte of memory which is where the blue component is stored.

Colors are &hAARRGGBB where AA is alpha, RR is red, GG is green, and BB is blue. Because of the endianess the low order byte comes first in memory and blue is actually the first byte: (blue) (green) (red) (alpha) (blue2) ...

Cyan is &h0000ffff or (255) (255) (0) (0)
If we have a Ubyte variable representing the blue component, 255 + 1 = 0 because of overflow and we get (0) (255) (0) (0) = &h0000ff00 = green.

Its seems you are thinking reversed blue/red. FB internal formats seem to be red/blue reversed relative to other graphics systems (VB/OpenGL), I had to correct for this when making VoxelGFX.

p[y] - Blue
p[y+1] - Green
p[y+2] - Red
p[y+3] - Alpha
fxm
Posts: 9947
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Access to pixels image

Postby fxm » May 28, 2013 22:06

Code: Select all

#Include "fbgfx.bi"
Using FB

Screen 17,32

Dim As Any Ptr img = ImageCreate(200,200)
Dim As Ubyte ptr p
ImageInfo img,,,,,p

Line img,-(99,99),&hff,BF
Line img,(100,100)-(200,200),&hff00,BF
Line img,(100,0)-(200,99),&hff80,BF
Line img,(0,100)-(99,200),&hff0000,BF
Put(0,0),img,pset
Sleep(1000)

Do
  For y As Integer = 0 To 200*200*4-1 Step 4
    p[y] += 1
  Next
  Put(0,0),img,pset
  Sleep(10)
Loop Until MultiKey(SC_ESCAPE)
D.J.Peters
Posts: 8179
Joined: May 28, 2005 3:28
Contact:

Re: Access to pixels image

Postby D.J.Peters » May 28, 2013 22:09

WIN GUI API = BGR
FreeBASIC gfx = RGBA
OpenGL supports GL_RGBA and GL_BGRA (and of course GL_RGB also)

First letter "B" or "R" is allways the lowest address in memory

Joshy
gothon
Posts: 224
Joined: Apr 11, 2011 22:22

Re: Access to pixels image

Postby gothon » May 28, 2013 23:55

D.J.Peters wrote:WIN GUI API = BGR
FreeBASIC gfx = RGBA
OpenGL supports GL_RGBA and GL_BGRA (and of course GL_RGB also)

First letter "B" or "R" is allways the lowest address in memory

Joshy


If the first letter is the lowest address in memory and FBGFX is RGBA then why is blue apparently at the lowest memory address instead of red?
fxm
Posts: 9947
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Access to pixels image

Postby fxm » May 29, 2013 7:18

In memory, the format is : &hAARRGGBB
Consequently the first byte address corresponds to the blue.
(the keyword RGBA(red, green, blue, alpha) returns an unsigned integer in the format &hAARRGGBB)

Code: Select all

#Include "fbgfx.bi"
Using FB

Screen 17,32

Dim As Any Ptr img = ImageCreate(100,100)
Dim As Ubyte ptr p
ImageInfo img,,,,,p

Line img,-(99,99),&hff,BF
Put(0,0),img,pset
Sleep(1000)

Do
  For y As Integer = 0 To 100*100*4-1 Step 4 ' format in memory: &hAARRGGBB => first byte address = blue
    p[y] += 1
  Next
  Put(0,0),img,pset
  Sleep(10)
Loop Until MultiKey(SC_ESCAPE)
VANYA
Posts: 1374
Joined: Oct 24, 2010 15:16
Location: Ярославль
Contact:

Re: Access to pixels image

Postby VANYA » May 29, 2013 10:27

Thank you all, it is now clear.

Return to “Beginners”

Who is online

Users browsing this forum: No registered users and 3 guests