blob: cc95b413df1e5769fecd5d2f348ed3ab927b3b65 [file] [log] [blame]
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +00001/*
2 * wrrle.c
3 *
DRC5033f3e2014-05-18 18:33:44 +00004 * This file was part of the Independent JPEG Group's software:
Thomas G. Lane489583f1996-02-07 00:00:00 +00005 * Copyright (C) 1991-1996, Thomas G. Lane.
DRC5033f3e2014-05-18 18:33:44 +00006 * It was modified by The libjpeg-turbo Project to include only code and
7 * information relevant to libjpeg-turbo.
DRC7e3acc02015-10-10 10:25:46 -05008 * For conditions of distribution and use, see the accompanying README.ijg
9 * file.
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000010 *
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
DRCe5eaf372014-05-09 18:00:32 +000022#include "cdjpeg.h" /* Common decls for cjpeg/djpeg applications */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000023
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
DRCe5eaf372014-05-09 18:00:32 +000053#define CMAPBITS 8
54#define CMAPLENGTH (1<<(CMAPBITS))
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000055
56typedef struct {
57 struct djpeg_dest_struct pub; /* public fields */
58
DRCe5eaf372014-05-09 18:00:32 +000059 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() */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000062
63} rle_dest_struct;
64
DRCbd498032016-02-19 08:53:33 -060065typedef rle_dest_struct *rle_dest_ptr;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000066
67/* Forward declarations */
Thomas G. Lane489583f1996-02-07 00:00:00 +000068METHODDEF(void) rle_put_pixel_rows
DRCbc56b752014-05-16 10:43:44 +000069 (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo,
70 JDIMENSION rows_supplied);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000071
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
Thomas G. Lane489583f1996-02-07 00:00:00 +000079METHODDEF(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000080start_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)
DRCe5eaf372014-05-09 18:00:32 +0000103 ERREXIT2(cinfo, JERR_RLE_DIMENSIONS, cinfo->output_width,
104 cinfo->output_height);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000105
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 */
DRC5de454b2014-05-18 19:04:03 +0000119 cmapsize = cinfo->out_color_components * CMAPLENGTH * sizeof(rle_map);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000120 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)
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000136 ((j_common_ptr) cinfo, dest->image, (JDIMENSION) 0, (JDIMENSION) 1, TRUE);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000137 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
Thomas G. Lane489583f1996-02-07 00:00:00 +0000155METHODDEF(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000156rle_put_pixel_rows (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo,
DRCe5eaf372014-05-09 18:00:32 +0000157 JDIMENSION rows_supplied)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000158{
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)
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000163 ((j_common_ptr) cinfo, dest->image,
164 cinfo->output_scanline, (JDIMENSION) 1, TRUE);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000165 }
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
Thomas G. Lane489583f1996-02-07 00:00:00 +0000174METHODDEF(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000175finish_output_rle (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo)
176{
177 rle_dest_ptr dest = (rle_dest_ptr) dinfo;
DRCe5eaf372014-05-09 18:00:32 +0000178 rle_hdr header; /* Output file information */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000179 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.
DRC5033f3e2014-05-18 18:33:44 +0000213 * We assume here that rle_pixel is represented the same as JSAMPLE.
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000214 */
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)
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000227 ((j_common_ptr) cinfo, dest->image,
DRCe5eaf372014-05-09 18:00:32 +0000228 (JDIMENSION) row, (JDIMENSION) 1, FALSE);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000229 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;
DRCbd498032016-02-19 08:53:33 -0600240 output_row = *(*cinfo->mem->access_virt_sarray)
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000241 ((j_common_ptr) cinfo, dest->image,
DRCe5eaf372014-05-09 18:00:32 +0000242 (JDIMENSION) row, (JDIMENSION) 1, FALSE);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000243 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
Thomas G. Lane489583f1996-02-07 00:00:00 +0000278GLOBAL(djpeg_dest_ptr)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000279jinit_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,
DRC5de454b2014-05-18 19:04:03 +0000286 sizeof(rle_dest_struct));
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000287 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)
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000300 ((j_common_ptr) cinfo, JPOOL_IMAGE, FALSE,
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000301 (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 */