• Print

Author Topic: QB64 PNG Export Library v 1.1  (Read 1249 times)

SMcNeill

  • Hero Member
  • *****
  • Posts: 2612
    • Email
Re: QB64 PNG Export Library v 1.1
« Reply #60 on: February 18, 2013, 01:10:32 PM »
That stretching is where you run into the problem at.   You don't stretch at every pixel, just at the ones that rounding duplicates.
Think of it like this:

We're with a native 2x1 image.   That image is "AB"     (work with me here.  I know it's going to generate pixel values, so A is hex for color 10 and B is hex for color 11...)
Now we run a WINDOW (3X1).

With your for loop, you want to get 3 pixels, not 2...
Pass 1, we get the 1st window-pixel's value (which would be the 1st pixel) as it'd correspond to the A
Pass 2, we get the 2nd window-pixel's value (which would be the 1.5th pixel)...  with rounding down we're going to get the A again.  If we round up, we'd get the B.
Pass 3, we're going to get the 3rd window-pixel's value (which would be the 2nd pixel) -- B.

So our 2x1 image which was "AB" has now became a 3x1 image of "AAB" or "ABB".   We no longer have a perfect representation of what is on the screen, thus we have false artifacts in the image...

Your 800x600 example isn't going to stretch each pixel uniformly.  It's only going to duplicate some of the pixels.  If you want a perfect representation, then copy your image to a non-windowed screen and export it from there.

Make sense, what I'm saying?
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

Clippy

  • Hero Member
  • *****
  • Posts: 16446
  • I LOVE π = 4 * ATN(1)    Use the QB64 WIKI >>>
    • Pete's Qbasic Site
    • Email
Re: QB64 PNG Export Library v 1.1
« Reply #61 on: February 18, 2013, 01:15:47 PM »
The image is 800 X 600 squares with 15 colors. It is a way to work with larger images. The bitmap routine had no problem and neither should the PNG program.

Code: [Select]
i& = _LOADIMAGE("Big-bmp.bmp", 32)
SCREEN i&
PRINT _WIDTH, _HEIGHT
SLEEP
« Last Edit: February 18, 2013, 01:21:54 PM by Clippy »
QB64 WIKI: Main Page
Download Q-Basics Code Demo: Q-Basics.zip
Download QB64 BAT, IconAdder and VBS shortcuts: QB64BAT.zip
Download QB64 DLL files in a ZIP: Program64.zip

SMcNeill

  • Hero Member
  • *****
  • Posts: 2612
    • Email
Re: QB64 PNG Export Library v 1.1
« Reply #62 on: February 18, 2013, 01:31:39 PM »
Try your example with something a little more defined that circles....   Let's use simple lines:

Code: [Select]
DECLARE SUB FourBIT (x1%, y1%, x2%, y2%, Filename$)

SCREEN 12
CLS: RANDOMIZE TIMER
FOR i = 0 TO 639
    clr = i MOD 16
    LINE (i, 0)-(i, 479), clr
NEXT
SLEEP
WINDOW SCREEN(0, 0)-(799, 599) '800x600 image surface
FourBIT 0, 0, 799, 599, "BIG-BMP"
WINDOW


SUB FourBIT (x1%, y1%, x2%, y2%, Filename$) 'SCREEN 12 bitmap maker
'fullscreen takes about 8 seconds
DIM FileCOLORS%(1 TO 48)
DIM Colors4%(0 TO 15)

IF INSTR(UCASE$(Filename$), ".BMP") = 0 THEN
    Filename$ = RTRIM$(LEFT$(Filename$, 8)) + ".BMP"
END IF

FileTYPE$ = "BM"
Reserved1% = 0
Reserved2% = 0
OffsetBITS& = 118
InfoHEADER& = 40
PictureWIDTH& = (x2% - x1%) + 1
PictureDEPTH& = (y2% - y1%) + 1
NumPLANES% = 1
BPP% = 4
Compression& = 0
WidthPELS& = 3780
DepthPELS& = 3780
NumCOLORS& = 16

IF PictureWIDTH& MOD 8 <> 0 THEN
    ZeroPAD$ = SPACE$((8 - PictureWIDTH& MOD 8) \ 2)
END IF

ImageSIZE& = (((PictureWIDTH& + LEN(ZeroPAD$)) * PictureDEPTH&) + .1) / 2
FileSize& = ImageSIZE& + OffsetBITS&

OUT &H3C7, 0 'start at color 0
FOR n = 1 TO 48 STEP 3
    FileCOLORS%(n) = INP(&H3C9)
    FileCOLORS%(n + 1) = INP(&H3C9)
    FileCOLORS%(n + 2) = INP(&H3C9)
