wrrle.c 9.2 KB

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