In the mail list there is a thread about reading JPG width and height
and just for future reference I want to include this script in the forum.
Please test and improve this script:
Code: Select all
on mouseUp
local temp
answer file "Select JPEG image"
put it into temp
open file temp for binary read
read from file temp for 100000
put it into temp1
close file temp
put numtobyte(255) & numtobyte(192) into tBaselineJPGFrame0
-- in Hexadecimal: FFC0 = JPEG Start-Of-Frame (SOF) marker FFC0
put numtobyte(255) & numtobyte(194) into tProgressiveJPGFrame0
-- in Hexadecimal: FFC2 = JPEG Start-Of-Frame (SOF) marker FFC2
set the casesensitive to true
put offset(tBaselineJPGFrame0,temp1) into tStartOFrame
if tStartOFrame is 0 then put offset(tProgressiveJPGFrame0,temp1) into tStartOFrame
if tStartOFrame is not empty
then
put byte (tStartOFrame + 5) of temp1 & byte (tStartOFrame + 6) of temp1 into tJPGHeight
put byte (tStartOFrame + 7) of temp1 & byte (tStartOFrame + 8) of temp1 into tJPGWidth
-- put baseConvert(baseConvert(bytetonum(byte 1 of tJPGHeight),10,16) & baseConvert(bytetonum(byte 2 of tJPGHeight),10,16),16,10) into tJPGHeight
put bytetonum(byte 1 of tJPGHeight) into q
put bytetonum(byte 2 of tJPGHeight) into w
put baseConvert(q,10,16) into r
if the number of bytes of r = 1 then put "0" & r into r
put baseConvert(w,10,16) into t
if the number of bytes of t = 1 then put "0" & t into t
put r & t into y
put baseConvert(y,16,10) into tJPGHeight
-- put baseConvert(baseConvert(bytetonum(byte 1 of tJPGWidth),10,16) & baseConvert(bytetonum(byte 2 of tJPGWidth),10,16),16,10) into tJPGWidth
put bytetonum(byte 1 of tJPGWidth) into q
put bytetonum(byte 2 of tJPGWidth) into w
put baseConvert(q,10,16) into r
if the number of bytes of r = 1 then put "0" & r into r
put baseConvert(w,10,16) into t
if the number of bytes of t = 1 then put "0" & t into t
put r & t into y
put baseConvert(y,16,10) into tJPGWidth
answer "JPG Width" && tJPGWidth & cr & "JPG Height" && tJPGHeight & cr & temp
else
answer "Could not find Start of Frame information in this JPEG image" & cr & temp
end if
end mouseUp
/*
Information from
https://stackoverflow.com/questions/35427283/get-width-and-height-from-jpeg-without-0xff-0xc0
The JPEG Start-Of-Frame (SOF) marker has 4 possible values:
FFC0 (baseline) - This is the usual mode chosen for photos and encodes
fully specified DCT blocks in groupings depending on the color/subsample
options chosen
FFC1 (extended) - This is similar to baseline, but has more than 8-bits
per color stimulus
FFC2 (progressive) - This mode is often found on web pages to allow the
image to load progressively as the data is received. Each "scan" of the
image progressively defines more coefficients of the DCT blocks until
they're fully defined. This effectively provides more and more detail as
more scans are decoded
FFC3 (lossless) - This mode uses a simple Huffman encoding to
losslessly encode the image. The only place I've seen this used is on
16-bit grayscale DICOM medical images
*/