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