wrrle.c revision c6a7fc661d57f86ac08cd637abc881cbe687b11a
1d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)/*
2d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles) * wrrle.c
3d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles) *
4d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles) * Copyright (C) 1991-1996, Thomas G. Lane.
5d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles) * This file is part of the Independent JPEG Group's software.
6d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles) * For conditions of distribution and use, see the accompanying README file.
7d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles) *
8323480423219ecd77329f8326dc5e0e3b50926d4Torne (Richard Coles) * This file contains routines to write output images in RLE format.
9323480423219ecd77329f8326dc5e0e3b50926d4Torne (Richard Coles) * The Utah Raster Toolkit library is required (version 3.1 or later).
10c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles) *
11d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles) * These routines may need modification for non-Unix environments or
12d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles) * specialized applications.  As they stand, they assume output to
137242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci * an ordinary stdio stream.
147242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci *
15d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles) * Based on code contributed by Mike Lijewski,
16d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles) * with updates from Robert Hutchinson.
17d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles) */
18d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)
19e38fbeeb576b5094e34e038ab88d9d6a5c5c2214Torne (Richard Coles)#include "cdjpeg.h"		/* Common decls for cjpeg/djpeg applications */
2043e7502580f146aa5b3db8267ba6dbb5c733a489Torne (Richard Coles)
21d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)#ifdef RLE_SUPPORTED
22d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)
23d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)/* rle.h is provided by the Utah Raster Toolkit. */
24d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)
25d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)#include <rle.h>
26d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)
277242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci/*
28d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles) * We assume that JSAMPLE has the same representation as rle_pixel,
29d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles) * to wit, "unsigned char".  Hence we can't cope with 12- or 16-bit samples.
30d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles) */
31323480423219ecd77329f8326dc5e0e3b50926d4Torne (Richard Coles)
32d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)#if BITS_IN_JSAMPLE != 8
33d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)  Sorry, this code only copes with 8-bit JSAMPLEs. /* deliberate syntax err */
34323480423219ecd77329f8326dc5e0e3b50926d4Torne (Richard Coles)#endif
35d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)
36d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)
37d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)/*
385d92fedcae5e801a8b224de090094f2d9df0b54aTorne (Richard Coles) * Since RLE stores scanlines bottom-to-top, we have to invert the image
395d92fedcae5e801a8b224de090094f2d9df0b54aTorne (Richard Coles) * from JPEG's top-to-bottom order.  To do this, we save the outgoing data
405d92fedcae5e801a8b224de090094f2d9df0b54aTorne (Richard Coles) * in a virtual array during put_pixel_row calls, then actually emit the
415d92fedcae5e801a8b224de090094f2d9df0b54aTorne (Richard Coles) * RLE file during finish_output.
427242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci */
437242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci
447242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci
455d92fedcae5e801a8b224de090094f2d9df0b54aTorne (Richard Coles)/*
465d92fedcae5e801a8b224de090094f2d9df0b54aTorne (Richard Coles) * For now, if we emit an RLE color map then it is always 256 entries long,
475d92fedcae5e801a8b224de090094f2d9df0b54aTorne (Richard Coles) * though not all of the entries need be used.
485d92fedcae5e801a8b224de090094f2d9df0b54aTorne (Richard Coles) */
495d92fedcae5e801a8b224de090094f2d9df0b54aTorne (Richard Coles)
505d92fedcae5e801a8b224de090094f2d9df0b54aTorne (Richard Coles)#define CMAPBITS	8
515d92fedcae5e801a8b224de090094f2d9df0b54aTorne (Richard Coles)#define CMAPLENGTH	(1<<(CMAPBITS))
525d92fedcae5e801a8b224de090094f2d9df0b54aTorne (Richard Coles)
535d92fedcae5e801a8b224de090094f2d9df0b54aTorne (Richard Coles)typedef struct {
545d92fedcae5e801a8b224de090094f2d9df0b54aTorne (Richard Coles)  struct djpeg_dest_struct pub; /* public fields */
555d92fedcae5e801a8b224de090094f2d9df0b54aTorne (Richard Coles)
565d92fedcae5e801a8b224de090094f2d9df0b54aTorne (Richard Coles)  jvirt_sarray_ptr image;	/* virtual array to store the output image */
575d92fedcae5e801a8b224de090094f2d9df0b54aTorne (Richard Coles)  rle_map *colormap;	 	/* RLE-style color map, or NULL if none */
585d92fedcae5e801a8b224de090094f2d9df0b54aTorne (Richard Coles)  rle_pixel **rle_row;		/* To pass rows to rle_putrow() */
595d92fedcae5e801a8b224de090094f2d9df0b54aTorne (Richard Coles)
605d92fedcae5e801a8b224de090094f2d9df0b54aTorne (Richard Coles)} rle_dest_struct;
61d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)
625d92fedcae5e801a8b224de090094f2d9df0b54aTorne (Richard Coles)typedef rle_dest_struct * rle_dest_ptr;
635d92fedcae5e801a8b224de090094f2d9df0b54aTorne (Richard Coles)
64d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)/* Forward declarations */
65d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)METHODDEF(void) rle_put_pixel_rows
66d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    JPP((j_decompress_ptr cinfo, djpeg_dest_ptr dinfo,
67d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)	 JDIMENSION rows_supplied));
687242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci
697242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci
70d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)/*
71d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles) * Write the file header.
72d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles) *
73d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles) * In this module it's easier to wait till finish_output to write anything.
74d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles) */
75d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)
76d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)METHODDEF(void)
77d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)start_output_rle (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo)
78d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles){
79d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)  rle_dest_ptr dest = (rle_dest_ptr) dinfo;
80d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)  size_t cmapsize;
81d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)  int i, ci;
82d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)#ifdef PROGRESS_REPORT
83d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)  cd_progress_ptr progress = (cd_progress_ptr) cinfo->progress;
84d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)#endif
85d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)
86d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)  /*
87d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)   * Make sure the image can be stored in RLE format.
88d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)   *
89d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)   * - RLE stores image dimensions as *signed* 16 bit integers.  JPEG
90d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)   *   uses unsigned, so we have to check the width.
91d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)   *
92d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)   * - Colorspace is expected to be grayscale or RGB.
93d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)   *
947242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci   * - The number of channels (components) is expected to be 1 (grayscale/
957242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci   *   pseudocolor) or 3 (truecolor/directcolor).
967242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci   *   (could be 2 or 4 if using an alpha channel, but we aren't)
97d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)   */
98d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)
99d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)  if (cinfo->output_width > 32767 || cinfo->output_height > 32767)
100d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    ERREXIT2(cinfo, JERR_RLE_DIMENSIONS, cinfo->output_width,
101d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)	     cinfo->output_height);
102d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)
1037242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci  if (cinfo->out_color_space != JCS_GRAYSCALE &&
1047242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci      cinfo->out_color_space != JCS_RGB)
1057242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci    ERREXIT(cinfo, JERR_RLE_COLORSPACE);
106d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)
1077242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci  if (cinfo->output_components != 1 && cinfo->output_components != 3)
108d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    ERREXIT1(cinfo, JERR_RLE_TOOMANYCHANNELS, cinfo->num_components);
109d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)
110d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)  /* Convert colormap, if any, to RLE format. */
111d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)
1127242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci  dest->colormap = NULL;
1137242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci
1147242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci  if (cinfo->quantize_colors) {
115d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    /* Allocate storage for RLE-style cmap, zero any extra entries */
116d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    cmapsize = cinfo->out_color_components * CMAPLENGTH * SIZEOF(rle_map);
117d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    dest->colormap = (rle_map *) (*cinfo->mem->alloc_small)
118d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)      ((j_common_ptr) cinfo, JPOOL_IMAGE, cmapsize);
1197242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci    MEMZERO(dest->colormap, cmapsize);
1207242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci
1217242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci    /* Save away data in RLE format --- note 8-bit left shift! */
122d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    /* Shifting would need adjustment for JSAMPLEs wider than 8 bits. */
1237242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci    for (ci = 0; ci < cinfo->out_color_components; ci++) {
124d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)      for (i = 0; i < cinfo->actual_number_of_colors; i++) {
125d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)        dest->colormap[ci * CMAPLENGTH + i] =
126d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)          GETJSAMPLE(cinfo->colormap[ci][i]) << 8;
127d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)      }
1287242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci    }
1297242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci  }
1307242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci
131d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)  /* Set the output buffer to the first row */
132d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)  dest->pub.buffer = (*cinfo->mem->access_virt_sarray)
133d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    ((j_common_ptr) cinfo, dest->image, (JDIMENSION) 0, (JDIMENSION) 1, TRUE);
134d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)  dest->pub.buffer_height = 1;
135d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)
136d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)  dest->pub.put_pixel_rows = rle_put_pixel_rows;
137d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)
138d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)#ifdef PROGRESS_REPORT
139d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)  if (progress != NULL) {
140d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    progress->total_extra_passes++;  /* count file writing as separate pass */
141d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)  }
142d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)#endif
143d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)}
144d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)
145d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)
146d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)/*
147d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles) * Write some pixel data.
148d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles) *
149d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles) * This routine just saves the data away in a virtual array.
150d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles) */
151d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)
152d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)METHODDEF(void)
153d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)rle_put_pixel_rows (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo,
154d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)		    JDIMENSION rows_supplied)
155d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles){
156d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)  rle_dest_ptr dest = (rle_dest_ptr) dinfo;
157d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)
158d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)  if (cinfo->output_scanline < cinfo->output_height) {
159d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    dest->pub.buffer = (*cinfo->mem->access_virt_sarray)
160d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)      ((j_common_ptr) cinfo, dest->image,
161d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)       cinfo->output_scanline, (JDIMENSION) 1, TRUE);
162d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)  }
163d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)}
164d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)
165d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)/*
166d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles) * Finish up at the end of the file.
167d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles) *
168d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles) * Here is where we really output the RLE file.
169d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles) */
170d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)
171d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)METHODDEF(void)
172d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)finish_output_rle (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo)
173d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles){
174d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)  rle_dest_ptr dest = (rle_dest_ptr) dinfo;
175d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)  rle_hdr header;		/* Output file information */
176d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)  rle_pixel **rle_row, *red, *green, *blue;
177d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)  JSAMPROW output_row;
178d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)  char cmapcomment[80];
179d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)  int row, col;
180d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)  int ci;
181d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)#ifdef PROGRESS_REPORT
182d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)  cd_progress_ptr progress = (cd_progress_ptr) cinfo->progress;
183d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)#endif
184d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)
185d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)  /* Initialize the header info */
186d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)  header = *rle_hdr_init(NULL);
187d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)  header.rle_file = dest->pub.output_file;
188d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)  header.xmin     = 0;
189d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)  header.xmax     = cinfo->output_width  - 1;
190d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)  header.ymin     = 0;
191d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)  header.ymax     = cinfo->output_height - 1;
192d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)  header.alpha    = 0;
193d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)  header.ncolors  = cinfo->output_components;
194d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)  for (ci = 0; ci < cinfo->output_components; ci++) {
195d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    RLE_SET_BIT(header, ci);
196d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)  }
197d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)  if (cinfo->quantize_colors) {
198d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    header.ncmap   = cinfo->out_color_components;
199d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    header.cmaplen = CMAPBITS;
200d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    header.cmap    = dest->colormap;
201d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    /* Add a comment to the output image with the true colormap length. */
202d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    sprintf(cmapcomment, "color_map_length=%d", cinfo->actual_number_of_colors);
203d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    rle_putcom(cmapcomment, &header);
204d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)  }
205d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)
206d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)  /* Emit the RLE header and color map (if any) */
207d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)  rle_put_setup(&header);
208d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)
209d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)  /* Now output the RLE data from our virtual array.
210d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)   * We assume here that (a) rle_pixel is represented the same as JSAMPLE,
211d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)   * and (b) we are not on a machine where FAR pointers differ from regular.
212d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)   */
213d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)
214d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)#ifdef PROGRESS_REPORT
215d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)  if (progress != NULL) {
2167242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci    progress->pub.pass_limit = cinfo->output_height;
217d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    progress->pub.pass_counter = 0;
218d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    (*progress->pub.progress_monitor) ((j_common_ptr) cinfo);
219d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)  }
220d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)#endif
221d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)
222d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)  if (cinfo->output_components == 1) {
223d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    for (row = cinfo->output_height-1; row >= 0; row--) {
224d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)      rle_row = (rle_pixel **) (*cinfo->mem->access_virt_sarray)
225d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)        ((j_common_ptr) cinfo, dest->image,
226d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)	 (JDIMENSION) row, (JDIMENSION) 1, FALSE);
227d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)      rle_putrow(rle_row, (int) cinfo->output_width, &header);
228d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)#ifdef PROGRESS_REPORT
229d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)      if (progress != NULL) {
230d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)        progress->pub.pass_counter++;
231d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)        (*progress->pub.progress_monitor) ((j_common_ptr) cinfo);
232e38fbeeb576b5094e34e038ab88d9d6a5c5c2214Torne (Richard Coles)      }
233d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)#endif
234d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    }
235d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)  } else {
2367242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci    for (row = cinfo->output_height-1; row >= 0; row--) {
2377242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci      rle_row = (rle_pixel **) dest->rle_row;
2387242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci      output_row = * (*cinfo->mem->access_virt_sarray)
239d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)        ((j_common_ptr) cinfo, dest->image,
240d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)	 (JDIMENSION) row, (JDIMENSION) 1, FALSE);
241d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)      red = rle_row[0];
242d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)      green = rle_row[1];
243d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)      blue = rle_row[2];
244323480423219ecd77329f8326dc5e0e3b50926d4Torne (Richard Coles)      for (col = cinfo->output_width; col > 0; col--) {
245323480423219ecd77329f8326dc5e0e3b50926d4Torne (Richard Coles)        *red++ = GETJSAMPLE(*output_row++);
246d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)        *green++ = GETJSAMPLE(*output_row++);
247d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)        *blue++ = GETJSAMPLE(*output_row++);
248d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)      }
249d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)      rle_putrow(rle_row, (int) cinfo->output_width, &header);
250d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)#ifdef PROGRESS_REPORT
251d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)      if (progress != NULL) {
252d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)        progress->pub.pass_counter++;
253d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)        (*progress->pub.progress_monitor) ((j_common_ptr) cinfo);
254d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)      }
255d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)#endif
256c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)    }
257  }
258
259#ifdef PROGRESS_REPORT
260  if (progress != NULL)
261    progress->completed_extra_passes++;
262#endif
263
264  /* Emit file trailer */
265  rle_puteof(&header);
266  fflush(dest->pub.output_file);
267  if (ferror(dest->pub.output_file))
268    ERREXIT(cinfo, JERR_FILE_WRITE);
269}
270
271
272/*
273 * The module selection routine for RLE format output.
274 */
275
276GLOBAL(djpeg_dest_ptr)
277jinit_write_rle (j_decompress_ptr cinfo)
278{
279  rle_dest_ptr dest;
280
281  /* Create module interface object, fill in method pointers */
282  dest = (rle_dest_ptr)
283      (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
284                                  SIZEOF(rle_dest_struct));
285  dest->pub.start_output = start_output_rle;
286  dest->pub.finish_output = finish_output_rle;
287
288  /* Calculate output image dimensions so we can allocate space */
289  jpeg_calc_output_dimensions(cinfo);
290
291  /* Allocate a work array for output to the RLE library. */
292  dest->rle_row = (*cinfo->mem->alloc_sarray)
293    ((j_common_ptr) cinfo, JPOOL_IMAGE,
294     cinfo->output_width, (JDIMENSION) cinfo->output_components);
295
296  /* Allocate a virtual array to hold the image. */
297  dest->image = (*cinfo->mem->request_virt_sarray)
298    ((j_common_ptr) cinfo, JPOOL_IMAGE, FALSE,
299     (JDIMENSION) (cinfo->output_width * cinfo->output_components),
300     cinfo->output_height, (JDIMENSION) 1);
301
302  return (djpeg_dest_ptr) dest;
303}
304
305#endif /* RLE_SUPPORTED */
306