/*
** $VER: GetImageInfo.rexx 1.0
** Christian Effenberger <youcan@amclust.de>
** Usage: rx GetImageInfo.rexx <FILE>
*/

call addlib('rexxsupport.library', 0, -30, 0)
call addlib('datatypes.library', 0, -30)

parse arg FILE
if ~exists(FILE) then exit 10

say FILE||' -> '||filetype(FILE)

/* Pseudo code start

Let FOO=''
Let DATA=''
Let PICW=0
Let PICH=0
Let OFFSET=0
Let SPACES=Char(32)||Char(160)||Char(9)||Char(10)||Char(12)||Char(13)
Let IMAGE=UpperCase(FILETYPE(FILE))

If Match(IMAGE,"ILBM","DEEP","RGBN","RGB8")
    If READBLOCK(FILE)=TRUE
        Let OFFSET=FindChars(DATA,CharsToHex("BMHD"))
        If OFFSET>0
            Let OFFSET=OFFSET+16
            Let PICW=HexToInteger(GetChars(DATA,OFFSET,4))
            Let OFFSET=OFFSET+4
            Let PICH=HexToInteger(GetChars(DATA,OFFSET,4))
        Else
            Let OFFSET=FindChars(DATA,CharsToHex("DGBL"))
            If OFFSET>0
                Let OFFSET=OFFSET+16
                Let PICW=HexToInteger(GetChars(DATA,OFFSET,4))
                Let OFFSET=OFFSET+4
                Let PICH=HexToInteger(GetChars(DATA,OFFSET,4))
            EndIf
        EndIf
    Else
        exit 20
    EndIf
ElseIf IMAGE="JPEG"
    If READBLOCK(FILE)=TRUE
        Let OFFSET=FindChars(DATA,IntegerToHex(4360))
        If OFFSET>0
            Let OFFSET=OFFSET+4
            Let PICH=HexToInteger(GetChars(DATA,OFFSET,4))
            Let OFFSET=OFFSET+4
            Let PICW=HexToInteger(GetChars(DATA,OFFSET,4))
        Else
            Let OFFSET=FindChars(DATA,IntegerToHex(2824))
            If OFFSET>0
                Let OFFSET=OFFSET+4
                Let PICH=HexToInteger(GetChars(DATA,OFFSET,4))
                Let OFFSET=OFFSET+4
                Let ,PICW=HexToInteger(GetChars(DATA,OFFSET,4))
            EndIf
        EndIf
    Else
        exit 20
    EndIf
ElseIf IMAGE="GIF"
    If READBLOCK(FILE)=TRUE
        Let OFFSET=FindChars(DATA,CharsToHex("GIF"))
        If OFFSET>0
            Let OFFSET=OFFSET+12
            Let PICW=HexToInteger(GetChars(DATA,OFFSET+2,2)||GetChars(DATA,OFFSET,2))
            Let OFFSET=OFFSET+4
            Let PICH=HexToInteger(GetChars(DATA,OFFSET+2,2)||GetChars(DATA,OFFSET,2))
        EndIf
    Else
        exit 20
    EndIf
ElseIf IMAGE="PCX"
    If READBLOCK(FILE)=TRUE
        Let OFFSET=FindChars(DATA,CharsToHex("IHDR"))
        If OFFSET>0
            Let OFFSET=OFFSET+12
            Let PICW=HexToInteger(GetChars(DATA,OFFSET,4))
            Let OFFSET=OFFSET+8
            Let PICH=HexToInteger(GetChars(DATA,OFFSET,4))
        EndIf
    Else
        exit 20
    EndIf
ElseIf IMAGE="PNG"
    If READBLOCK(FILE)=TRUE
        Let OFFSET=FindChars(DATA,CharsToHex("IHDR"))
        If OFFSET>0
            Let OFFSET=OFFSET+12
            Let PICW=HexToInteger(GetChars(DATA,OFFSET,4))
            Let OFFSET=OFFSET+8
            Let PICH=HexToInteger(GetChars(DATA,OFFSET,4))
        EndIf
    Else
        exit 20
    EndIf
ElseIf IMAGE="TIFF"
    If READBLOCK(FILE)=TRUE
        Let OFFSET=FindChars(DATA,"4D4D002A")
        If OFFSET>0
            Let OFFSET=OFFSET+60
            Let PICW=HexToInteger(GetChars(DATA,OFFSET,4))
            Let OFFSET=OFFSET+24
            Let PICH=HexToInteger(GetChars(DATA,OFFSET,4))
        Else
            Let OFFSET=FindChars(DATA,"49492A00")
            If OFFSET>0
                Let OFFSET=OFFSET+60
                Let PICW=HexToInteger(GetChars(DATA,OFFSET+2,2)||GetChars(DATA,OFFSET,2))
                Let OFFSET=OFFSET+24
                Let PICH=HexToInteger(GetChars(DATA,OFFSET+2,2)||GetChars(DATA,OFFSET,2))
            EndIf
        EndIf
    Else
        exit 20
    EndIf
ElseIf IMAGE="TARGA"                /* IMAGE Value = 'Targa' */
    If READBLOCK(FILE)=TRUE
        Let OFFSET=FindChars(DATA,"43")
        If OFFSET>0
            Let OFFSET=OFFSET+24
            Let PICW=HexToInteger(GetChars(DATA,OFFSET+2,2)||GetChars(DATA,OFFSET,2))
            Let OFFSET=OFFSET+4
            Let PICH=HexToInteger(GetChars(DATA,OFFSET+2,2)||GetChars(DATA,OFFSET,2))
        EndIf
    Else
        exit 20
    EndIf
