Jpg File Dimensions / Html 
Author Message
 Jpg File Dimensions / Html

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

Reason I'm asking is I like playing with Qbasic and use it to write
HTML pages sometimes.  As HTML pages display faster when you include
the size of the graphic it would be great to able to get pictures
dimensions with Qbasic instead of having to do it manually.

Thanks in advance!



Tue, 28 Aug 2001 03:00:00 GMT  
 Jpg File Dimensions / Html

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

> Reason I'm asking is I like playing with Qbasic and use it to write
> HTML pages sometimes.  As HTML pages display faster when you include
> the size of the graphic it would be great to able to get pictures
> dimensions with Qbasic instead of having to do it manually.

> Thanks in advance!

 if right$(file$,3)="JPG" or right$(file$,3)="JIF" then
 marker$ = chr$(255) + chr$(192)
 open file$ for input as #jpg
 code$ = input$(#jpg,lof(#jpg)): close #jpg
 x = instr(code$,marker$): marker1 = x
 x = instr(code$,marker$,marker1+2): marker2 = x
 if marker2 = 0 then x = marker1 else x = marker2
 hi = (asc(mid$(code$,x+5,1))*256) + asc(mid$(code$,x+6,1))
 wi = (asc(mid$(code$,x+7,1))*256) + asc(mid$(code$,x+8,1))

 code$ = ""
 end if
 fwi=wi:fhi=hi

This is code for Liberty basic. Hope this helps.

--
Doyle Whisenant

http://mechanic.webjump.com



Wed, 29 Aug 2001 03:00:00 GMT  
 Jpg File Dimensions / Html
Hey Doyle,

Thanks for the Liberty basic code you posted.  I converted it over to
Qbasic and it works great except at times 'marker1', and 'marker2'
both equal 0.  I have no idea why it does that on some JPG's, or how
to get the correct dimensions when it does.  You got any ideas?

Thanks in advance!

Randy ...

On Sat, 13 Mar 1999 22:57:52 -0600, Doyle Whisenant

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

>> Reason I'm asking is I like playing with Qbasic and use it to write
>> HTML pages sometimes.  As HTML pages display faster when you include
>> the size of the graphic it would be great to able to get pictures
>> dimensions with Qbasic instead of having to do it manually.

>> Thanks in advance!

> if right$(file$,3)="JPG" or right$(file$,3)="JIF" then
> marker$ = chr$(255) + chr$(192)
> open file$ for input as #jpg
> code$ = input$(#jpg,lof(#jpg)): close #jpg
> x = instr(code$,marker$): marker1 = x
> x = instr(code$,marker$,marker1+2): marker2 = x
> if marker2 = 0 then x = marker1 else x = marker2
> hi = (asc(mid$(code$,x+5,1))*256) + asc(mid$(code$,x+6,1))
> wi = (asc(mid$(code$,x+7,1))*256) + asc(mid$(code$,x+8,1))

> code$ = ""
> end if
> fwi=wi:fhi=hi

>This is code for Liberty basic. Hope this helps.
>--
>Doyle Whisenant

>http://mechanic.webjump.com



Thu, 30 Aug 2001 03:00:00 GMT  
 Jpg File Dimensions / Html

Quote:

> Hey Doyle,

> Thanks for the Liberty basic code you posted.  I converted it over to
> Qbasic and it works great except at times 'marker1', and 'marker2'
> both equal 0.  I have no idea why it does that on some JPG's, or how
> to get the correct dimensions when it does.  You got any ideas?

Hi Randy, sorry about that. I searched my old email postings and found
the note below. I believe this one is corrected, but haven't tried it.
Hope it helps.

Posted by Brian on the Liberty Basic email list:

 The snippet I posted a few days ago for determining the dimensions of
JPEG
 graphics DID work . . . but . . .

 I didn't realise that if the JPEG is saved with an embedded thumbnail
(I
 never
 save thumbnails) that's the result you get, not the actual size of the
JPEG
 image. The pointers to the thumbnail dims and image dims are the same

 The thumbnail dimensions are stored first, then the thumbnail data, and
 then
 the actual image size and data

 This routine now looks TWICE for the same pointers
 If the second marker returns 0, then there isn't a thumbnail, so
marker1 is
 used

 It's up to you whether you null the variable "code$" or not when
finished -
 it's just
 to clear out variable memory space (is this needed?)

 marker$ = chr$(255) + chr$(192)
 open "c:\temp\nview\jpg.jpg" for input as #jpg
 code$ = input$(#jpg,lof(#jpg)): close #jpg
 x = instr(code$,marker$): marker1 = x
 x = instr(code$,marker$,marker1+2): marker2 = x
 if marker2 = 0 then x = marker1 else x = marker2

 PicSizeY = (asc(mid$(code$,x+5,1))*256) + asc(mid$(code$,x+6,1))
 PicSizeX = (asc(mid$(code$,x+7,1))*256) + asc(mid$(code$,x+8,1))
 print PicSizeX;" x ";PicSizeY

 code$ = ""

--
Doyle Whisenant

http://mechanic.webjump.com



Fri, 31 Aug 2001 03:00:00 GMT  
 Jpg File Dimensions / Html
hi

i can not help you to solve your problem.
but you can maybe help me. i tryed a long time to display pics in 640*480
with qbasic 4.5 but with a 256 color-palette in a gif-pic qb will only
display 320*200.
is svga possible in qb??
please help!

--

  \\\||||||///////////

  >>>>>>>         \        EVIL IGEL
   >O~~~O-------O        ICQ: 13391675


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

>Reason I'm asking is I like playing with Qbasic and use it to write
>HTML pages sometimes.  As HTML pages display faster when you include
>the size of the graphic it would be great to able to get pictures
>dimensions with Qbasic instead of having to do it manually.

>Thanks in advance!



Sun, 02 Sep 2001 03:00:00 GMT  
 Jpg File Dimensions / Html

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



Sun, 02 Sep 2001 03:00:00 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. Using VBA in A2k to get dimensions of a jpg file

2. jpg file dimension retrieval

3. Dimensions of GIF and JPG from files

4. Help! - jpg file dimensions

5. jpg file dimension retrieval

6. Dimensions of GIF and JPG from files

7. .bmp, .gif, .jpg dimensions

8. ? How to get dimensions of gif or jpg

9. Dimensions on gif / jpg

10. single dimension array into multiple dimension array

11. Dimensioning of single dimension arrays within a class

12. How to open HTML file from VB6 code and let this HTML file show up

 

 
Powered by phpBB® Forum Software