roentgen b75cab
/* $Id: tiff-palette.c,v 1.5 2010-06-08 18:55:15 bfriesen Exp $ */
roentgen b75cab
roentgen b75cab
/*
roentgen b75cab
 * tiff-palette.c -- create a Class P (palette) TIFF file
roentgen b75cab
 *
roentgen b75cab
 * Copyright 1990 by Digital Equipment Corporation, Maynard, Massachusetts.
roentgen b75cab
 *
roentgen b75cab
 *                        All Rights Reserved
roentgen b75cab
 *
roentgen b75cab
 * Permission to use, copy, modify, and distribute this software and its
roentgen b75cab
 * documentation for any purpose and without fee is hereby granted,
roentgen b75cab
 * provided that the above copyright notice appear in all copies and that
roentgen b75cab
 * both that copyright notice and this permission notice appear in
roentgen b75cab
 * supporting documentation, and that the name of Digital not be
roentgen b75cab
 * used in advertising or publicity pertaining to distribution of the
roentgen b75cab
 * software without specific, written prior permission.
roentgen b75cab
 *
roentgen b75cab
 * DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
roentgen b75cab
 * ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
roentgen b75cab
 * DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
roentgen b75cab
 * ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
roentgen b75cab
 * WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
roentgen b75cab
 * ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
roentgen b75cab
 * SOFTWARE.
roentgen b75cab
 */
