gdevtifs.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. /* Copyright (C) 1994, 1995 Aladdin Enterprises. All rights reserved.
  2. This software is provided AS-IS with no warranty, either express or
  3. implied.
  4. This software is distributed under license and may not be copied,
  5. modified or distributed except as expressly authorized under the terms
  6. of the license contained in the file LICENSE in this distribution.
  7. For more information about licensing, please refer to
  8. http://www.ghostscript.com/licensing/. For information on
  9. commercial licensing, go to http://www.artifex.com/licensing/ or
  10. contact Artifex Software, Inc., 101 Lucas Valley Road #110,
  11. San Rafael, CA 94903, U.S.A., +1(415)492-9861.
  12. */
  13. /* $Id: gdevtifs.c,v 1.7 2002/10/07 08:28:56 ghostgum Exp $ */
  14. /* TIFF-writing substructure */
  15. #include "stdio_.h"
  16. #include "time_.h"
  17. #include "gstypes.h"
  18. #include "gscdefs.h"
  19. #include "gdevprn.h"
  20. #include "gdevtifs.h"
  21. /*
  22. * Define the standard contents of a TIFF directory.
  23. * Clients may add more items, also sorted in increasing tag order.
  24. */
  25. typedef struct TIFF_std_directory_entries_s {
  26. TIFF_dir_entry SubFileType;
  27. TIFF_dir_entry ImageWidth;
  28. TIFF_dir_entry ImageLength;
  29. TIFF_dir_entry StripOffsets;
  30. TIFF_dir_entry Orientation;
  31. TIFF_dir_entry RowsPerStrip;
  32. TIFF_dir_entry StripByteCounts;
  33. TIFF_dir_entry XResolution;
  34. TIFF_dir_entry YResolution;
  35. TIFF_dir_entry PlanarConfig;
  36. TIFF_dir_entry ResolutionUnit;
  37. TIFF_dir_entry PageNumber;
  38. TIFF_dir_entry Software;
  39. TIFF_dir_entry DateTime;
  40. } TIFF_std_directory_entries;
  41. /* Define values that follow the directory entries. */
  42. typedef struct TIFF_std_directory_values_s {
  43. TIFF_ulong diroff; /* offset to next directory */
  44. TIFF_ulong xresValue[2]; /* XResolution indirect value */
  45. TIFF_ulong yresValue[2]; /* YResolution indirect value */
  46. #define maxSoftware 40
  47. char softwareValue[maxSoftware]; /* Software indirect value */
  48. char dateTimeValue[20]; /* DateTime indirect value */
  49. } TIFF_std_directory_values;
  50. private const TIFF_std_directory_entries std_entries_initial =
  51. {
  52. {TIFFTAG_SubFileType, TIFF_LONG, 1, SubFileType_page},
  53. {TIFFTAG_ImageWidth, TIFF_LONG, 1},
  54. {TIFFTAG_ImageLength, TIFF_LONG, 1},
  55. {TIFFTAG_StripOffsets, TIFF_LONG, 1},
  56. {TIFFTAG_Orientation, TIFF_SHORT, 1, Orientation_top_left},
  57. {TIFFTAG_RowsPerStrip, TIFF_LONG, 1},
  58. {TIFFTAG_StripByteCounts, TIFF_LONG, 1},
  59. {TIFFTAG_XResolution, TIFF_RATIONAL | TIFF_INDIRECT, 1,
  60. offset_of(TIFF_std_directory_values, xresValue[0])},
  61. {TIFFTAG_YResolution, TIFF_RATIONAL | TIFF_INDIRECT, 1,
  62. offset_of(TIFF_std_directory_values, yresValue[0])},
  63. {TIFFTAG_PlanarConfig, TIFF_SHORT, 1, PlanarConfig_contig},
  64. {TIFFTAG_ResolutionUnit, TIFF_SHORT, 1, ResolutionUnit_inch},
  65. {TIFFTAG_PageNumber, TIFF_SHORT, 2},
  66. {TIFFTAG_Software, TIFF_ASCII | TIFF_INDIRECT, 0,
  67. offset_of(TIFF_std_directory_values, softwareValue[0])},
  68. {TIFFTAG_DateTime, TIFF_ASCII | TIFF_INDIRECT, 20,
  69. offset_of(TIFF_std_directory_values, dateTimeValue[0])}
  70. };
  71. private const TIFF_std_directory_values std_values_initial =
  72. {
  73. 0,
  74. {0, 1},
  75. {0, 1},
  76. {0},
  77. {0, 0}
  78. };
  79. /* Fix up tag values on big-endian machines if necessary. */
  80. #if arch_is_big_endian
  81. private void
  82. tiff_fixup_tag(TIFF_dir_entry * dp)
  83. {
  84. switch (dp->type) {
  85. case TIFF_SHORT:
  86. case TIFF_SSHORT:
  87. /* We may have two shorts packed into a TIFF_ulong. */
  88. dp->value = (dp->value << 16) + (dp->value >> 16);
  89. break;
  90. case TIFF_BYTE:
  91. case TIFF_SBYTE:
  92. dp->value <<= 24;
  93. break;
  94. }
  95. }
  96. #else
  97. # define tiff_fixup_tag(dp) DO_NOTHING
  98. #endif
  99. /* Begin a TIFF page. */
  100. int
  101. gdev_tiff_begin_page(gx_device_printer * pdev, gdev_tiff_state * tifs,
  102. FILE * fp,
  103. const TIFF_dir_entry * entries, int entry_count,
  104. const byte * values, int value_size, long max_strip_size)
  105. {
  106. gs_memory_t *mem = pdev->memory;
  107. TIFF_std_directory_entries std_entries;
  108. const TIFF_dir_entry *pse;
  109. const TIFF_dir_entry *pce;
  110. TIFF_dir_entry entry;
  111. #define std_entry_count\
  112. (sizeof(TIFF_std_directory_entries) / sizeof(TIFF_dir_entry))
  113. int nse, nce, ntags;
  114. TIFF_std_directory_values std_values;
  115. #define std_value_size sizeof(TIFF_std_directory_values)
  116. tifs->mem = mem;
  117. if (gdev_prn_file_is_new(pdev)) {
  118. /* This is a new file; write the TIFF header. */
  119. static const TIFF_header hdr = {
  120. #if arch_is_big_endian
  121. TIFF_magic_big_endian,
  122. #else
  123. TIFF_magic_little_endian,
  124. #endif
  125. TIFF_version_value,
  126. sizeof(TIFF_header)
  127. };
  128. fwrite((const char *)&hdr, sizeof(hdr), 1, fp);
  129. tifs->prev_dir = 0;
  130. } else { /* Patch pointer to this directory from previous. */
  131. TIFF_ulong offset = (TIFF_ulong) tifs->dir_off;
  132. fseek(fp, tifs->prev_dir, SEEK_SET);
  133. fwrite((char *)&offset, sizeof(offset), 1, fp);
  134. fseek(fp, tifs->dir_off, SEEK_SET);
  135. }
  136. /* We're going to shuffle the two tag lists together. */
  137. /* Both lists are sorted; entries in the client list */
  138. /* replace entries with the same tag in the standard list. */
  139. for (ntags = 0, pse = (const TIFF_dir_entry *)&std_entries_initial,
  140. nse = std_entry_count, pce = entries, nce = entry_count;
  141. nse && nce; ++ntags
  142. ) {
  143. if (pse->tag < pce->tag)
  144. ++pse, --nse;
  145. else if (pce->tag < pse->tag)
  146. ++pce, --nce;
  147. else
  148. ++pse, --nse, ++pce, --nce;
  149. }
  150. ntags += nse + nce;
  151. tifs->ntags = ntags;
  152. /* Write count of tags in directory. */
  153. {
  154. TIFF_short dircount = ntags;
  155. fwrite((char *)&dircount, sizeof(dircount), 1, fp);
  156. }
  157. tifs->dir_off = ftell(fp);
  158. /* Fill in standard directory tags. */
  159. std_entries = std_entries_initial;
  160. std_values = std_values_initial;
  161. std_entries.ImageWidth.value = pdev->width;
  162. std_entries.ImageLength.value = pdev->height;
  163. if (max_strip_size == 0) {
  164. tifs->strip_count = 1;
  165. tifs->rows = pdev->height;
  166. std_entries.RowsPerStrip.value = pdev->height;
  167. } else {
  168. int max_strip_rows =
  169. max_strip_size / gdev_mem_bytes_per_scan_line((gx_device *)pdev);
  170. int rps = max(1, max_strip_rows);
  171. tifs->strip_count = (pdev->height + rps - 1) / rps;
  172. tifs->rows = rps;
  173. std_entries.RowsPerStrip.value = rps;
  174. std_entries.StripOffsets.count = tifs->strip_count;
  175. std_entries.StripByteCounts.count = tifs->strip_count;
  176. }
  177. tifs->StripOffsets = (TIFF_ulong *)gs_alloc_bytes(mem,
  178. (tifs->strip_count)*2*sizeof(TIFF_ulong),
  179. "gdev_tiff_begin_page(StripOffsets)");
  180. tifs->StripByteCounts = &(tifs->StripOffsets[tifs->strip_count]);
  181. if (tifs->StripOffsets == 0)
  182. return_error(gs_error_VMerror);
  183. std_entries.PageNumber.value = (TIFF_ulong) pdev->PageCount;
  184. std_values.xresValue[0] = (TIFF_ulong)pdev->x_pixels_per_inch;
  185. std_values.yresValue[0] = (TIFF_ulong)pdev->y_pixels_per_inch;
  186. {
  187. char revs[10];
  188. strncpy(std_values.softwareValue, gs_product, maxSoftware);
  189. std_values.softwareValue[maxSoftware - 1] = 0;
  190. sprintf(revs, " %1.2f", gs_revision / 100.0);
  191. strncat(std_values.softwareValue, revs,
  192. maxSoftware - strlen(std_values.softwareValue) - 1);
  193. std_entries.Software.count =
  194. strlen(std_values.softwareValue) + 1;
  195. }
  196. {
  197. struct tm tms;
  198. time_t t;
  199. time(&t);
  200. tms = *localtime(&t);
  201. sprintf(std_values.dateTimeValue,
  202. "%04d:%02d:%02d %02d:%02d:%02d",
  203. tms.tm_year + 1900, tms.tm_mon + 1, tms.tm_mday,
  204. tms.tm_hour, tms.tm_min, tms.tm_sec);
  205. }
  206. /* Write the merged directory. */
  207. for (pse = (const TIFF_dir_entry *)&std_entries,
  208. nse = std_entry_count, pce = entries, nce = entry_count;
  209. nse || nce;
  210. ) {
  211. bool std;
  212. if (nce == 0 || (nse != 0 && pse->tag < pce->tag))
  213. std = true, entry = *pse++, --nse;
  214. else if (nse == 0 || (nce != 0 && pce->tag < pse->tag))
  215. std = false, entry = *pce++, --nce;
  216. else
  217. std = false, ++pse, --nse, entry = *pce++, --nce;
  218. if (entry.tag == TIFFTAG_StripOffsets) {
  219. if (tifs->strip_count > 1) {
  220. tifs->offset_StripOffsets = tifs->dir_off +
  221. (ntags * sizeof(TIFF_dir_entry)) + std_value_size + value_size;
  222. entry.value = tifs->offset_StripOffsets;
  223. } else {
  224. tifs->offset_StripOffsets = ftell(fp) +
  225. offset_of(TIFF_dir_entry, value);
  226. }
  227. }
  228. if (entry.tag == TIFFTAG_StripByteCounts) {
  229. if (tifs->strip_count > 1) {
  230. tifs->offset_StripByteCounts = tifs->dir_off +
  231. (ntags * sizeof(TIFF_dir_entry)) + std_value_size + value_size +
  232. (sizeof(TIFF_ulong) * tifs->strip_count);
  233. entry.value = tifs->offset_StripByteCounts;
  234. } else {
  235. tifs->offset_StripByteCounts = ftell(fp) +
  236. offset_of(TIFF_dir_entry, value);
  237. }
  238. }
  239. tiff_fixup_tag(&entry); /* don't fix up indirects */
  240. if (entry.type & TIFF_INDIRECT) {
  241. /* Fix up the offset for an indirect value. */
  242. entry.type -= TIFF_INDIRECT;
  243. entry.value +=
  244. tifs->dir_off + ntags * sizeof(TIFF_dir_entry) +
  245. (std ? 0 : std_value_size);
  246. }
  247. fwrite((char *)&entry, sizeof(entry), 1, fp);
  248. }
  249. /* Write the indirect values. */
  250. fwrite((const char *)&std_values, sizeof(std_values), 1, fp);
  251. fwrite((const char *)values, value_size, 1, fp);
  252. /* Write placeholders for the strip offsets. */
  253. fwrite(tifs->StripOffsets, sizeof(TIFF_ulong), 2 * tifs->strip_count, fp);
  254. tifs->strip_index = 0;
  255. tifs->StripOffsets[0] = ftell(fp);
  256. return 0;
  257. }
  258. /* End a TIFF strip. */
  259. /* Record the size of the current strip, update the */
  260. /* start of the next strip and bump the strip_index */
  261. int
  262. gdev_tiff_end_strip(gdev_tiff_state * tifs, FILE * fp)
  263. {
  264. TIFF_ulong strip_size;
  265. TIFF_ulong next_strip_start;
  266. int pad = 0;
  267. next_strip_start = (TIFF_ulong)ftell(fp);
  268. strip_size = next_strip_start - tifs->StripOffsets[tifs->strip_index];
  269. if (next_strip_start & 1) {
  270. next_strip_start++; /* WORD alignment */
  271. fwrite(&pad, 1, 1, fp);
  272. }
  273. tifs->StripByteCounts[tifs->strip_index++] = strip_size;
  274. if (tifs->strip_index < tifs->strip_count)
  275. tifs->StripOffsets[tifs->strip_index] = next_strip_start;
  276. return 0;
  277. }
  278. /* End a TIFF page. */
  279. int
  280. gdev_tiff_end_page(gdev_tiff_state * tifs, FILE * fp)
  281. {
  282. gs_memory_t *mem = tifs->mem;
  283. long dir_off = tifs->dir_off;
  284. int tags_size = tifs->ntags * sizeof(TIFF_dir_entry);
  285. tifs->prev_dir =
  286. dir_off + tags_size + offset_of(TIFF_std_directory_values, diroff);
  287. tifs->dir_off = ftell(fp);
  288. /* Patch strip byte counts and offsets values. */
  289. /* The offset in the file was determined at begin_page and may be indirect */
  290. fseek(fp, tifs->offset_StripOffsets, SEEK_SET);
  291. fwrite(tifs->StripOffsets, sizeof(TIFF_ulong), tifs->strip_count, fp);
  292. fseek(fp, tifs->offset_StripByteCounts, SEEK_SET);
  293. fwrite(tifs->StripByteCounts, sizeof(TIFF_ulong), tifs->strip_count, fp);
  294. gs_free_object(mem, tifs->StripOffsets, "gdev_tiff_begin_page(StripOffsets)");
  295. return 0;
  296. }