Quote:
>Hi Everyone,
>I was wondering if anyone would have an code example of how to get the
>dimensions of a JPG file, and write it to a text file. No - not
>display the JPG - just get it's pixel dimenions.
>For example: mainlogo.jpg width=640 height=480
Here's a program that can do exactly what you want. It can't display a
.JPG, but will obtain the file information, including the dimensions.
I forgot where I found it, or who created it...
DECLARE FUNCTION CheckIfValid% ()
DECLARE SUB CloseDown ()
DECLARE FUNCTION Get2Bytes& ()
DECLARE SUB GetNextMarker ()
DECLARE SUB GetSofMarker ()
DECLARE SUB GetDHT ()
DECLARE SUB GetDAC ()
DECLARE SUB GetDQT ()
DECLARE SUB GetDRI ()
DECLARE SUB GetAppMarker ()
DECLARE FUNCTION GetTables% ()
DECLARE FUNCTION OpenFile% ()
DECLARE SUB ReadFileHeader ()
CONST DataPrecision = 8 'either 8, 12 or 16 bit, only 8 bit
supported
TYPE JPGtype
Iheight AS INTEGER
Iwidth AS INTEGER
Ilength AS INTEGER
Iprecis AS INTEGER
Icomp AS INTEGER
ColourSpace AS LONG
Arith AS INTEGER
Density AS LONG
DensityX AS LONG
DensityY AS LONG
ComponentIndex AS LONG
ComponentId AS LONG
SampFactorH AS LONG
SampFactorV AS LONG
QuantisTblNo AS LONG
END TYPE
DIM SHARED JPGinfo AS JPGtype
DIM SHARED Fbyte1 AS STRING * 1, Fbyte2 AS STRING * 1
DIM SHARED JPGfileName$, FileHandle AS INTEGER
CLS
INPUT "Enter JPG file to view : ", JPGfileName$
IF OpenFile% = 0 THEN CloseDown
PRINT "File "; JPGfileName$
IF CheckIfValid% = 0 THEN CloseDown
IF GetTables = 0 THEN CloseDown
ReadFileHeader
CloseDown
FUNCTION CheckIfValid%
'confirm if valid JPG file
GET #FileHandle, , Fbyte1
GET #FileHandle, , Fbyte2
CheckIfValid% = -1
IF Fbyte1 = CHR$(255) AND Fbyte2 = CHR$(216) THEN
PRINT "Found valid JPG start of information header"
ELSE
PRINT "Warning ! may be invalid JPG file - continue Y/N"
PRINT
z$ = UCASE$(INPUT$(1))
IF z$ = "N" THEN CheckIfValid% = 0 'cancel
END IF
END FUNCTION
SUB CloseDown
'clean up
CLOSE FileHandle
END
END SUB
FUNCTION Get2Bytes&
'returns 2 byte marker parameter length field
GET #FileHandle, , Fbyte1
GET #FileHandle, , Fbyte2
Get2Bytes& = ASC(Fbyte1) * 256 + ASC(Fbyte2)
END FUNCTION
SUB GetAppMarker
Alength = Get2Bytes - 2
'check if JFIF marker present
IF Alength >= 14 THEN
FOR i = 1 TO 14
GET #FileHandle, , Fbyte1
a$ = a$ + Fbyte1
NEXT
Alength = Alength - 14
IF LEFT$(a$, 4) = "JFIF" THEN 'found JFIF marker
Ver$ = LTRIM$(STR$(ASC(MID$(a$, 6, 1)))) + "."
Ver$ = Ver$ + LTRIM$(STR$(ASC(MID$(a$, 7, 1))))
'check version - major version must be 1
IF ASC(MID$(a$, 6, 1)) <> 1 THEN
PRINT "Unsupported JFIF revision number "; Ver$
CloseDown
ELSE
'minor version should be 0 or 1 ?
IF ASC(MID$(a$, 7, 1)) <> 0 AND ASC(MID$(a$, 7, 1)) <> 1 THEN
PRINT "Warning ! unknown JFIF revision number "; Ver$;
PRINT " - trying to process it"
ELSE
PRINT "Found valid JFIF version "; Ver$
END IF
END IF
JPGinfo.Density = ASC(MID$(a$, 8, 1))
JPGinfo.DensityX = ASC(MID$(a$, 9, 1)) * 256 + ASC(MID$(a$, 10, 1))
JPGinfo.DensityY = ASC(MID$(a$, 11, 1)) * 256 + ASC(MID$(a$, 12,
1))
PRINT "Col Density "; JPGinfo.Density
PRINT "Col Density X "; JPGinfo.DensityX
PRINT "Col Density Y "; JPGinfo.DensityY
'colourspace info correct ?
IF JPGinfo.ColourSpace = 4 THEN 'unknown colourspace
JPGinfo.ColourSpace = 3
END IF
ELSE
PRINT "Unknown marker is not JFIF"
CloseDown
END IF
ELSE
PRINT "Error ! short marker length"
CloseDown
END IF
'skip remaining data
WHILE Alength > 0
GET #FileHandle, , Fbyte1
Alength = Alength - 1
WEND
END SUB
SUB GetDAC
END SUB
SUB GetDHT
END SUB
SUB GetDQT
END SUB
SUB GetDRI
END SUB
SUB GetNextMarker
bytes = 0
DO
DO 'skip non-FF bytes
bytes = bytes + 1
GET #FileHandle, , Fbyte1
LOOP WHILE Fbyte1 <> CHR$(255)
DO 'skip duplicate FFs
bytes = bytes + 1
GET #FileHandle, , Fbyte1
LOOP WHILE Fbyte1 = CHR$(255)
LOOP WHILE Fbyte1 = CHR$(0) 'loop if stuffed FF/00
END SUB
SUB GetSofMarker
'process SOF marker
JPGinfo.Ilength = Get2Bytes&
GET #FileHandle, , Fbyte1
JPGinfo.Iprecis = ASC(Fbyte1)
JPGinfo.Iheight = Get2Bytes&
JPGinfo.Iwidth = Get2Bytes&
GET #FileHandle, , Fbyte1
JPGinfo.Icomp = ASC(Fbyte1)
IF JPGinfo.Iprecis <> DataPrecision THEN
PRINT "Error ! "; JPGinfo.Iprecis; " bit samples not supported, only"
PRINT DataPrecision; " samples are supported"
CloseDown
END IF
IF JPGinfo.Iheight < 1 THEN
PRINT "Error ! invalid image height or no image in file !"
CloseDown
END IF
IF JPGinfo.Iwidth < 1 THEN
PRINT "Error ! invalid image width or no image in file !"
CloseDown
END IF
IF JPGinfo.Ilength <> JPGinfo.Icomp * 3 + 8 THEN
PRINT "Error ! invalid SOF length"
CloseDown
END IF
'this loop is WRONG !
FOR c = 0 TO JPGinfo.Icomp
JPGinfo.ComponentIndex = c
GET #FileHandle, , Fbyte1
JPGinfo.ComponentId = ASC(Fbyte1)
GET #FileHandle, , Fbyte1
d& = ASC(Fbyte1)
JPGinfo.SampFactorH = (d& * (4 * 4 * 4 * 4)) AND 15
'JPGinfo.SampFactorH = (d& ^ 4) AND 15
JPGinfo.SampFactorV = d& AND 15
GET #FileHandle, , Fbyte1
JPGinfo.QuantisTblNo = ASC(Fbyte1)
NEXT
END SUB
FUNCTION GetTables%
'process JPEG markers that appear in any order
'SOI, EOI, SOFn, or SOS
DO
GetNextMarker
SELECT CASE ASC(Fbyte1)
CASE 192 TO 195, 197 TO 203, 205 TO 207, 216 TO 218
GetTables% = -1
EXIT FUNCTION
CASE 196
GetDHT
CASE 204
GetDAC
CASE 219
GetDQT
CASE 221
GetDRI
CASE 224
GetAppMarker
CASE 1, 208 TO 215
PRINT "Error ! unexpected marker "; Fbyte1; ASC(Fbyte1)
GetTables% = 0
EXIT FUNCTION
CASE ELSE 'must be DNL, DHP, EXP, APPn, JPGn, COM, or RESn
Alength = Get2Bytes
FOR i = Alength - 2 TO 0 STEP -1
GET #FileHandle, , Fbyte1
NEXT
END SELECT
LOOP
END FUNCTION
FUNCTION OpenFile%
'open file and check if valid
FileHandle = FREEFILE
OPEN JPGfileName$ FOR BINARY AS #FileHandle
OpenFile% = -1
IF LOF(1) = 0 THEN
PRINT "File does not exist or invalid file length"
OpenFile% = 0
END IF
END FUNCTION
SUB ReadFileHeader
SELECT CASE ASC(Fbyte1)
CASE 192, 193
GetSofMarker
JPGinfo.Arith = 0 'huffman
CASE 201
GetSofMarker
JPGinfo.Arith = -1 'arithmetic
CASE ELSE
PRINT "Unsupported file version !"
CloseDown
END SELECT
PRINT "Image Height "; JPGinfo.Iheight
PRINT "Image Width "; JPGinfo.Iwidth
PRINT "SOF length "; JPGinfo.Ilength
PRINT "Bit Sample "; JPGinfo.Iprecis
PRINT "Comp. Length "; JPGinfo.Icomp
'get colourspace
SELECT CASE Icomp
CASE 1
JPGinfo.ColourSpace = 1 'gray scale
CASE 3
JPGinfo.ColourSpace = 3 'CMYK ?
CASE 4
JPGinfo.ColourSpace = 4 'CMYK
CASE ELSE
JPGinfo.ColourSpace = 0 'unknown
END SELECT
END SUB
--
Marc van den Dikkenberg
--
The powerbasic Archives (EU) -- http://www.xs4all.nl/~excel/pb.html
The PowerBasic Archives (US) -- http://www.basicguru.com/dikkenberg
All Basic Code Archives -- http://come.to/abcpackets