[LOCALSPL_WINETEST] Sync with Wine Staging 4.18. CORE-16441
[reactos.git] / dll / 3rdparty / libjpeg / wrrle.c
1 /*
2 * wrrle.c
3 *
4 * Copyright (C) 1991-1996, Thomas G. Lane.
5 * Modified 2017 by Guido Vollbeding.
6 * This file is part of the Independent JPEG Group's software.
7 * For conditions of distribution and use, see the accompanying README file.
8 *
9 * This file contains routines to write output images in RLE format.
10 * The Utah Raster Toolkit library is required (version 3.1 or later).
11 *
12 * These routines may need modification for non-Unix environments or
13 * specialized applications. As they stand, they assume output to
14 * an ordinary stdio stream.
15 *
16 * Based on code contributed by Mike Lijewski,
17 * with updates from Robert Hutchinson.
18 */
19
20 #include "cdjpeg.h" /* Common decls for cjpeg/djpeg applications */
21
22 #ifdef RLE_SUPPORTED
23
24 /* rle.h is provided by the Utah Raster Toolkit. */
25
26 #include <rle.h>
27
28 /*
29 * We assume that JSAMPLE has the same representation as rle_pixel,
30 * to wit, "unsigned char". Hence we can't cope with 12- or 16-bit samples.
31 */
32
33 #if BITS_IN_JSAMPLE != 8
34 Sorry, this code only copes with 8-bit JSAMPLEs. /* deliberate syntax err */
35 #endif
36
37
38 /*
39 * Since RLE stores scanlines bottom-to-top, we have to invert the image
40 * from JPEG's top-to-bottom order. To do this, we save the outgoing data
41 * in a virtual array during put_pixel_row calls, then actually emit the
42 * RLE file during finish_output.
43 */
44
45
46 /*
47 * For now, if we emit an RLE color map then it is always 256 entries long,
48 * though not all of the entries need be used.
49 */
50
51 #define CMAPBITS 8
52 #define CMAPLENGTH (1<<(CMAPBITS))
53
54 typedef struct {
55 struct djpeg_dest_struct pub; /* public fields */
56
57 jvirt_sarray_ptr image; /* virtual array to store the output image */
58 rle_map *colormap; /* RLE-style color map, or NULL if none */
59 rle_pixel **rle_row; /* To pass rows to rle_putrow() */
60
61 } rle_dest_struct;
62
63 typedef rle_dest_struct * rle_dest_ptr;
64
65 /* Forward declarations */
66 METHODDEF(void) rle_put_pixel_rows
67 JPP((j_decompress_ptr cinfo, djpeg_dest_ptr dinfo,
68 JDIMENSION rows_supplied));
69
70
71 /*
72 * Write the file header.
73 *
74 * In this module it's easier to wait till finish_output to write anything.
75 */
76
77 METHODDEF(void)
78 start_output_rle (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo)
79 {
80 rle_dest_ptr dest = (rle_dest_ptr) dinfo;
81 size_t cmapsize;
82 int i, ci;
83 #ifdef PROGRESS_REPORT
84 cd_progress_ptr progress = (cd_progress_ptr) cinfo->progress;
85 #endif
86
87 /*
88 * Make sure the image can be stored in RLE format.
89 *
90 * - RLE stores image dimensions as *signed* 16 bit integers. JPEG
91 * uses unsigned, so we have to check the width.
92 *
93 * - Colorspace is expected to be grayscale or RGB.
94 *
95 * - The number of channels (components) is expected to be 1 (grayscale/
96 * pseudocolor) or 3 (truecolor/directcolor).
97 * (could be 2 or 4 if using an alpha channel, but we aren't)
98 */
99
100 if (cinfo->output_width > 32767 || cinfo->output_height > 32767)
101 ERREXIT2(cinfo, JERR_RLE_DIMENSIONS, cinfo->output_width,
102 cinfo->output_height);
103
104 if (cinfo->out_color_space != JCS_GRAYSCALE &&
105 cinfo->out_color_space != JCS_RGB)
106 ERREXIT(cinfo, JERR_RLE_COLORSPACE);
107
108 if (cinfo->output_components != 1 && cinfo->output_components != 3)
109 ERREXIT1(cinfo, JERR_RLE_TOOMANYCHANNELS, cinfo->num_components);
110
111 /* Convert colormap, if any, to RLE format. */
112
113 dest->colormap = NULL;
114
115 if (cinfo->quantize_colors) {
116 /* Allocate storage for RLE-style cmap, zero any extra entries */
117 cmapsize = cinfo->out_color_components * CMAPLENGTH * SIZEOF(rle_map);
118 dest->colormap = (rle_map *) (*cinfo->mem->alloc_small)
119 ((j_common_ptr) cinfo, JPOOL_IMAGE, cmapsize);
120 MEMZERO(dest->colormap, cmapsize);
121
122 /* Save away data in RLE format --- note 8-bit left shift! */
123 /* Shifting would need adjustment for JSAMPLEs wider than 8 bits. */
124 for (ci = 0; ci < cinfo->out_color_components; ci++) {
125 for (i = 0; i < cinfo->actual_number_of_colors; i++) {
126 dest->colormap[ci * CMAPLENGTH + i] =
127 GETJSAMPLE(cinfo->colormap[ci][i]) << 8;
128 }
129 }
130 }
131
132 /* Set the output buffer to the first row */
133 dest->pub.buffer = (*cinfo->mem->access_virt_sarray)
134 ((j_common_ptr) cinfo, dest->image, (JDIMENSION) 0, (JDIMENSION) 1, TRUE);
135 dest->pub.buffer_height = 1;
136
137 dest->pub.put_pixel_rows = rle_put_pixel_rows;
138
139 #ifdef PROGRESS_REPORT
140 if (progress != NULL) {
141 progress->total_extra_passes++; /* count file writing as separate pass */
142 }
143 #endif
144 }
145
146
147 /*
148 * Write some pixel data.
149 *
150 * This routine just saves the data away in a virtual array.
151 */
152
153 METHODDEF(void)
154 rle_put_pixel_rows (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo,
155 JDIMENSION rows_supplied)
156 {
157 rle_dest_ptr dest = (rle_dest_ptr) dinfo;
158
159 if (cinfo->output_scanline < cinfo->output_height) {
160 dest->pub.buffer = (*cinfo->mem->access_virt_sarray)
161 ((j_common_ptr) cinfo, dest->image,
162 cinfo->output_scanline, (JDIMENSION) 1, TRUE);
163 }
164 }
165
166 /*
167 * Finish up at the end of the file.
168 *
169 * Here is where we really output the RLE file.
170 */
171
172 METHODDEF(void)
173 finish_output_rle (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo)
174 {
175 rle_dest_ptr dest = (rle_dest_ptr) dinfo;
176 rle_hdr header; /* Output file information */
177 rle_pixel **rle_row, *red, *green, *blue;
178 JSAMPROW output_row;
179 char cmapcomment[80];
180 int row, col;
181 int ci;
182 #ifdef PROGRESS_REPORT
183 cd_progress_ptr progress = (cd_progress_ptr) cinfo->progress;
184 #endif
185
186 /* Initialize the header info */
187 header = *rle_hdr_init(NULL);
188 header.rle_file = dest->pub.output_file;
189 header.xmin = 0;
190 header.xmax = cinfo->output_width - 1;
191 header.ymin = 0;
192 header.ymax = cinfo->output_height - 1;
193 header.alpha = 0;
194 header.ncolors = cinfo->output_components;
195 for (ci = 0; ci < cinfo->output_components; ci++) {
196 RLE_SET_BIT(header, ci);
197 }
198 if (cinfo->quantize_colors) {
199 header.ncmap = cinfo->out_color_components;
200 header.cmaplen = CMAPBITS;
201 header.cmap = dest->colormap;
202 /* Add a comment to the output image with the true colormap length. */
203 sprintf(cmapcomment, "color_map_length=%d", cinfo->actual_number_of_colors);
204 rle_putcom(cmapcomment, &header);
205 }
206
207 /* Emit the RLE header and color map (if any) */
208 rle_put_setup(&header);
209
210 /* Now output the RLE data from our virtual array.
211 * We assume here that (a) rle_pixel is represented the same as JSAMPLE,
212 * and (b) we are not on a machine where FAR pointers differ from regular.
213 */
214
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);
220 }
221 #endif
222
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);
233 }
234 #endif
235 }
236 } else {
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);
242 red = rle_row[0];
243 green = rle_row[1];
244 blue = rle_row[2];
245 for (col = cinfo->output_width; col > 0; col--) {
246 *red++ = GETJSAMPLE(*output_row++);
247 *green++ = GETJSAMPLE(*output_row++);
248 *blue++ = GETJSAMPLE(*output_row++);
249 }
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);
255 }
256 #endif
257 }
258 }
259
260 #ifdef PROGRESS_REPORT
261 if (progress != NULL)
262 progress->completed_extra_passes++;
263 #endif
264
265 /* Emit file trailer */
266 rle_puteof(&header);
267 JFFLUSH(dest->pub.output_file);
268 if (JFERROR(dest->pub.output_file))
269 ERREXIT(cinfo, JERR_FILE_WRITE);
270 }
271
272
273 /*
274 * The module selection routine for RLE format output.
275 */
276
277 GLOBAL(djpeg_dest_ptr)
278 jinit_write_rle (j_decompress_ptr cinfo)
279 {
280 rle_dest_ptr dest;
281
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;
288
289 /* Calculate output image dimensions so we can allocate space */
290 jpeg_calc_output_dimensions(cinfo);
291
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);
296
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);
302
303 return &dest->pub;
304 }
305
306 #endif /* RLE_SUPPORTED */