Jpg /Png export

Blitz3D Forums/Blitz3D Programming/Jpg /Png export

The beginnings of the exporter are included below. I've got a real stinking cold right now so I'm not exactly on top form. Please excuse any stupid errors!

I didn't realise quite how complex PNG is. I've got to implement LZ77 compression and CRC32 creation just to get a very basic PNG file created. This is something I will start doing tomorrow (if I get out of bed at all).

pngexporter.bb
;------------------------------------------------------------------------------

; Native Blitz 3D PNG exporter

; Version 0.01

; Copyright 2005 Stefan Holmes.

; You may use this file freely so long as the text "PNG exporter by Stefan
; Holmes" appears either in your application's 'help, about' or in the main
; documentation.

;------------------------------------------------------------------------------

Function pngExport(file$,img%)

    ;--------------------------------------------------------------------------
    ;
    ; ARGUMENTS:
    ;
    ; file$     : Full path and name of the file to be saved.
    ;
    ; image%    : Valid Blitz 3D image handle.
    ;--------------------------------------------------------------------------
    ;
    ; RETURNS:
    ;
    ; Not 0 : Success.
    ;
    ; 0     : Failure.
    ;--------------------------------------------------------------------------

    DeleteFile file$ 
    fh% = WriteFile(file$) : If fh%=0 Then Return 0

    Restore pngExporter_sig
    For i=1 To 8
        Read d%
        WriteByte(fh%,d%)
    Next

    ; IHDR chunk.
    pngExporterStartChunk(fh%,13,"IHDR")
    pngExporterWriteInt(fh%,ImageWidth(img%))   ; Width                 4 bytes
    pngExporterWriteInt(fh%,ImageHeight(img%))  ; Height                4 bytes
    WriteByte(fh%,8)                            ; Bitdepth              1 byte
    WriteByte(fh%,2)                            ; Colour type           1 byte
    WriteByte(fh%,0)                            ; Compression method    1 byte
    WriteByte(fh%,0)                            ; Filter method         1 byte
    WriteByte(fh%,0)                            ; Interlace method      1 byte  

    ; IEND chunk.
    pngExporterStartChunk(fh%,0,"IEND")
    WriteInt(fh%,0)


    CloseFile fh%

    Return fh%
    
End Function
.pngExporter_sig
Data  137,80,78,71,13,10,26,10


Function pngExporterStartChunk(filehandle%,length%,chunk$)
    pngExporterWriteInt(filehandle%,length%)
    pngExporterWriteStr(filehandle%,chunk$)
End Function


Function pngExporterWriteInt(filehandle%,value%)
    ; Taken from Starfox a.k.a David Dawkin's WriteMotorolaInt function
    ; available at <a href="http://www.blitzbasic.com/codearcs/codearcs.php?code=674" target="_blank">http://www.blitzbasic.com/codearcs/codearcs.php?code=674</a>
    ; Thanks, Starfox.
    WriteByte(filehandle%, value% Shr 24 And $FF)
    WriteByte(filehandle%, value% Shr 16 And $FF)
    WriteByte(filehandle%, value% Shr 8 And $FF)
    WriteByte(filehandle%, value% And $FF)
End Function 


Function pngExporterWriteStr(filehandle%,strng$)
    For i% = 1 To Len(strng$)
        WriteByte( filehandle%, Asc( Mid(strng$,i%,1) ) )
    Next
End Function
tester.bb
;------------------------------------------------------------------------------

; Test program for "Native Blitz 3D PNG exporter"

; Version 0.01

; Copyright 2005 Stefan Holmes.

; This code is 'public domain' without limitation or condition.

;------------------------------------------------------------------------------

Include "pngexporter.bb"

;------------------------------------------------------------------------------

img%=LoadImage("kodim20.png")

If img%>0
    Graphics ImageWidth(img%)+16,ImageHeight(img%)+22,32,2
    img%=LoadImage("kodim20.png")

    DrawImage img%,8,17
    Color 255,255,255
    Rect 7,16,GraphicsWidth()-14,GraphicsHeight()-20,False
    Color 0,0,0

    txt("Exporting PNG...")
    err%=pngExport("a_out.png",img%)

    If err%>0
        Txt("PNG saved.")
    Else
        txt("Error when saving.")
    EndIf 
    
Else
    Print "Unable to load image; exiting."
    Delay 4000
    End 
EndIf

Function txt(txt$)
    Color 0,0,0
    Rect 0,0,GraphicsWidth(),16
    Color 255,255,255
    Text 0,0,txt$
End Function

Here is the kodim20.png test image I'm using.