home *** CD-ROM | disk | FTP | other *** search
/ rtsi.com / 2014.01.www.rtsi.com.tar / www.rtsi.com / OS9 / OSK / GRAPHICS / jpeglib_5a.lzh / JPEG_5A / wrrle.c < prev    next >
Text File  |  1995-01-14  |  9KB  |  303 lines

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