kusano 7d535a
/*
kusano 7d535a
 * rdrle.c
kusano 7d535a
 *
kusano 7d535a
 * Copyright (C) 1991-1996, Thomas G. Lane.
kusano 7d535a
 * This file is part of the Independent JPEG Group's software.
kusano 7d535a
 * For conditions of distribution and use, see the accompanying README file.
kusano 7d535a
 *
kusano 7d535a
 * This file contains routines to read input images in Utah 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 input from
kusano 7d535a
 * an ordinary stdio stream.  They further assume that reading begins
kusano 7d535a
 * at the start of the file; start_input may need work if the
kusano 7d535a
 * user interface has already read some data (e.g., to determine that
kusano 7d535a
 * the file is indeed RLE format).
kusano 7d535a
 *
kusano 7d535a
 * Based on code contributed by Mike Lijewski,
kusano 7d535a
 * with updates from Robert Hutchinson.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
#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
 * We support the following types of RLE files:
kusano 7d535a
 *   
kusano 7d535a
 *   GRAYSCALE   - 8 bits, no colormap
kusano 7d535a
 *   MAPPEDGRAY  - 8 bits, 1 channel colomap
kusano 7d535a
 *   PSEUDOCOLOR - 8 bits, 3 channel colormap
kusano 7d535a
 *   TRUECOLOR   - 24 bits, 3 channel colormap
kusano 7d535a
 *   DIRECTCOLOR - 24 bits, no colormap
kusano 7d535a
 *
kusano 7d535a
 * For now, we ignore any alpha channel in the image.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
typedef enum
kusano 7d535a
  { GRAYSCALE, MAPPEDGRAY, PSEUDOCOLOR, TRUECOLOR, DIRECTCOLOR } rle_kind;
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * Since RLE stores scanlines bottom-to-top, we have to invert the image
kusano 7d535a
 * to conform to JPEG's top-to-bottom order.  To do this, we read the
kusano 7d535a
 * incoming image into a virtual array on the first get_pixel_rows call,
kusano 7d535a
 * then fetch the required row from the virtual array on subsequent calls.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
typedef struct _rle_source_struct * rle_source_ptr;
kusano 7d535a
kusano 7d535a
typedef struct _rle_source_struct {
kusano 7d535a
  struct cjpeg_source_struct pub; /* public fields */
kusano 7d535a
kusano 7d535a
  rle_kind visual;              /* actual type of input file */
kusano 7d535a
  jvirt_sarray_ptr image;       /* virtual array to hold the image */
kusano 7d535a
  JDIMENSION row;		/* current row # in the virtual array */
kusano 7d535a
  rle_hdr header;               /* Input file information */
kusano 7d535a
  rle_pixel** rle_row;          /* holds a row returned by rle_getrow() */
