Welcome Serena Central users! CLICK HERE
The migration of the Serena Central community is currently underway. Be sure to read THIS MESSAGE to get your new login set up to access your account.
SteveEly Absent Member.
Absent Member.
1898 views

How to get jpg dimensions in Cobol

Greetings,

 

Does anyone know of a way to programatically read the dimensions of a jpg image in Cobol?

Preferably straight from Cobol but if you are aware of a an activex that would be ok too.

 

Thanks,

Steve

0 Likes
2 Replies
SteveEly Absent Member.
Absent Member.

RE: How to get jpg dimensions in Cobol

I figured it out.

This works:

      SELECT jpgFile ASSIGN TO INPUT-OUTPUT jpg-file-name

              ORGANIZATION IS BINARY SEQUENTIAL.

      DATA DIVISION.

      FILE SECTION.

      fd  jpgFile.

      01  jpgFile-REC pic x.

      WORKING-STORAGE SECTION.

     * Start of Frame 0 line

      01 SOF0       pic xx value x"FFC0".

      01 SOF0-line.

        03 SOF0-Len   pic xx.

        03 bit-depth  pic x.

        03 jpg-height pic xx.

        03 jpg-width  pic xx.

      01 test-bytes.

        03  last-byte  pic x.

        03  cur-byte   pic x.

      01 hex-in-byte   pic x.

      01 dec-out       pic 9(3).

      01 dec-pos1   pic 9(3).

      01 dec-pos2   pic 9(3).

      01 my-hex.

        03  my-hex1  pic x.

        03  my-hex2  pic x.

      01 ws-image-dim  pic 9(5).

      01 image-height  pic 9(5).

      01 image-width   pic 9(5).

      LINKAGE SECTION.

      77  L-JPG-FILE-NAME  PIC X(250).

      77  L-IMAGE-HEIGHT   PIC 9(5).

      77  L-IMAGE-WIDTH    PIC 9(5).

      PROCEDURE DIVISION USING L-JPG-FILE-NAME, L-IMAGE-HEIGHT,

                                                L-IMAGE-WIDTH.                        

      1-MAIN-LOGIC.

          move L-JPG-FILE-NAME to jpg-file-name.

          perform READ-IMAGE-FILE

          move image-height to L-IMAGE-HEIGHT.

          move image-width to L-IMAGE-WIDTH.

          exit program.

          stop run.

      READ-IMAGE-FILE.

          OPEN INPUT jpgFile.

          MOVE "N" TO EOF.

          move x"01" to last-byte

          perform until eof = "Y"

             READ jpgFile AT END MOVE "Y" TO EOF

             end-read

             IF EOF = "N"

                move jpgFile-REC to cur-byte

                if test-bytes = SOF0

                   perform PROCESS-SOF0-LINE

                   move "Y" to eof

                else

                   move cur-byte to last-byte

                end-if

             end-if

          end-perform.

          close jpgFile.

      PROCESS-SOF0-LINE.

     * read the next 7 bytes into the SOF0-line to get the height and width.

          move 1 to sub

          perform 7 times

             READ jpgFile AT END MOVE "Y" TO EOF

             end-read

             IF EOF = "N"

                move jpgFile-REC to SOF0-line(sub:1)

             end-if

             add 1 to sub

          end-perform

          move jpg-height to my-hex.

          perform CALC-DIM.

          move ws-image-dim to image-height.

          move jpg-width to my-hex.

          perform CALC-DIM.

          move ws-image-dim to image-width.

      CALC-DIM.

          move my-hex1 to hex-in-byte

          perform hex2dec.

          move dec-out to dec-pos1

          move my-hex2 to hex-in-byte

          perform hex2dec.

          move dec-out to dec-pos2

          multiply dec-pos1 by 256 giving ws-image-dim

          add dec-pos2 to ws-image-dim giving ws-image-dim.

      HEX2DEC.

          EVALUATE hex-in-byte

             WHEN X"00" MOVE 000 TO dec-out

             WHEN X"01" MOVE 001 TO dec-out

             WHEN X"02" MOVE 002 TO dec-out

             WHEN X"03" MOVE 003 TO dec-out

             WHEN X"04" MOVE 004 TO dec-out

             etc, etc through x"FF"

0 Likes
eddwoo Absent Member.
Absent Member.

RE: How to get jpg dimensions in Cobol

haha, thanks for posting the solution here, it helps me solve the image dimension issue.Big Smile

____________________________________________________

tags: image programming    image width & height

0 Likes
The opinions expressed above are the personal opinions of the authors, not of Micro Focus. By using this site, you accept the Terms of Use and Rules of Participation. Certain versions of content ("Material") accessible here may contain branding from Hewlett-Packard Company (now HP Inc.) and Hewlett Packard Enterprise Company. As of September 1, 2017, the Material is now offered by Micro Focus, a separately owned and operated company. Any reference to the HP and Hewlett Packard Enterprise/HPE marks is historical in nature, and the HP and Hewlett Packard Enterprise/HPE marks are the property of their respective owners.