blob: 226c528943e4b3e344836f34ace1b42a6d6d27ae [file] [log] [blame]
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +00001/*
2 * rdrle.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.
DRC5de454b2014-05-18 19:04:03 +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 read input images in Utah 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 input from
16 * an ordinary stdio stream. They further assume that reading begins
17 * at the start of the file; start_input may need work if the
18 * user interface has already read some data (e.g., to determine that
19 * the file is indeed RLE format).
20 *
21 * Based on code contributed by Mike Lijewski,
22 * with updates from Robert Hutchinson.
23 */
24
DRCe5eaf372014-05-09 18:00:32 +000025#include "cdjpeg.h" /* Common decls for cjpeg/djpeg applications */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000026
27#ifdef RLE_SUPPORTED
28
29/* rle.h is provided by the Utah Raster Toolkit. */
30
31#include <rle.h>
32
33/*
34 * We assume that JSAMPLE has the same representation as rle_pixel,
35 * to wit, "unsigned char". Hence we can't cope with 12- or 16-bit samples.
36 */
37
38#if BITS_IN_JSAMPLE != 8
39 Sorry, this code only copes with 8-bit JSAMPLEs. /* deliberate syntax err */
40#endif
41
42/*
43 * We support the following types of RLE files:
DRCe5eaf372014-05-09 18:00:32 +000044 *
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000045 * GRAYSCALE - 8 bits, no colormap
46 * MAPPEDGRAY - 8 bits, 1 channel colomap
47 * PSEUDOCOLOR - 8 bits, 3 channel colormap
48 * TRUECOLOR - 24 bits, 3 channel colormap
49 * DIRECTCOLOR - 24 bits, no colormap
50 *
51 * For now, we ignore any alpha channel in the image.
52 */
53
54typedef enum
55 { GRAYSCALE, MAPPEDGRAY, PSEUDOCOLOR, TRUECOLOR, DIRECTCOLOR } rle_kind;
56
57
58/*
59 * Since RLE stores scanlines bottom-to-top, we have to invert the image
60 * to conform to JPEG's top-to-bottom order. To do this, we read the
61 * incoming image into a virtual array on the first get_pixel_rows call,
62 * then fetch the required row from the virtual array on subsequent calls.
63 */
64
DRCbd498032016-02-19 08:53:33 -060065typedef struct _rle_source_struct *rle_source_ptr;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000066
67typedef struct _rle_source_struct {
68 struct cjpeg_source_struct pub; /* public fields */
69
70 rle_kind visual; /* actual type of input file */
71 jvirt_sarray_ptr image; /* virtual array to hold the image */
DRCe5eaf372014-05-09 18:00:32 +000072 JDIMENSION row; /* current row # in the virtual array */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000073 rle_hdr header; /* Input file information */
DRCf76c01d2016-02-19 10:56:13 -060074 rle_pixel **rle_row; /* holds a row returned by rle_getrow() */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000075
76} rle_source_struct;
77
78
79/*
80 * Read the file header; return image size and component count.
81 */
82
Thomas G. Lane489583f1996-02-07 00:00:00 +000083METHODDEF(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000084start_input_rle (j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
85{
86 rle_source_ptr source = (rle_source_ptr) sinfo;
87 JDIMENSION width, height;
88#ifdef PROGRESS_REPORT
89 cd_progress_ptr progress = (cd_progress_ptr) cinfo->progress;
90#endif
91
92 /* Use RLE library routine to get the header info */
93 source->header = *rle_hdr_init(NULL);
94 source->header.rle_file = source->pub.input_file;
95 switch (rle_get_setup(&(source->header))) {
96 case RLE_SUCCESS:
97 /* A-OK */
98 break;
99 case RLE_NOT_RLE:
100 ERREXIT(cinfo, JERR_RLE_NOT);
101 break;
102 case RLE_NO_SPACE:
103 ERREXIT(cinfo, JERR_RLE_MEM);
104 break;
105 case RLE_EMPTY:
106 ERREXIT(cinfo, JERR_RLE_EMPTY);
107 break;
108 case RLE_EOF:
109 ERREXIT(cinfo, JERR_RLE_EOF);
110 break;
111 default:
112 ERREXIT(cinfo, JERR_RLE_BADERROR);
113 break;
114 }
115
116 /* Figure out what we have, set private vars and return values accordingly */
DRCe5eaf372014-05-09 18:00:32 +0000117
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000118 width = source->header.xmax - source->header.xmin + 1;
119 height = source->header.ymax - source->header.ymin + 1;
DRCe5eaf372014-05-09 18:00:32 +0000120 source->header.xmin = 0; /* realign horizontally */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000121 source->header.xmax = width-1;
122
123 cinfo->image_width = width;
124 cinfo->image_height = height;
125 cinfo->data_precision = 8; /* we can only handle 8 bit data */
126
127 if (source->header.ncolors == 1 && source->header.ncmap == 0) {
128 source->visual = GRAYSCALE;
129 TRACEMS2(cinfo, 1, JTRC_RLE_GRAY, width, height);
130 } else if (source->header.ncolors == 1 && source->header.ncmap == 1) {
131 source->visual = MAPPEDGRAY;
132 TRACEMS3(cinfo, 1, JTRC_RLE_MAPGRAY, width, height,
133 1 << source->header.cmaplen);
134 } else if (source->header.ncolors == 1 && source->header.ncmap == 3) {
135 source->visual = PSEUDOCOLOR;
136 TRACEMS3(cinfo, 1, JTRC_RLE_MAPPED, width, height,
DRCe5eaf372014-05-09 18:00:32 +0000137 1 << source->header.cmaplen);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000138 } else if (source->header.ncolors == 3 && source->header.ncmap == 3) {
139 source->visual = TRUECOLOR;
140 TRACEMS3(cinfo, 1, JTRC_RLE_FULLMAP, width, height,
DRCe5eaf372014-05-09 18:00:32 +0000141 1 << source->header.cmaplen);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000142 } else if (source->header.ncolors == 3 && source->header.ncmap == 0) {
143 source->visual = DIRECTCOLOR;
144 TRACEMS2(cinfo, 1, JTRC_RLE, width, height);
145 } else
146 ERREXIT(cinfo, JERR_RLE_UNSUPPORTED);
DRCe5eaf372014-05-09 18:00:32 +0000147
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000148 if (source->visual == GRAYSCALE || source->visual == MAPPEDGRAY) {
149 cinfo->in_color_space = JCS_GRAYSCALE;
150 cinfo->input_components = 1;
151 } else {
152 cinfo->in_color_space = JCS_RGB;
153 cinfo->input_components = 3;
154 }
155
156 /*
157 * A place to hold each scanline while it's converted.
158 * (GRAYSCALE scanlines don't need converting)
159 */
160 if (source->visual != GRAYSCALE) {
161 source->rle_row = (rle_pixel**) (*cinfo->mem->alloc_sarray)
162 ((j_common_ptr) cinfo, JPOOL_IMAGE,
163 (JDIMENSION) width, (JDIMENSION) cinfo->input_components);
164 }
165
166 /* request a virtual array to hold the image */
167 source->image = (*cinfo->mem->request_virt_sarray)
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000168 ((j_common_ptr) cinfo, JPOOL_IMAGE, FALSE,
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000169 (JDIMENSION) (width * source->header.ncolors),
170 (JDIMENSION) height, (JDIMENSION) 1);
171
172#ifdef PROGRESS_REPORT
173 if (progress != NULL) {
174 /* count file input as separate pass */
175 progress->total_extra_passes++;
176 }
177#endif
178
179 source->pub.buffer_height = 1;
180}
181
182
183/*
184 * Read one row of pixels.
185 * Called only after load_image has read the image into the virtual array.
186 * Used for GRAYSCALE, MAPPEDGRAY, TRUECOLOR, and DIRECTCOLOR images.
187 */
188
Thomas G. Lane489583f1996-02-07 00:00:00 +0000189METHODDEF(JDIMENSION)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000190get_rle_row (j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
191{
192 rle_source_ptr source = (rle_source_ptr) sinfo;
193
Thomas G. Lane9ba2f5e1994-12-07 00:00:00 +0000194 source->row--;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000195 source->pub.buffer = (*cinfo->mem->access_virt_sarray)
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000196 ((j_common_ptr) cinfo, source->image, source->row, (JDIMENSION) 1, FALSE);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000197
198 return 1;
199}
200
201/*
202 * Read one row of pixels.
203 * Called only after load_image has read the image into the virtual array.
204 * Used for PSEUDOCOLOR images.
205 */
206
Thomas G. Lane489583f1996-02-07 00:00:00 +0000207METHODDEF(JDIMENSION)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000208get_pseudocolor_row (j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
209{
210 rle_source_ptr source = (rle_source_ptr) sinfo;
211 JSAMPROW src_row, dest_row;
212 JDIMENSION col;
213 rle_map *colormap;
214 int val;
215
216 colormap = source->header.cmap;
217 dest_row = source->pub.buffer[0];
Thomas G. Lane9ba2f5e1994-12-07 00:00:00 +0000218 source->row--;
DRCbd498032016-02-19 08:53:33 -0600219 src_row = *(*cinfo->mem->access_virt_sarray)
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000220 ((j_common_ptr) cinfo, source->image, source->row, (JDIMENSION) 1, FALSE);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000221
222 for (col = cinfo->image_width; col > 0; col--) {
223 val = GETJSAMPLE(*src_row++);
Thomas G. Lane9ba2f5e1994-12-07 00:00:00 +0000224 *dest_row++ = (JSAMPLE) (colormap[val ] >> 8);
225 *dest_row++ = (JSAMPLE) (colormap[val + 256] >> 8);
226 *dest_row++ = (JSAMPLE) (colormap[val + 512] >> 8);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000227 }
228
229 return 1;
230}
231
232
233/*
234 * Load the image into a virtual array. We have to do this because RLE
235 * files start at the lower left while the JPEG standard has them starting
236 * in the upper left. This is called the first time we want to get a row
237 * of input. What we do is load the RLE data into the array and then call
238 * the appropriate routine to read one row from the array. Before returning,
239 * we set source->pub.get_pixel_rows so that subsequent calls go straight to
240 * the appropriate row-reading routine.
241 */
242
Thomas G. Lane489583f1996-02-07 00:00:00 +0000243METHODDEF(JDIMENSION)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000244load_image (j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
245{
246 rle_source_ptr source = (rle_source_ptr) sinfo;
247 JDIMENSION row, col;
248 JSAMPROW scanline, red_ptr, green_ptr, blue_ptr;
249 rle_pixel **rle_row;
250 rle_map *colormap;
251 char channel;
252#ifdef PROGRESS_REPORT
253 cd_progress_ptr progress = (cd_progress_ptr) cinfo->progress;
254#endif
255
256 colormap = source->header.cmap;
257 rle_row = source->rle_row;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000258
259 /* Read the RLE data into our virtual array.
DRC5033f3e2014-05-18 18:33:44 +0000260 * We assume here that rle_pixel is represented the same as JSAMPLE.
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000261 */
262 RLE_CLR_BIT(source->header, RLE_ALPHA); /* don't read the alpha channel */
263
264#ifdef PROGRESS_REPORT
265 if (progress != NULL) {
266 progress->pub.pass_limit = cinfo->image_height;
267 progress->pub.pass_counter = 0;
268 (*progress->pub.progress_monitor) ((j_common_ptr) cinfo);
269 }
270#endif
271
272 switch (source->visual) {
273
274 case GRAYSCALE:
275 case PSEUDOCOLOR:
Thomas G. Lane9ba2f5e1994-12-07 00:00:00 +0000276 for (row = 0; row < cinfo->image_height; row++) {
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000277 rle_row = (rle_pixel **) (*cinfo->mem->access_virt_sarray)
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000278 ((j_common_ptr) cinfo, source->image, row, (JDIMENSION) 1, TRUE);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000279 rle_getrow(&source->header, rle_row);
280#ifdef PROGRESS_REPORT
281 if (progress != NULL) {
282 progress->pub.pass_counter++;
283 (*progress->pub.progress_monitor) ((j_common_ptr) cinfo);
284 }
285#endif
286 }
287 break;
288
289 case MAPPEDGRAY:
290 case TRUECOLOR:
Thomas G. Lane9ba2f5e1994-12-07 00:00:00 +0000291 for (row = 0; row < cinfo->image_height; row++) {
DRCbd498032016-02-19 08:53:33 -0600292 scanline = *(*cinfo->mem->access_virt_sarray)
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000293 ((j_common_ptr) cinfo, source->image, row, (JDIMENSION) 1, TRUE);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000294 rle_row = source->rle_row;
295 rle_getrow(&source->header, rle_row);
296
297 for (col = 0; col < cinfo->image_width; col++) {
298 for (channel = 0; channel < source->header.ncolors; channel++) {
299 *scanline++ = (JSAMPLE)
Thomas G. Lane9ba2f5e1994-12-07 00:00:00 +0000300 (colormap[GETJSAMPLE(rle_row[channel][col]) + 256 * channel] >> 8);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000301 }
302 }
303
304#ifdef PROGRESS_REPORT
305 if (progress != NULL) {
306 progress->pub.pass_counter++;
307 (*progress->pub.progress_monitor) ((j_common_ptr) cinfo);
308 }
309#endif
310 }
311 break;
312
313 case DIRECTCOLOR:
Thomas G. Lane9ba2f5e1994-12-07 00:00:00 +0000314 for (row = 0; row < cinfo->image_height; row++) {
DRCbd498032016-02-19 08:53:33 -0600315 scanline = *(*cinfo->mem->access_virt_sarray)
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000316 ((j_common_ptr) cinfo, source->image, row, (JDIMENSION) 1, TRUE);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000317 rle_getrow(&source->header, rle_row);
318
319 red_ptr = rle_row[0];
320 green_ptr = rle_row[1];
321 blue_ptr = rle_row[2];
322
323 for (col = cinfo->image_width; col > 0; col--) {
324 *scanline++ = *red_ptr++;
325 *scanline++ = *green_ptr++;
326 *scanline++ = *blue_ptr++;
327 }
328
329#ifdef PROGRESS_REPORT
330 if (progress != NULL) {
331 progress->pub.pass_counter++;
332 (*progress->pub.progress_monitor) ((j_common_ptr) cinfo);
333 }
334#endif
335 }
336 }
337
338#ifdef PROGRESS_REPORT
339 if (progress != NULL)
340 progress->completed_extra_passes++;
341#endif
342
343 /* Set up to call proper row-extraction routine in future */
344 if (source->visual == PSEUDOCOLOR) {
345 source->pub.buffer = source->rle_row;
346 source->pub.get_pixel_rows = get_pseudocolor_row;
347 } else {
348 source->pub.get_pixel_rows = get_rle_row;
349 }
Thomas G. Lane9ba2f5e1994-12-07 00:00:00 +0000350 source->row = cinfo->image_height;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000351
352 /* And fetch the topmost (bottommost) row */
DRCe5eaf372014-05-09 18:00:32 +0000353 return (*source->pub.get_pixel_rows) (cinfo, sinfo);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000354}
355
356
357/*
358 * Finish up at the end of the file.
359 */
360
Thomas G. Lane489583f1996-02-07 00:00:00 +0000361METHODDEF(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000362finish_input_rle (j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
363{
364 /* no work */
365}
366
367
368/*
369 * The module selection routine for RLE format input.
370 */
371
Thomas G. Lane489583f1996-02-07 00:00:00 +0000372GLOBAL(cjpeg_source_ptr)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000373jinit_read_rle (j_compress_ptr cinfo)
374{
375 rle_source_ptr source;
376
377 /* Create module interface object */
378 source = (rle_source_ptr)
379 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
DRC5de454b2014-05-18 19:04:03 +0000380 sizeof(rle_source_struct));
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000381 /* Fill in method ptrs */
382 source->pub.start_input = start_input_rle;
383 source->pub.finish_input = finish_input_rle;
384 source->pub.get_pixel_rows = load_image;
385
386 return (cjpeg_source_ptr) source;
387}
388
389#endif /* RLE_SUPPORTED */