NEXT n

OPEN Filename$ FOR BINARY AS #1
'Header bytes
PUT #1, , FileTYPE$ '2 '1 to 2
PUT #1, , FileSize& '4
PUT #1, , Reserved1% 'should be zero  '2
PUT #1, , Reserved2% 'should be zero  '2
PUT #1, , OffsetBITS& '4
PUT #1, , InfoHEADER& '4
PUT #1, , PictureWIDTH& '4
PUT #1, , PictureDEPTH& '4
PUT #1, , NumPLANES% '2
PUT #1, , BPP% '2
PUT #1, , Compression& '4
PUT #1, , ImageSIZE& '4
PUT #1, , WidthPELS& '4
PUT #1, , DepthPELS& '4
PUT #1, , NumCOLORS& '4
PUT #1, , SigCOLORS& '4 '51 - 54

u$ = " " 'unused byte
FOR n% = 1 TO 46 STEP 3 'PUT as BGR order colors
    Colr$ = CHR$(FileCOLORS%(n% + 2) * 4)
    PUT #1, , Colr$
    Colr$ = CHR$(FileCOLORS%(n% + 1) * 4)
    PUT #1, , Colr$
    Colr$ = CHR$(FileCOLORS%(n%) * 4)
    PUT #1, , Colr$
    PUT #1, , u$ 'add Unused byte
NEXT n%

FOR y = y2% TO y1% STEP -1 'Place from bottom up
    FOR x = x1% TO x2% STEP 2 'nibble steps
        HiX = POINT(x, y): Colors4%(HiX) = 1 'added here
        LoX = POINT(x + 1, y): Colors4%(LoX) = 1
        HiNIBBLE$ = HEX$(HiX)
        LoNIBBLE$ = HEX$(LoX)
        HexVAL$ = "&H" + HiNIBBLE$ + LoNIBBLE$
        a$ = CHR$(VAL(HexVAL$))
        PUT #1, , a$
    NEXT x
    PUT #1, , ZeroPAD$
NEXT y

FOR n = 0 TO 15
    IF Colors4%(n) = 1 THEN SigCOLORS& = SigCOLORS& + 1
NEXT n
PUT #1, 51, SigCOLORS& 'new PUT

CLOSE #1
END SUB

Run this, and look at your screen.    It'll sleep on you before it saves it.   Click a key, let it save it.   

Does the image match what was on your screen??
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

Clippy

  • Hero Member
  • *****
  • Posts: 16446
  • I LOVE π = 4 * ATN(1)    Use the QB64 WIKI >>>
    • Pete's Qbasic Site
    • Email
Re: QB64 PNG Export Library v 1.1
« Reply #63 on: February 18, 2013, 02:25:59 PM »
The color values on the left look off, but that happens in Qbasic too. That would be something in the BMP file routine I gather.
QB64 WIKI: Main Page
Download Q-Basics Code Demo: Q-Basics.zip
Download QB64 BAT, IconAdder and VBS shortcuts: QB64BAT.zip
Download QB64 DLL files in a ZIP: Program64.zip

SMcNeill

  • Hero Member
  • *****
  • Posts: 2612
    • Email
Re: QB64 PNG Export Library v 1.1
« Reply #64 on: February 18, 2013, 02:38:21 PM »
It's where you're stretching the 640 lines we had originally in SCREEN 12 over to the WINDOW's 800 lines.  Zoom in and look REALLY close at the picture.   Every line should be the same width as each line was only 1 pixel wide.  By saving it as an 800 pixel image, some of those lines become 2 pixels wide.   And it's those extra pixel lines that are tossing the image off.   Take out the WINDOW and run it (or copy the image to a screen without a window), and give it a run.   Those colors won't be off then, and things will look as they do on your screen.
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

Clippy

  • Hero Member
  • *****
  • Posts: 16446
  • I LOVE π = 4 * ATN(1)    Use the QB64 WIKI >>>
    • Pete's Qbasic Site
    • Email
Re: QB64 PNG Export Library v 1.1
« Reply #65 on: February 18, 2013, 03:34:28 PM »
Yeah, but that is not anything that your PNG routine has to worry about.

It's kinda late to be going over the inadequacies of WINDOW. We can use _PUTIMAGE now.

Quote
17 Guests, 1 User
Users active in past 15 minutes:
Clippy
« Last Edit: February 20, 2013, 11:49:01 AM by Clippy »
QB64 WIKI: Main Page
Download Q-Basics Code Demo: Q-Basics.zip
Download QB64 BAT, IconAdder and VBS shortcuts: QB64BAT.zip
Download QB64 DLL files in a ZIP: Program64.zip

  • Print