Version 2 of Reading PNG image dimensions

Updated 2002-01-09 18:36:03

Developed specially for the Wiki by Donal Fellows...

 proc pngsize {filename} {
     if {[file size $filename] < 33} {
         error "File $filename not large enough to contain PNG header"
     }
     set f [open $filename r]
     fconfigure $f -encoding binary -translation binary

     # Read PNG file signature
     binary scan [read $f 8] c8 sig
     foreach b1 $sig b2 {-119 80 78 71 13 10 26 10} {
         if {$b1 != $b2} {
             close $f
             error "$filename is not a PNG file"
         }
     }

     # Read IHDR chunk signature
     binary scan [read $f 8] c8 sig
     foreach b1 $sig b2 {0 0 0 13 73 72 68 82} {
         if {$b1 != $b2} {
             close $f
             error "$filename is missing a leading IHDR chunk"
         }
     }

     # Read off the size of the image
     binary scan [read $f 8] II width height
     # Ignore the rest of the data, including the chunk CRC!
     #binary scan [read $f 5] ccccc depth type compression filter interlace
     #binary scan [read $f 4] I chunkCRC

     close $f
     return [list $width $height]
 }

See also "Reading GIF image dimensions", "CRC", and "Reading JPEG image dimensions".


Category Graphics