kusano 7d535a
kusano 7d535a
} rle_source_struct;
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * Read the file header; return image size and component count.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
METHODDEF(void)
kusano 7d535a
start_input_rle (j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
kusano 7d535a
{
kusano 7d535a
  rle_source_ptr source = (rle_source_ptr) sinfo;
kusano 7d535a
  JDIMENSION width, height;
kusano 7d535a
#ifdef PROGRESS_REPORT
kusano 7d535a
  cd_progress_ptr progress = (cd_progress_ptr) cinfo->progress;
kusano 7d535a
#endif
kusano 7d535a
kusano 7d535a
  /* Use RLE library routine to get the header info */
kusano 7d535a
  source->header = *rle_hdr_init(NULL);
kusano 7d535a
  source->header.rle_file = source->pub.input_file;
kusano 7d535a
  switch (rle_get_setup(&(source->header))) {
kusano 7d535a
  case RLE_SUCCESS:
kusano 7d535a
    /* A-OK */
kusano 7d535a
    break;
kusano 7d535a
  case RLE_NOT_RLE:
kusano 7d535a
    ERREXIT(cinfo, JERR_RLE_NOT);
kusano 7d535a
    break;
kusano 7d535a
  case RLE_NO_SPACE:
kusano 7d535a
    ERREXIT(cinfo, JERR_RLE_MEM);
kusano 7d535a
    break;
kusano 7d535a
  case RLE_EMPTY:
kusano 7d535a
    ERREXIT(cinfo, JERR_RLE_EMPTY);
kusano 7d535a
    break;
kusano 7d535a
  case RLE_EOF:
kusano 7d535a
    ERREXIT(cinfo, JERR_RLE_EOF);
kusano 7d535a
    break;
kusano 7d535a
  default:
kusano 7d535a
    ERREXIT(cinfo, JERR_RLE_BADERROR);
kusano 7d535a
    break;
kusano 7d535a
  }
kusano 7d535a
kusano 7d535a
  /* Figure out what we have, set private vars and return values accordingly */
kusano 7d535a
  
kusano 7d535a
  width  = source->header.xmax - source->header.xmin + 1;
kusano 7d535a
  height = source->header.ymax - source->header.ymin + 1;
kusano 7d535a
  source->header.xmin = 0;		/* realign horizontally */
kusano 7d535a
  source->header.xmax = width-1;
kusano 7d535a
kusano 7d535a
  cinfo->image_width      = width;
kusano 7d535a
  cinfo->image_height     = height;
kusano 7d535a
  cinfo->data_precision   = 8;  /* we can only handle 8 bit data */
kusano 7d535a
kusano 7d535a
  if (source->header.ncolors == 1 && source->header.ncmap == 0) {
kusano 7d535a
    source->visual     = GRAYSCALE;
kusano 7d535a
    TRACEMS2(cinfo, 1, JTRC_RLE_GRAY, width, height);
kusano 7d535a
  } else if (source->header.ncolors == 1 && source->header.ncmap == 1) {
kusano 7d535a
    source->visual     = MAPPEDGRAY;
kusano 7d535a
    TRACEMS3(cinfo, 1, JTRC_RLE_MAPGRAY, width, height,
kusano 7d535a
             1 << source->header.cmaplen);
kusano 7d535a
  } else if (source->header.ncolors == 1 && source->header.ncmap == 3) {
kusano 7d535a
    source->visual     = PSEUDOCOLOR;
kusano 7d535a
    TRACEMS3(cinfo, 1, JTRC_RLE_MAPPED, width, height,
kusano 7d535a
	     1 << source->header.cmaplen);
kusano 7d535a
  } else if (source->header.ncolors == 3 && source->header.ncmap == 3) {
kusano 7d535a
    source->visual     = TRUECOLOR;
kusano 7d535a
    TRACEMS3(cinfo, 1, JTRC_RLE_FULLMAP, width, height,
kusano 7d535a
	     1 << source->header.cmaplen);
kusano 7d535a
  } else if (source->header.ncolors == 3 && source->header.ncmap == 0) {
kusano 7d535a
    source->visual     = DIRECTCOLOR;
kusano 7d535a
    TRACEMS2(cinfo, 1, JTRC_RLE, width, height);
kusano 7d535a
  } else
kusano 7d535a
    ERREXIT(cinfo, JERR_RLE_UNSUPPORTED);
kusano 7d535a
  
kusano 7d535a
  if (source->visual == GRAYSCALE || source->visual == MAPPEDGRAY) {
kusano 7d535a
    cinfo->in_color_space   = JCS_GRAYSCALE;
kusano 7d535a
    cinfo->input_components = 1;
kusano 7d535a
  } else {
kusano 7d535a
    cinfo->in_color_space   = JCS_RGB;
kusano 7d535a
    cinfo->input_components = 3;
kusano 7d535a
  }
kusano 7d535a
kusano 7d535a
  /*
kusano 7d535a
   * A place to hold each scanline while it's converted.
kusano 7d535a
   * (GRAYSCALE scanlines don't need converting)
kusano 7d535a
   */
kusano 7d535a
  if (source->visual != GRAYSCALE) {
kusano 7d535a
    source->rle_row = (rle_pixel**) (*cinfo->mem->alloc_sarray)
kusano 7d535a
      ((j_common_ptr) cinfo, JPOOL_IMAGE,
kusano 7d535a
       (JDIMENSION) width, (JDIMENSION) cinfo->input_components);
kusano 7d535a
  }
kusano 7d535a
kusano 7d535a
  /* request a virtual array to hold the image */
kusano 7d535a
  source->image = (*cinfo->mem->request_virt_sarray)
kusano 7d535a
    ((j_common_ptr) cinfo, JPOOL_IMAGE, FALSE,
kusano 7d535a
     (JDIMENSION) (width * source->header.ncolors),
kusano 7d535a
     (JDIMENSION) height, (JDIMENSION) 1);
kusano 7d535a
kusano 7d535a
#ifdef PROGRESS_REPORT
kusano 7d535a
  if (progress != NULL) {
kusano 7d535a
    /* count file input as separate pass */
kusano 7d535a
    progress->total_extra_passes++;
kusano 7d535a
  }
kusano 7d535a
#endif
kusano 7d535a
kusano 7d535a
  source->pub.buffer_height = 1;
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * Read one row of pixels.
kusano 7d535a
 * Called only after load_image has read the image into the virtual array.
kusano 7d535a
 * Used for GRAYSCALE, MAPPEDGRAY, TRUECOLOR, and DIRECTCOLOR images.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
METHODDEF(JDIMENSION)
kusano 7d535a
get_rle_row (j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
kusano 7d535a
{
kusano 7d535a
  rle_source_ptr source = (rle_source_ptr) sinfo;
kusano 7d535a
kusano 7d535a
  source->row--;
kusano 7d535a
  source->pub.buffer = (*cinfo->mem->access_virt_sarray)
kusano 7d535a
    ((j_common_ptr) cinfo, source->image, source->row, (JDIMENSION) 1, FALSE);
kusano 7d535a
kusano 7d535a
  return 1;
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * Read one row of pixels.
kusano 7d535a
 * Called only after load_image has read the image into the virtual array.
kusano 7d535a
 * Used for PSEUDOCOLOR images.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
METHODDEF(JDIMENSION)
kusano 7d535a
get_pseudocolor_row (j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
kusano 7d535a
{
kusano 7d535a
  rle_source_ptr source = (rle_source_ptr) sinfo;
kusano 7d535a
  JSAMPROW src_row, dest_row;
kusano 7d535a
  JDIMENSION col;
kusano 7d535a
  rle_map *colormap;
kusano 7d535a
  int val;
kusano 7d535a
kusano 7d535a
  colormap = source->header.cmap;
kusano 7d535a
  dest_row = source->pub.buffer[0];
kusano 7d535a
  source->row--;
kusano 7d535a
  src_row = * (*cinfo->mem->access_virt_sarray)
kusano 7d535a
    ((j_common_ptr) cinfo, source->image, source->row, (JDIMENSION) 1, FALSE);
kusano 7d535a
kusano 7d535a
  for (col = cinfo->image_width; col > 0; col--) {
kusano 7d535a
    val = GETJSAMPLE(*src_row++);
kusano 7d535a
    *dest_row++ = (JSAMPLE) (colormap[val      ] >> 8);
kusano 7d535a
    *dest_row++ = (JSAMPLE) (colormap[val + 256] >> 8);
kusano 7d535a
    *dest_row++ = (JSAMPLE) (colormap[val + 512] >> 8);
kusano 7d535a
  }
kusano 7d535a
kusano 7d535a
  return 1;
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * Load the image into a virtual array.  We have to do this because RLE
kusano 7d535a
 * files start at the lower left while the JPEG standard has them starting
kusano 7d535a
 * in the upper left.  This is called the first time we want to get a row
kusano 7d535a
 * of input.  What we do is load the RLE data into the array and then call
kusano 7d535a
 * the appropriate routine to read one row from the array.  Before returning,
kusano 7d535a
 * we set source->pub.get_pixel_rows so that subsequent calls go straight to
kusano 7d535a
 * the appropriate row-reading routine.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
METHODDEF(JDIMENSION)
kusano 7d535a
load_image (j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
kusano 7d535a
{
kusano 7d535a
  rle_source_ptr source = (rle_source_ptr) sinfo;
kusano 7d535a
  JDIMENSION row, col;
kusano 7d535a
  JSAMPROW  scanline, red_ptr, green_ptr, blue_ptr;
kusano 7d535a
  rle_pixel **rle_row;
kusano 7d535a
  rle_map *colormap;
kusano 7d535a
  char channel;
kusano 7d535a
#ifdef PROGRESS_REPORT
kusano 7d535a
  cd_progress_ptr progress = (cd_progress_ptr) cinfo->progress;
kusano 7d535a
#endif
kusano 7d535a
kusano 7d535a
  colormap = source->header.cmap;
kusano 7d535a
  rle_row = source->rle_row;
kusano 7d535a
kusano 7d535a
  /* Read the RLE data into our virtual array.
kusano 7d535a
   * We assume here that (a) rle_pixel is represented the same as JSAMPLE,
kusano 7d535a
   * and (b) we are not on a machine where FAR pointers differ from regular.
kusano 7d535a
   */
kusano 7d535a
  RLE_CLR_BIT(source->header, RLE_ALPHA); /* don't read the alpha channel */
kusano 7d535a
kusano 7d535a
#ifdef PROGRESS_REPORT
kusano 7d535a
  if (progress != NULL) {
kusano 7d535a
    progress->pub.pass_limit = cinfo->image_height;
kusano 7d535a
    progress->pub.pass_counter = 0;
kusano 7d535a
    (*progress->pub.progress_monitor) ((j_common_ptr) cinfo);
kusano 7d535a
  }
kusano 7d535a
#endif
kusano 7d535a
kusano 7d535a
  switch (source->visual) {
kusano 7d535a
kusano 7d535a
  case GRAYSCALE:
kusano 7d535a
  case PSEUDOCOLOR:
kusano 7d535a
    for (row = 0; row < cinfo->image_height; row++) {
kusano 7d535a
      rle_row = (rle_pixel **) (*cinfo->mem->access_virt_sarray)
kusano 7d535a
         ((j_common_ptr) cinfo, source->image, row, (JDIMENSION) 1, TRUE);
kusano 7d535a
      rle_getrow(&source->header, rle_row);
kusano 7d535a
#ifdef PROGRESS_REPORT
kusano 7d535a
      if (progress != NULL) {
kusano 7d535a
        progress->pub.pass_counter++;
kusano 7d535a
        (*progress->pub.progress_monitor) ((j_common_ptr) cinfo);
kusano 7d535a
      }
kusano 7d535a
#endif
kusano 7d535a
    }
kusano 7d535a
    break;
kusano 7d535a
kusano 7d535a
  case MAPPEDGRAY:
kusano 7d535a
  case TRUECOLOR:
kusano 7d535a
    for (row = 0; row < cinfo->image_height; row++) {
kusano 7d535a
      scanline = * (*cinfo->mem->access_virt_sarray)
kusano 7d535a
        ((j_common_ptr) cinfo, source->image, row, (JDIMENSION) 1, TRUE);
kusano 7d535a
      rle_row = source->rle_row;
kusano 7d535a
      rle_getrow(&source->header, rle_row);
kusano 7d535a
kusano 7d535a
      for (col = 0; col < cinfo->image_width; col++) {
kusano 7d535a
        for (channel = 0; channel < source->header.ncolors; channel++) {
kusano 7d535a
          *scanline++ = (JSAMPLE)
kusano 7d535a
            (colormap[GETJSAMPLE(rle_row[channel][col]) + 256 * channel] >> 8);
kusano 7d535a
        }
kusano 7d535a
      }
kusano 7d535a
kusano 7d535a
#ifdef PROGRESS_REPORT
kusano 7d535a
      if (progress != NULL) {
kusano 7d535a
        progress->pub.pass_counter++;
kusano 7d535a
        (*progress->pub.progress_monitor) ((j_common_ptr) cinfo);
kusano 7d535a
      }
kusano 7d535a
#endif
kusano 7d535a
    }
kusano 7d535a
    break;
kusano 7d535a
kusano 7d535a
  case DIRECTCOLOR:
kusano 7d535a
    for (row = 0; row < cinfo->image_height; row++) {
kusano 7d535a
      scanline = * (*cinfo->mem->access_virt_sarray)
kusano 7d535a
        ((j_common_ptr) cinfo, source->image, row, (JDIMENSION) 1, TRUE);
kusano 7d535a
      rle_getrow(&source->header, rle_row);
kusano 7d535a
kusano 7d535a
      red_ptr   = rle_row[0];
kusano 7d535a
      green_ptr = rle_row[1];
kusano 7d535a
      blue_ptr  = rle_row[2];
kusano 7d535a
kusano 7d535a
      for (col = cinfo->image_width; col > 0; col--) {
kusano 7d535a
        *scanline++ = *red_ptr++;
kusano 7d535a
        *scanline++ = *green_ptr++;
kusano 7d535a
        *scanline++ = *blue_ptr++;
kusano 7d535a
      }
kusano 7d535a
kusano 7d535a
#ifdef PROGRESS_REPORT
kusano 7d535a
      if (progress != NULL) {
kusano 7d535a
        progress->pub.pass_counter++;
kusano 7d535a
        (*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
  /* Set up to call proper row-extraction routine in future */
kusano 7d535a
  if (source->visual == PSEUDOCOLOR) {
kusano 7d535a
    source->pub.buffer = source->rle_row;
kusano 7d535a
    source->pub.get_pixel_rows = get_pseudocolor_row;
kusano 7d535a
  } else {
kusano 7d535a
    source->pub.get_pixel_rows = get_rle_row;
kusano 7d535a
  }
kusano 7d535a
  source->row = cinfo->image_height;
kusano 7d535a
kusano 7d535a
  /* And fetch the topmost (bottommost) row */
kusano 7d535a
  return (*source->pub.get_pixel_rows) (cinfo, sinfo);   
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * Finish up at the end of the file.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
METHODDEF(void)
kusano 7d535a
finish_input_rle (j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
kusano 7d535a
{
kusano 7d535a
  /* no work */
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * The module selection routine for RLE format input.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
GLOBAL(cjpeg_source_ptr)
kusano 7d535a
jinit_read_rle (j_compress_ptr cinfo)
kusano 7d535a
{
kusano 7d535a
  rle_source_ptr source;
kusano 7d535a
kusano 7d535a
  /* Create module interface object */
kusano 7d535a
  source = (rle_source_ptr)
kusano 7d535a
      (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
kusano 7d535a
                                  SIZEOF(rle_source_struct));
kusano 7d535a
  /* Fill in method ptrs */
kusano 7d535a
  source->pub.start_input = start_input_rle;
kusano 7d535a
  source->pub.finish_input = finish_input_rle;
kusano 7d535a
  source->pub.get_pixel_rows = load_image;
kusano 7d535a
kusano 7d535a
  return (cjpeg_source_ptr) source;
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
#endif /* RLE_SUPPORTED */