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

  1. /*********************************************************************
  2.  LitPNG - Raphaël Vanney, 09/95
  3.  
  4.  The purpose of this REXX program is to get information from a PNG file.
  5.  There's of course no waranty of any kind associated to it. Use freely.
  6. **********************************************************************/
  7.  
  8. Debug=1                  /* turn this off to avoid output */
  9. Parse Arg NomF
  10. Say GetPNGDesc(NomF)
  11. Drop NomF PNG.
  12. Exit
  13.  
  14. GetPNGDesc:
  15. /* Get description for a PNG file ; if first character of returned
  16.    string is a "(", an error occured and the returned string is a
  17.    description of the error rather than that of the file.
  18.    Otherwise, returns "width x height (color info)".
  19.  
  20.    Extra info is available in PNG. if no error occured :
  21.      PNG.width      width of image
  22.      PNG.height     height of image
  23.      PNG.bits       bits per pixel
  24.      PNG.ctype      color type
  25.      PNG.comp       compression type
  26.      PNG.filter     filter type
  27.      PNG.inter      interlace type
  28.      PNG.info       text info regarding colors (based on ctype & bits)
  29. */
  30. Parse Arg FName
  31. Drop PNG.
  32.  
  33. If FName="" Then Return "(must supply a filename)"
  34.  
  35. /* read file header */
  36.  
  37. Hdr=CharIn(FName, 1, 8)
  38. If Hdr<>(D2C(137)||"PNG"||D2C(13)||D2C(10)||D2C(26)||D2C(10)) Then
  39.      Return "(not a PNG file)"
  40.  
  41. NxtSeg=9
  42. Do While (Seg.Type<>"IEND") & (NxtSeg<>-1) & (PNG.height="PNG.HEIGHT")
  43.      NxtSeg=LitSegment(NxtSeg)
  44. End
  45.  
  46. Drop Hdr NxtSeg Seg. Res FName
  47. rc=CharOut(FName)
  48.  
  49. If PNG.height<>"PNG.HEIGHT"
  50. Then Return PNG.width||" x "||PNG.height||" ("||PNG.info||")"
  51. Else Return "(size not found)"
  52.  
  53. LitSegment:         /* reads a PNG chunk's header from the input file */
  54. Arg SegPos
  55.  
  56. Seg.len=C2D(CharIn(FName, SegPos, 4))   /* length of chunk's DATA */
  57. Seg.type=CharIn(FName, , 4)             /* chunk type */
  58. Res=SegPos+12+Seg.len                   /* position of next chunk */
  59.  
  60. If Debug Then Say "Chunk at "D2X(SegPos, 4)", type "Seg.type", length "Seg.len
  61.  
  62. If Seg.type="IHDR" Then
  63. Do
  64.      /* header chunk */
  65.      PNG.width=C2D(CharIn(FName, , 4))
  66.      PNG.height=C2D(CharIn(FName, , 4))
  67.      PNG.bits=C2D(CharIn(FName))
  68.      PNG.ctype=C2D(CharIn(FName))
  69.      PNG.comp=C2D(CharIn(FName))
  70.      PNG.filter=C2D(CharIn(FName))
  71.      PNG.inter=C2D(CharIn(FName))
  72.      Select
  73.           When PNG.ctype=0 Then
  74.                PNG.info=2**PNG.bits||" gray levels"
  75.           When PNG.ctype=2 Then
  76.                PNG.info=3*PNG.bits||"-bit RGB color"
  77.           When PNG.ctype=3 Then
  78.                PNG.info=2**PNG.bits||" colors"
  79.           When PNG.ctype=4 Then
  80.                PNG.info=2**PNG.bits||" gray levels with alpha chanel"
  81.           When PNG.ctype=6 Then
  82.                PNG.info=3*PNG.bits||"-bit RGB color with alpha chanel"
  83.           Otherwise
  84.                PNG.info="unknow color type ("||PNG.ctype||")"
  85.      End
  86. End
  87.  
  88. Return Res
  89.