home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 18 REXX / 18-REXX.zip / grfxrexx.zip / LitJPEG.Cmd < prev    next >
OS/2 REXX Batch file  |  1995-09-23  |  3KB  |  94 lines

  1. /*********************************************************************
  2.  LitJPEG - Raphaël Vanney, 09/95
  3.  
  4.  The purpose of this REXX program is to get information from a JPEG
  5.  file. There's of course no waranty of any kind associated to it. Use
  6.  freely.
  7. **********************************************************************/
  8.  
  9. Debug=1                  /* turn this off to avoid output */
  10. Parse Arg NomF
  11. Say GetJPEGDesc(NomF)
  12. Drop NomF JPEG.
  13. Exit
  14.  
  15. GetJPEGDesc:
  16. /* Get description for a JPEG file ; if first character of returned
  17.    string is a "(", an error occured and the returned string is a
  18.    description of the error rather than that of the file.
  19.    Otherwise, returns "width x height (color info)".
  20.  
  21.    Extra info is available in JPEG. if no error occured :
  22.    JPEG.bps         bits per sample
  23.    JPEG.height      image height
  24.    JPEG.width       image width
  25.    JPEG.noc         number of components (I don't know either)
  26.    JPEG.color       color information
  27. */
  28. Parse Arg FName
  29. Drop JPEG.
  30.  
  31. If FName="" Then Return "(must supply a filename)"
  32.  
  33. /* read file header */
  34.  
  35. Hdr=C2X(CharIn(FName, 1, 2))
  36.  
  37. If Debug Then Say "File header (SOI) : "Hdr
  38. If Hdr<>"FFD8" Then Return "(not a JPEG file)"
  39.  
  40. NxtSeg=3
  41. Do While (Seg.Type<>"D9") & (NxtSeg<>-1) & (JPEG.height="JPEG.HEIGHT")
  42.      NxtSeg=LitSegment(NxtSeg)
  43. End
  44.  
  45. Drop Hdr NxtSeg Seg. Res FName
  46.  
  47. If JPEG.height<>"JPEG.HEIGHT"
  48. Then Return JPEG.width||" x "||JPEG.height||" ("||JPEG.color||")"
  49. Else Return "(size not found)"
  50.  
  51. LitSegment:         /* reads a JPEG segment's header from the input file */
  52. Arg SegPos
  53.  
  54. Seg.marker=C2X(CharIn(FName, SegPos))
  55. If Seg.marker<>"FF" Then
  56. Do
  57.      If Debug Then Say "Invalid segment at "SegPos" ("Seg.marker")"
  58.      Return -1
  59. End
  60. Seg.Type=C2X(CharIn(FName))
  61. Res=SegPos+2                  /* position of next segment */
  62. If (Seg.Type="01") | ((Seg.Type>="D0") & (Seg.Type<="D9")) Then
  63. Do   /* these segments contain no data nor length info */
  64.      Seg.Len=0
  65. End
  66. Else
  67. Do
  68.      Seg.Len=C2D(CharIn(FName, , 2))
  69. End
  70. Res=Res+Seg.Len
  71.  
  72. If (Seg.Type="C0") | (Seg.Type="C2") Then
  73. Do
  74.      /* start of frame 0 */
  75.      JPEG.bps=C2D(CharIn(FName))             /* bits per sample */
  76.      JPEG.height=C2D(CharIn(FName, , 2))
  77.      JPEG.width=C2D(CharIn(FName, , 2))
  78.      JPEG.noc=C2D(CharIn(FName))             /* number of components */
  79.      Select
  80.           When JPEG.noc=1 Then
  81.                JPEG.color="grayscale"
  82.           When JPEG.noc=3 Then
  83.                JPEG.color="color YCbCr"
  84.           When JPEG.noc=4 Then
  85.                JPEG.color="color CMYK"
  86.           Otherwise
  87.                JPEG.color="?"
  88.      End
  89. End
  90.  
  91. If Debug Then Say "Segment at "D2X(SegPos, 4)", type "Seg.Type", length "Seg.Len
  92.  
  93. Return Res
  94.