Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
How to detect the dimension of JPG
Message
From
13/09/2002 02:42:11
 
 
To
13/09/2002 02:27:18
General information
Forum:
Visual FoxPro
Category:
Pictures and Image processing
Miscellaneous
Thread ID:
00696132
Message ID:
00700055
Views:
33
>Sorry, as I still use VFP 5.0
>it doesn't support JPG, and thus I use foxhwnd to display JPG/GIF.
>
>For some other reason,
>(so many programs were writen on Select-on-Cursor,
>which NOT availble at 6.0), I can't upgrade it. :(
>
>do you have another way to do?!
>
>I have test both:
>Createobject('image') and Createobject('foxhwnd.foxhwndctrl.1')
>
>Neither of them can fix my problem.
>

This function will return (hopefully) dimensions of a specified JPEG or GIF file (adapted from Perl, unknow original author):
* usage
STORE 0 TO x, y

getpictdimensions(getpict(),@x,@y)

? x,y

PROCEDURE GetPictDimensions
LPARAMETERS cFileName, nWidth, nHeight
LOCAL lnImg, lnGet

  STORE -1 TO nWidth, nHeight

  * try to open picture file
  IF FILE(cFileName)

    lnImg = FOPEN(cFileName,10)

    IF lnImg>=0

      * successfully opened, so get first two bytes
      lcChars = FREAD(lnImg,2)

      DO CASE
      * 0xFFD8 signals a jpeg image
      CASE lcChars = CHR(0xFF)+CHR(0xD8)

        * fetch next marker
        DO WHILE !FEOF(lnImg)

          lcChars = FREAD(lnImg,1)

          * found a marker
          IF lcChars=CHR(0xFF)

            * must discard extra 0xFFs
            DO WHILE lcChars=CHR(0xFF)
              lcChars = FREAD(lnImg,1)
            ENDDO

            * is this the SOF marker?
            IF BETWEEN(ASC(lcChars),0xC0,0xC3)

              FREAD(lnImg,3)
              * read dimensions
              lcChars = FREAD(lnImg,1)
              nHeight = ASC(lcChars)*256
              lcChars = FREAD(lnImg,1)
              nHeight = nHeight+ASC(lcChars)

              lcChars = FREAD(lnImg,1)
              nWidth = ASC(lcChars)*256
              lcChars = FREAD(lnImg,1)
              nWidth = nWidth+ASC(lcChars)

              EXIT

            * if not, jump to the next marker
            ELSE

              * get the offset
              lcChars = FREAD(lnImg,1)
              lnOffset = ASC(lcChars)*256
              lcChars = FREAD(lnImg,1)
              lnOffset = lnOffset+ASC(lcChars)

              * go ahead to next marker
              IF lnOffset>2
                FSEEK(lnImg,lnOffset-2,1)
              ELSE
                EXIT
              ENDIF
            ENDIF
          ENDIF
        ENDDO

      * if not a jpeg image, go for a gif
      CASE lcChars = "GI"

        * look for remain gif signal
        lcChars = FREAD(lnImg,4)
        IF INLIST(lcChars,"F87a","F89a")

          * it's a GIF file, fetch logical screen size
          lcChars = FREAD(lnImg,1)
          nWidth = ASC(lcChars)
          lcChars = FREAD(lnImg,1)
          nWidth = nWidth+ASC(lcChars)*256

          lcChars = FREAD(lnImg,1)
          nHeight = ASC(lcChars)
          lcChars = FREAD(lnImg,1)
          nHeight = nHeight+ASC(lcChars)*256

        ENDIF

      ENDCASE

      FCLOSE(lnImg)

    ENDIF
  ENDIF
ENDPROC
----------------------------------
António Tavares Lopes
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform