' Note: In this file, I have included the declarations for the
'       gfxlib headers, and the zlib functions.  This was because
'       of the problems involved in being 0.16 and 0.17 compatible

#Ifndef _PNG_IMAGE_BI_
#Define _PNG_IMAGE_BI_

#Include "fbpng.bi"
#Include "crt.bi"

#Ifndef NULL
#Define NULL CPtr( Any ptr, 0 )
#EndIf

#Define PNG_DEFAULT_ALPHA 255

#Define get_u32(p) (((CUInt( CPtr( UByte ptr, p )[0] ) ) Shl 24) _
                 Or ((CUInt( CPtr( UByte ptr, p )[1] ) ) Shl 16) _
                 Or ((CUInt( CPtr( UByte ptr, p )[2] ) ) Shl 8) _
                 Or  (CUInt( CPtr( UByte ptr, p )[3] ) ))

#Define get_u16(p) (((CUShort( CPtr( UByte ptr, p )[0] ) ) Shl 8) _
                 Or  (CUShort( CPtr( UByte ptr, p )[1] ) ))

#Define IS_CHUNK_TYPE(t,s) (*CPtr( UInteger ptr, @(t) ) = *CPtr( UInteger ptr, (s) ))

#Ifdef PNG_DEBUG
#Define DEBUGPRINT(msg) puts( msg )
#Else
#Define DEBUGPRINT(msg)
#EndIf


Type OLD_HEADER Field = 1
	bpp   : 3  As UShort
	Width : 13 As UShort
	height     As UShort
End Type

Type NEW_HEADER Field = 1
	Union
		old  As OLD_HEADER
		Type As UInteger
	End Union
	bpp                As Integer
	Width              As UInteger
	height             As UInteger
	pitch              As UInteger
	_reserved(1 To 12) As UByte
End Type

Type png_chunk_t
	length As UInteger
	Type   As ZString * 4
	Data   As UByte ptr
	crc32  As UInteger
End Type

Type png_RGB8_t
	r As UByte
	g As UByte
	b As UByte
End Type

Type plot_func_t As Sub _
	( _
		ByVal p As UInteger ptr, _
		ByVal x As Integer, _
		ByVal y As Integer, _
		ByVal c As UInteger, _
		ByVal w As Integer, _
		ByVal h As Integer _
	)

Type png_image_t
' IHDR
	Width                   As UInteger
	height                  As UInteger
	bitdepth                As UByte
	colortype               As UByte
	compressionmethod       As UByte
	filtermethod            As UByte
	interlacemethod         As UByte
' PLTE
	PLTE(0 To 255)          As png_RGB8_t
	PLTE_count              As UInteger
' IDAT
	IDAT                    As UByte ptr
	IDAT_len                As UInteger
' tRNS
	has_tRNS                As Integer
	tRNS_3(0 To 255)        As UByte
	tRNS_0                  As UShort
	tRNS_2r                 As UShort
	tRNS_2g                 As UShort
	tRNS_2b                 As UShort
' Other
	buffer                  As UByte ptr
	buffer_len              As UInteger
	buffer_pos              As UInteger
	bpp                     As UInteger
	chunk                   As png_chunk_t ptr ptr
	chunk_count             As UInteger
	initialized             As Integer
	prepared                As Integer
End Type 

Declare Sub png_image_init _
	( _
		ByRef png_image  As png_image_t, _
		ByVal buffer     As Any ptr, _
		ByVal buffer_len As UInteger _
	)

Declare Sub png_image_deinit _
	( _
		ByRef png_image As png_image_t _
	)

Declare Sub png_image_prepare _
	( _
		ByRef png_image As png_image_t _
	)

Declare Function png_image_convert _
	( _
		ByRef png_image As png_image_t, _
		ByVal target    As png_target_e _
	) As Any ptr

Declare Function chunk_find _
	( _
		ByRef png_image As png_image_t, _
		ByVal _type_    As ZString ptr, _
		ByVal start_pos As Integer _
	) As Integer

Declare Function calc_scan_size _
	( _
		ByRef png_image As png_image_t, _
		ByVal pass      As Integer _
	) As Integer

Extern As Integer tb_wfac(1 To 7)
Extern As Integer tb_hfac(1 To 7)
Extern As Integer tb_xoff(1 To 7)
Extern As Integer tb_yoff(1 To 7)
Extern As UByte png_sig(0 To 7)

Const As Integer PUT_HEADER_NEW = &h7

Type uInt As UInteger
Type Bytef As Byte
Type charf As Byte
Type intf As Integer
Type uIntf As uInt
Type uLongf As ULong
Type voidpc As Any ptr
Type voidpf As Any ptr
Type voidp As Any ptr

/'extern "c"
declare function compress (byval dest as Bytef ptr, byval destLen as uLongf ptr, byval source as Bytef ptr, byval sourceLen as uLong) as integer
declare function compress2 (byval dest as Bytef ptr, byval destLen as uLongf ptr, byval source as Bytef ptr, byval sourceLen as uLong, byval level as integer) as integer
declare function compressBound (byval sourceLen as uLong) as uLong
declare function uncompress (byval dest as Bytef ptr, byval destLen as uLongf ptr, byval source as Bytef ptr, byval sourceLen as uLong) as integer
declare function crc32 (byval crc as uLong, byval buf as Bytef ptr, byval len as uInt) as uLong
end extern'/

#EndIf '_PNG_IMAGE_BI_
