• Print

Author Topic: IBTK Error with simple math  (Read 72 times)

SMcNeill

  • Hero Member
  • *****
  • Posts: 2414
    • Email
IBTK Error with simple math
« on: December 02, 2012, 07:57:36 AM »
I've been working a PNG export program, and I've ran into an issue with my ability to do simple math this morning...

How it should work:
We read the screen from top to bottom.  These are our scanlines, and each pixel point is 3 bytes.  (Red, Green, Blue)
At the start of each scanline we first place a 0.
And we do this for each line on the screen.

So, I've been working out the math on this, and for some reason  I'm not coming up with matching numbers.  Someone take a look and see what's wrong with this:

Code: [Select]
scanline = 3 * (_WIDTH + 1) + 1
total = (_HEIGHT + 1) * scanline
PRINT total
DIM filebuff(total) AS _UNSIGNED _BYTE

z = 0
FOR y = 0 TO _HEIGHT
    z = z + 1
    'filebuff(z) = 0
    z = z + 1
    FOR x = 0 TO _WIDTH
        'filebuff(z) = _RED(POINT(x, y))
        'filebuff(z + 1) = _GREEN(POINT(x, y))
        'filebuff(z + 2) = _BLUE(POINT(x, y))
        z = z + 3
    NEXT
NEXT
z = z - 3 ' account for the extra +3 at then end of the k loop
PRINT z
END
http://bit.ly/TextImage -- Library of QB64 code to manipulate text and images, as a BM library.
http://bit.ly/Color32 -- A set of color CONST for use in 32 bit mode, as a BI library.

http://bit.ly/DataToDrive - A set of routines to quickly and easily get data to and from the disk.  BI and BM files

Klangaroo

  • Full Member
  • ***
  • Posts: 202
  • Video Game Dad
    • ManFightDragon Indie Dev Blog
Re: IBTK Error with simple math
« Reply #1 on: December 02, 2012, 08:06:23 AM »
What is the colour depth of the screen? You haven't defined a screen image there at all.
Also please don't do that z, z+1, z+2 thing with you array. Use a 3D array like this
Code: [Select]
CONST Red = 0
CONST Green = 1
CONST Blue = 2

DIM SHARED myBytes(_WIDTH,_HEIGHT, 2) AS _UNSIGNED BYTE
FOR y = 0 to _HEIGHT
FOR x = 0 to _WIDTH
myBytes(x,y, Red) = _RED(blah)
myBytes(x,y, Green) = _GREEN(blah)
myBytes(x,y, Blue) = _BLUE(blah)
NEXT
NEXT

FOR BufferTheY = 0 TO _HEIGHT
FileBuffer$ = FileBuffer$ + "0"
FOR BufferTheX = 0 TO _WIDTH
FOR BufferTheZ = Red TO Blue
FileBuffer$ = FileBuffer$ + str$(ltrim$(rtrim$(MyBytes(BufferTheX, BufferTheY,BufferTheZ))
NEXT
NEXT
NEXT
 

Will that do? Obviously replace "blah" with whatever method you want to use to read from the screen, and make sure you use _RED32() etc if reading from a 32bit canvas.
« Last Edit: December 02, 2012, 08:16:50 AM by Klangaroo »
I make video games over at Man Fight Dragon and I have Twitter so do whatever really.

SMcNeill

  • Hero Member
  • *****
  • Posts: 2414
    • Email
Re: IBTK Error with simple math
« Reply #2 on: December 02, 2012, 08:22:10 AM »
I've got it.   It was simple math that was throwing it all out of whack.

Code: [Select]
DIM total AS _UNSIGNED LONG
DIM z AS _UNSIGNED LONG
SCREEN _NEWIMAGE(1280, 720, 32)
scanline = 3 * (_WIDTH + 1) + 1
total = (_HEIGHT + 1) * scanline
PRINT total, _HEIGHT, _WIDTH
DIM filebuff(total) AS _UNSIGNED _BYTE

z = 0
FOR y = 0 TO 720
    z = z + 1
    filebuff(z) = 0
    FOR x = 0 TO 1280
        'filebuff(z + 1) = _RED(POINT(x, y))
        'filebuff(z + 2) = _GREEN(POINT(x, y))
        'filebuff(z + 3) = _BLUE(POINT(x, y))
        z = z + 3
    NEXT
NEXT
PRINT z
END

Quote
What is the colour depth of the screen? You haven't defined a screen image there at all.
Also please don't do that z, z+1, z+2 thing with you array. Use a 3D array like this

Normally I wouldn't toss it all into one array like this, but its how PNG files want their compression routine to work.   A simple array,   FilleBuff(x), runs through their compress function and becomes CompBuff(x).   Data needs to be for each scanline on the screen -- 0 for start of line, and then Red, Green, Blue for each pixel till end of line.  And then we scan the next line down, from top to bottom, until we're finally finished.  :)

I'm thinking this is the simple fix I need.  My logic was just broken for a bit there and generating me extra lines in the array causing it to go out of bounds.  Thanks for the assist though.  It's appreciated.  :)
http://bit.ly/TextImage -- Library of QB64 code to manipulate text and images, as a BM library.
http://bit.ly/Color32 -- A set of color CONST for use in 32 bit mode, as a BI library.

http://bit.ly/DataToDrive - A set of routines to quickly and easily get data to and from the disk.  BI and BM files

  • Print