# getjsize.pl # # (p) Jerry Nagasaki # Last update: 2007/11/10 # Extracts JPG/JPEG picture size in pixels from input file # get file name $filename = $ARGV[0] || ""; $jpglen = -s $filename; # get file size open(FILE, "<./$filename") || die "File not found\n"; binmode(FILE); # kill possible M$-DOS 0x0D 0x0A sequence for "\n" $readbytes = read(FILE, $jpgfile, $jpglen); close(FILE); # convert file string into "indexed binary" array for easy access via $jpg[$address] @jpg = split(//, $jpgfile); # convert each byte into array-element @jpg = map(ord, @jpg); # convert each elements to binary value $width = 0; $height = 0; $i = 0; # scan the file for "Baseline DCT" (0xFF, 0xC0) while ($i < ($jpglen - 9)) { if (($jpg[$i] == 0xFF) && ($jpg[$i + 1] == 0xC0)) { # Baseline DCT coding: # # +0 +1 +2 +3 +4 +5 +6 +7 +8 # FF C0 xx xx xx HH HL WH WL (XH = High Byte, XL = Low Byte, HX = Height, WX = Width) $height = $jpg[$i + 5] * 256 + $jpg[$i + 6]; $width = $jpg[$i + 7] * 256 + $jpg[$i + 8]; last; # "Baseline DCT" found and end while-loop immediately } $i++; } if ($width && $height) # both unequal to 0? { print "Picture Height: $height\n"; print "Picture Width: $width\n"; } else { print "ERROR: No Baseline DCT found\n"; }