home *** CD-ROM | disk | FTP | other *** search
/ Practical Algorithms for Image Analysis / Practical Algorithms for Image Analysis.iso / TARFILE.GZ / tarfile / libtiff / contrib / dbs / tiff-palette.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-09-11  |  7.5 KB  |  278 lines

  1. /*
  2.  * tiff-palette.c -- create a Class P (palette) TIFF file
  3.  *
  4.  * Copyright 1990 by Digital Equipment Corporation, Maynard, Massachusetts.
  5.  *
  6.  *                        All Rights Reserved
  7.  *
  8.  * Permission to use, copy, modify, and distribute this software and its
  9.  * documentation for any purpose and without fee is hereby granted,
  10.  * provided that the above copyright notice appear in all copies and that
  11.  * both that copyright notice and this permission notice appear in
  12.  * supporting documentation, and that the name of Digital not be
  13.  * used in advertising or publicity pertaining to distribution of the
  14.  * software without specific, written prior permission.
  15.  *
  16.  * DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
  17.  * ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
  18.  * DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
  19.  * ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  20.  * WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
  21.  * ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  22.  * SOFTWARE.
  23.  */
  24.  
  25. #include <stdio.h>
  26. #include <tiffio.h>
  27.  
  28. #define WIDTH       512
  29. #define HEIGHT      WIDTH
  30. #define SCALE(x)    ((x) * 257L)
  31.  
  32. typedef    unsigned char u_char;
  33. typedef    unsigned short u_short;
  34.  
  35. char *              programName;
  36. void                Usage();
  37.  
  38. void
  39. main(argc, argv)
  40.     int             argc;
  41.     char **         argv;
  42. {
  43.     int             bits_per_pixel, cmsize, i, j, k,
  44.                     cmap_index, chunk_size, nchunks;
  45.     u_char *        scan_line;
  46.     u_short         *red, *green, *blue;
  47.     TIFF *          tif;
  48.  
  49.     programName = argv[0];
  50.  
  51.     if (argc != 4)
  52.         Usage();
  53.  
  54.     if (!strcmp(argv[1], "-depth"))
  55.          bits_per_pixel = atoi(argv[2]);
  56.     else
  57.          Usage();
  58.  
  59.     switch (bits_per_pixel) {
  60.         case 8:
  61.             nchunks = 16;
  62.             chunk_size = 32;
  63.             break;
  64.         case 4:
  65.             nchunks = 4;
  66.             chunk_size = 128;
  67.             break;
  68.         case 2:
  69.             nchunks = 2;
  70.             chunk_size = 256;
  71.             break;
  72.     case 1:
  73.         nchunks = 2;
  74.         chunk_size = 256;
  75.         break;
  76.         default:
  77.             Usage();
  78.     }
  79.  
  80.     if (bits_per_pixel != 1) {
  81.     cmsize = nchunks * nchunks;
  82.     } else {
  83.     cmsize = 2;
  84.     }
  85.     red = (u_short *) malloc(cmsize * sizeof(u_short));
  86.     green = (u_short *) malloc(cmsize * sizeof(u_short));
  87.     blue = (u_short *) malloc(cmsize * sizeof(u_short));
  88.  
  89.     switch (bits_per_pixel) {
  90.     case 8:
  91.         for (i = 0; i < cmsize; i++) {
  92.             if (i < 32)
  93.                 red[i] = 0;
  94.             else if (i < 64)
  95.                 red[i] = SCALE(36);
  96.             else if (i < 96)
  97.                 red[i] = SCALE(73);
  98.             else if (i < 128)
  99.                 red[i] = SCALE(109);
  100.             else if (i < 160)
  101.                 red[i] = SCALE(146);
  102.             else if (i < 192)
  103.                 red[i] = SCALE(182);
  104.             else if (i < 224)
  105.                 red[i] = SCALE(219);
  106.             else if (i < 256)
  107.                 red[i] = SCALE(255);
  108.  
  109.             if ((i % 32) < 4)
  110.                 green[i] = 0;
  111.             else if (i < 8)
  112.                 green[i] = SCALE(36);
  113.             else if ((i % 32) < 12)
  114.                 green[i] = SCALE(73);
  115.             else if ((i % 32) < 16)
  116.                 green[i] = SCALE(109);
  117.             else if ((i % 32) < 20)
  118.                 green[i] = SCALE(146);
  119.             else if ((i % 32) < 24)
  120.                 green[i] = SCALE(182);
  121.             else if ((i % 32) < 28)
  122.                 green[i] = SCALE(219);
  123.             else if ((i % 32) < 32)
  124.                 green[i] = SCALE(255);
  125.  
  126.             if ((i % 4) == 0)
  127.                 blue[i] = SCALE(0);
  128.             else if ((i % 4) == 1)
  129.                 blue[i] = SCALE(85);
  130.             else if ((i % 4) == 2)
  131.                 blue[i] = SCALE(170);
  132.             else if ((i % 4) == 3)
  133.                 blue[i] = SCALE(255);
  134.         }
  135.         break;
  136.     case 4:
  137.         red[0] = SCALE(255);
  138.         green[0] = 0;
  139.         blue[0] = 0;
  140.  
  141.         red[1] = 0;
  142.         green[1] = SCALE(255);
  143.         blue[1] = 0;
  144.  
  145.         red[2] = 0;
  146.         green[2] = 0;
  147.         blue[2] = SCALE(255);
  148.  
  149.         red[3] = SCALE(255);
  150.         green[3] = SCALE(255);
  151.         blue[3] = SCALE(255);
  152.  
  153.         red[4] = 0;
  154.         green[4] = SCALE(255);
  155.         blue[4] = SCALE(255);
  156.  
  157.         red[5] = SCALE(255);
  158.         green[5] = 0;
  159.         blue[5] = SCALE(255);
  160.  
  161.         red[6] = SCALE(255);
  162.         green[6] = SCALE(255);
  163.         blue[6] = 0;
  164.  
  165.         red[7] = 0;
  166.         green[7] = 0;
  167.         blue[7] = 0;
  168.  
  169.         red[8] = SCALE(176);
  170.         green[8] = SCALE(224);
  171.         blue[8] = SCALE(230);
  172.         red[9] = SCALE(100);
  173.         green[9] = SCALE(149);
  174.         blue[9] = SCALE(237);
  175.         red[10] = SCALE(46);
  176.         green[10] = SCALE(139);
  177.         blue[10] = SCALE(87);
  178.         red[11] = SCALE(160);
  179.         green[11] = SCALE(82);
  180.         blue[11] = SCALE(45);
  181.         red[12] = SCALE(238);
  182.         green[12] = SCALE(130);
  183.         blue[12] = SCALE(238);
  184.         red[13] = SCALE(176);
  185.         green[13] = SCALE(48);
  186.         blue[13] = SCALE(96);
  187.         red[14] = SCALE(50);
  188.         green[14] = SCALE(205);
  189.         blue[14] = SCALE(50);
  190.         red[15] = SCALE(240);
  191.         green[15] = SCALE(152);
  192.         blue[15] = SCALE(35);
  193.         break;
  194.     case 2:
  195.         red[0] = SCALE(255);
  196.         green[0] = 0;
  197.         blue[0] = 0;
  198.  
  199.         red[1] = 0;
  200.         green[1] = SCALE(255);
  201.         blue[1] = 0;
  202.  
  203.         red[2] = 0;
  204.         green[2] = 0;
  205.         blue[2] = SCALE(255);
  206.         red[3] = SCALE(255);
  207.         green[3] = SCALE(255);
  208.         blue[3] = SCALE(255);
  209.         break;
  210.     case 1:
  211.         red[0] = 0;
  212.         green[0] = 0;
  213.         blue[0] = 0;
  214.  
  215.         red[1] = SCALE(255);
  216.         green[1] = SCALE(255);
  217.         blue[1] = SCALE(255);
  218.         break;
  219.     }
  220.  
  221.     if ((tif = TIFFOpen(argv[3], "w")) == NULL) {
  222.         fprintf(stderr, "can't open %s as a TIFF file\n", argv[3]);
  223.         exit(0);
  224.     }
  225.  
  226.     TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, WIDTH);
  227.     TIFFSetField(tif, TIFFTAG_IMAGELENGTH, HEIGHT);
  228.     TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, bits_per_pixel);
  229.     TIFFSetField(tif, TIFFTAG_COMPRESSION, COMPRESSION_NONE);
  230.     TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_PALETTE);
  231.     TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, 1);
  232.     TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP, 1);
  233.     TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
  234.     TIFFSetField(tif, TIFFTAG_RESOLUTIONUNIT, RESUNIT_NONE);
  235.     TIFFSetField(tif, TIFFTAG_COLORMAP, red, green, blue);
  236.  
  237.     scan_line = (u_char *) malloc(WIDTH / (8 / bits_per_pixel));
  238.  
  239.     for (i = 0; i < HEIGHT; i++) {
  240.         for (j = 0, k = 0; j < WIDTH;) {
  241.             cmap_index = (j / chunk_size) + ((i / chunk_size) * nchunks);
  242.  
  243.             switch (bits_per_pixel) {
  244.             case 8:
  245.                 scan_line[k++] = cmap_index;
  246.                 j++;
  247.                 break;
  248.             case 4:
  249.                 scan_line[k++] = (cmap_index << 4) + cmap_index;
  250.                 j += 2;
  251.                 break;
  252.             case 2:
  253.                 scan_line[k++] = (cmap_index << 6) + (cmap_index << 4)
  254.                     + (cmap_index << 2) + cmap_index;
  255.                 j += 4;
  256.                 break;
  257.         case 1:
  258.         scan_line[k++] =
  259.             ((j / chunk_size) == (i / chunk_size)) ? 0x00 : 0xff;
  260.         j += 8;
  261.         break;
  262.             }
  263.         }
  264.         TIFFWriteScanline(tif, scan_line, i, 0);
  265.     }
  266.  
  267.     free(scan_line);
  268.     TIFFClose(tif);
  269.     exit(0);
  270. }
  271.  
  272. void
  273. Usage()
  274. {
  275.     fprintf(stderr, "Usage: %s -depth (8 | 4 | 2 | 1) tiff-image\n", programName);
  276.     exit(0);
  277. }
  278.