ras2tif.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. #ifndef lint
  2. static char sccsid[] = "@(#)ras2tif.c 1.2 90/03/06";
  3. #endif
  4. /*-
  5. * ras2tif.c - Converts from a Sun Rasterfile to a Tagged Image File.
  6. *
  7. * Copyright (c) 1990 by Sun Microsystems, Inc.
  8. *
  9. * Author: Patrick J. Naughton
  10. * naughton@wind.sun.com
  11. *
  12. * Permission to use, copy, modify, and distribute this software and its
  13. * documentation for any purpose and without fee is hereby granted,
  14. * provided that the above copyright notice appear in all copies and that
  15. * both that copyright notice and this permission notice appear in
  16. * supporting documentation.
  17. *
  18. * This file is provided AS IS with no warranties of any kind. The author
  19. * shall have no liability with respect to the infringement of copyrights,
  20. * trade secrets or any patents by this file or any part thereof. In no
  21. * event will the author be liable for any lost revenue or profits or
  22. * other special, indirect and consequential damages.
  23. *
  24. * Comments and additions should be sent to the author:
  25. *
  26. * Patrick J. Naughton
  27. * Sun Microsystems
  28. * 2550 Garcia Ave, MS 14-40
  29. * Mountain View, CA 94043
  30. * (415) 336-1080
  31. *
  32. * Revision History:
  33. * 11-Jan-89: Created.
  34. * 06-Mar-90: fix bug in SCALE() macro.
  35. * got rid of xres and yres, (they weren't working anyways).
  36. * fixed bpsl calculation.
  37. * 25-Nov-99: y2k fix (year as 1900 + tm_year) <mike@onshore.com>
  38. *
  39. * Description:
  40. * This program takes a Sun Rasterfile [see rasterfile(5)] as input and
  41. * writes a MicroSoft/Aldus "Tagged Image File Format" image or "TIFF" file.
  42. * The input file may be standard input, but the output TIFF file must be a
  43. * real file since seek(2) is used.
  44. */
  45. #include <stdio.h>
  46. #include <sys/time.h>
  47. #include <pixrect/pixrect_hs.h>
  48. #include "tiffio.h"
  49. typedef int boolean;
  50. #define True (1)
  51. #define False (0)
  52. #define SCALE(x) (((x)*((1L<<16)-1))/255)
  53. boolean Verbose = False;
  54. boolean dummyinput = False;
  55. char *pname; /* program name (used for error messages) */
  56. void
  57. error(s1, s2)
  58. char *s1,
  59. *s2;
  60. {
  61. fprintf(stderr, s1, pname, s2);
  62. exit(1);
  63. }
  64. void
  65. usage()
  66. {
  67. error("usage: %s -[vq] [-|rasterfile] TIFFfile\n", NULL);
  68. }
  69. main(argc, argv)
  70. int argc;
  71. char *argv[];
  72. {
  73. char *inf = NULL;
  74. char *outf = NULL;
  75. FILE *fp;
  76. int depth,
  77. i;
  78. long row;
  79. TIFF *tif;
  80. Pixrect *pix; /* The Sun Pixrect */
  81. colormap_t Colormap; /* The Pixrect Colormap */
  82. u_short red[256],
  83. green[256],
  84. blue[256];
  85. struct tm *ct;
  86. struct timeval tv;
  87. long width,
  88. height;
  89. long rowsperstrip;
  90. int year;
  91. short photometric;
  92. short samplesperpixel;
  93. short bitspersample;
  94. int bpsl;
  95. static char *version = "ras2tif 1.0";
  96. static char *datetime = "1990:01:01 12:00:00";
  97. gettimeofday(&tv, (struct timezone *) NULL);
  98. ct = localtime(&tv.tv_sec);
  99. year=1900 + ct->tm_year;
  100. sprintf(datetime, "%04d:%02d:%02d %02d:%02d:%02d",
  101. year, ct->tm_mon + 1, ct->tm_mday,
  102. ct->tm_hour, ct->tm_min, ct->tm_sec);
  103. setbuf(stderr, NULL);
  104. pname = argv[0];
  105. while (--argc) {
  106. if ((++argv)[0][0] == '-') {
  107. switch (argv[0][1]) {
  108. case 'v':
  109. Verbose = True;
  110. break;
  111. case 'q':
  112. usage();
  113. break;
  114. case '\0':
  115. if (inf == NULL)
  116. dummyinput = True;
  117. else
  118. usage();
  119. break;
  120. default:
  121. fprintf(stderr, "%s: illegal option -%c.\n", pname,
  122. argv[0][1]);
  123. exit(1);
  124. }
  125. } else if (inf == NULL && !dummyinput) {
  126. inf = argv[0];
  127. } else if (outf == NULL)
  128. outf = argv[0];
  129. else
  130. usage();
  131. }
  132. if (outf == NULL)
  133. error("%s: can't write output file to a stream.\n", NULL);
  134. if (dummyinput || inf == NULL) {
  135. inf = "Standard Input";
  136. fp = stdin;
  137. } else if ((fp = fopen(inf, "r")) == NULL)
  138. error("%s: %s couldn't be opened.\n", inf);
  139. if (Verbose)
  140. fprintf(stderr, "Reading rasterfile from %s...", inf);
  141. pix = pr_load(fp, &Colormap);
  142. if (pix == NULL)
  143. error("%s: %s is not a raster file.\n", inf);
  144. if (Verbose)
  145. fprintf(stderr, "done.\n");
  146. if (Verbose)
  147. fprintf(stderr, "Writing %s...", outf);
  148. tif = TIFFOpen(outf, "w");
  149. if (tif == NULL)
  150. error("%s: error opening TIFF file %s", outf);
  151. width = pix->pr_width;
  152. height = pix->pr_height;
  153. depth = pix->pr_depth;
  154. switch (depth) {
  155. case 1:
  156. samplesperpixel = 1;
  157. bitspersample = 1;
  158. photometric = PHOTOMETRIC_MINISBLACK;
  159. break;
  160. case 8:
  161. samplesperpixel = 1;
  162. bitspersample = 8;
  163. photometric = PHOTOMETRIC_PALETTE;
  164. break;
  165. case 24:
  166. samplesperpixel = 3;
  167. bitspersample = 8;
  168. photometric = PHOTOMETRIC_RGB;
  169. break;
  170. case 32:
  171. samplesperpixel = 4;
  172. bitspersample = 8;
  173. photometric = PHOTOMETRIC_RGB;
  174. break;
  175. default:
  176. error("%s: bogus depth: %d\n", depth);
  177. }
  178. bpsl = ((depth * width + 15) >> 3) & ~1;
  179. rowsperstrip = (8 * 1024) / bpsl;
  180. TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, width);
  181. TIFFSetField(tif, TIFFTAG_IMAGELENGTH, height);
  182. TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, bitspersample);
  183. TIFFSetField(tif, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT);
  184. TIFFSetField(tif, TIFFTAG_COMPRESSION, COMPRESSION_LZW);
  185. TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, photometric);
  186. TIFFSetField(tif, TIFFTAG_DOCUMENTNAME, inf);
  187. TIFFSetField(tif, TIFFTAG_IMAGEDESCRIPTION, "converted Sun rasterfile");
  188. TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, samplesperpixel);
  189. TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP, rowsperstrip);
  190. TIFFSetField(tif, TIFFTAG_STRIPBYTECOUNTS, height / rowsperstrip);
  191. TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
  192. TIFFSetField(tif, TIFFTAG_SOFTWARE, version);
  193. TIFFSetField(tif, TIFFTAG_DATETIME, datetime);
  194. memset(red, 0, sizeof(red));
  195. memset(green, 0, sizeof(green));
  196. memset(blue, 0, sizeof(blue));
  197. if (depth == 8) {
  198. TIFFSetField(tif, TIFFTAG_COLORMAP, red, green, blue);
  199. for (i = 0; i < Colormap.length; i++) {
  200. red[i] = SCALE(Colormap.map[0][i]);
  201. green[i] = SCALE(Colormap.map[1][i]);
  202. blue[i] = SCALE(Colormap.map[2][i]);
  203. }
  204. }
  205. if (Verbose)
  206. fprintf(stderr, "%dx%dx%d image, ", width, height, depth);
  207. for (row = 0; row < height; row++)
  208. if (TIFFWriteScanline(tif,
  209. (u_char *) mprd_addr(mpr_d(pix), 0, row),
  210. row, 0) < 0) {
  211. fprintf("failed a scanline write (%d)\n", row);
  212. break;
  213. }
  214. TIFFFlushData(tif);
  215. TIFFClose(tif);
  216. if (Verbose)
  217. fprintf(stderr, "done.\n");
  218. pr_destroy(pix);
  219. exit(0);
  220. }
  221. /*
  222. * Local Variables:
  223. * mode: c
  224. * c-basic-offset: 8
  225. * fill-column: 78
  226. * End:
  227. */