blob: 542bc37490cc563f89da25a686144099984496d8 [file] [log] [blame]
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +00001/*
2 * rdrle.c
3 *
Thomas G. Lane489583f1996-02-07 00:00:00 +00004 * Copyright (C) 1991-1996, Thomas G. Lane.
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +00005 * This file is part of the Independent JPEG Group's software.
6 * For conditions of distribution and use, see the accompanying README file.
7 *
8 * This file contains routines to read input images in Utah RLE format.
9 * The Utah Raster Toolkit library is required (version 3.1 or later).
10 *
11 * These routines may need modification for non-Unix environments or
12 * specialized applications. As they stand, they assume input from
13 * an ordinary stdio stream. They further assume that reading begins
14 * at the start of the file; start_input may need work if the
15 * user interface has already read some data (e.g., to determine that
16 * the file is indeed RLE format).
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 * We support the following types of RLE files:
41 *
42 * GRAYSCALE - 8 bits, no colormap
43 * MAPPEDGRAY - 8 bits, 1 channel colomap
44 * PSEUDOCOLOR - 8 bits, 3 channel colormap
45 * TRUECOLOR - 24 bits, 3 channel colormap
46 * DIRECTCOLOR - 24 bits, no colormap
47 *
48 * For now, we ignore any alpha channel in the image.
49 */
50
51typedef enum
52 { GRAYSCALE, MAPPEDGRAY, PSEUDOCOLOR, TRUECOLOR, DIRECTCOLOR } rle_kind;
53
54
55/*
56 * Since RLE stores scanlines bottom-to-top, we have to invert the image
57 * to conform to JPEG's top-to-bottom order. To do this, we read the
58 * incoming image into a virtual array on the first get_pixel_rows call,
59 * then fetch the required row from the virtual array on subsequent calls.
60 */
61
62typedef struct _rle_source_struct * rle_source_ptr;
63
64typedef struct _rle_source_struct {
65 struct cjpeg_source_struct pub; /* public fields */
66
67 rle_kind visual; /* actual type of input file */
68 jvirt_sarray_ptr image; /* virtual array to hold the image */
Thomas G. Lane9ba2f5e1994-12-07 00:00:00 +000069 JDIMENSION row; /* current row # in the virtual array */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000070 rle_hdr header; /* Input file information */
71 rle_pixel** rle_row; /* holds a row returned by rle_getrow() */
72
73} rle_source_struct;
74
75
76/*
77 * Read the file header; return image size and component count.
78 */
79
Thomas G. Lane489583f1996-02-07 00:00:00 +000080METHODDEF(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000081start_input_rle (j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
82{
83 rle_source_ptr source = (rle_source_ptr) sinfo;
84 JDIMENSION width, height;
85#ifdef PROGRESS_REPORT
86 cd_progress_ptr progress = (cd_progress_ptr) cinfo->progress;
87#endif
88
89 /* Use RLE library routine to get the header info */
90 source->header = *rle_hdr_init(NULL);
91 source->header.rle_file = source->pub.input_file;
92 switch (rle_get_setup(&(source->header))) {
93 case RLE_SUCCESS:
94 /* A-OK */
95 break;
96 case RLE_NOT_RLE:
97 ERREXIT(cinfo, JERR_RLE_NOT);
98 break;
99 case RLE_NO_SPACE:
100 ERREXIT(cinfo, JERR_RLE_MEM);
101 break;
102 case RLE_EMPTY:
103 ERREXIT(cinfo, JERR_RLE_EMPTY);
104 break;
105 case RLE_EOF:
106 ERREXIT(cinfo, JERR_RLE_EOF);
107 break;
108 default:
109 ERREXIT(cinfo, JERR_RLE_BADERROR);
110 break;
111 }
112
113 /* Figure out what we have, set private vars and return values accordingly */
114
115 width = source->header.xmax - source->header.xmin + 1;
116 height = source->header.ymax - source->header.ymin + 1;
117 source->header.xmin = 0; /* realign horizontally */
118 source->header.xmax = width-1;
119
120 cinfo->image_width = width;
121 cinfo->image_height = height;
122 cinfo->data_precision = 8; /* we can only handle 8 bit data */
123
124 if (source->header.ncolors == 1 && source->header.ncmap == 0) {
125 source->visual = GRAYSCALE;
126 TRACEMS2(cinfo, 1, JTRC_RLE_GRAY, width, height);
127 } else if (source->header.ncolors == 1 && source->header.ncmap == 1) {
128 source->visual = MAPPEDGRAY;
129 TRACEMS3(cinfo, 1, JTRC_RLE_MAPGRAY, width, height,
130 1 << source->header.cmaplen);
131 } else if (source->header.ncolors == 1 && source->header.ncmap == 3) {
132 source->visual = PSEUDOCOLOR;
133 TRACEMS3(cinfo, 1, JTRC_RLE_MAPPED, width, height,
134 1 << source->header.cmaplen);
135 } else if (source->header.ncolors == 3 && source->header.ncmap == 3) {
136 source->visual = TRUECOLOR;
137 TRACEMS3(cinfo, 1, JTRC_RLE_FULLMAP, width, height,
138 1 << source->header.cmaplen);
139 } else if (source->header.ncolors == 3 && source->header.ncmap == 0) {
140 source->visual = DIRECTCOLOR;
141 TRACEMS2(cinfo, 1, JTRC_RLE, width, height);
142 } else
143 ERREXIT(cinfo, JERR_RLE_UNSUPPORTED);
144
145 if (source->visual == GRAYSCALE || source->visual == MAPPEDGRAY) {
146 cinfo->in_color_space = JCS_GRAYSCALE;
147 cinfo->input_components = 1;
148 } else {
149 cinfo->in_color_space = JCS_RGB;
150 cinfo->input_components = 3;
151 }
152
153 /*
154 * A place to hold each scanline while it's converted.
155 * (GRAYSCALE scanlines don't need converting)
156 */
157 if (source->visual != GRAYSCALE) {
158 source->rle_row = (rle_pixel**) (*cinfo->mem->alloc_sarray)
159 ((j_common_ptr) cinfo, JPOOL_IMAGE,
160 (JDIMENSION) width, (JDIMENSION) cinfo->input_components);
161 }
162
163 /* request a virtual array to hold the image */
164 source->image = (*cinfo->mem->request_virt_sarray)
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000165 ((j_common_ptr) cinfo, JPOOL_IMAGE, FALSE,
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000166 (JDIMENSION) (width * source->header.ncolors),
167 (JDIMENSION) height, (JDIMENSION) 1);
168
169#ifdef PROGRESS_REPORT
170 if (progress != NULL) {
171 /* count file input as separate pass */
172 progress->total_extra_passes++;
173 }
174#endif
175
176 source->pub.buffer_height = 1;
177}
178
179
180/*
181 * Read one row of pixels.
182 * Called only after load_image has read the image into the virtual array.
183 * Used for GRAYSCALE, MAPPEDGRAY, TRUECOLOR, and DIRECTCOLOR images.
184 */
185
Thomas G. Lane489583f1996-02-07 00:00:00 +0000186METHODDEF(JDIMENSION)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000187get_rle_row (j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
188{
189 rle_source_ptr source = (rle_source_ptr) sinfo;
190
Thomas G. Lane9ba2f5e1994-12-07 00:00:00 +0000191 source->row--;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000192 source->pub.buffer = (*cinfo->mem->access_virt_sarray)
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000193 ((j_common_ptr) cinfo, source->image, source->row, (JDIMENSION) 1, FALSE);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000194
195 return 1;
196}
197
198/*
199 * Read one row of pixels.
200 * Called only after load_image has read the image into the virtual array.
201 * Used for PSEUDOCOLOR images.
202 */
203
Thomas G. Lane489583f1996-02-07 00:00:00 +0000204METHODDEF(JDIMENSION)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000205get_pseudocolor_row (j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
206{
207 rle_source_ptr source = (rle_source_ptr) sinfo;
208 JSAMPROW src_row, dest_row;
209 JDIMENSION col;
210 rle_map *colormap;
211 int val;
212
213 colormap = source->header.cmap;
214 dest_row = source->pub.buffer[0];
Thomas G. Lane9ba2f5e1994-12-07 00:00:00 +0000215 source->row--;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000216 src_row = * (*cinfo->mem->access_virt_sarray)
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000217 ((j_common_ptr) cinfo, source->image, source->row, (JDIMENSION) 1, FALSE);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000218
219 for (col = cinfo->image_width; col > 0; col--) {
220 val = GETJSAMPLE(*src_row++);
Thomas G. Lane9ba2f5e1994-12-07 00:00:00 +0000221 *dest_row++ = (JSAMPLE) (colormap[val ] >> 8);
222 *dest_row++ = (JSAMPLE) (colormap[val + 256] >> 8);
223 *dest_row++ = (JSAMPLE) (colormap[val + 512] >> 8);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000224 }
225
226 return 1;
227}
228
229
230/*
231 * Load the image into a virtual array. We have to do this because RLE
232 * files start at the lower left while the JPEG standard has them starting
233 * in the upper left. This is called the first time we want to get a row
234 * of input. What we do is load the RLE data into the array and then call
235 * the appropriate routine to read one row from the array. Before returning,
236 * we set source->pub.get_pixel_rows so that subsequent calls go straight to
237 * the appropriate row-reading routine.
238 */
239
Thomas G. Lane489583f1996-02-07 00:00:00 +0000240METHODDEF(JDIMENSION)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000241load_image (j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
242{
243 rle_source_ptr source = (rle_source_ptr) sinfo;
244 JDIMENSION row, col;
245 JSAMPROW scanline, red_ptr, green_ptr, blue_ptr;
246 rle_pixel **rle_row;
247 rle_map *colormap;
248 char channel;
249#ifdef PROGRESS_REPORT
250 cd_progress_ptr progress = (cd_progress_ptr) cinfo->progress;
251#endif
252
253 colormap = source->header.cmap;
254 rle_row = source->rle_row;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000255
256 /* Read the RLE data into our virtual array.
257 * We assume here that (a) rle_pixel is represented the same as JSAMPLE,
258 * and (b) we are not on a machine where FAR pointers differ from regular.
259 */
260 RLE_CLR_BIT(source->header, RLE_ALPHA); /* don't read the alpha channel */
261
262#ifdef PROGRESS_REPORT
263 if (progress != NULL) {
264 progress->pub.pass_limit = cinfo->image_height;
265 progress->pub.pass_counter = 0;
266 (*progress->pub.progress_monitor) ((j_common_ptr) cinfo);
267 }
268#endif
269
270 switch (source->visual) {
271
272 case GRAYSCALE:
273 case PSEUDOCOLOR:
Thomas G. Lane9ba2f5e1994-12-07 00:00:00 +0000274 for (row = 0; row < cinfo->image_height; row++) {
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000275 rle_row = (rle_pixel **) (*cinfo->mem->access_virt_sarray)
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000276 ((j_common_ptr) cinfo, source->image, row, (JDIMENSION) 1, TRUE);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000277 rle_getrow(&source->header, rle_row);
278#ifdef PROGRESS_REPORT
279 if (progress != NULL) {
280 progress->pub.pass_counter++;
281 (*progress->pub.progress_monitor) ((j_common_ptr) cinfo);
282 }
283#endif
284 }
285 break;
286
287 case MAPPEDGRAY:
288 case TRUECOLOR:
Thomas G. Lane9ba2f5e1994-12-07 00:00:00 +0000289 for (row = 0; row < cinfo->image_height; row++) {
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000290 scanline = * (*cinfo->mem->access_virt_sarray)
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000291 ((j_common_ptr) cinfo, source->image, row, (JDIMENSION) 1, TRUE);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000292 rle_row = source->rle_row;
293 rle_getrow(&source->header, rle_row);
294
295 for (col = 0; col < cinfo->image_width; col++) {
296 for (channel = 0; channel < source->header.ncolors; channel++) {
297 *scanline++ = (JSAMPLE)
Thomas G. Lane9ba2f5e1994-12-07 00:00:00 +0000298 (colormap[GETJSAMPLE(rle_row[channel][col]) + 256 * channel] >> 8);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000299 }
300 }
301
302#ifdef PROGRESS_REPORT
303 if (progress != NULL) {
304 progress->pub.pass_counter++;
305 (*progress->pub.progress_monitor) ((j_common_ptr) cinfo);
306 }
307#endif
308 }
309 break;
310
311 case DIRECTCOLOR:
Thomas G. Lane9ba2f5e1994-12-07 00:00:00 +0000312 for (row = 0; row < cinfo->image_height; row++) {
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000313 scanline = * (*cinfo->mem->access_virt_sarray)
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000314 ((j_common_ptr) cinfo, source->image, row, (JDIMENSION) 1, TRUE);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000315 rle_getrow(&source->header, rle_row);
316
317 red_ptr = rle_row[0];
318 green_ptr = rle_row[1];
319 blue_ptr = rle_row[2];
320
321 for (col = cinfo->image_width; col > 0; col--) {
322 *scanline++ = *red_ptr++;
323 *scanline++ = *green_ptr++;
324 *scanline++ = *blue_ptr++;
325 }
326
327#ifdef PROGRESS_REPORT
328 if (progress != NULL) {
329 progress->pub.pass_counter++;
330 (*progress->pub.progress_monitor) ((j_common_ptr) cinfo);
331 }
332#endif
333 }
334 }
335
336#ifdef PROGRESS_REPORT
337 if (progress != NULL)
338 progress->completed_extra_passes++;
339#endif
340
341 /* Set up to call proper row-extraction routine in future */
342 if (source->visual == PSEUDOCOLOR) {
343 source->pub.buffer = source->rle_row;
344 source->pub.get_pixel_rows = get_pseudocolor_row;
345 } else {
346 source->pub.get_pixel_rows = get_rle_row;
347 }
Thomas G. Lane9ba2f5e1994-12-07 00:00:00 +0000348 source->row = cinfo->image_height;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000349
350 /* And fetch the topmost (bottommost) row */
351 return (*source->pub.get_pixel_rows) (cinfo, sinfo);
352}
353
354
355/*
356 * Finish up at the end of the file.
357 */
358
Thomas G. Lane489583f1996-02-07 00:00:00 +0000359METHODDEF(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000360finish_input_rle (j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
361{
362 /* no work */
363}
364
365
366/*
367 * The module selection routine for RLE format input.
368 */
369
Thomas G. Lane489583f1996-02-07 00:00:00 +0000370GLOBAL(cjpeg_source_ptr)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000371jinit_read_rle (j_compress_ptr cinfo)
372{
373 rle_source_ptr source;
374
375 /* Create module interface object */
376 source = (rle_source_ptr)
377 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
378 SIZEOF(rle_source_struct));
379 /* Fill in method ptrs */
380 source->pub.start_input = start_input_rle;
381 source->pub.finish_input = finish_input_rle;
382 source->pub.get_pixel_rows = load_image;
383
384 return (cjpeg_source_ptr) source;
385}
386
387#endif /* RLE_SUPPORTED */