home *** CD-ROM | disk | FTP | other *** search
/ ftp.cs.arizona.edu / ftp.cs.arizona.edu.tar / ftp.cs.arizona.edu / icon / historic / v941.tgz / icon.v941src.tar / icon.v941src / ipl / progs / imagetyp.icn < prev    next >
Text File  |  2001-05-02  |  3KB  |  110 lines

  1. ############################################################################
  2. #
  3. #    File:     imagetyp.icn
  4. #
  5. #    Subject:  Program to show types of image files
  6. #
  7. #    Author:   Ralph E. Griswold
  8. #
  9. #    Date:     May 2, 2001
  10. #
  11. ############################################################################
  12. #
  13. #   This file is in the public domain.
  14. #
  15. ############################################################################
  16. #
  17. #  This program accepts file names from standard input and writes their
  18. #  image type to standard output.
  19. #
  20. #  imagetyp(s) attempts to determine the type of image file named s.
  21. #  This is, of course, problematical and corrupted or fake files can
  22. #  easily fool it.  Furthermore, examples of some image files types
  23. #  were not available for testing.
  24. #
  25. #  The types presently recognized are:
  26. #
  27. #    value returned        image file type
  28. #
  29. #    ps            PostScript document
  30. #    cgm text        Computer Graphics Metafile, text
  31. #    cgm binary        Computer Graphics Metafile, binary
  32. #    cgm char        Computer Graphics Metafile, character
  33. #    sundraw            SunDraw document
  34. #    ras            UNIX raster image
  35. #    iris            Iris image
  36. #    rle            UNIX RLE image
  37. #    pbm            PBM image
  38. #    pgm            PGM image
  39. #    ppm            PPM image
  40. #    xwd            X Window dump
  41. #    gif            Compuserv GIF image
  42. #    bmp            BMP image
  43. #    xmp            XMP image
  44. #    xpm            XPM image
  45. #    pcx            PCX image
  46. #    tiff            TIFF image
  47. #    iff            IFF/ILBM image
  48. #    ?            unknown type
  49. #
  50. #  If the file cannot be opened or is empty, imagetyp() fails.
  51. #
  52. ############################################################################
  53. #
  54. #  Links:  bincvt
  55. #
  56. ############################################################################
  57.  
  58. link bincvt
  59.  
  60. procedure main()
  61.    local s
  62.  
  63.    while s := writes(read()) do write(" ", imagetyp(s))
  64.  
  65. end
  66.  
  67. procedure imagetyp(s)
  68.    local input, header, type
  69.  
  70.    input := open(s, "u") | fail            # must be untranslated
  71.  
  72.    header := reads(input, 640) | fail
  73.  
  74.    type := {
  75.       header ? {
  76.          if ="%!" then "ps"
  77.          else if ="\x59\xa6\x6a\x95" then "ras"
  78.          else if ="\122\314" then "rle"
  79.          else if ="GIF8" then "gif"
  80.          else if =("\111\111\52\0" | "\115\115\0\52") then "tiff"
  81.          else if find("BMHD") then "iff"
  82.          else if find("PNTG") then "mac paint"
  83.          else if ="BEGMF" then "cgm text"
  84.          else if ="\001\332" then "iris"
  85.          else if ="#define" & find("width ") then "xbm"
  86.          else if ="/* XPM */" then "xpm"
  87.          else if =("P1" | "P4") then "pbm"
  88.          else if =("P2" | "P5") then "pgm"
  89.          else if =("P3" | "P6") then "ppm"
  90.          else if move(4) & raw(move(4)) = 7 then "xwd"
  91.          else if move(10) & ="sundraw" then "sundraw"
  92.          else if raw(move(2)) = 12320 then "cgm char"
  93.          else if iand(raw(move(2)), 65504) = 32 then "cgm binary"
  94.          else if ="\x0a" & raw(move(1)) = (0 | 2 | 3 | 4 | 5) & tab(65) &
  95.             raw(move(1)) = 0 then "pcx"
  96.          else if move(512) & move(11) & =("\x11" | "\x00\x11") then "pict"
  97.          else &fail            # none of that worked
  98.          }
  99.       }
  100.  
  101.    if \type then return type
  102.  
  103.    seek(input, -17)            # and now for one at the end ...
  104.  
  105.    if read(input) == "TRUEVISION-TARGA\x0" then return "targa"
  106.  
  107.    return "?"                # who knows?
  108.  
  109. end
  110.