blob: 5c98ec060efe359c1ebbfb675808104e84927c8e [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.
Leon Scroggins IIIbd7903e2018-02-28 14:05:04 -05006 * libjpeg-turbo Modifications:
7 * Copyright (C) 2017, D. R. Commander.
Alex Naidis6eb7d372016-10-16 23:10:08 +02008 * 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
Leon Scroggins III3993b372018-07-16 10:43:45 -040054#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
Alex Naidis6eb7d372016-10-16 23:10:08 +020065typedef rle_dest_struct *rle_dest_ptr;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000066
67/* Forward declarations */
Leon Scroggins III3993b372018-07-16 10:43:45 -040068METHODDEF(void) rle_put_pixel_rows(j_decompress_ptr cinfo,
69 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)
Leon Scroggins III3993b372018-07-16 10:43:45 -040080start_output_rle(j_decompress_ptr cinfo, djpeg_dest_ptr dinfo)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000081{
Leon Scroggins III3993b372018-07-16 10:43:45 -040082 rle_dest_ptr dest = (rle_dest_ptr)dinfo;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000083 size_t cmapsize;
84 int i, ci;
85#ifdef PROGRESS_REPORT
Leon Scroggins III3993b372018-07-16 10:43:45 -040086 cd_progress_ptr progress = (cd_progress_ptr)cinfo->progress;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000087#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);
Leon Scroggins III3993b372018-07-16 10:43:45 -0400120 dest->colormap = (rle_map *)(*cinfo->mem->alloc_small)
121 ((j_common_ptr)cinfo, JPOOL_IMAGE, cmapsize);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000122 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)
Leon Scroggins III3993b372018-07-16 10:43:45 -0400136 ((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)
Leon Scroggins III3993b372018-07-16 10:43:45 -0400156rle_put_pixel_rows(j_decompress_ptr cinfo, djpeg_dest_ptr dinfo,
157 JDIMENSION rows_supplied)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000158{
Leon Scroggins III3993b372018-07-16 10:43:45 -0400159 rle_dest_ptr dest = (rle_dest_ptr)dinfo;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000160
161 if (cinfo->output_scanline < cinfo->output_height) {
162 dest->pub.buffer = (*cinfo->mem->access_virt_sarray)
Leon Scroggins III3993b372018-07-16 10:43:45 -0400163 ((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)
Leon Scroggins III3993b372018-07-16 10:43:45 -0400175finish_output_rle(j_decompress_ptr cinfo, djpeg_dest_ptr dinfo)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000176{
Leon Scroggins III3993b372018-07-16 10:43:45 -0400177 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
Leon Scroggins III3993b372018-07-16 10:43:45 -0400185 cd_progress_ptr progress = (cd_progress_ptr)cinfo->progress;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000186#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. */
Leon Scroggins III3993b372018-07-16 10:43:45 -0400205 sprintf(cmapcomment, "color_map_length=%d",
206 cinfo->actual_number_of_colors);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000207 rle_putcom(cmapcomment, &header);
208 }
209
210 /* Emit the RLE header and color map (if any) */
211 rle_put_setup(&header);
212
213 /* Now output the RLE data from our virtual array.
DRC5033f3e2014-05-18 18:33:44 +0000214 * We assume here that rle_pixel is represented the same as JSAMPLE.
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000215 */
216
217#ifdef PROGRESS_REPORT
218 if (progress != NULL) {
219 progress->pub.pass_limit = cinfo->output_height;
220 progress->pub.pass_counter = 0;
Leon Scroggins III3993b372018-07-16 10:43:45 -0400221 (*progress->pub.progress_monitor) ((j_common_ptr)cinfo);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000222 }
223#endif
224
225 if (cinfo->output_components == 1) {
Leon Scroggins III3993b372018-07-16 10:43:45 -0400226 for (row = cinfo->output_height - 1; row >= 0; row--) {
227 rle_row = (rle_pixel **)(*cinfo->mem->access_virt_sarray)
228 ((j_common_ptr)cinfo, dest->image,
229 (JDIMENSION)row, (JDIMENSION)1, FALSE);
230 rle_putrow(rle_row, (int)cinfo->output_width, &header);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000231#ifdef PROGRESS_REPORT
232 if (progress != NULL) {
233 progress->pub.pass_counter++;
Leon Scroggins III3993b372018-07-16 10:43:45 -0400234 (*progress->pub.progress_monitor) ((j_common_ptr)cinfo);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000235 }
236#endif
237 }
238 } else {
Leon Scroggins III3993b372018-07-16 10:43:45 -0400239 for (row = cinfo->output_height - 1; row >= 0; row--) {
240 rle_row = (rle_pixel **)dest->rle_row;
Alex Naidis6eb7d372016-10-16 23:10:08 +0200241 output_row = *(*cinfo->mem->access_virt_sarray)
Leon Scroggins III3993b372018-07-16 10:43:45 -0400242 ((j_common_ptr)cinfo, dest->image,
243 (JDIMENSION)row, (JDIMENSION)1, FALSE);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000244 red = rle_row[0];
245 green = rle_row[1];
246 blue = rle_row[2];
247 for (col = cinfo->output_width; col > 0; col--) {
248 *red++ = GETJSAMPLE(*output_row++);
249 *green++ = GETJSAMPLE(*output_row++);
250 *blue++ = GETJSAMPLE(*output_row++);
251 }
Leon Scroggins III3993b372018-07-16 10:43:45 -0400252 rle_putrow(rle_row, (int)cinfo->output_width, &header);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000253#ifdef PROGRESS_REPORT
254 if (progress != NULL) {
255 progress->pub.pass_counter++;
Leon Scroggins III3993b372018-07-16 10:43:45 -0400256 (*progress->pub.progress_monitor) ((j_common_ptr)cinfo);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000257 }
258#endif
259 }
260 }
261
262#ifdef PROGRESS_REPORT
263 if (progress != NULL)
264 progress->completed_extra_passes++;
265#endif
266
267 /* Emit file trailer */
268 rle_puteof(&header);
269 fflush(dest->pub.output_file);
270 if (ferror(dest->pub.output_file))
271 ERREXIT(cinfo, JERR_FILE_WRITE);
272}
273
274
275/*
276 * The module selection routine for RLE format output.
277 */
278
Thomas G. Lane489583f1996-02-07 00:00:00 +0000279GLOBAL(djpeg_dest_ptr)
Leon Scroggins III3993b372018-07-16 10:43:45 -0400280jinit_write_rle(j_decompress_ptr cinfo)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000281{
282 rle_dest_ptr dest;
283
284 /* Create module interface object, fill in method pointers */
285 dest = (rle_dest_ptr)
Leon Scroggins III3993b372018-07-16 10:43:45 -0400286 (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
287 sizeof(rle_dest_struct));
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000288 dest->pub.start_output = start_output_rle;
289 dest->pub.finish_output = finish_output_rle;
Leon Scroggins IIIbd7903e2018-02-28 14:05:04 -0500290 dest->pub.calc_buffer_dimensions = NULL;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000291
292 /* Calculate output image dimensions so we can allocate space */
293 jpeg_calc_output_dimensions(cinfo);
294
295 /* Allocate a work array for output to the RLE library. */
296 dest->rle_row = (*cinfo->mem->alloc_sarray)
Leon Scroggins III3993b372018-07-16 10:43:45 -0400297 ((j_common_ptr)cinfo, JPOOL_IMAGE,
298 cinfo->output_width, (JDIMENSION)cinfo->output_components);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000299
300 /* Allocate a virtual array to hold the image. */
301 dest->image = (*cinfo->mem->request_virt_sarray)
Leon Scroggins III3993b372018-07-16 10:43:45 -0400302 ((j_common_ptr)cinfo, JPOOL_IMAGE, FALSE,
303 (JDIMENSION)(cinfo->output_width * cinfo->output_components),
304 cinfo->output_height, (JDIMENSION)1);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000305
Leon Scroggins III3993b372018-07-16 10:43:45 -0400306 return (djpeg_dest_ptr)dest;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000307}
308
309#endif /* RLE_SUPPORTED */