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