Get JPG Width/ Height

Share your advanced PureBasic knowledge/code with the community.
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Get JPG Width/ Height

Post by BackupUser »

Code updated for 5.20+

Restored from previous forum. Originally posted by tranquil.

Howdy ho, here comes a small procedure wich allows you to get the Width/ Height of a JPG Picture. Have fun. More Filetypes will follow next. :)

Code: Select all

; Get JPG FileInformations (Width and Height of an Image)
;
; by Tranquilizer/ Secretly!
; Email: mike_delling@web.de

; Someone knows how to find out the Color-Depth???? Please tell me!

; Procedure Returns the Location of the FileHeader containing Fileinformations if everything is okay
; otherwise it returns a negative errorcode
;
; -1 > File could not be opened/ Found
; -2 > Informationheader not Found
; -3 > Not an JPEG File?
;
; imageinfo\width  -> X Size
; imageinfo\height -> Y Size

UseJPEG2000ImageDecoder()

Structure my_imageinfo
  width.l
  height.l
EndStructure

imageinfo.my_imageinfo

Procedure getjpginfo(file$)
  Shared imageinfo
  If ReadFile(0, file$) <> 0
    header1.w = ReadByte(0)
    header2.w = ReadByte(0)
    header3.w = ReadByte(0)
    header4.w = ReadByte(0)  
    If header1.w = $FF And header2.w = $D8 And header3.w = $FF And header4.w = $E0
      finish = 0
      Repeat
        char.w = ReadByte(0)
        If char.w = 255 
          testchar.w = ReadByte(0)
          If testchar.w => $c0 And testchar.w <= $c3
            finish = 1
          EndIf
        EndIf
      Until finish = 1 Or Eof(0)
      If finish = 1
        res = Loc(0): FileSeek(0, res + 3)
        imageinfo\height = ReadByte(0) * 256 + ReadByte(0)
        imageinfo\width = ReadByte(0) * 256 + ReadByte(0)
      Else
        res = -2
      EndIf
      res = -3
    EndIf
    CloseFile(0)
  Else
    res = -1
  EndIf
  ProcedureReturn res
EndProcedure
Franco, you are totaly right! Thanks a lot!


Tranquilizer/ Secretly!
Registred PureBasic User

Edited by - tranquil on 18 December 2001 02:49:14
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Franco.

Hi Tranquil,
it seems to me that your code is not complete.
There is missing a 'EndIf' and a 'EndProcedure' and probably some code more...



Have a nice day...
Franco
Post Reply