]> granicus.if.org Git - libjpeg-turbo/blob - wrrle.c
AppVeyor: Use MinGW-builds instead of MSYS2 MinGW
[libjpeg-turbo] / wrrle.c
1 /*
2  * wrrle.c
3  *
4  * This file was part of the Independent JPEG Group's software:
5  * Copyright (C) 1991-1996, Thomas G. Lane.
6  * libjpeg-turbo Modifications:
7  * Copyright (C) 2017, D. R. Commander.
8  * For conditions of distribution and use, see the accompanying README.ijg
9  * file.
10  *
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
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 /*
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
53 #define CMAPBITS        8
54 #define CMAPLENGTH      (1 << (CMAPBITS))
55
56 typedef struct {
57   struct djpeg_dest_struct pub; /* public fields */
58
59   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() */
62
63 } rle_dest_struct;
64
65 typedef rle_dest_struct *rle_dest_ptr;
66
67 /* Forward declarations */
68 METHODDEF(void) rle_put_pixel_rows(j_decompress_ptr cinfo,
69                                    djpeg_dest_ptr dinfo,
70                                    JDIMENSION rows_supplied);
71
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
79 METHODDEF(void)
80 start_output_rle(j_decompress_ptr cinfo, djpeg_dest_ptr dinfo)
81 {
82   rle_dest_ptr dest = (rle_dest_ptr)dinfo;
83   size_t cmapsize;
84   int i, ci;
85 #ifdef PROGRESS_REPORT
86   cd_progress_ptr progress = (cd_progress_ptr)cinfo->progress;
87 #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)
103     ERREXIT2(cinfo, JERR_RLE_DIMENSIONS, cinfo->output_width,
104              cinfo->output_height);
105
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 */
119     cmapsize = cinfo->out_color_components * CMAPLENGTH * sizeof(rle_map);
120     dest->colormap = (rle_map *)(*cinfo->mem->alloc_small)
121       ((j_common_ptr)cinfo, JPOOL_IMAGE, cmapsize);
122     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)
136     ((j_common_ptr)cinfo, dest->image, (JDIMENSION)0, (JDIMENSION)1, TRUE);
137   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
155 METHODDEF(void)
156 rle_put_pixel_rows(j_decompress_ptr cinfo, djpeg_dest_ptr dinfo,
157                    JDIMENSION rows_supplied)
158 {
159   rle_dest_ptr dest = (rle_dest_ptr)dinfo;
160
161   if (cinfo->output_scanline < cinfo->output_height) {
162     dest->pub.buffer = (*cinfo->mem->access_virt_sarray)
163       ((j_common_ptr)cinfo, dest->image,
164        cinfo->output_scanline, (JDIMENSION)1, TRUE);
165   }
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
174 METHODDEF(void)
175 finish_output_rle(j_decompress_ptr cinfo, djpeg_dest_ptr dinfo)
176 {
177   rle_dest_ptr dest = (rle_dest_ptr)dinfo;
178   rle_hdr header;               /* Output file information */
179   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
185   cd_progress_ptr progress = (cd_progress_ptr)cinfo->progress;
186 #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. */
205     sprintf(cmapcomment, "color_map_length=%d",
206             cinfo->actual_number_of_colors);
207     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.
214    * We assume here that rle_pixel is represented the same as JSAMPLE.
215    */
216
217 #ifdef PROGRESS_REPORT
218   if (progress != NULL) {
219     progress->pub.pass_limit = cinfo->output_height;
220     progress->pub.pass_counter = 0;
221     (*progress->pub.progress_monitor) ((j_common_ptr)cinfo);
222   }
223 #endif
224
225   if (cinfo->output_components == 1) {
226     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);
231 #ifdef PROGRESS_REPORT
232       if (progress != NULL) {
233         progress->pub.pass_counter++;
234         (*progress->pub.progress_monitor) ((j_common_ptr)cinfo);
235       }
236 #endif
237     }
238   } else {
239     for (row = cinfo->output_height - 1; row >= 0; row--) {
240       rle_row = (rle_pixel **)dest->rle_row;
241       output_row = *(*cinfo->mem->access_virt_sarray)
242         ((j_common_ptr)cinfo, dest->image,
243          (JDIMENSION)row, (JDIMENSION)1, FALSE);
244       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       }
252       rle_putrow(rle_row, (int)cinfo->output_width, &header);
253 #ifdef PROGRESS_REPORT
254       if (progress != NULL) {
255         progress->pub.pass_counter++;
256         (*progress->pub.progress_monitor) ((j_common_ptr)cinfo);
257       }
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
279 GLOBAL(djpeg_dest_ptr)
280 jinit_write_rle(j_decompress_ptr cinfo)
281 {
282   rle_dest_ptr dest;
283
284   /* Create module interface object, fill in method pointers */
285   dest = (rle_dest_ptr)
286     (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
287                                 sizeof(rle_dest_struct));
288   dest->pub.start_output = start_output_rle;
289   dest->pub.finish_output = finish_output_rle;
290   dest->pub.calc_buffer_dimensions = NULL;
291
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)
297     ((j_common_ptr)cinfo, JPOOL_IMAGE,
298      cinfo->output_width, (JDIMENSION)cinfo->output_components);
299
300   /* Allocate a virtual array to hold the image. */
301   dest->image = (*cinfo->mem->request_virt_sarray)
302     ((j_common_ptr)cinfo, JPOOL_IMAGE, FALSE,
303      (JDIMENSION)(cinfo->output_width * cinfo->output_components),
304      cinfo->output_height, (JDIMENSION)1);
305
306   return (djpeg_dest_ptr)dest;
307 }
308
309 #endif /* RLE_SUPPORTED */