|
kusano |
7d535a |
/*
|
|
kusano |
7d535a |
* wrrle.c
|
|
kusano |
7d535a |
*
|
|
shun-iwasawa |
82a8f5 |
* This file was part of the Independent JPEG Group's software:
|
|
kusano |
7d535a |
* Copyright (C) 1991-1996, Thomas G. Lane.
|
|
shun-iwasawa |
82a8f5 |
* libjpeg-turbo Modifications:
|
|
shun-iwasawa |
82a8f5 |
* Copyright (C) 2017, D. R. Commander.
|
|
shun-iwasawa |
82a8f5 |
* For conditions of distribution and use, see the accompanying README.ijg
|
|
shun-iwasawa |
82a8f5 |
* file.
|
|
kusano |
7d535a |
*
|
|
kusano |
7d535a |
* This file contains routines to write output images in RLE format.
|
|
kusano |
7d535a |
* The Utah Raster Toolkit library is required (version 3.1 or later).
|
|
kusano |
7d535a |
*
|
|
kusano |
7d535a |
* These routines may need modification for non-Unix environments or
|
|
kusano |
7d535a |
* specialized applications. As they stand, they assume output to
|
|
kusano |
7d535a |
* an ordinary stdio stream.
|
|
kusano |
7d535a |
*
|
|
kusano |
7d535a |
* Based on code contributed by Mike Lijewski,
|
|
kusano |
7d535a |
* with updates from Robert Hutchinson.
|
|
kusano |
7d535a |
*/
|
|
kusano |
7d535a |
|
|
shun-iwasawa |
82a8f5 |
#include "cdjpeg.h" /* Common decls for cjpeg/djpeg applications */
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
#ifdef RLE_SUPPORTED
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
/* rle.h is provided by the Utah Raster Toolkit. */
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
#include <rle.h></rle.h>
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
/*
|
|
kusano |
7d535a |
* We assume that JSAMPLE has the same representation as rle_pixel,
|
|
kusano |
7d535a |
* to wit, "unsigned char". Hence we can't cope with 12- or 16-bit samples.
|
|
kusano |
7d535a |
*/
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
#if BITS_IN_JSAMPLE != 8
|
|
kusano |
7d535a |
Sorry, this code only copes with 8-bit JSAMPLEs. /* deliberate syntax err */
|
|
kusano |
7d535a |
#endif
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
/*
|
|
kusano |
7d535a |
* Since RLE stores scanlines bottom-to-top, we have to invert the image
|
|
kusano |
7d535a |
* from JPEG's top-to-bottom order. To do this, we save the outgoing data
|
|
kusano |
7d535a |
* in a virtual array during put_pixel_row calls, then actually emit the
|
|
kusano |
7d535a |
* RLE file during finish_output.
|
|
kusano |
7d535a |
*/
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
/*
|
|
kusano |
7d535a |
* For now, if we emit an RLE color map then it is always 256 entries long,
|
|
kusano |
7d535a |
* though not all of the entries need be used.
|
|
kusano |
7d535a |
*/
|
|
kusano |
7d535a |
|
|
shun-iwasawa |
82a8f5 |
#define CMAPBITS 8
|
|
shun-iwasawa |
82a8f5 |
#define CMAPLENGTH (1 << (CMAPBITS))
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
typedef struct {
|
|
kusano |
7d535a |
struct djpeg_dest_struct pub; /* public fields */
|
|
kusano |
7d535a |
|
|
shun-iwasawa |
82a8f5 |
jvirt_sarray_ptr image; /* virtual array to store the output image */
|
|
shun-iwasawa |
82a8f5 |
rle_map *colormap; /* RLE-style color map, or NULL if none */
|
|
shun-iwasawa |
82a8f5 |
rle_pixel **rle_row; /* To pass rows to rle_putrow() */
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
} rle_dest_struct;
|
|
kusano |
7d535a |
|
|
shun-iwasawa |
82a8f5 |
typedef rle_dest_struct *rle_dest_ptr;
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
/* Forward declarations */
|
|
shun-iwasawa |
82a8f5 |
METHODDEF(void) rle_put_pixel_rows(j_decompress_ptr cinfo,
|
|
shun-iwasawa |
82a8f5 |
djpeg_dest_ptr dinfo,
|
|
shun-iwasawa |
82a8f5 |
JDIMENSION rows_supplied);
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
/*
|
|
kusano |
7d535a |
* Write the file header.
|
|
kusano |
7d535a |
*
|
|
kusano |
7d535a |
* In this module it's easier to wait till finish_output to write anything.
|
|
kusano |
7d535a |
*/
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
METHODDEF(void)
|
|
shun-iwasawa |
82a8f5 |
start_output_rle(j_decompress_ptr cinfo, djpeg_dest_ptr dinfo)
|
|
kusano |
7d535a |
{
|
|
shun-iwasawa |
82a8f5 |
rle_dest_ptr dest = (rle_dest_ptr)dinfo;
|
|
kusano |
7d535a |
size_t cmapsize;
|
|
kusano |
7d535a |
int i, ci;
|
|
kusano |
7d535a |
#ifdef PROGRESS_REPORT
|
|
shun-iwasawa |
82a8f5 |
cd_progress_ptr progress = (cd_progress_ptr)cinfo->progress;
|
|
kusano |
7d535a |
#endif
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
/*
|
|
kusano |
7d535a |
* Make sure the image can be stored in RLE format.
|
|
kusano |
7d535a |
*
|
|
kusano |
7d535a |
* - RLE stores image dimensions as *signed* 16 bit integers. JPEG
|
|
kusano |
7d535a |
* uses unsigned, so we have to check the width.
|
|
kusano |
7d535a |
*
|
|
kusano |
7d535a |
* - Colorspace is expected to be grayscale or RGB.
|
|
kusano |
7d535a |
*
|
|
kusano |
7d535a |
* - The number of channels (components) is expected to be 1 (grayscale/
|
|
kusano |
7d535a |
* pseudocolor) or 3 (truecolor/directcolor).
|
|
kusano |
7d535a |
* (could be 2 or 4 if using an alpha channel, but we aren't)
|
|
kusano |
7d535a |
*/
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
if (cinfo->output_width > 32767 || cinfo->output_height > 32767)
|
|
shun-iwasawa |
82a8f5 |
ERREXIT2(cinfo, JERR_RLE_DIMENSIONS, cinfo->output_width,
|
|
shun-iwasawa |
82a8f5 |
cinfo->output_height);
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
if (cinfo->out_color_space != JCS_GRAYSCALE &&
|
|
kusano |
7d535a |
cinfo->out_color_space != JCS_RGB)
|
|
kusano |
7d535a |
ERREXIT(cinfo, JERR_RLE_COLORSPACE);
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
if (cinfo->output_components != 1 && cinfo->output_components != 3)
|
|
kusano |
7d535a |
ERREXIT1(cinfo, JERR_RLE_TOOMANYCHANNELS, cinfo->num_components);
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
/* Convert colormap, if any, to RLE format. */
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
dest->colormap = NULL;
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
if (cinfo->quantize_colors) {
|
|
kusano |
7d535a |
/* Allocate storage for RLE-style cmap, zero any extra entries */
|
|
shun-iwasawa |
82a8f5 |
cmapsize = cinfo->out_color_components * CMAPLENGTH * sizeof(rle_map);
|
|
shun-iwasawa |
82a8f5 |
dest->colormap = (rle_map *)(*cinfo->mem->alloc_small)
|
|
shun-iwasawa |
82a8f5 |
((j_common_ptr)cinfo, JPOOL_IMAGE, cmapsize);
|
|
kusano |
7d535a |
MEMZERO(dest->colormap, cmapsize);
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
/* Save away data in RLE format --- note 8-bit left shift! */
|
|
kusano |
7d535a |
/* Shifting would need adjustment for JSAMPLEs wider than 8 bits. */
|
|
kusano |
7d535a |
for (ci = 0; ci < cinfo->out_color_components; ci++) {
|
|
kusano |
7d535a |
for (i = 0; i < cinfo->actual_number_of_colors; i++) {
|
|
kusano |
7d535a |
dest->colormap[ci * CMAPLENGTH + i] =
|
|
kusano |
7d535a |
GETJSAMPLE(cinfo->colormap[ci][i]) << 8;
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
/* Set the output buffer to the first row */
|
|
kusano |
7d535a |
dest->pub.buffer = (*cinfo->mem->access_virt_sarray)
|
|
shun-iwasawa |
82a8f5 |
((j_common_ptr)cinfo, dest->image, (JDIMENSION)0, (JDIMENSION)1, TRUE);
|
|
kusano |
7d535a |
dest->pub.buffer_height = 1;
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
dest->pub.put_pixel_rows = rle_put_pixel_rows;
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
#ifdef PROGRESS_REPORT
|
|
kusano |
7d535a |
if (progress != NULL) {
|
|
kusano |
7d535a |
progress->total_extra_passes++; /* count file writing as separate pass */
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
#endif
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
/*
|
|
kusano |
7d535a |
* Write some pixel data.
|
|
kusano |
7d535a |
*
|
|
kusano |
7d535a |
* This routine just saves the data away in a virtual array.
|
|
kusano |
7d535a |
*/
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
METHODDEF(void)
|
|
shun-iwasawa |
82a8f5 |
rle_put_pixel_rows(j_decompress_ptr cinfo, djpeg_dest_ptr dinfo,
|
|
shun-iwasawa |
82a8f5 |
JDIMENSION rows_supplied)
|
|
kusano |
7d535a |
{
|
|
shun-iwasawa |
82a8f5 |
rle_dest_ptr dest = (rle_dest_ptr)dinfo;
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
if (cinfo->output_scanline < cinfo->output_height) {
|
|
kusano |
7d535a |
dest->pub.buffer = (*cinfo->mem->access_virt_sarray)
|
|
shun-iwasawa |
82a8f5 |
((j_common_ptr)cinfo, dest->image,
|
|
shun-iwasawa |
82a8f5 |
cinfo->output_scanline, (JDIMENSION)1, TRUE);
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
/*
|
|
kusano |
7d535a |
* Finish up at the end of the file.
|
|
kusano |
7d535a |
*
|
|
kusano |
7d535a |
* Here is where we really output the RLE file.
|
|
kusano |
7d535a |
*/
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
METHODDEF(void)
|
|
shun-iwasawa |
82a8f5 |
finish_output_rle(j_decompress_ptr cinfo, djpeg_dest_ptr dinfo)
|
|
kusano |
7d535a |
{
|
|
shun-iwasawa |
82a8f5 |
rle_dest_ptr dest = (rle_dest_ptr)dinfo;
|
|
shun-iwasawa |
82a8f5 |
rle_hdr header; /* Output file information */
|
|
kusano |
7d535a |
rle_pixel **rle_row, *red, *green, *blue;
|
|
kusano |
7d535a |
JSAMPROW output_row;
|
|
kusano |
7d535a |
char cmapcomment[80];
|
|
kusano |
7d535a |
int row, col;
|
|
kusano |
7d535a |
int ci;
|
|
kusano |
7d535a |
#ifdef PROGRESS_REPORT
|
|
shun-iwasawa |
82a8f5 |
cd_progress_ptr progress = (cd_progress_ptr)cinfo->progress;
|
|
kusano |
7d535a |
#endif
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
/* Initialize the header info */
|
|
kusano |
7d535a |
header = *rle_hdr_init(NULL);
|
|
kusano |
7d535a |
header.rle_file = dest->pub.output_file;
|
|
kusano |
7d535a |
header.xmin = 0;
|
|
kusano |
7d535a |
header.xmax = cinfo->output_width - 1;
|
|
kusano |
7d535a |
header.ymin = 0;
|
|
kusano |
7d535a |
header.ymax = cinfo->output_height - 1;
|
|
kusano |
7d535a |
header.alpha = 0;
|
|
kusano |
7d535a |
header.ncolors = cinfo->output_components;
|
|
kusano |
7d535a |
for (ci = 0; ci < cinfo->output_components; ci++) {
|
|
kusano |
7d535a |
RLE_SET_BIT(header, ci);
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
if (cinfo->quantize_colors) {
|
|
kusano |
7d535a |
header.ncmap = cinfo->out_color_components;
|
|
kusano |
7d535a |
header.cmaplen = CMAPBITS;
|
|
kusano |
7d535a |
header.cmap = dest->colormap;
|
|
kusano |
7d535a |
/* Add a comment to the output image with the true colormap length. */
|
|
shun-iwasawa |
82a8f5 |
sprintf(cmapcomment, "color_map_length=%d",
|
|
shun-iwasawa |
82a8f5 |
cinfo->actual_number_of_colors);
|
|
kusano |
7d535a |
rle_putcom(cmapcomment, &header);
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
/* Emit the RLE header and color map (if any) */
|
|
kusano |
7d535a |
rle_put_setup(&header);
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
/* Now output the RLE data from our virtual array.
|
|
shun-iwasawa |
82a8f5 |
* We assume here that rle_pixel is represented the same as JSAMPLE.
|
|
kusano |
7d535a |
*/
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
#ifdef PROGRESS_REPORT
|
|
kusano |
7d535a |
if (progress != NULL) {
|
|
kusano |
7d535a |
progress->pub.pass_limit = cinfo->output_height;
|
|
kusano |
7d535a |
progress->pub.pass_counter = 0;
|
|
shun-iwasawa |
82a8f5 |
(*progress->pub.progress_monitor) ((j_common_ptr)cinfo);
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
#endif
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
if (cinfo->output_components == 1) {
|
|
shun-iwasawa |
82a8f5 |
for (row = cinfo->output_height - 1; row >= 0; row--) {
|
|
shun-iwasawa |
82a8f5 |
rle_row = (rle_pixel **)(*cinfo->mem->access_virt_sarray)
|
|
shun-iwasawa |
82a8f5 |
((j_common_ptr)cinfo, dest->image,
|
|
shun-iwasawa |
82a8f5 |
(JDIMENSION)row, (JDIMENSION)1, FALSE);
|
|
shun-iwasawa |
82a8f5 |
rle_putrow(rle_row, (int)cinfo->output_width, &header);
|
|
kusano |
7d535a |
#ifdef PROGRESS_REPORT
|
|
kusano |
7d535a |
if (progress != NULL) {
|
|
kusano |
7d535a |
progress->pub.pass_counter++;
|
|
shun-iwasawa |
82a8f5 |
(*progress->pub.progress_monitor) ((j_common_ptr)cinfo);
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
#endif
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
} else {
|
|
shun-iwasawa |
82a8f5 |
for (row = cinfo->output_height - 1; row >= 0; row--) {
|
|
shun-iwasawa |
82a8f5 |
rle_row = (rle_pixel **)dest->rle_row;
|
|
shun-iwasawa |
82a8f5 |
output_row = *(*cinfo->mem->access_virt_sarray)
|
|
shun-iwasawa |
82a8f5 |
((j_common_ptr)cinfo, dest->image,
|
|
shun-iwasawa |
82a8f5 |
(JDIMENSION)row, (JDIMENSION)1, FALSE);
|
|
kusano |
7d535a |
red = rle_row[0];
|
|
kusano |
7d535a |
green = rle_row[1];
|
|
kusano |
7d535a |
blue = rle_row[2];
|
|
kusano |
7d535a |
for (col = cinfo->output_width; col > 0; col--) {
|
|
kusano |
7d535a |
*red++ = GETJSAMPLE(*output_row++);
|
|
kusano |
7d535a |
*green++ = GETJSAMPLE(*output_row++);
|
|
kusano |
7d535a |
*blue++ = GETJSAMPLE(*output_row++);
|
|
kusano |
7d535a |
}
|
|
shun-iwasawa |
82a8f5 |
rle_putrow(rle_row, (int)cinfo->output_width, &header);
|
|
kusano |
7d535a |
#ifdef PROGRESS_REPORT
|
|
kusano |
7d535a |
if (progress != NULL) {
|
|
kusano |
7d535a |
progress->pub.pass_counter++;
|
|
shun-iwasawa |
82a8f5 |
(*progress->pub.progress_monitor) ((j_common_ptr)cinfo);
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
#endif
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
#ifdef PROGRESS_REPORT
|
|
kusano |
7d535a |
if (progress != NULL)
|
|
kusano |
7d535a |
progress->completed_extra_passes++;
|
|
kusano |
7d535a |
#endif
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
/* Emit file trailer */
|
|
kusano |
7d535a |
rle_puteof(&header);
|
|
kusano |
7d535a |
fflush(dest->pub.output_file);
|
|
kusano |
7d535a |
if (ferror(dest->pub.output_file))
|
|
kusano |
7d535a |
ERREXIT(cinfo, JERR_FILE_WRITE);
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
/*
|
|
kusano |
7d535a |
* The module selection routine for RLE format output.
|
|
kusano |
7d535a |
*/
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
GLOBAL(djpeg_dest_ptr)
|
|
shun-iwasawa |
82a8f5 |
jinit_write_rle(j_decompress_ptr cinfo)
|
|
kusano |
7d535a |
{
|
|
kusano |
7d535a |
rle_dest_ptr dest;
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
/* Create module interface object, fill in method pointers */
|
|
kusano |
7d535a |
dest = (rle_dest_ptr)
|
|
shun-iwasawa |
82a8f5 |
(*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
|
|
shun-iwasawa |
82a8f5 |
sizeof(rle_dest_struct));
|
|
kusano |
7d535a |
dest->pub.start_output = start_output_rle;
|
|
kusano |
7d535a |
dest->pub.finish_output = finish_output_rle;
|
|
shun-iwasawa |
82a8f5 |
dest->pub.calc_buffer_dimensions = NULL;
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
/* Calculate output image dimensions so we can allocate space */
|
|
kusano |
7d535a |
jpeg_calc_output_dimensions(cinfo);
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
/* Allocate a work array for output to the RLE library. */
|
|
kusano |
7d535a |
dest->rle_row = (*cinfo->mem->alloc_sarray)
|
|
shun-iwasawa |
82a8f5 |
((j_common_ptr)cinfo, JPOOL_IMAGE,
|
|
shun-iwasawa |
82a8f5 |
cinfo->output_width, (JDIMENSION)cinfo->output_components);
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
/* Allocate a virtual array to hold the image. */
|
|
kusano |
7d535a |
dest->image = (*cinfo->mem->request_virt_sarray)
|
|
shun-iwasawa |
82a8f5 |
((j_common_ptr)cinfo, JPOOL_IMAGE, FALSE,
|
|
shun-iwasawa |
82a8f5 |
(JDIMENSION)(cinfo->output_width * cinfo->output_components),
|
|
shun-iwasawa |
82a8f5 |
cinfo->output_height, (JDIMENSION)1);
|
|
kusano |
7d535a |
|
|
shun-iwasawa |
82a8f5 |
return (djpeg_dest_ptr)dest;
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
#endif /* RLE_SUPPORTED */
|