home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / general / procssng / ccs / ccs-11.lha / ccs-lib / tools / convert / totiff.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-04-15  |  6.7 KB  |  222 lines

  1. /*
  2. %    TOTIFF . C
  3. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  4.  
  5. This software is copyright (C) by the Lawrence Berkeley Laboratory.
  6. Permission is granted to reproduce this software for non-commercial
  7. purposes provided that this notice is left intact.
  8.  
  9. It is acknowledged that the U.S. Government has rights to this software
  10. under Contract DE-AC03-765F00098 between the U.S.  Department of Energy
  11. and the University of California.
  12.  
  13. This software is provided as a professional and academic contribution
  14. for joint exchange. Thus, it is experimental, and is provided ``as is'',
  15. with no warranties of any kind whatsoever, no support, no promise of
  16. updates, or printed documentation. By using this software, you
  17. acknowledge that the Lawrence Berkeley Laboratory and Regents of the
  18. University of California shall have no liability with respect to the
  19. infringement of other copyrights by any part of this software.
  20.  
  21. For further information about this notice, contact William Johnston,
  22. Bld. 50B, Rm. 2239, Lawrence Berkeley Laboratory, Berkeley, CA, 94720.
  23. (wejohnston@lbl.gov)
  24.  
  25. For further information about this software, contact:
  26.     Jin Guojun
  27.     Bld. 50B, Rm. 2275,
  28.     Lawrence Berkeley Laboratory, Berkeley, CA, 94720
  29.     g_jin@lbl.gov
  30. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  31. %
  32. % AUTHOR:    Jin Guojun - LBL    1991
  33. */
  34.  
  35. #include "header.def"
  36. #include "imagedef.h"
  37.  
  38. arg_fmt_list_string    arg_fmt[] =    {
  39.     {"-none", "%Nh", COMPRESSION_NONE, 1, 0,
  40.         "no compression at all (default = LZW)"},
  41.     {"-pac", "%Nh", COMPRESSION_PACKBITS, 1, 0, "packbits"},
  42.     {"-g3", "%Nh", COMPRESSION_CCITTFAX3, 1, 0, "compress"},
  43.     {"-g4", "%Nh", COMPRESSION_CCITTFAX4, 1, 0, "compress"},
  44.     {"-pred", "%h", 0, 1, 1, "predictor"},
  45.     {"-row", "%d", 0, 1, 1, "rowsperstrip"},
  46.     {"-msb", "%Nh", FILLORDER_MSB2LSB, 1, 0, "MSB to LSB"},
  47.     {"-lsb", "%Nh", FILLORDER_LSB2MSB, 1, 0, "LSB to MSB"},
  48.     {"-2d", "%|h", GROUP3OPT_2DENCODING, 1, 0, "2D encoding"},
  49.     {"-fill", "%|h", GROUP3OPT_FILLBITS, 1, 0, "fill bits"},
  50.     {"-k", "%b", True, 1, 0, "keep working, ignore input error"},
  51.     {"-revs", "%+", No, 1, 0, "RGB to BGR"},
  52.     {"-sep", "%b", PLANARCONFIG_SEPARATE, 1, 0,
  53.         "SEPARATE PLANE for 24-bit image?"},
  54. {"I/O:    [<] input [[> |] output]", "0", 0, 0, 0, "end of usage"},
  55.     NULL    };
  56.  
  57. bool    revs;
  58. U_IMAGE    uimg;
  59. TIFF*    TIFFout;
  60.  
  61. #define    rows    uimg.height
  62. #define    cols    uimg.width
  63. #define    ocfm    uimg.color_form
  64. #define    SetTiFF(ctrl, val)    TIFFSetField(TIFFout, ctrl, val)
  65. #define    WriteTiFF(buf, w, rgb)    TIFFWriteScanline(TIFFout, buf, w, rgb)
  66.  
  67. main(ac, av)
  68. int    ac;
  69. char*    av[];
  70. {
  71. char*    *fl;
  72. unsigned short    compression=COMPRESSION_LZW, config=PLANARCONFIG_CONTIG,
  73.     fillorder=0, predictor=0, photometric=PHOTOMETRIC_MINISBLACK,
  74.     samplesperpixel=1, bitspersample=8;
  75. long    g3options=0, rowsperstrip;
  76. int    bytesperrow, i, nostop, row;
  77. register byte    *obp;
  78.  
  79.     if ((i=parse_argus(&fl, ac, av, arg_fmt,
  80.         &compression, &compression, &compression, &compression,
  81.         &predictor, &rowsperstrip,
  82.         &fillorder, &fillorder,
  83.         &g3options, &g3options,
  84.         &nostop, &revs,    &config)) < 0)
  85.         exit(i);
  86.     if (predictor < 0 && predictor > 2 || rowsperstrip < 0)    {
  87. info:        parse_usage(arg_fmt);    exit(0);    }
  88.  
  89.     if (i && (in_fp=freopen(fl[0], "rb", stdin)) == NULL)
  90.         syserr("open input %s", fl[0]);
  91.  
  92. uimg.color_dpy = -1;
  93. format_init(&uimg, IMAGE_INIT_TYPE, RLE, TiFF, *av, "S1-2");
  94.  
  95. io_test(fileno(in_fp), goto    info);
  96.  
  97. if ((*uimg.header_handle)(HEADER_READ, &uimg, 0, 0, Yes))
  98.     syserr("unknown image type");
  99.  
  100. TIFFout = TIFFFdOpen(stdout_fd, "Standard Output", "w");
  101. if (!TIFFout)
  102.     syserr("open TIFF output file");
  103. if (fillorder)
  104.     revs = !revs,
  105.     SetTiFF(TIFFTAG_FILLORDER, fillorder);    /* for tiff.3.0    */
  106. if (revs && ocfm==CFM_ILC)    ocfm = CFM_ILL;
  107. (*uimg.std_swif)(FI_LOAD_FILE, &uimg, nostop ? NULL : uimg.name,
  108.     True /* don't change format, and save PNM loading buffer */);
  109.  
  110.     switch (ocfm) {    /* Figure out TIFF parameters. */
  111.     case CFM_BITMAP:
  112.         bitspersample = 1;
  113.         bytesperrow = (cols + 7) / 8;
  114.         if (uimg.in_color != ocfm)
  115.             uimg.dest = nzalloc(bytesperrow, 1, "bm-obuf");
  116.         if (uimg.in_type==RAS)    photometric = PHOTOMETRIC_MINISWHITE;
  117.         break;
  118.     case CFM_SGF:
  119.         bitspersample = min_bits(255);
  120.         i = 8 / bitspersample;
  121.         bytesperrow = (cols + i - 1) / i;
  122.         break;
  123.     case CFM_SCF:
  124.         photometric = PHOTOMETRIC_PALETTE;
  125.         bytesperrow = cols;
  126.         if (!r_cmap)
  127.             r_cmap = (sht_cmap_t*) rle_dflt_hdr.cmap;
  128.         break;
  129.     case CFM_ILL:
  130.         uimg.dest = nzalloc(cols, 3, "24-obuf");
  131.     case CFM_ILC:
  132.     default:
  133.         samplesperpixel = 3;
  134.         photometric = PHOTOMETRIC_RGB;
  135.         bytesperrow = cols * 3;
  136.     }
  137.  
  138.     if (!rowsperstrip)    rowsperstrip = 8192 / bytesperrow;
  139.  
  140. /* Set TIFF parameters. */
  141.     SetTiFF(TIFFTAG_IMAGEWIDTH, cols);
  142.     SetTiFF(TIFFTAG_IMAGELENGTH, rows);
  143.     SetTiFF(TIFFTAG_BITSPERSAMPLE, bitspersample);
  144.     SetTiFF(TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT);
  145.     SetTiFF(TIFFTAG_COMPRESSION, compression);
  146.     if (compression == COMPRESSION_CCITTFAX3 && g3options)
  147.         SetTiFF(TIFFTAG_GROUP3OPTIONS, g3options);
  148.     if (compression == COMPRESSION_LZW && predictor)
  149.         SetTiFF(TIFFTAG_PREDICTOR, predictor);
  150.     SetTiFF(TIFFTAG_PHOTOMETRIC, photometric);
  151.     if (uimg.desc)
  152.         SetTiFF(TIFFTAG_DOCUMENTNAME, uimg.desc);
  153.     SetTiFF(TIFFTAG_IMAGEDESCRIPTION, "totiff");
  154.     SetTiFF(TIFFTAG_SAMPLESPERPIXEL, samplesperpixel);
  155.     SetTiFF(TIFFTAG_ROWSPERSTRIP, rowsperstrip);
  156.     /*    SetTiFF(TIFFTAG_STRIPBYTECOUNTS, rows / rowsperstrip);    */
  157.     SetTiFF(TIFFTAG_PLANARCONFIG, config);
  158.     if (reg_cmap[0])
  159.         regmap_to_rlemap(reg_cmap, uimg.cmaplen, 3, &rle_dflt_hdr),
  160.         i = 1 << rle_dflt_hdr.cmaplen,
  161.         TIFFSetField(TIFFout, TIFFTAG_COLORMAP,
  162.             r_cmap, r_cmap+i, r_cmap+(i<<1));
  163.  
  164.     obp = (byte*)uimg.src;
  165.  
  166.     /*    Write the TIFF buf    */
  167.     for (row=0; row < rows; row++, obp+=cols) {
  168.     switch (ocfm) {
  169.     case CFM_ILL:
  170.         if    (config == PLANARCONFIG_SEPARATE)    {
  171.         if (WriteTiFF(obp, row, 0) < 0)    goto    wrterr;
  172.         obp += cols;
  173.         if (WriteTiFF(obp, row, 1) < 0)    goto    wrterr;
  174.         obp += cols;
  175.         if (WriteTiFF(obp, row, 2) < 0)    goto    wrterr;
  176.         }
  177.         else    {
  178.         register byte*    bp = uimg.dest, *inr=obp, *ing=(obp+=cols);
  179.         if (revs) for (i=0, obp+=cols; i<cols; i++)    {
  180.             *bp++ = obp[i];
  181.             *bp++ = ing[i];
  182.             *bp++ = inr[i];
  183.         }
  184.         else    for (i=0, obp+=cols; i<cols; i++)    {
  185.             *bp++ = inr[i];    *bp++ = ing[i];    *bp++ = obp[i];
  186.         }
  187.         goto    wrt;
  188.         }    break;
  189.     case CFM_BITMAP:
  190.     if (uimg.in_color == ocfm)
  191.     case CFM_SGF:
  192.     case CFM_SCF:
  193.     case CFM_ILC:
  194.         uimg.dest = (char*)uimg.src + bytesperrow * row;
  195.     else    {
  196.     register int    col, bitshift;
  197.     register char    *bp = uimg.dest;
  198.  
  199.         bitshift = 8 - bitspersample;
  200.         *bp = 0;
  201.         for (col=0; col < cols; col++) {
  202.         if (obp[col])
  203.             *bp |= 1 << bitshift;
  204.         bitshift -= bitspersample;
  205.         if (bitshift < 0) {
  206.             bitshift = 8 - bitspersample;
  207.             *++bp = 0;
  208.         }
  209.         }
  210.     }
  211. wrt:    if (WriteTiFF(uimg.dest, row, 0) < 0)
  212. wrterr:        syserr("write scanline on row %d", row);
  213.     break;
  214.     default:    prgmerr(-1, "unknown format");
  215.     }
  216.     }
  217. TIFFFlushData(TIFFout);
  218. TIFFClose(TIFFout);
  219.  
  220. exit(0);
  221. }
  222.