Robin Posted September 7, 2009 Author Share Posted September 7, 2009 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? Link to comment Share on other sites More sharing options...
Robin Posted September 7, 2009 Author Share Posted September 7, 2009 Darryl hooked me up with a sexy header reader for jpg + png.Disregard this. Link to comment Share on other sites More sharing options...
Robin Posted September 7, 2009 Author Share Posted September 7, 2009 Here's the code if anyone wants it.```' PNG Header dataPrivate Type PNGHeader  Width As Integer  Height As IntegerEnd Type``````Private Function GetPNGSize(ByVal filename As String) As PNGHeaderDim Buffer() As ByteDim ChunkSize As LongDim ChunkType As StringDim 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 #fEnd Function``` Link to comment Share on other sites More sharing options...
jna Posted September 7, 2009 Share Posted September 7, 2009 I wish i knew VB6 Link to comment Share on other sites More sharing options...
emblem Posted September 7, 2009 Share Posted September 7, 2009 Thanks Robin, will use. Link to comment Share on other sites More sharing options...
The Oracle Posted September 7, 2009 Share Posted September 7, 2009 Trippple post!!!elevenlol thanks for that. should be useful Link to comment Share on other sites More sharing options...
Homicidal Monkey Posted September 8, 2009 Share Posted September 8, 2009 I think it would be much easier to make a C++ dll to do that since I think JBufferedImage will read and convert images to raw image files but w/e. Also, JBufferedImage isn't a standard C++ class. Link to comment Share on other sites More sharing options...
Robin Posted September 8, 2009 Author Share Posted September 8, 2009 @Zetta:> I think it would be much easier to make a C++ dll to do that since I think JBufferedImage will read and convert images to raw image files but w/e. Also, JBufferedImage isn't a standard C++ class.I originally tried to convert some Java code over, but half-way through someone pointed me to a class which did the work, so I just ripped the PNG part out and made a function out of it :P Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now