Mirage Source

Free ORPG making software.
It is currently Thu Mar 28, 2024 7:38 pm

All times are UTC




Post new topic Reply to topic  [ 5 posts ] 
Author Message
 Post subject: PNG headers
PostPosted: Mon Sep 07, 2009 1:08 pm 
Offline
Submit-Happy
User avatar

Joined: Fri Jun 16, 2006 7:01 am
Posts: 2768
Location: Yorkshire, UK
PNG's header is confusing. I want to grab the image's width + height for loading it in dx8 without having to know the size beforehand.

Help?

_________________
Quote:
Robin:
Why aren't maps and shit loaded up in a dynamic array?
Jacob:
the 4 people that know how are lazy
Robin:
Who are those 4 people?
Jacob:
um
you, me, and 2 others?


Image


Top
 Profile  
 
 Post subject: Re: PNG headers
PostPosted: Mon Sep 07, 2009 2:41 pm 
Offline
Submit-Happy
User avatar

Joined: Fri Jun 16, 2006 7:01 am
Posts: 2768
Location: Yorkshire, UK
Darryl hooked me up with a sexy header reader for jpg + png.

Disregard this.

_________________
Quote:
Robin:
Why aren't maps and shit loaded up in a dynamic array?
Jacob:
the 4 people that know how are lazy
Robin:
Who are those 4 people?
Jacob:
um
you, me, and 2 others?


Image


Top
 Profile  
 
 Post subject: Re: PNG headers
PostPosted: Mon Sep 07, 2009 3:02 pm 
Offline
Submit-Happy
User avatar

Joined: Fri Jun 16, 2006 7:01 am
Posts: 2768
Location: Yorkshire, UK
Here's the code if anyone wants it.

Code:
' PNG Header data
Private Type PNGHeader
    Width As Integer
    Height As Integer
End Type


Code:
Private Function GetPNGSize(ByVal filename As String) As PNGHeader
Dim Buffer() As Byte
Dim ChunkSize As Long
Dim ChunkType As String
Dim f As Long

    f = FreeFile()
    Open filename For Binary As #f
        If Err <> 0 Then Exit Function
        If LOF(f) = 0 Then Exit Function
   
        ' Read the first 2 bytes to identifier
        ReDim Buffer(0 To 1)
        Get #f, , Buffer
   
        ' Get the next 6 bytes to confirms that a PNG
        ReDim Buffer(0 To 5)
        Get #f, , Buffer
        If Buffer(0) <> &H4E Or Buffer(1) <> &H47 Or Buffer(2) <> &HD Or _
           Buffer(3) <> &HA Or Buffer(4) <> &H1A Or Buffer(5) <> &HA Then
            Exit Function
        End If
       
        ' Dim the array. Both the chunk size and chunk type have 4 bytes
        ReDim Buffer(0 To 3)
       
        Do While True
            ' Get the chunk size
            Get #f, , Buffer
            ChunkSize = (Buffer(0) * (2 ^ 24)) + (Buffer(1) * (2 ^ 16)) + (Buffer(2) * (2 ^ 8)) + Buffer(3)
           
            ' Get the chunk type
            Get #f, , Buffer
            ChunkType = Chr(Buffer(0)) & Chr(Buffer(1)) & Chr(Buffer(2)) & Chr(Buffer(3))
           
            ' Set the array to chunk size all chunk
            ReDim Buffer(0 To ChunkSize - 1)
            Get #f, , Buffer
           
            Select Case ChunkType
                Case "IHDR"
                    ' Main Chunk with Height, Width
                    GetPNGSize.Width = (Buffer(0) * (2 ^ 24)) + (Buffer(1) * (2 ^ 16)) + (Buffer(2) * (2 ^ 8)) + Buffer(3)
                    GetPNGSize.Height = (Buffer(4) * (2 ^ 24)) + (Buffer(5) * (2 ^ 16)) + (Buffer(6) * (2 ^ 8)) + Buffer(7)
                   
                Case "IDAT", "IEND"
                    Exit Do
            End Select
       
            ReDim Buffer(0 To 3)
           
            ' Jump 4 bytes
            Get #f, , Buffer
        Loop
    Close #f
   
End Function

_________________
Quote:
Robin:
Why aren't maps and shit loaded up in a dynamic array?
Jacob:
the 4 people that know how are lazy
Robin:
Who are those 4 people?
Jacob:
um
you, me, and 2 others?


Image


Top
 Profile  
 
 Post subject: Re: PNG headers
PostPosted: Mon Sep 07, 2009 5:59 pm 
Offline
Persistant Poster
User avatar

Joined: Thu Mar 29, 2007 10:30 pm
Posts: 1510
Location: Virginia, USA
Google Talk: hpmccloud@gmail.com
Wow very nice...

_________________
Nean wrote:
Yes harold. Give it to me.

Image
Image


Top
 Profile  
 
 Post subject: Re: PNG headers
PostPosted: Wed Sep 09, 2009 1:09 pm 
Offline
Regular

Joined: Thu Aug 13, 2009 7:06 pm
Posts: 39
From what you said I imagened the code to be ALOT simpiler,
Wow thats quite something :shock:


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 4 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group