roentgen b75cab
roentgen b75cab
#include <stdio.h></stdio.h>
roentgen b75cab
#include <stdlib.h></stdlib.h>
roentgen b75cab
#include <string.h></string.h>
roentgen b75cab
roentgen b75cab
#include "tiffio.h"
roentgen b75cab
roentgen b75cab
#define WIDTH       512
roentgen b75cab
#define HEIGHT      WIDTH
roentgen b75cab
#define SCALE(x)    ((x) * 257L)
roentgen b75cab
roentgen b75cab
char *              programName;
roentgen b75cab
void                Usage();
roentgen b75cab
roentgen b75cab
int main(int argc, char **argv)
roentgen b75cab
{
roentgen b75cab
    int             bits_per_pixel = 8, cmsize, i, j, k,
roentgen b75cab
                    cmap_index, chunk_size = 32, nchunks = 16;
roentgen b75cab
    unsigned char * scan_line;
roentgen b75cab
    uint16          *red, *green, *blue;
roentgen b75cab
    TIFF *          tif;
roentgen b75cab
roentgen b75cab
    programName = argv[0];
roentgen b75cab
roentgen b75cab
    if (argc != 4)
roentgen b75cab
        Usage();
roentgen b75cab
roentgen b75cab
    if (!strcmp(argv[1], "-depth"))
roentgen b75cab
         bits_per_pixel = atoi(argv[2]);
roentgen b75cab
    else
roentgen b75cab
         Usage();
roentgen b75cab
roentgen b75cab
    switch (bits_per_pixel) {
roentgen b75cab
        case 8:
roentgen b75cab
            nchunks = 16;
roentgen b75cab
            chunk_size = 32;
roentgen b75cab
            break;
roentgen b75cab
        case 4:
roentgen b75cab
            nchunks = 4;
roentgen b75cab
            chunk_size = 128;
roentgen b75cab
            break;
roentgen b75cab
        case 2:
roentgen b75cab
            nchunks = 2;
roentgen b75cab
            chunk_size = 256;
roentgen b75cab
            break;
roentgen b75cab
	case 1:
roentgen b75cab
	    nchunks = 2;
roentgen b75cab
	    chunk_size = 256;
roentgen b75cab
	    break;
roentgen b75cab
        default:
roentgen b75cab
            Usage();
roentgen b75cab
    }
roentgen b75cab
roentgen b75cab
    if (bits_per_pixel != 1) {
roentgen b75cab
	cmsize = nchunks * nchunks;
roentgen b75cab
    } else {
roentgen b75cab
	cmsize = 2;
roentgen b75cab
    }
roentgen b75cab
    red = (uint16 *) malloc(cmsize * sizeof(uint16));
roentgen b75cab
    green = (uint16 *) malloc(cmsize * sizeof(uint16));
roentgen b75cab
    blue = (uint16 *) malloc(cmsize * sizeof(uint16));
roentgen b75cab
roentgen b75cab
    switch (bits_per_pixel) {
roentgen b75cab
    case 8:
roentgen b75cab
        for (i = 0; i < cmsize; i++) {
roentgen b75cab
            if (i < 32)
roentgen b75cab
                red[i] = 0;
roentgen b75cab
            else if (i < 64)
roentgen b75cab
                red[i] = SCALE(36);
roentgen b75cab
            else if (i < 96)
roentgen b75cab
                red[i] = SCALE(73);
roentgen b75cab
            else if (i < 128)
roentgen b75cab
                red[i] = SCALE(109);
roentgen b75cab
            else if (i < 160)
roentgen b75cab
                red[i] = SCALE(146);
roentgen b75cab
            else if (i < 192)
roentgen b75cab
                red[i] = SCALE(182);
roentgen b75cab
            else if (i < 224)
roentgen b75cab
                red[i] = SCALE(219);
roentgen b75cab
            else if (i < 256)
roentgen b75cab
                red[i] = SCALE(255);
roentgen b75cab
roentgen b75cab
            if ((i % 32) < 4)
roentgen b75cab
                green[i] = 0;
roentgen b75cab
            else if (i < 8)
roentgen b75cab
                green[i] = SCALE(36);
roentgen b75cab
            else if ((i % 32) < 12)
roentgen b75cab
                green[i] = SCALE(73);
roentgen b75cab
            else if ((i % 32) < 16)
roentgen b75cab
                green[i] = SCALE(109);
roentgen b75cab
            else if ((i % 32) < 20)
roentgen b75cab
                green[i] = SCALE(146);
roentgen b75cab
            else if ((i % 32) < 24)
roentgen b75cab
                green[i] = SCALE(182);
roentgen b75cab
            else if ((i % 32) < 28)
roentgen b75cab
                green[i] = SCALE(219);
roentgen b75cab
            else if ((i % 32) < 32)
roentgen b75cab
                green[i] = SCALE(255);
roentgen b75cab
roentgen b75cab
            if ((i % 4) == 0)
roentgen b75cab
                blue[i] = SCALE(0);
roentgen b75cab
            else if ((i % 4) == 1)
roentgen b75cab
                blue[i] = SCALE(85);
roentgen b75cab
            else if ((i % 4) == 2)
roentgen b75cab
                blue[i] = SCALE(170);
roentgen b75cab
            else if ((i % 4) == 3)
roentgen b75cab
                blue[i] = SCALE(255);
roentgen b75cab
        }
roentgen b75cab
        break;
roentgen b75cab
    case 4:
roentgen b75cab
        red[0] = SCALE(255);
roentgen b75cab
        green[0] = 0;
roentgen b75cab
        blue[0] = 0;
roentgen b75cab
roentgen b75cab
        red[1] = 0;
roentgen b75cab
        green[1] = SCALE(255);
roentgen b75cab
        blue[1] = 0;
roentgen b75cab
roentgen b75cab
        red[2] = 0;
roentgen b75cab
        green[2] = 0;
roentgen b75cab
        blue[2] = SCALE(255);
roentgen b75cab
roentgen b75cab
        red[3] = SCALE(255);
roentgen b75cab
        green[3] = SCALE(255);
roentgen b75cab
        blue[3] = SCALE(255);
roentgen b75cab
roentgen b75cab
        red[4] = 0;
roentgen b75cab
        green[4] = SCALE(255);
roentgen b75cab
        blue[4] = SCALE(255);
roentgen b75cab
roentgen b75cab
        red[5] = SCALE(255);
roentgen b75cab
        green[5] = 0;
roentgen b75cab
        blue[5] = SCALE(255);
roentgen b75cab
roentgen b75cab
        red[6] = SCALE(255);
roentgen b75cab
        green[6] = SCALE(255);
roentgen b75cab
        blue[6] = 0;
roentgen b75cab
roentgen b75cab
        red[7] = 0;
roentgen b75cab
        green[7] = 0;
roentgen b75cab
        blue[7] = 0;
roentgen b75cab
roentgen b75cab
        red[8] = SCALE(176);
roentgen b75cab
        green[8] = SCALE(224);
roentgen b75cab
        blue[8] = SCALE(230);
roentgen b75cab
        red[9] = SCALE(100);
roentgen b75cab
        green[9] = SCALE(149);
roentgen b75cab
        blue[9] = SCALE(237);
roentgen b75cab
        red[10] = SCALE(46);
roentgen b75cab
        green[10] = SCALE(139);
roentgen b75cab
        blue[10] = SCALE(87);
roentgen b75cab
        red[11] = SCALE(160);
roentgen b75cab
        green[11] = SCALE(82);
roentgen b75cab
        blue[11] = SCALE(45);
roentgen b75cab
        red[12] = SCALE(238);
roentgen b75cab
        green[12] = SCALE(130);
roentgen b75cab
        blue[12] = SCALE(238);
roentgen b75cab
        red[13] = SCALE(176);
roentgen b75cab
        green[13] = SCALE(48);
roentgen b75cab
        blue[13] = SCALE(96);
roentgen b75cab
        red[14] = SCALE(50);
roentgen b75cab
        green[14] = SCALE(205);
roentgen b75cab
        blue[14] = SCALE(50);
roentgen b75cab
        red[15] = SCALE(240);
roentgen b75cab
        green[15] = SCALE(152);
roentgen b75cab
        blue[15] = SCALE(35);
roentgen b75cab
        break;
roentgen b75cab
    case 2:
roentgen b75cab
        red[0] = SCALE(255);
roentgen b75cab
        green[0] = 0;
roentgen b75cab
        blue[0] = 0;
roentgen b75cab
roentgen b75cab
        red[1] = 0;
roentgen b75cab
        green[1] = SCALE(255);
roentgen b75cab
        blue[1] = 0;
roentgen b75cab
roentgen b75cab
        red[2] = 0;
roentgen b75cab
        green[2] = 0;
roentgen b75cab
        blue[2] = SCALE(255);
roentgen b75cab
        red[3] = SCALE(255);
roentgen b75cab
        green[3] = SCALE(255);
roentgen b75cab
        blue[3] = SCALE(255);
roentgen b75cab
        break;
roentgen b75cab
    case 1:
roentgen b75cab
        red[0] = 0;
roentgen b75cab
        green[0] = 0;
roentgen b75cab
        blue[0] = 0;
roentgen b75cab
roentgen b75cab
        red[1] = SCALE(255);
roentgen b75cab
        green[1] = SCALE(255);
roentgen b75cab
        blue[1] = SCALE(255);
roentgen b75cab
        break;
roentgen b75cab
    }
roentgen b75cab
roentgen b75cab
    if ((tif = TIFFOpen(argv[3], "w")) == NULL) {
roentgen b75cab
        fprintf(stderr, "can't open %s as a TIFF file\n", argv[3]);
roentgen b75cab
		free(red);free(green);free(blue);
roentgen b75cab
        return 0;
roentgen b75cab
    }
roentgen b75cab
roentgen b75cab
    TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, WIDTH);
