Blame gtkmm-osx/jpeg-6b/wrrle.c

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