tiff-rgb.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /* $Id: tiff-rgb.c,v 1.4 2010-06-08 18:55:15 bfriesen Exp $ */
  2. /*
  3. * tiff-rgb.c -- create a 24-bit Class R (rgb) TIFF file
  4. *
  5. * Copyright 1990 by Digital Equipment Corporation, Maynard, Massachusetts.
  6. *
  7. * All Rights Reserved
  8. *
  9. * Permission to use, copy, modify, and distribute this software and its
  10. * documentation for any purpose and without fee is hereby granted,
  11. * provided that the above copyright notice appear in all copies and that
  12. * both that copyright notice and this permission notice appear in
  13. * supporting documentation, and that the name of Digital not be
  14. * used in advertising or publicity pertaining to distribution of the
  15. * software without specific, written prior permission.
  16. *
  17. * DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
  18. * ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
  19. * DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
  20. * ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  21. * WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
  22. * ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  23. * SOFTWARE.
  24. */
  25. #include <math.h>
  26. #include <stdio.h>
  27. #include <stdlib.h>
  28. #include <string.h>
  29. #include "tiffio.h"
  30. #define ROUND(x) (uint16) ((x) + 0.5)
  31. #define CMSIZE 256
  32. #define WIDTH 525
  33. #define HEIGHT 512
  34. #define TIFF_GAMMA 2.2
  35. void Usage();
  36. char * programName;
  37. int main(int argc, char **argv)
  38. {
  39. char * input_file = NULL;
  40. double image_gamma = TIFF_GAMMA;
  41. int i, j;
  42. TIFF * tif;
  43. unsigned char * scan_line;
  44. uint16 red[CMSIZE], green[CMSIZE], blue[CMSIZE];
  45. float refblackwhite[2*3];
  46. programName = argv[0];
  47. switch (argc) {
  48. case 2:
  49. image_gamma = TIFF_GAMMA;
  50. input_file = argv[1];
  51. break;
  52. case 4:
  53. if (!strcmp(argv[1], "-gamma")) {
  54. image_gamma = atof(argv[2]);
  55. input_file = argv[3];
  56. } else
  57. Usage();
  58. break;
  59. default:
  60. Usage();
  61. }
  62. for (i = 0; i < CMSIZE; i++) {
  63. if (i == 0)
  64. red[i] = green[i] = blue[i] = 0;
  65. else {
  66. red[i] = ROUND((pow(i / 255.0, 1.0 / image_gamma) * 65535.0));
  67. green[i] = ROUND((pow(i / 255.0, 1.0 / image_gamma) * 65535.0));
  68. blue[i] = ROUND((pow(i / 255.0, 1.0 / image_gamma) * 65535.0));
  69. }
  70. }
  71. refblackwhite[0] = 0.0; refblackwhite[1] = 255.0;
  72. refblackwhite[2] = 0.0; refblackwhite[3] = 255.0;
  73. refblackwhite[4] = 0.0; refblackwhite[5] = 255.0;
  74. if ((tif = TIFFOpen(input_file, "w")) == NULL) {
  75. fprintf(stderr, "can't open %s as a TIFF file\n", input_file);
  76. exit(0);
  77. }
  78. TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, WIDTH);
  79. TIFFSetField(tif, TIFFTAG_IMAGELENGTH, HEIGHT);
  80. TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, 8);
  81. TIFFSetField(tif, TIFFTAG_COMPRESSION, COMPRESSION_NONE);
  82. TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB);
  83. TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, 3);
  84. TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP, 1);
  85. TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
  86. TIFFSetField(tif, TIFFTAG_RESOLUTIONUNIT, RESUNIT_NONE);
  87. #ifdef notdef
  88. TIFFSetField(tif, TIFFTAG_WHITEPOINT, whitex, whitey);
  89. TIFFSetField(tif, TIFFTAG_PRIMARYCHROMATICITIES, primaries);
  90. #endif
  91. TIFFSetField(tif, TIFFTAG_REFERENCEBLACKWHITE, refblackwhite);
  92. TIFFSetField(tif, TIFFTAG_TRANSFERFUNCTION, red, green, blue);
  93. scan_line = (unsigned char *) malloc(WIDTH * 3);
  94. for (i = 0; i < 255; i++) {
  95. for (j = 0; j < 75; j++) {
  96. scan_line[j * 3] = 255;
  97. scan_line[(j * 3) + 1] = 255 - i;
  98. scan_line[(j * 3) + 2] = 255 - i;
  99. }
  100. for (j = 75; j < 150; j++) {
  101. scan_line[j * 3] = 255 - i;
  102. scan_line[(j * 3) + 1] = 255;
  103. scan_line[(j * 3) + 2] = 255 - i;
  104. }
  105. for (j = 150; j < 225; j++) {
  106. scan_line[j * 3] = 255 - i;
  107. scan_line[(j * 3) + 1] = 255 - i;
  108. scan_line[(j * 3) + 2] = 255;
  109. }
  110. for (j = 225; j < 300; j++) {
  111. scan_line[j * 3] = (i - 1) / 2;
  112. scan_line[(j * 3) + 1] = (i - 1) / 2;
  113. scan_line[(j * 3) + 2] = (i - 1) / 2;
  114. }
  115. for (j = 300; j < 375; j++) {
  116. scan_line[j * 3] = 255 - i;
  117. scan_line[(j * 3) + 1] = 255;
  118. scan_line[(j * 3) + 2] = 255;
  119. }
  120. for (j = 375; j < 450; j++) {
  121. scan_line[j * 3] = 255;
  122. scan_line[(j * 3) + 1] = 255 - i;
  123. scan_line[(j * 3) + 2] = 255;
  124. }
  125. for (j = 450; j < 525; j++) {
  126. scan_line[j * 3] = 255;
  127. scan_line[(j * 3) + 1] = 255;
  128. scan_line[(j * 3) + 2] = 255 - i;
  129. }
  130. TIFFWriteScanline(tif, scan_line, i, 0);
  131. }
  132. for (i = 255; i < 512; i++) {
  133. for (j = 0; j < 75; j++) {
  134. scan_line[j * 3] = i;
  135. scan_line[(j * 3) + 1] = 0;
  136. scan_line[(j * 3) + 2] = 0;
  137. }
  138. for (j = 75; j < 150; j++) {
  139. scan_line[j * 3] = 0;
  140. scan_line[(j * 3) + 1] = i;
  141. scan_line[(j * 3) + 2] = 0;
  142. }
  143. for (j = 150; j < 225; j++) {
  144. scan_line[j * 3] = 0;
  145. scan_line[(j * 3) + 1] = 0;
  146. scan_line[(j * 3) + 2] = i;
  147. }
  148. for (j = 225; j < 300; j++) {
  149. scan_line[j * 3] = (i - 1) / 2;
  150. scan_line[(j * 3) + 1] = (i - 1) / 2;
  151. scan_line[(j * 3) + 2] = (i - 1) / 2;
  152. }
  153. for (j = 300; j < 375; j++) {
  154. scan_line[j * 3] = 0;
  155. scan_line[(j * 3) + 1] = i;
  156. scan_line[(j * 3) + 2] = i;
  157. }
  158. for (j = 375; j < 450; j++) {
  159. scan_line[j * 3] = i;
  160. scan_line[(j * 3) + 1] = 0;
  161. scan_line[(j * 3) + 2] = i;
  162. }
  163. for (j = 450; j < 525; j++) {
  164. scan_line[j * 3] = i;
  165. scan_line[(j * 3) + 1] = i;
  166. scan_line[(j * 3) + 2] = 0;
  167. }
  168. TIFFWriteScanline(tif, scan_line, i, 0);
  169. }
  170. free(scan_line);
  171. TIFFClose(tif);
  172. exit(0);
  173. }
  174. void
  175. Usage()
  176. {
  177. fprintf(stderr, "Usage: %s -gamma gamma tiff-image\n", programName);
  178. exit(0);
  179. }
  180. /*
  181. * Local Variables:
  182. * mode: c
  183. * c-basic-offset: 8
  184. * fill-column: 78
  185. * End:
  186. */