wrrle.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. /*
  2. * wrrle.c
  3. *
  4. * Copyright (C) 1991-1996, Thomas G. Lane.
  5. * This file is part of the Independent JPEG Group's software.
  6. * For conditions of distribution and use, see the accompanying README file.
  7. *
  8. * This file contains routines to write output images in RLE format.
  9. * The Utah Raster Toolkit library is required (version 3.1 or later).
  10. *
  11. * These routines may need modification for non-Unix environments or
  12. * specialized applications. As they stand, they assume output to
  13. * an ordinary stdio stream.
  14. *
  15. * Based on code contributed by Mike Lijewski,
  16. * with updates from Robert Hutchinson.
  17. */
  18. #include "cdjpeg.h" /* Common decls for cjpeg/djpeg applications */
  19. #ifdef RLE_SUPPORTED
  20. /* rle.h is provided by the Utah Raster Toolkit. */
  21. #include <rle.h>
  22. /*
  23. * We assume that JSAMPLE has the same representation as rle_pixel,
  24. * to wit, "unsigned char". Hence we can't cope with 12- or 16-bit samples.
  25. */
  26. #if BITS_IN_JSAMPLE != 8
  27. Sorry, this code only copes with 8-bit JSAMPLEs. /* deliberate syntax err */
  28. #endif
  29. /*
  30. * Since RLE stores scanlines bottom-to-top, we have to invert the image
  31. * from JPEG's top-to-bottom order. To do this, we save the outgoing data
  32. * in a virtual array during put_pixel_row calls, then actually emit the
  33. * RLE file during finish_output.
  34. */
  35. /*
  36. * For now, if we emit an RLE color map then it is always 256 entries long,
  37. * though not all of the entries need be used.
  38. */
  39. #define CMAPBITS 8
  40. #define CMAPLENGTH (1<<(CMAPBITS))
  41. typedef struct {
  42. struct djpeg_dest_struct pub; /* public fields */
  43. jvirt_sarray_ptr image; /* virtual array to store the output image */
  44. rle_map *colormap; /* RLE-style color map, or NULL if none */
  45. rle_pixel **rle_row; /* To pass rows to rle_putrow() */
  46. } rle_dest_struct;
  47. typedef rle_dest_struct * rle_dest_ptr;
  48. /* Forward declarations */
  49. METHODDEF(void) rle_put_pixel_rows
  50. JPP((j_decompress_ptr cinfo, djpeg_dest_ptr dinfo,
  51. JDIMENSION rows_supplied));
  52. /*
  53. * Write the file header.
  54. *
  55. * In this module it's easier to wait till finish_output to write anything.
  56. */
  57. METHODDEF(void)
  58. start_output_rle (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo)
  59. {
  60. rle_dest_ptr dest = (rle_dest_ptr) dinfo;
  61. size_t cmapsize;
  62. int i, ci;
  63. #ifdef PROGRESS_REPORT
  64. cd_progress_ptr progress = (cd_progress_ptr) cinfo->progress;
  65. #endif
  66. /*
  67. * Make sure the image can be stored in RLE format.
  68. *
  69. * - RLE stores image dimensions as *signed* 16 bit integers. JPEG
  70. * uses unsigned, so we have to check the width.
  71. *
  72. * - Colorspace is expected to be grayscale or RGB.
  73. *
  74. * - The number of channels (components) is expected to be 1 (grayscale/
  75. * pseudocolor) or 3 (truecolor/directcolor).
  76. * (could be 2 or 4 if using an alpha channel, but we aren't)
  77. */
  78. if (cinfo->output_width > 32767 || cinfo->output_height > 32767)
  79. ERREXIT2(cinfo, JERR_RLE_DIMENSIONS, cinfo->output_width,
  80. cinfo->output_height);
  81. if (cinfo->out_color_space != JCS_GRAYSCALE &&
  82. cinfo->out_color_space != JCS_RGB)
  83. ERREXIT(cinfo, JERR_RLE_COLORSPACE);
  84. if (cinfo->output_components != 1 && cinfo->output_components != 3)
  85. ERREXIT1(cinfo, JERR_RLE_TOOMANYCHANNELS, cinfo->num_components);
  86. /* Convert colormap, if any, to RLE format. */
  87. dest->colormap = NULL;
  88. if (cinfo->quantize_colors) {
  89. /* Allocate storage for RLE-style cmap, zero any extra entries */
  90. cmapsize = cinfo->out_color_components * CMAPLENGTH * SIZEOF(rle_map);
  91. dest->colormap = (rle_map *) (*cinfo->mem->alloc_small)
  92. ((j_common_ptr) cinfo, JPOOL_IMAGE, cmapsize);
  93. MEMZERO(dest->colormap, cmapsize);
  94. /* Save away data in RLE format --- note 8-bit left shift! */
  95. /* Shifting would need adjustment for JSAMPLEs wider than 8 bits. */
  96. for (ci = 0; ci < cinfo->out_color_components; ci++) {
  97. for (i = 0; i < cinfo->actual_number_of_colors; i++) {
  98. dest->colormap[ci * CMAPLENGTH + i] =
  99. GETJSAMPLE(cinfo->colormap[ci][i]) << 8;
  100. }
  101. }
  102. }
  103. /* Set the output buffer to the first row */
  104. dest->pub.buffer = (*cinfo->mem->access_virt_sarray)
  105. ((j_common_ptr) cinfo, dest->image, (JDIMENSION) 0, (JDIMENSION) 1, TRUE);
  106. dest->pub.buffer_height = 1;
  107. dest->pub.put_pixel_rows = rle_put_pixel_rows;
  108. #ifdef PROGRESS_REPORT
  109. if (progress != NULL) {
  110. progress->total_extra_passes++; /* count file writing as separate pass */
  111. }
  112. #endif
  113. }
  114. /*
  115. * Write some pixel data.
  116. *
  117. * This routine just saves the data away in a virtual array.
  118. */
  119. METHODDEF(void)
  120. rle_put_pixel_rows (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo,
  121. JDIMENSION rows_supplied)
  122. {
  123. rle_dest_ptr dest = (rle_dest_ptr) dinfo;
  124. if (cinfo->output_scanline < cinfo->output_height) {
  125. dest->pub.buffer = (*cinfo->mem->access_virt_sarray)
  126. ((j_common_ptr) cinfo, dest->image,
  127. cinfo->output_scanline, (JDIMENSION) 1, TRUE);
  128. }
  129. }
  130. /*
  131. * Finish up at the end of the file.
  132. *
  133. * Here is where we really output the RLE file.
  134. */
  135. METHODDEF(void)
  136. finish_output_rle (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo)
  137. {
  138. rle_dest_ptr dest = (rle_dest_ptr) dinfo;
  139. rle_hdr header; /* Output file information */
  140. rle_pixel **rle_row, *red, *green, *blue;
  141. JSAMPROW output_row;
  142. char cmapcomment[80];
  143. int row, col;
  144. int ci;
  145. #ifdef PROGRESS_REPORT
  146. cd_progress_ptr progress = (cd_progress_ptr) cinfo->progress;
  147. #endif
  148. /* Initialize the header info */
  149. header = *rle_hdr_init(NULL);
  150. header.rle_file = dest->pub.output_file;
  151. header.xmin = 0;
  152. header.xmax = cinfo->output_width - 1;
  153. header.ymin = 0;
  154. header.ymax = cinfo->output_height - 1;
  155. header.alpha = 0;
  156. header.ncolors = cinfo->output_components;
  157. for (ci = 0; ci < cinfo->output_components; ci++) {
  158. RLE_SET_BIT(header, ci);
  159. }
  160. if (cinfo->quantize_colors) {
  161. header.ncmap = cinfo->out_color_components;
  162. header.cmaplen = CMAPBITS;
  163. header.cmap = dest->colormap;
  164. /* Add a comment to the output image with the true colormap length. */
  165. sprintf(cmapcomment, "color_map_length=%d", cinfo->actual_number_of_colors);
  166. rle_putcom(cmapcomment, &header);
  167. }
  168. /* Emit the RLE header and color map (if any) */
  169. rle_put_setup(&header);
  170. /* Now output the RLE data from our virtual array.
  171. * We assume here that (a) rle_pixel is represented the same as JSAMPLE,
  172. * and (b) we are not on a machine where FAR pointers differ from regular.
  173. */
  174. #ifdef PROGRESS_REPORT
  175. if (progress != NULL) {
  176. progress->pub.pass_limit = cinfo->output_height;
  177. progress->pub.pass_counter = 0;
  178. (*progress->pub.progress_monitor) ((j_common_ptr) cinfo);
  179. }
  180. #endif
  181. if (cinfo->output_components == 1) {
  182. for (row = cinfo->output_height-1; row >= 0; row--) {
  183. rle_row = (rle_pixel **) (*cinfo->mem->access_virt_sarray)
  184. ((j_common_ptr) cinfo, dest->image,
  185. (JDIMENSION) row, (JDIMENSION) 1, FALSE);
  186. rle_putrow(rle_row, (int) cinfo->output_width, &header);
  187. #ifdef PROGRESS_REPORT
  188. if (progress != NULL) {
  189. progress->pub.pass_counter++;
  190. (*progress->pub.progress_monitor) ((j_common_ptr) cinfo);
  191. }
  192. #endif
  193. }
  194. } else {
  195. for (row = cinfo->output_height-1; row >= 0; row--) {
  196. rle_row = (rle_pixel **) dest->rle_row;
  197. output_row = * (*cinfo->mem->access_virt_sarray)
  198. ((j_common_ptr) cinfo, dest->image,
  199. (JDIMENSION) row, (JDIMENSION) 1, FALSE);
  200. red = rle_row[0];
  201. green = rle_row[1];
  202. blue = rle_row[2];
  203. for (col = cinfo->output_width; col > 0; col--) {
  204. *red++ = GETJSAMPLE(*output_row++);
  205. *green++ = GETJSAMPLE(*output_row++);
  206. *blue++ = GETJSAMPLE(*output_row++);
  207. }
  208. rle_putrow(rle_row, (int) cinfo->output_width, &header);
  209. #ifdef PROGRESS_REPORT
  210. if (progress != NULL) {
  211. progress->pub.pass_counter++;
  212. (*progress->pub.progress_monitor) ((j_common_ptr) cinfo);
  213. }
  214. #endif
  215. }
  216. }
  217. #ifdef PROGRESS_REPORT
  218. if (progress != NULL)
  219. progress->completed_extra_passes++;
  220. #endif
  221. /* Emit file trailer */
  222. rle_puteof(&header);
  223. fflush(dest->pub.output_file);
  224. if (ferror(dest->pub.output_file))
  225. ERREXIT(cinfo, JERR_FILE_WRITE);
  226. }
  227. /*
  228. * The module selection routine for RLE format output.
  229. */
  230. GLOBAL(djpeg_dest_ptr)
  231. jinit_write_rle (j_decompress_ptr cinfo)
  232. {
  233. rle_dest_ptr dest;
  234. /* Create module interface object, fill in method pointers */
  235. dest = (rle_dest_ptr)
  236. (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  237. SIZEOF(rle_dest_struct));
  238. dest->pub.start_output = start_output_rle;
  239. dest->pub.finish_output = finish_output_rle;
  240. /* Calculate output image dimensions so we can allocate space */
  241. jpeg_calc_output_dimensions(cinfo);
  242. /* Allocate a work array for output to the RLE library. */
  243. dest->rle_row = (*cinfo->mem->alloc_sarray)
  244. ((j_common_ptr) cinfo, JPOOL_IMAGE,
  245. cinfo->output_width, (JDIMENSION) cinfo->output_components);
  246. /* Allocate a virtual array to hold the image. */
  247. dest->image = (*cinfo->mem->request_virt_sarray)
  248. ((j_common_ptr) cinfo, JPOOL_IMAGE, FALSE,
  249. (JDIMENSION) (cinfo->output_width * cinfo->output_components),
  250. cinfo->output_height, (JDIMENSION) 1);
  251. return (djpeg_dest_ptr) dest;
  252. }
  253. #endif /* RLE_SUPPORTED */