wrrle.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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. * It was modified by The libjpeg-turbo Project to include only code and
  7. * information relevant to libjpeg-turbo.
  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
  53. (j_decompress_ptr cinfo, 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", cinfo->actual_number_of_colors);
  169. rle_putcom(cmapcomment, &header);
  170. }
  171. /* Emit the RLE header and color map (if any) */
  172. rle_put_setup(&header);
  173. /* Now output the RLE data from our virtual array.
  174. * We assume here that rle_pixel is represented the same as JSAMPLE.
  175. */
  176. #ifdef PROGRESS_REPORT
  177. if (progress != NULL) {
  178. progress->pub.pass_limit = cinfo->output_height;
  179. progress->pub.pass_counter = 0;
  180. (*progress->pub.progress_monitor) ((j_common_ptr) cinfo);
  181. }
  182. #endif
  183. if (cinfo->output_components == 1) {
  184. for (row = cinfo->output_height-1; row >= 0; row--) {
  185. rle_row = (rle_pixel **) (*cinfo->mem->access_virt_sarray)
  186. ((j_common_ptr) cinfo, dest->image,
  187. (JDIMENSION) row, (JDIMENSION) 1, FALSE);
  188. rle_putrow(rle_row, (int) cinfo->output_width, &header);
  189. #ifdef PROGRESS_REPORT
  190. if (progress != NULL) {
  191. progress->pub.pass_counter++;
  192. (*progress->pub.progress_monitor) ((j_common_ptr) cinfo);
  193. }
  194. #endif
  195. }
  196. } else {
  197. for (row = cinfo->output_height-1; row >= 0; row--) {
  198. rle_row = (rle_pixel **) dest->rle_row;
  199. output_row = *(*cinfo->mem->access_virt_sarray)
  200. ((j_common_ptr) cinfo, dest->image,
  201. (JDIMENSION) row, (JDIMENSION) 1, FALSE);
  202. red = rle_row[0];
  203. green = rle_row[1];
  204. blue = rle_row[2];
  205. for (col = cinfo->output_width; col > 0; col--) {
  206. *red++ = GETJSAMPLE(*output_row++);
  207. *green++ = GETJSAMPLE(*output_row++);
  208. *blue++ = GETJSAMPLE(*output_row++);
  209. }
  210. rle_putrow(rle_row, (int) cinfo->output_width, &header);
  211. #ifdef PROGRESS_REPORT
  212. if (progress != NULL) {
  213. progress->pub.pass_counter++;
  214. (*progress->pub.progress_monitor) ((j_common_ptr) cinfo);
  215. }
  216. #endif
  217. }
  218. }
  219. #ifdef PROGRESS_REPORT
  220. if (progress != NULL)
  221. progress->completed_extra_passes++;
  222. #endif
  223. /* Emit file trailer */
  224. rle_puteof(&header);
  225. fflush(dest->pub.output_file);
  226. if (ferror(dest->pub.output_file))
  227. ERREXIT(cinfo, JERR_FILE_WRITE);
  228. }
  229. /*
  230. * The module selection routine for RLE format output.
  231. */
  232. GLOBAL(djpeg_dest_ptr)
  233. jinit_write_rle (j_decompress_ptr cinfo)
  234. {
  235. rle_dest_ptr dest;
  236. /* Create module interface object, fill in method pointers */
  237. dest = (rle_dest_ptr)
  238. (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  239. sizeof(rle_dest_struct));
  240. dest->pub.start_output = start_output_rle;
  241. dest->pub.finish_output = finish_output_rle;
  242. /* Calculate output image dimensions so we can allocate space */
  243. jpeg_calc_output_dimensions(cinfo);
  244. /* Allocate a work array for output to the RLE library. */
  245. dest->rle_row = (*cinfo->mem->alloc_sarray)
  246. ((j_common_ptr) cinfo, JPOOL_IMAGE,
  247. cinfo->output_width, (JDIMENSION) cinfo->output_components);
  248. /* Allocate a virtual array to hold the image. */
  249. dest->image = (*cinfo->mem->request_virt_sarray)
  250. ((j_common_ptr) cinfo, JPOOL_IMAGE, FALSE,
  251. (JDIMENSION) (cinfo->output_width * cinfo->output_components),
  252. cinfo->output_height, (JDIMENSION) 1);
  253. return (djpeg_dest_ptr) dest;
  254. }
  255. #endif /* RLE_SUPPORTED */