tiff-grayscale.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /* $Id: tiff-grayscale.c,v 1.6 2010-06-08 18:55:15 bfriesen Exp $ */
  2. /*
  3. * tiff-grayscale.c -- create a Class G (grayscale) TIFF file
  4. * with a gray response curve in linear optical density
  5. *
  6. * Copyright 1990 by Digital Equipment Corporation, Maynard, Massachusetts.
  7. *
  8. * All Rights Reserved
  9. *
  10. * Permission to use, copy, modify, and distribute this software and its
  11. * documentation for any purpose and without fee is hereby granted,
  12. * provided that the above copyright notice appear in all copies and that
  13. * both that copyright notice and this permission notice appear in
  14. * supporting documentation, and that the name of Digital not be
  15. * used in advertising or publicity pertaining to distribution of the
  16. * software without specific, written prior permission.
  17. *
  18. * DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
  19. * ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
  20. * DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
  21. * ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  22. * WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
  23. * ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  24. * SOFTWARE.
  25. */
  26. #include <math.h>
  27. #include <stdio.h>
  28. #include <stdlib.h>
  29. #include <string.h>
  30. #include "tiffio.h"
  31. #define WIDTH 512
  32. #define HEIGHT WIDTH
  33. char * programName;
  34. void Usage();
  35. int main(int argc, char **argv)
  36. {
  37. int bits_per_pixel = 8, cmsize, i, j, k,
  38. gray_index, chunk_size = 32, nchunks = 16;
  39. unsigned char * scan_line;
  40. uint16 * gray;
  41. float refblackwhite[2*1];
  42. TIFF * tif;
  43. programName = argv[0];
  44. if (argc != 4)
  45. Usage();
  46. if (!strcmp(argv[1], "-depth"))
  47. bits_per_pixel = atoi(argv[2]);
  48. else
  49. Usage();
  50. switch (bits_per_pixel) {
  51. case 8:
  52. nchunks = 16;
  53. chunk_size = 32;
  54. break;
  55. case 4:
  56. nchunks = 4;
  57. chunk_size = 128;
  58. break;
  59. case 2:
  60. nchunks = 2;
  61. chunk_size = 256;
  62. break;
  63. default:
  64. Usage();
  65. }
  66. cmsize = nchunks * nchunks;
  67. gray = (uint16 *) malloc(cmsize * sizeof(uint16));
  68. gray[0] = 3000;
  69. for (i = 1; i < cmsize; i++)
  70. gray[i] = (uint16) (-log10((double) i / (cmsize - 1)) * 1000);
  71. refblackwhite[0] = 0.0;
  72. refblackwhite[1] = (float)((1L<<bits_per_pixel) - 1);
  73. if ((tif = TIFFOpen(argv[3], "w")) == NULL) {
  74. fprintf(stderr, "can't open %s as a TIFF file\n", argv[3]);
  75. free(gray);
  76. return 0;
  77. }
  78. TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, WIDTH);
  79. TIFFSetField(tif, TIFFTAG_IMAGELENGTH, HEIGHT);
  80. TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, bits_per_pixel);
  81. TIFFSetField(tif, TIFFTAG_COMPRESSION, COMPRESSION_NONE);
  82. TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISBLACK);
  83. TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, 1);
  84. TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP, 1);
  85. TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
  86. TIFFSetField(tif, TIFFTAG_REFERENCEBLACKWHITE, refblackwhite);
  87. TIFFSetField(tif, TIFFTAG_TRANSFERFUNCTION, gray);
  88. TIFFSetField(tif, TIFFTAG_RESOLUTIONUNIT, RESUNIT_NONE);
  89. scan_line = (unsigned char *) malloc(WIDTH / (8 / bits_per_pixel));
  90. for (i = 0; i < HEIGHT; i++) {
  91. for (j = 0, k = 0; j < WIDTH;) {
  92. gray_index = (j / chunk_size) + ((i / chunk_size) * nchunks);
  93. switch (bits_per_pixel) {
  94. case 8:
  95. scan_line[k++] = gray_index;
  96. j++;
  97. break;
  98. case 4:
  99. scan_line[k++] = (gray_index << 4) + gray_index;
  100. j += 2;
  101. break;
  102. case 2:
  103. scan_line[k++] = (gray_index << 6) + (gray_index << 4)
  104. + (gray_index << 2) + gray_index;
  105. j += 4;
  106. break;
  107. }
  108. }
  109. TIFFWriteScanline(tif, scan_line, i, 0);
  110. }
  111. free(scan_line);
  112. TIFFClose(tif);
  113. return 0;
  114. }
  115. void
  116. Usage()
  117. {
  118. fprintf(stderr, "Usage: %s -depth (8 | 4 | 2) tiff-image\n", programName);
  119. exit(0);
  120. }
  121. /*
  122. * Local Variables:
  123. * mode: c
  124. * c-basic-offset: 8
  125. * fill-column: 78
  126. * End:
  127. */