PageRenderTime 23ms CodeModel.GetById 0ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/tiff-3.9.5/contrib/dbs/tiff-grayscale.c

https://github.com/bsnizek/mapmatchingimporter
C | 146 lines | 94 code | 20 blank | 32 comment | 10 complexity | 6183c2337bb813548ef173d8e8d63d81 MD5 | raw file
  1. /* $Id: tiff-grayscale.c,v 1.4.2.1 2010-06-08 18:50:40 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. return 0;
  76. }
  77. TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, WIDTH);
  78. TIFFSetField(tif, TIFFTAG_IMAGELENGTH, HEIGHT);
  79. TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, bits_per_pixel);
  80. TIFFSetField(tif, TIFFTAG_COMPRESSION, COMPRESSION_NONE);
  81. TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISBLACK);
  82. TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, 1);
  83. TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP, 1);
  84. TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
  85. TIFFSetField(tif, TIFFTAG_REFERENCEBLACKWHITE, refblackwhite);
  86. TIFFSetField(tif, TIFFTAG_TRANSFERFUNCTION, gray);
  87. TIFFSetField(tif, TIFFTAG_RESOLUTIONUNIT, RESUNIT_NONE);
  88. scan_line = (unsigned char *) malloc(WIDTH / (8 / bits_per_pixel));
  89. for (i = 0; i < HEIGHT; i++) {
  90. for (j = 0, k = 0; j < WIDTH;) {
  91. gray_index = (j / chunk_size) + ((i / chunk_size) * nchunks);
  92. switch (bits_per_pixel) {
  93. case 8:
  94. scan_line[k++] = gray_index;
  95. j++;
  96. break;
  97. case 4:
  98. scan_line[k++] = (gray_index << 4) + gray_index;
  99. j += 2;
  100. break;
  101. case 2:
  102. scan_line[k++] = (gray_index << 6) + (gray_index << 4)
  103. + (gray_index << 2) + gray_index;
  104. j += 4;
  105. break;
  106. }
  107. }
  108. TIFFWriteScanline(tif, scan_line, i, 0);
  109. }
  110. free(scan_line);
  111. TIFFClose(tif);
  112. return 0;
  113. }
  114. void
  115. Usage()
  116. {
  117. fprintf(stderr, "Usage: %s -depth (8 | 4 | 2) tiff-image\n", programName);
  118. exit(0);
  119. }
  120. /*
  121. * Local Variables:
  122. * mode: c
  123. * c-basic-offset: 8
  124. * fill-column: 78
  125. * End:
  126. */