Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 1 | /* |
| 2 | * wrrle.c |
| 3 | * |
DRC | 5033f3e | 2014-05-18 18:33:44 +0000 | [diff] [blame] | 4 | * This file was part of the Independent JPEG Group's software: |
Thomas G. Lane | 489583f | 1996-02-07 00:00:00 +0000 | [diff] [blame] | 5 | * Copyright (C) 1991-1996, Thomas G. Lane. |
Leon Scroggins III | bd7903e | 2018-02-28 14:05:04 -0500 | [diff] [blame] | 6 | * libjpeg-turbo Modifications: |
| 7 | * Copyright (C) 2017, D. R. Commander. |
Alex Naidis | 6eb7d37 | 2016-10-16 23:10:08 +0200 | [diff] [blame] | 8 | * For conditions of distribution and use, see the accompanying README.ijg |
| 9 | * file. |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 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 | |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 22 | #include "cdjpeg.h" /* Common decls for cjpeg/djpeg applications */ |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 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 | |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 53 | #define CMAPBITS 8 |
Leon Scroggins III | 3993b37 | 2018-07-16 10:43:45 -0400 | [diff] [blame] | 54 | #define CMAPLENGTH (1 << (CMAPBITS)) |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 55 | |
| 56 | typedef struct { |
| 57 | struct djpeg_dest_struct pub; /* public fields */ |
| 58 | |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 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() */ |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 62 | |
| 63 | } rle_dest_struct; |
| 64 | |
Alex Naidis | 6eb7d37 | 2016-10-16 23:10:08 +0200 | [diff] [blame] | 65 | typedef rle_dest_struct *rle_dest_ptr; |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 66 | |
| 67 | /* Forward declarations */ |
Leon Scroggins III | 3993b37 | 2018-07-16 10:43:45 -0400 | [diff] [blame] | 68 | METHODDEF(void) rle_put_pixel_rows(j_decompress_ptr cinfo, |
| 69 | djpeg_dest_ptr dinfo, |
| 70 | JDIMENSION rows_supplied); |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 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 | |
Thomas G. Lane | 489583f | 1996-02-07 00:00:00 +0000 | [diff] [blame] | 79 | METHODDEF(void) |
Leon Scroggins III | 3993b37 | 2018-07-16 10:43:45 -0400 | [diff] [blame] | 80 | start_output_rle(j_decompress_ptr cinfo, djpeg_dest_ptr dinfo) |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 81 | { |
Leon Scroggins III | 3993b37 | 2018-07-16 10:43:45 -0400 | [diff] [blame] | 82 | rle_dest_ptr dest = (rle_dest_ptr)dinfo; |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 83 | size_t cmapsize; |
| 84 | int i, ci; |
| 85 | #ifdef PROGRESS_REPORT |
Leon Scroggins III | 3993b37 | 2018-07-16 10:43:45 -0400 | [diff] [blame] | 86 | cd_progress_ptr progress = (cd_progress_ptr)cinfo->progress; |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 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) |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 103 | ERREXIT2(cinfo, JERR_RLE_DIMENSIONS, cinfo->output_width, |
| 104 | cinfo->output_height); |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 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 */ |
DRC | 5de454b | 2014-05-18 19:04:03 +0000 | [diff] [blame] | 119 | cmapsize = cinfo->out_color_components * CMAPLENGTH * sizeof(rle_map); |
Leon Scroggins III | 3993b37 | 2018-07-16 10:43:45 -0400 | [diff] [blame] | 120 | dest->colormap = (rle_map *)(*cinfo->mem->alloc_small) |
| 121 | ((j_common_ptr)cinfo, JPOOL_IMAGE, cmapsize); |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 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) |
Leon Scroggins III | 3993b37 | 2018-07-16 10:43:45 -0400 | [diff] [blame] | 136 | ((j_common_ptr)cinfo, dest->image, (JDIMENSION)0, (JDIMENSION)1, TRUE); |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 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 | |
Thomas G. Lane | 489583f | 1996-02-07 00:00:00 +0000 | [diff] [blame] | 155 | METHODDEF(void) |
Leon Scroggins III | 3993b37 | 2018-07-16 10:43:45 -0400 | [diff] [blame] | 156 | rle_put_pixel_rows(j_decompress_ptr cinfo, djpeg_dest_ptr dinfo, |
| 157 | JDIMENSION rows_supplied) |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 158 | { |
Leon Scroggins III | 3993b37 | 2018-07-16 10:43:45 -0400 | [diff] [blame] | 159 | rle_dest_ptr dest = (rle_dest_ptr)dinfo; |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 160 | |
| 161 | if (cinfo->output_scanline < cinfo->output_height) { |
| 162 | dest->pub.buffer = (*cinfo->mem->access_virt_sarray) |
Leon Scroggins III | 3993b37 | 2018-07-16 10:43:45 -0400 | [diff] [blame] | 163 | ((j_common_ptr)cinfo, dest->image, |
| 164 | cinfo->output_scanline, (JDIMENSION)1, TRUE); |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 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 | |
Thomas G. Lane | 489583f | 1996-02-07 00:00:00 +0000 | [diff] [blame] | 174 | METHODDEF(void) |
Leon Scroggins III | 3993b37 | 2018-07-16 10:43:45 -0400 | [diff] [blame] | 175 | finish_output_rle(j_decompress_ptr cinfo, djpeg_dest_ptr dinfo) |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 176 | { |
Leon Scroggins III | 3993b37 | 2018-07-16 10:43:45 -0400 | [diff] [blame] | 177 | rle_dest_ptr dest = (rle_dest_ptr)dinfo; |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 178 | rle_hdr header; /* Output file information */ |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 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 |
Leon Scroggins III | 3993b37 | 2018-07-16 10:43:45 -0400 | [diff] [blame] | 185 | cd_progress_ptr progress = (cd_progress_ptr)cinfo->progress; |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 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. */ |
Leon Scroggins III | 3993b37 | 2018-07-16 10:43:45 -0400 | [diff] [blame] | 205 | sprintf(cmapcomment, "color_map_length=%d", |
| 206 | cinfo->actual_number_of_colors); |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 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. |
DRC | 5033f3e | 2014-05-18 18:33:44 +0000 | [diff] [blame] | 214 | * We assume here that rle_pixel is represented the same as JSAMPLE. |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 215 | */ |
| 216 | |
| 217 | #ifdef PROGRESS_REPORT |
| 218 | if (progress != NULL) { |
| 219 | progress->pub.pass_limit = cinfo->output_height; |
| 220 | progress->pub.pass_counter = 0; |
Leon Scroggins III | 3993b37 | 2018-07-16 10:43:45 -0400 | [diff] [blame] | 221 | (*progress->pub.progress_monitor) ((j_common_ptr)cinfo); |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 222 | } |
| 223 | #endif |
| 224 | |
| 225 | if (cinfo->output_components == 1) { |
Leon Scroggins III | 3993b37 | 2018-07-16 10:43:45 -0400 | [diff] [blame] | 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); |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 231 | #ifdef PROGRESS_REPORT |
| 232 | if (progress != NULL) { |
| 233 | progress->pub.pass_counter++; |
Leon Scroggins III | 3993b37 | 2018-07-16 10:43:45 -0400 | [diff] [blame] | 234 | (*progress->pub.progress_monitor) ((j_common_ptr)cinfo); |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 235 | } |
| 236 | #endif |
| 237 | } |
| 238 | } else { |
Leon Scroggins III | 3993b37 | 2018-07-16 10:43:45 -0400 | [diff] [blame] | 239 | for (row = cinfo->output_height - 1; row >= 0; row--) { |
| 240 | rle_row = (rle_pixel **)dest->rle_row; |
Alex Naidis | 6eb7d37 | 2016-10-16 23:10:08 +0200 | [diff] [blame] | 241 | output_row = *(*cinfo->mem->access_virt_sarray) |
Leon Scroggins III | 3993b37 | 2018-07-16 10:43:45 -0400 | [diff] [blame] | 242 | ((j_common_ptr)cinfo, dest->image, |
| 243 | (JDIMENSION)row, (JDIMENSION)1, FALSE); |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 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 | } |
Leon Scroggins III | 3993b37 | 2018-07-16 10:43:45 -0400 | [diff] [blame] | 252 | rle_putrow(rle_row, (int)cinfo->output_width, &header); |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 253 | #ifdef PROGRESS_REPORT |
| 254 | if (progress != NULL) { |
| 255 | progress->pub.pass_counter++; |
Leon Scroggins III | 3993b37 | 2018-07-16 10:43:45 -0400 | [diff] [blame] | 256 | (*progress->pub.progress_monitor) ((j_common_ptr)cinfo); |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 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 | |
Thomas G. Lane | 489583f | 1996-02-07 00:00:00 +0000 | [diff] [blame] | 279 | GLOBAL(djpeg_dest_ptr) |
Leon Scroggins III | 3993b37 | 2018-07-16 10:43:45 -0400 | [diff] [blame] | 280 | jinit_write_rle(j_decompress_ptr cinfo) |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 281 | { |
| 282 | rle_dest_ptr dest; |
| 283 | |
| 284 | /* Create module interface object, fill in method pointers */ |
| 285 | dest = (rle_dest_ptr) |
Leon Scroggins III | 3993b37 | 2018-07-16 10:43:45 -0400 | [diff] [blame] | 286 | (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE, |
| 287 | sizeof(rle_dest_struct)); |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 288 | dest->pub.start_output = start_output_rle; |
| 289 | dest->pub.finish_output = finish_output_rle; |
Leon Scroggins III | bd7903e | 2018-02-28 14:05:04 -0500 | [diff] [blame] | 290 | dest->pub.calc_buffer_dimensions = NULL; |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 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) |
Leon Scroggins III | 3993b37 | 2018-07-16 10:43:45 -0400 | [diff] [blame] | 297 | ((j_common_ptr)cinfo, JPOOL_IMAGE, |
| 298 | cinfo->output_width, (JDIMENSION)cinfo->output_components); |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 299 | |
| 300 | /* Allocate a virtual array to hold the image. */ |
| 301 | dest->image = (*cinfo->mem->request_virt_sarray) |
Leon Scroggins III | 3993b37 | 2018-07-16 10:43:45 -0400 | [diff] [blame] | 302 | ((j_common_ptr)cinfo, JPOOL_IMAGE, FALSE, |
| 303 | (JDIMENSION)(cinfo->output_width * cinfo->output_components), |
| 304 | cinfo->output_height, (JDIMENSION)1); |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 305 | |
Leon Scroggins III | 3993b37 | 2018-07-16 10:43:45 -0400 | [diff] [blame] | 306 | return (djpeg_dest_ptr)dest; |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 307 | } |
| 308 | |
| 309 | #endif /* RLE_SUPPORTED */ |