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 file.
10 * This file contains routines to write output images in RLE format.
11 * The Utah Raster Toolkit library is required (version 3.1 or later).
13 * These routines may need modification for non-Unix environments or
14 * specialized applications. As they stand, they assume output to
15 * an ordinary stdio stream.
17 * Based on code contributed by Mike Lijewski,
18 * with updates from Robert Hutchinson.
21 #include "cdjpeg.h" /* Common decls for cjpeg/djpeg applications */
25 /* rle.h is provided by the Utah Raster Toolkit. */
30 * We assume that JSAMPLE has the same representation as rle_pixel,
31 * to wit, "unsigned char". Hence we can't cope with 12- or 16-bit samples.
34 #if BITS_IN_JSAMPLE != 8
35 Sorry, this code only copes with 8-bit JSAMPLEs. /* deliberate syntax err */
40 * Since RLE stores scanlines bottom-to-top, we have to invert the image
41 * from JPEG's top-to-bottom order. To do this, we save the outgoing data
42 * in a virtual array during put_pixel_row calls, then actually emit the
43 * RLE file during finish_output.
48 * For now, if we emit an RLE color map then it is always 256 entries long,
49 * though not all of the entries need be used.
53 #define CMAPLENGTH (1<<(CMAPBITS))
56 struct djpeg_dest_struct pub; /* public fields */
58 jvirt_sarray_ptr image; /* virtual array to store the output image */
59 rle_map *colormap; /* RLE-style color map, or NULL if none */
60 rle_pixel **rle_row; /* To pass rows to rle_putrow() */
64 typedef rle_dest_struct * rle_dest_ptr;
66 /* Forward declarations */
67 METHODDEF(void) rle_put_pixel_rows
68 (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo,
69 JDIMENSION rows_supplied);
73 * Write the file header.
75 * In this module it's easier to wait till finish_output to write anything.
79 start_output_rle (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo)
81 rle_dest_ptr dest = (rle_dest_ptr) dinfo;
84 #ifdef PROGRESS_REPORT
85 cd_progress_ptr progress = (cd_progress_ptr) cinfo->progress;
89 * Make sure the image can be stored in RLE format.
91 * - RLE stores image dimensions as *signed* 16 bit integers. JPEG
92 * uses unsigned, so we have to check the width.
94 * - Colorspace is expected to be grayscale or RGB.
96 * - The number of channels (components) is expected to be 1 (grayscale/
97 * pseudocolor) or 3 (truecolor/directcolor).
98 * (could be 2 or 4 if using an alpha channel, but we aren't)
101 if (cinfo->output_width > 32767 || cinfo->output_height > 32767)
102 ERREXIT2(cinfo, JERR_RLE_DIMENSIONS, cinfo->output_width,
103 cinfo->output_height);
105 if (cinfo->out_color_space != JCS_GRAYSCALE &&
106 cinfo->out_color_space != JCS_RGB)
107 ERREXIT(cinfo, JERR_RLE_COLORSPACE);
109 if (cinfo->output_components != 1 && cinfo->output_components != 3)
110 ERREXIT1(cinfo, JERR_RLE_TOOMANYCHANNELS, cinfo->num_components);
112 /* Convert colormap, if any, to RLE format. */
114 dest->colormap = NULL;
116 if (cinfo->quantize_colors) {
117 /* Allocate storage for RLE-style cmap, zero any extra entries */
118 cmapsize = cinfo->out_color_components * CMAPLENGTH * sizeof(rle_map);
119 dest->colormap = (rle_map *) (*cinfo->mem->alloc_small)
120 ((j_common_ptr) cinfo, JPOOL_IMAGE, cmapsize);
121 MEMZERO(dest->colormap, cmapsize);
123 /* Save away data in RLE format --- note 8-bit left shift! */
124 /* Shifting would need adjustment for JSAMPLEs wider than 8 bits. */
125 for (ci = 0; ci < cinfo->out_color_components; ci++) {
126 for (i = 0; i < cinfo->actual_number_of_colors; i++) {
127 dest->colormap[ci * CMAPLENGTH + i] =
128 GETJSAMPLE(cinfo->colormap[ci][i]) << 8;
133 /* Set the output buffer to the first row */
134 dest->pub.buffer = (*cinfo->mem->access_virt_sarray)
135 ((j_common_ptr) cinfo, dest->image, (JDIMENSION) 0, (JDIMENSION) 1, TRUE);
136 dest->pub.buffer_height = 1;
138 dest->pub.put_pixel_rows = rle_put_pixel_rows;
140 #ifdef PROGRESS_REPORT
141 if (progress != NULL) {
142 progress->total_extra_passes++; /* count file writing as separate pass */
149 * Write some pixel data.
151 * This routine just saves the data away in a virtual array.
155 rle_put_pixel_rows (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo,
156 JDIMENSION rows_supplied)
158 rle_dest_ptr dest = (rle_dest_ptr) dinfo;
160 if (cinfo->output_scanline < cinfo->output_height) {
161 dest->pub.buffer = (*cinfo->mem->access_virt_sarray)
162 ((j_common_ptr) cinfo, dest->image,
163 cinfo->output_scanline, (JDIMENSION) 1, TRUE);
168 * Finish up at the end of the file.
170 * Here is where we really output the RLE file.
174 finish_output_rle (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo)
176 rle_dest_ptr dest = (rle_dest_ptr) dinfo;
177 rle_hdr header; /* Output file information */
178 rle_pixel **rle_row, *red, *green, *blue;
180 char cmapcomment[80];
183 #ifdef PROGRESS_REPORT
184 cd_progress_ptr progress = (cd_progress_ptr) cinfo->progress;
187 /* Initialize the header info */
188 header = *rle_hdr_init(NULL);
189 header.rle_file = dest->pub.output_file;
191 header.xmax = cinfo->output_width - 1;
193 header.ymax = cinfo->output_height - 1;
195 header.ncolors = cinfo->output_components;
196 for (ci = 0; ci < cinfo->output_components; ci++) {
197 RLE_SET_BIT(header, ci);
199 if (cinfo->quantize_colors) {
200 header.ncmap = cinfo->out_color_components;
201 header.cmaplen = CMAPBITS;
202 header.cmap = dest->colormap;
203 /* Add a comment to the output image with the true colormap length. */
204 sprintf(cmapcomment, "color_map_length=%d", cinfo->actual_number_of_colors);
205 rle_putcom(cmapcomment, &header);
208 /* Emit the RLE header and color map (if any) */
209 rle_put_setup(&header);
211 /* Now output the RLE data from our virtual array.
212 * We assume here that rle_pixel is represented the same as JSAMPLE.
215 #ifdef PROGRESS_REPORT
216 if (progress != NULL) {
217 progress->pub.pass_limit = cinfo->output_height;
218 progress->pub.pass_counter = 0;
219 (*progress->pub.progress_monitor) ((j_common_ptr) cinfo);
223 if (cinfo->output_components == 1) {
224 for (row = cinfo->output_height-1; row >= 0; row--) {
225 rle_row = (rle_pixel **) (*cinfo->mem->access_virt_sarray)
226 ((j_common_ptr) cinfo, dest->image,
227 (JDIMENSION) row, (JDIMENSION) 1, FALSE);
228 rle_putrow(rle_row, (int) cinfo->output_width, &header);
229 #ifdef PROGRESS_REPORT
230 if (progress != NULL) {
231 progress->pub.pass_counter++;
232 (*progress->pub.progress_monitor) ((j_common_ptr) cinfo);
237 for (row = cinfo->output_height-1; row >= 0; row--) {
238 rle_row = (rle_pixel **) dest->rle_row;
239 output_row = * (*cinfo->mem->access_virt_sarray)
240 ((j_common_ptr) cinfo, dest->image,
241 (JDIMENSION) row, (JDIMENSION) 1, FALSE);
245 for (col = cinfo->output_width; col > 0; col--) {
246 *red++ = GETJSAMPLE(*output_row++);
247 *green++ = GETJSAMPLE(*output_row++);
248 *blue++ = GETJSAMPLE(*output_row++);
250 rle_putrow(rle_row, (int) cinfo->output_width, &header);
251 #ifdef PROGRESS_REPORT
252 if (progress != NULL) {
253 progress->pub.pass_counter++;
254 (*progress->pub.progress_monitor) ((j_common_ptr) cinfo);
260 #ifdef PROGRESS_REPORT
261 if (progress != NULL)
262 progress->completed_extra_passes++;
265 /* Emit file trailer */
267 fflush(dest->pub.output_file);
268 if (ferror(dest->pub.output_file))
269 ERREXIT(cinfo, JERR_FILE_WRITE);
274 * The module selection routine for RLE format output.
277 GLOBAL(djpeg_dest_ptr)
278 jinit_write_rle (j_decompress_ptr cinfo)
282 /* Create module interface object, fill in method pointers */
283 dest = (rle_dest_ptr)
284 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
285 sizeof(rle_dest_struct));
286 dest->pub.start_output = start_output_rle;
287 dest->pub.finish_output = finish_output_rle;
289 /* Calculate output image dimensions so we can allocate space */
290 jpeg_calc_output_dimensions(cinfo);
292 /* Allocate a work array for output to the RLE library. */
293 dest->rle_row = (*cinfo->mem->alloc_sarray)
294 ((j_common_ptr) cinfo, JPOOL_IMAGE,
295 cinfo->output_width, (JDIMENSION) cinfo->output_components);
297 /* Allocate a virtual array to hold the image. */
298 dest->image = (*cinfo->mem->request_virt_sarray)
299 ((j_common_ptr) cinfo, JPOOL_IMAGE, FALSE,
300 (JDIMENSION) (cinfo->output_width * cinfo->output_components),
301 cinfo->output_height, (JDIMENSION) 1);
303 return (djpeg_dest_ptr) dest;
306 #endif /* RLE_SUPPORTED */