ElseIf GetChars(IMAGE,1,3)="SUN"    /* IMAGE Value = 'SUN Raster' */
    If READBLOCK(FILE)=TRUE
        Let OFFSET=FindChars(DATA,"59A66A")
        If OFFSET>0
            Let OFFSET=OFFSET+12
            Let PICW=HexToInteger(GetChars(DATA,OFFSET,4))
            Let OFFSET=OFFSET+8
            Let PICH=HexToInteger(GetChars(DATA,OFFSET,4))
        EndIf
    Else
        exit 20
    EndIf
ElseIf GetChars(IMAGE,1,3)="PBM"    /* IMAGE Value = 'PBM Plus True Color Raw' */
    If READBLOCK(FILE)=TRUE
        Let DATA=HexToChars(DATA)
        Let OFFSET=2
        Let FOO=GetWord(DATA,OFFSET,SPACES)
        While GetChars(FOO,1,1)="#"
            Let OFFSET=OFFSET+1
            Let FOO=GetWord(DATA,OFFSET,SPACES)
        EndLoop
        Let PICW=Integer(GetWord(DATA,OFFSET,SPACES))
        Let PICH=Integer(GetWord(DATA,OFFSET+1,SPACES))
    Else
        exit 20
    EndIf
ElseIf GetChars(IMAGE,1,5)="ADOBE"  /* IMAGE Value = 'Adobe Photoshop®' */
    If READBLOCK(FILE)=TRUE
        Let OFFSET=FindChars(DATA,CharsToHex("8BPS"))
        If OFFSET>0
            Let OFFSET=OFFSET+32
            Let PICH=HexToInteger(GetChars(DATA,OFFSET,4))
            Let OFFSET=OFFSET+8
            Let PICW=HexToInteger(GetChars(DATA,OFFSET,4))
        EndIf
    Else
        exit 20
    EndIf
ElseIf GetChars(IMAGE,1,7)="WINDOWS"/* IMAGE Value = 'Windows Bitmap' */
    If READBLOCK(FILE)=TRUE
        Let OFFSET=FindChars(DATA,CharsToHex("BM"))
        If OFFSET>0
            Let OFFSET=OFFSET+36
            Let PICW=HexToInteger(GetChars(DATA,OFFSET+2,2)||GetChars(DATA,OFFSET,2))
            Let OFFSET=OFFSET+8
            Let PICH=HexToInteger(GetChars(DATA,OFFSET+2,2)||GetChars(DATA,OFFSET,2))
        EndIf
    Else
        exit 20
    EndIf
Else

    say 'ERROR: ErrorText...'
    exit 10

EndIf

say FILE||' -> '||IMAGE||': '||PICW||'x'||PICH

 Pseudo code end   */

exit 0

FILETYPE: procedure
parse arg FILE
return strip(translate(examinedt(FILE,,var), '', '0'x))

READBLOCK: procedure
parse arg FILE
/* Pseudo code start

OpenFile FILE,"BUFFER",READONLY ,OLDFILE
IfError
    return FALSE
Else
    FileReadChars "BUFFER",DATA,512,HEX
    IfError
        Close "BUFFER"
        return FALSE
    Else
        Close "BUFFER"
        return TRUE
    EndIf
EndIf

 Pseudo code end   */

