tiff-palette.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. /* $Id: tiff-palette.c,v 1.5 2010-06-08 18:55:15 bfriesen Exp $ */
  2. /*
  3. * tiff-palette.c -- create a Class P (palette) 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 <stdio.h>
  26. #include <stdlib.h>
  27. #include <string.h>
  28. #include "tiffio.h"
  29. #define WIDTH 512
  30. #define HEIGHT WIDTH
  31. #define SCALE(x) ((x) * 257L)
  32. char * programName;
  33. void Usage();
  34. int main(int argc, char **argv)
  35. {
  36. int bits_per_pixel = 8, cmsize, i, j, k,
  37. cmap_index, chunk_size = 32, nchunks = 16;
  38. unsigned char * scan_line;
  39. uint16 *red, *green, *blue;
  40. TIFF * tif;
  41. programName = argv[0];
  42. if (argc != 4)
  43. Usage();
  44. if (!strcmp(argv[1], "-depth"))
  45. bits_per_pixel = atoi(argv[2]);
  46. else
  47. Usage();
  48. switch (bits_per_pixel) {
  49. case 8:
  50. nchunks = 16;
  51. chunk_size = 32;
  52. break;
  53. case 4:
  54. nchunks = 4;
  55. chunk_size = 128;
  56. break;
  57. case 2:
  58. nchunks = 2;
  59. chunk_size = 256;
  60. break;
  61. case 1:
  62. nchunks = 2;
  63. chunk_size = 256;
  64. break;
  65. default:
  66. Usage();
  67. }
  68. if (bits_per_pixel != 1) {
  69. cmsize = nchunks * nchunks;
  70. } else {
  71. cmsize = 2;
  72. }
  73. red = (uint16 *) malloc(cmsize * sizeof(uint16));
  74. green = (uint16 *) malloc(cmsize * sizeof(uint16));
  75. blue = (uint16 *) malloc(cmsize * sizeof(uint16));
  76. switch (bits_per_pixel) {
  77. case 8:
  78. for (i = 0; i < cmsize; i++) {
  79. if (i < 32)
  80. red[i] = 0;
  81. else if (i < 64)
  82. red[i] = SCALE(36);
  83. else if (i < 96)
  84. red[i] = SCALE(73);
  85. else if (i < 128)
  86. red[i] = SCALE(109);
  87. else if (i < 160)
  88. red[i] = SCALE(146);
  89. else if (i < 192)
  90. red[i] = SCALE(182);
  91. else if (i < 224)
  92. red[i] = SCALE(219);
  93. else if (i < 256)
  94. red[i] = SCALE(255);
  95. if ((i % 32) < 4)
  96. green[i] = 0;
  97. else if (i < 8)
  98. green[i] = SCALE(36);
  99. else if ((i % 32) < 12)
  100. green[i] = SCALE(73);
  101. else if ((i % 32) < 16)
  102. green[i] = SCALE(109);
  103. else if ((i % 32) < 20)
  104. green[i] = SCALE(146);
  105. else if ((i % 32) < 24)
  106. green[i] = SCALE(182);
  107. else if ((i % 32) < 28)
  108. green[i] = SCALE(219);
  109. else if ((i % 32) < 32)
  110. green[i] = SCALE(255);
  111. if ((i % 4) == 0)
  112. blue[i] = SCALE(0);
  113. else if ((i % 4) == 1)
  114. blue[i] = SCALE(85);
  115. else if ((i % 4) == 2)
  116. blue[i] = SCALE(170);
  117. else if ((i % 4) == 3)
  118. blue[i] = SCALE(255);
  119. }
  120. break;
  121. case 4:
  122. red[0] = SCALE(255);
  123. green[0] = 0;
  124. blue[0] = 0;
  125. red[1] = 0;
  126. green[1] = SCALE(255);
  127. blue[1] = 0;
  128. red[2] = 0;
  129. green[2] = 0;
  130. blue[2] = SCALE(255);
  131. red[3] = SCALE(255);
  132. green[3] = SCALE(255);
  133. blue[3] = SCALE(255);
  134. red[4] = 0;
  135. green[4] = SCALE(255);
  136. blue[4] = SCALE(255);
  137. red[5] = SCALE(255);
  138. green[5] = 0;
  139. blue[5] = SCALE(255);
  140. red[6] = SCALE(255);
  141. green[6] = SCALE(255);
  142. blue[6] = 0;
  143. red[7] = 0;
  144. green[7] = 0;
  145. blue[7] = 0;
  146. red[8] = SCALE(176);
  147. green[8] = SCALE(224);
  148. blue[8] = SCALE(230);
  149. red[9] = SCALE(100);
  150. green[9] = SCALE(149);
  151. blue[9] = SCALE(237);
  152. red[10] = SCALE(46);
  153. green[10] = SCALE(139);
  154. blue[10] = SCALE(87);
  155. red[11] = SCALE(160);
  156. green[11] = SCALE(82);
  157. blue[11] = SCALE(45);
  158. red[12] = SCALE(238);
  159. green[12] = SCALE(130);
  160. blue[12] = SCALE(238);
  161. red[13] = SCALE(176);
  162. green[13] = SCALE(48);
  163. blue[13] = SCALE(96);
  164. red[14] = SCALE(50);
  165. green[14] = SCALE(205);
  166. blue[14] = SCALE(50);
  167. red[15] = SCALE(240);
  168. green[15] = SCALE(152);
  169. blue[15] = SCALE(35);
  170. break;
  171. case 2:
  172. red[0] = SCALE(255);
  173. green[0] = 0;
  174. blue[0] = 0;
  175. red[1] = 0;
  176. green[1] = SCALE(255);
  177. blue[1] = 0;
  178. red[2] = 0;
  179. green[2] = 0;
  180. blue[2] = SCALE(255);
  181. red[3] = SCALE(255);
  182. green[3] = SCALE(255);
  183. blue[3] = SCALE(255);
  184. break;
  185. case 1:
  186. red[0] = 0;
  187. green[0] = 0;
  188. blue[0] = 0;
  189. red[1] = SCALE(255);
  190. green[1] = SCALE(255);
  191. blue[1] = SCALE(255);
  192. break;
  193. }
  194. if ((tif = TIFFOpen(argv[3], "w")) == NULL) {
  195. fprintf(stderr, "can't open %s as a TIFF file\n", argv[3]);
  196. free(red);free(green);free(blue);
  197. return 0;
  198. }
  199. TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, WIDTH);
  200. TIFFSetField(tif, TIFFTAG_IMAGELENGTH, HEIGHT);
  201. TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, bits_per_pixel);
  202. TIFFSetField(tif, TIFFTAG_COMPRESSION, COMPRESSION_NONE);
  203. TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_PALETTE);
  204. TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, 1);
  205. TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP, 1);
  206. TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
  207. TIFFSetField(tif, TIFFTAG_RESOLUTIONUNIT, RESUNIT_NONE);
  208. TIFFSetField(tif, TIFFTAG_COLORMAP, red, green, blue);
  209. scan_line = (unsigned char *) malloc(WIDTH / (8 / bits_per_pixel));
  210. for (i = 0; i < HEIGHT; i++) {
  211. for (j = 0, k = 0; j < WIDTH;) {
  212. cmap_index = (j / chunk_size) + ((i / chunk_size) * nchunks);
  213. switch (bits_per_pixel) {
  214. case 8:
  215. scan_line[k++] = cmap_index;
  216. j++;
  217. break;
  218. case 4:
  219. scan_line[k++] = (cmap_index << 4) + cmap_index;
  220. j += 2;
  221. break;
  222. case 2:
  223. scan_line[k++] = (cmap_index << 6) + (cmap_index << 4)
  224. + (cmap_index << 2) + cmap_index;
  225. j += 4;
  226. break;
  227. case 1:
  228. scan_line[k++] =
  229. ((j / chunk_size) == (i / chunk_size)) ? 0x00 : 0xff;
  230. j += 8;
  231. break;
  232. }
  233. }
  234. TIFFWriteScanline(tif, scan_line, i, 0);
  235. }
  236. free(scan_line);
  237. TIFFClose(tif);
  238. return 0;
  239. }
  240. void
  241. Usage()
  242. {
  243. fprintf(stderr, "Usage: %s -depth (8 | 4 | 2 | 1) tiff-image\n", programName);
  244. exit(0);
  245. }
  246. /*
  247. * Local Variables:
  248. * mode: c
  249. * c-basic-offset: 8
  250. * fill-column: 78
  251. * End:
  252. */