Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
PNG headers
#1
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?
Reply
#2
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?
Reply
#3
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?
Reply
#4
Wow very nice...
Reply
#5
From what you said I imagened the code to be ALOT simpiler,
Wow thats quite something Confusedhock:
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)