roentgen b75cab
    TIFFSetField(tif, TIFFTAG_IMAGELENGTH, HEIGHT);
roentgen b75cab
    TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, bits_per_pixel);
roentgen b75cab
    TIFFSetField(tif, TIFFTAG_COMPRESSION, COMPRESSION_NONE);
roentgen b75cab
    TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_PALETTE);
roentgen b75cab
    TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, 1);
roentgen b75cab
    TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP, 1);
roentgen b75cab
    TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
roentgen b75cab
    TIFFSetField(tif, TIFFTAG_RESOLUTIONUNIT, RESUNIT_NONE);
roentgen b75cab
    TIFFSetField(tif, TIFFTAG_COLORMAP, red, green, blue);
roentgen b75cab
roentgen b75cab
    scan_line = (unsigned char *) malloc(WIDTH / (8 / bits_per_pixel));
roentgen b75cab
roentgen b75cab
    for (i = 0; i < HEIGHT; i++) {
roentgen b75cab
        for (j = 0, k = 0; j < WIDTH;) {
roentgen b75cab
            cmap_index = (j / chunk_size) + ((i / chunk_size) * nchunks);
roentgen b75cab
roentgen b75cab
            switch (bits_per_pixel) {
roentgen b75cab
            case 8:
roentgen b75cab
                scan_line[k++] = cmap_index;
roentgen b75cab
                j++;
roentgen b75cab
                break;
roentgen b75cab
            case 4:
roentgen b75cab
                scan_line[k++] = (cmap_index << 4) + cmap_index;
roentgen b75cab
                j += 2;
roentgen b75cab
                break;
roentgen b75cab
            case 2:
roentgen b75cab
                scan_line[k++] = (cmap_index << 6) + (cmap_index << 4)
roentgen b75cab
                    + (cmap_index << 2) + cmap_index;
roentgen b75cab
                j += 4;
roentgen b75cab
                break;
roentgen b75cab
	    case 1:
roentgen b75cab
		scan_line[k++] =
roentgen b75cab
			((j / chunk_size) == (i / chunk_size)) ? 0x00 : 0xff;
roentgen b75cab
		j += 8;
roentgen b75cab
		break;
roentgen b75cab
            }
roentgen b75cab
        }
roentgen b75cab
        TIFFWriteScanline(tif, scan_line, i, 0);
roentgen b75cab
    }
roentgen b75cab
roentgen b75cab
    free(scan_line);
roentgen b75cab
    TIFFClose(tif);
roentgen b75cab
    return 0;
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
void
roentgen b75cab
Usage()
roentgen b75cab
{
roentgen b75cab
    fprintf(stderr, "Usage: %s -depth (8 | 4 | 2 | 1) tiff-image\n", programName);
roentgen b75cab
    exit(0);
roentgen b75cab
}
roentgen b75cab
/*
roentgen b75cab
 * Local Variables:
roentgen b75cab
 * mode: c
roentgen b75cab
 * c-basic-offset: 8
roentgen b75cab
 * fill-column: 78
roentgen b75cab
 * End:
roentgen b75cab
 */