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 / rdppm.c < prev    next >
Text File  |  1995-01-20  |  14KB  |  455 lines

  1. /*
  2.  * rdppm.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 read input images in PPM/PGM format.
  9.  * The extended 2-byte-per-sample raw PPM/PGM formats are supported.
  10.  * The PBMPLUS library is NOT required to compile this software
  11.  * (but it is highly useful as a set of PPM image manipulation programs).
  12.  *
  13.  * These routines may need modification for non-Unix environments or
  14.  * specialized applications.  As they stand, they assume input from
  15.  * an ordinary stdio stream.  They further assume that reading begins
  16.  * at the start of the file; start_input may need work if the
  17.  * user interface has already read some data (e.g., to determine that
  18.  * the file is indeed PPM format).
  19.  */
  20.  
  21. #include "cdjpeg.h"        /* Common decls for cjpeg/djpeg applications */
  22.  
  23. #ifdef PPM_SUPPORTED
  24.  
  25.  
  26. /* Portions of this code are based on the PBMPLUS library, which is:
  27. **
  28. ** Copyright (C) 1988 by Jef Poskanzer.
  29. **
  30. ** Permission to use, copy, modify, and distribute this software and its
  31. ** documentation for any purpose and without fee is hereby granted, provided
  32. ** that the above copyright notice appear in all copies and that both that
  33. ** copyright notice and this permission notice appear in supporting
  34. ** documentation.  This software is provided "as is" without express or
  35. ** implied warranty.
  36. */
  37.  
  38.  
  39. /* Macros to deal with unsigned chars as efficiently as compiler allows */
  40.  
  41. #ifdef HAVE_UNSIGNED_CHAR
  42. typedef unsigned char U_CHAR;
  43. #define UCH(x)    ((int) (x))
  44. #else /* !HAVE_UNSIGNED_CHAR */
  45. #ifdef CHAR_IS_UNSIGNED
  46. typedef char U_CHAR;
  47. #define UCH(x)    ((int) (x))
  48. #else
  49. typedef char U_CHAR;
  50. #define UCH(x)    ((int) (x) & 0xFF)
  51. #endif
  52. #endif /* HAVE_UNSIGNED_CHAR */
  53.  
  54.  
  55. #define    ReadOK(file,buffer,len)    (JFREAD(file,buffer,len) == ((size_t) (len)))
  56.  
  57.  
  58. /*
  59.  * On most systems, reading individual bytes with getc() is drastically less
  60.  * efficient than buffering a row at a time with fread().  On PCs, we must
  61.  * allocate the buffer in near data space, because we are assuming small-data
  62.  * memory model, wherein fread() can't reach far memory.  If you need to
  63.  * process very wide images on a PC, you might have to compile in large-memory
  64.  * model, or else replace fread() with a getc() loop --- which will be much
  65.  * slower.
  66.  */
  67.  
  68.  
  69. /* Private version of data source object */
  70.  
  71. typedef struct {
  72.   struct cjpeg_source_struct pub; /* public fields */
  73.  
  74.   U_CHAR *iobuffer;        /* non-FAR pointer to I/O buffer */
  75.   JSAMPROW pixrow;        /* FAR pointer to same */
  76.   size_t buffer_width;        /* width of I/O buffer */
  77.   JSAMPLE *rescale;        /* => maxval-remapping array, or NULL */
  78. } ppm_source_struct;
  79.  
  80. typedef ppm_source_struct * ppm_source_ptr;
  81.  
  82.  
  83. LOCAL int
  84. pbm_getc (FILE * infile)
  85. /* Read next char, skipping over any comments */
  86. /* A comment/newline sequence is returned as a newline */
  87. {
  88.   register int ch;
  89.  
  90.   ch = getc(infile);
  91.   if (ch == '#') {
  92.     do {
  93.       ch = getc(infile);
  94.     } while (ch != '\n' && ch != EOF);
  95.   }
  96.   return ch;
  97. }
  98.  
  99.  
  100. LOCAL unsigned int
  101. read_pbm_integer (j_compress_ptr cinfo, FILE * infile)
  102. /* Read an unsigned decimal integer from the PPM file */
  103. /* Swallows one trailing character after the integer */
  104. /* Note that on a 16-bit-int machine, only values up to 64k can be read. */
  105. /* This should not be a problem in practice. */
  106. {
  107.   register int ch;
  108.   register unsigned int val;
  109.  
  110.   /* Skip any leading whitespace */
  111.   do {
  112.     ch = pbm_getc(infile);
  113.     if (ch == EOF)
  114.       ERREXIT(cinfo, JERR_INPUT_EOF);
  115. #ifndef OSK
  116.   } while (ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r');
  117. #else
  118.   } while (ch == ' ' || ch == '\t' || ch == '\n' || ch == '\l');
  119. #endif
  120.  
  121.   if (ch < '0' || ch > '9')
  122.     ERREXIT(cinfo, JERR_PPM_NONNUMERIC);
  123.  
  124.   val = ch - '0';
  125.   while ((ch = pbm_getc(infile)) >= '0' && ch <= '9') {
  126.     val *= 10;
  127.     val += ch - '0';
  128.   }
  129.   return val;
  130. }
  131.  
  132.  
  133. /*
  134.  * Read one row of pixels.
  135.  *
  136.  * We provide several different versions depending on input file format.
  137.  * In all cases, input is scaled to the size of JSAMPLE.
  138.  *
  139.  * A really fast path is provided for reading byte/sample raw files with
  140.  * maxval = MAXJSAMPLE, which is the normal case for 8-bit data.
  141.  */
  142.  
  143.  
  144. METHODDEF JDIMENSION
  145. get_text_gray_row (j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
  146. /* This version is for reading text-format PGM files with any maxval */
  147. {
  148.   ppm_source_ptr source = (ppm_source_ptr) sinfo;
  149.   FILE * infile = source->pub.input_file;
  150.   register JSAMPROW ptr;
  151.   register JSAMPLE *rescale = source->rescale;
  152.   JDIMENSION col;
  153.  
  154.   ptr = source->pub.buffer[0];
  155.   for (col = cinfo->image_width; col > 0; col--) {
  156.     *ptr++ = rescale[read_pbm_integer(cinfo, infile)];
  157.   }
  158.   return 1;
  159. }
  160.  
  161.  
  162. METHODDEF JDIMENSION
  163. get_text_rgb_row (j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
  164. /* This version is for reading text-format PPM files with any maxval */
  165. {
  166.   ppm_source_ptr source = (ppm_source_ptr) sinfo;
  167.   FILE * infile = source->pub.input_file;
  168.   register JSAMPROW ptr;
  169.   register JSAMPLE *rescale = source->rescale;
  170.   JDIMENSION col;
  171.  
  172.   ptr = source->pub.buffer[0];
  173.   for (col = cinfo->image_width; col > 0; col--) {
  174.     *ptr++ = rescale[read_pbm_integer(cinfo, infile)];
  175.     *ptr++ = rescale[read_pbm_integer(cinfo, infile)];
  176.     *ptr++ = rescale[read_pbm_integer(cinfo, infile)];
  177.   }
  178.   return 1;
  179. }
  180.  
  181.  
  182. METHODDEF JDIMENSION
  183. get_scaled_gray_row (j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
  184. /* This version is for reading raw-byte-format PGM files with any maxval */
  185. {
  186.   ppm_source_ptr source = (ppm_source_ptr) sinfo;
  187.   register JSAMPROW ptr;
  188.   register U_CHAR * bufferptr;
  189.   register JSAMPLE *rescale = source->rescale;
  190.   JDIMENSION col;
  191.  
  192.   if (! ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
  193.     ERREXIT(cinfo, JERR_INPUT_EOF);
  194.   ptr = source->pub.buffer[0];
  195.   bufferptr = source->iobuffer;
  196.   for (col = cinfo->image_width; col > 0; col--) {
  197.     *ptr++ = rescale[UCH(*bufferptr++)];
  198.   }
  199.   return 1;
  200. }
  201.  
  202.  
  203. METHODDEF JDIMENSION
  204. get_scaled_rgb_row (j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
  205. /* This version is for reading raw-byte-format PPM files with any maxval */
  206. {
  207.   ppm_source_ptr source = (ppm_source_ptr) sinfo;
  208.   register JSAMPROW ptr;
  209.   register U_CHAR * bufferptr;
  210.   register JSAMPLE *rescale = source->rescale;
  211.   JDIMENSION col;
  212.  
  213.   if (! ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
  214.     ERREXIT(cinfo, JERR_INPUT_EOF);
  215.   ptr = source->pub.buffer[0];
  216.   bufferptr = source->iobuffer;
  217.   for (col = cinfo->image_width; col > 0; col--) {
  218.     *ptr++ = rescale[UCH(*bufferptr++)];
  219.     *ptr++ = rescale[UCH(*bufferptr++)];
  220.     *ptr++ = rescale[UCH(*bufferptr++)];
  221.   }
  222.   return 1;
  223. }
  224.  
  225.  
  226. METHODDEF JDIMENSION
  227. get_raw_row (j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
  228. /* This version is for reading raw-byte-format files with maxval = MAXJSAMPLE.
  229.  * In this case we just read right into the JSAMPLE buffer!
  230.  * Note that same code works for PPM and PGM files.
  231.  */
  232. {
  233.   ppm_source_ptr source = (ppm_source_ptr) sinfo;
  234.  
  235.   if (! ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
  236.     ERREXIT(cinfo, JERR_INPUT_EOF);
  237.   return 1;
  238. }
  239.  
  240.  
  241. METHODDEF JDIMENSION
  242. get_word_gray_row (j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
  243. /* This version is for reading raw-word-format PGM files with any maxval */
  244. {
  245.   ppm_source_ptr source = (ppm_source_ptr) sinfo;
  246.   register JSAMPROW ptr;
  247.   register U_CHAR * bufferptr;
  248.   register JSAMPLE *rescale = source->rescale;
  249.   JDIMENSION col;
  250.  
  251.   if (! ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
  252.     ERREXIT(cinfo, JERR_INPUT_EOF);
  253.   ptr = source->pub.buffer[0];
  254.   bufferptr = source->iobuffer;
  255.   for (col = cinfo->image_width; col > 0; col--) {
  256.     register int temp;
  257.     temp  = UCH(*bufferptr++);
  258.     temp |= UCH(*bufferptr++) << 8;
  259.     *ptr++ = rescale[temp];
  260.   }
  261.   return 1;
  262. }
  263.  
  264.  
  265. METHODDEF JDIMENSION
  266. get_word_rgb_row (j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
  267. /* This version is for reading raw-word-format PPM files with any maxval */
  268. {
  269.   ppm_source_ptr source = (ppm_source_ptr) sinfo;
  270.   register JSAMPROW ptr;
  271.   register U_CHAR * bufferptr;
  272.   register JSAMPLE *rescale = source->rescale;
  273.   JDIMENSION col;
  274.  
  275.   if (! ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
  276.     ERREXIT(cinfo, JERR_INPUT_EOF);
  277.   ptr = source->pub.buffer[0];
  278.   bufferptr = source->iobuffer;
  279.   for (col = cinfo->image_width; col > 0; col--) {
  280.     register int temp;
  281.     temp  = UCH(*bufferptr++);
  282.     temp |= UCH(*bufferptr++) << 8;
  283.     *ptr++ = rescale[temp];
  284.     temp  = UCH(*bufferptr++);
  285.     temp |= UCH(*bufferptr++) << 8;
  286.     *ptr++ = rescale[temp];
  287.     temp  = UCH(*bufferptr++);
  288.     temp |= UCH(*bufferptr++) << 8;
  289.     *ptr++ = rescale[temp];
  290.   }
  291.   return 1;
  292. }
  293.  
  294.  
  295. /*
  296.  * Read the file header; return image size and component count.
  297.  */
  298.  
  299. METHODDEF void
  300. start_input_ppm (j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
  301. {
  302.   ppm_source_ptr source = (ppm_source_ptr) sinfo;
  303.   int c;
  304.   unsigned int w, h, maxval;
  305.   boolean need_iobuffer, use_raw_buffer, need_rescale;
  306.  
  307.   if (getc(source->pub.input_file) != 'P')
  308.     ERREXIT(cinfo, JERR_PPM_NOT);
  309.  
  310.   c = getc(source->pub.input_file); /* save format discriminator for a sec */
  311.  
  312.   /* fetch the remaining header info */
  313.   w = read_pbm_integer(cinfo, source->pub.input_file);
  314.   h = read_pbm_integer(cinfo, source->pub.input_file);
  315.   maxval = read_pbm_integer(cinfo, source->pub.input_file);
  316.  
  317.   if (w <= 0 || h <= 0 || maxval <= 0) /* error check */
  318.     ERREXIT(cinfo, JERR_PPM_NOT);
  319.  
  320.   cinfo->data_precision = BITS_IN_JSAMPLE; /* we always rescale data to this */
  321.   cinfo->image_width = (JDIMENSION) w;
  322.   cinfo->image_height = (JDIMENSION) h;
  323.  
  324.   /* initialize flags to most common settings */
  325.   need_iobuffer = TRUE;        /* do we need an I/O buffer? */
  326.   use_raw_buffer = FALSE;    /* do we map input buffer onto I/O buffer? */
  327.   need_rescale = TRUE;        /* do we need a rescale array? */
  328.  
  329.   switch (c) {
  330.   case '2':            /* it's a text-format PGM file */
  331.     cinfo->input_components = 1;
  332.     cinfo->in_color_space = JCS_GRAYSCALE;
  333.     TRACEMS2(cinfo, 1, JTRC_PGM_TEXT, w, h);
  334.     source->pub.get_pixel_rows = get_text_gray_row;
  335.     need_iobuffer = FALSE;
  336.     break;
  337.  
  338.   case '3':            /* it's a text-format PPM file */
  339.     cinfo->input_components = 3;
  340.     cinfo->in_color_space = JCS_RGB;
  341.     TRACEMS2(cinfo, 1, JTRC_PPM_TEXT, w, h);
  342.     source->pub.get_pixel_rows = get_text_rgb_row;
  343.     need_iobuffer = FALSE;
  344.     break;
  345.  
  346.   case '5':            /* it's a raw-format PGM file */
  347.     cinfo->input_components = 1;
  348.     cinfo->in_color_space = JCS_GRAYSCALE;
  349.     TRACEMS2(cinfo, 1, JTRC_PGM, w, h);
  350.     if (maxval > 255) {
  351.       source->pub.get_pixel_rows = get_word_gray_row;
  352.     } else if (maxval == MAXJSAMPLE && SIZEOF(JSAMPLE) == SIZEOF(U_CHAR)) {
  353.       source->pub.get_pixel_rows = get_raw_row;
  354.       use_raw_buffer = TRUE;
  355.       need_rescale = FALSE;
  356.     } else {
  357.       source->pub.get_pixel_rows = get_scaled_gray_row;
  358.     }
  359.     break;
  360.  
  361.   case '6':            /* it's a raw-format PPM file */
  362.     cinfo->input_components = 3;
  363.     cinfo->in_color_space = JCS_RGB;
  364.     TRACEMS2(cinfo, 1, JTRC_PPM, w, h);
  365.     if (maxval > 255) {
  366.       source->pub.get_pixel_rows = get_word_rgb_row;
  367.     } else if (maxval == MAXJSAMPLE && SIZEOF(JSAMPLE) == SIZEOF(U_CHAR)) {
  368.       source->pub.get_pixel_rows = get_raw_row;
  369.       use_raw_buffer = TRUE;
  370.       need_rescale = FALSE;
  371.     } else {
  372.       source->pub.get_pixel_rows = get_scaled_rgb_row;
  373.     }
  374.     break;
  375.  
  376.   default:
  377.     ERREXIT(cinfo, JERR_PPM_NOT);
  378.     break;
  379.   }
  380.  
  381.   /* Allocate space for I/O buffer: 1 or 3 bytes or words/pixel. */
  382.   if (need_iobuffer) {
  383.     source->buffer_width = (size_t) w * cinfo->input_components *
  384.       ((maxval<=255) ? SIZEOF(U_CHAR) : (2*SIZEOF(U_CHAR)));
  385.     source->iobuffer = (U_CHAR *)
  386.       (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  387.                   source->buffer_width);
  388.   }
  389.  
  390.   /* Create compressor input buffer. */
  391.   if (use_raw_buffer) {
  392.     /* For unscaled raw-input case, we can just map it onto the I/O buffer. */
  393.     /* Synthesize a JSAMPARRAY pointer structure */
  394.     /* Cast here implies near->far pointer conversion on PCs */
  395.     source->pixrow = (JSAMPROW) source->iobuffer;
  396.     source->pub.buffer = & source->pixrow;
  397.     source->pub.buffer_height = 1;
  398.   } else {
  399.     /* Need to translate anyway, so make a separate sample buffer. */
  400.     source->pub.buffer = (*cinfo->mem->alloc_sarray)
  401.       ((j_common_ptr) cinfo, JPOOL_IMAGE,
  402.        (JDIMENSION) w * cinfo->input_components, (JDIMENSION) 1);
  403.     source->pub.buffer_height = 1;
  404.   }
  405.  
  406.   /* Compute the rescaling array if required. */
  407.   if (need_rescale) {
  408.     INT32 val, half_maxval;
  409.  
  410.     /* On 16-bit-int machines we have to be careful of maxval = 65535 */
  411.     source->rescale = (JSAMPLE *)
  412.       (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  413.                   (size_t) (((long) maxval + 1L) * SIZEOF(JSAMPLE)));
  414.     half_maxval = maxval / 2;
  415.     for (val = 0; val <= (INT32) maxval; val++) {
  416.       /* The multiplication here must be done in 32 bits to avoid overflow */
  417.       source->rescale[val] = (JSAMPLE) ((val*MAXJSAMPLE + half_maxval)/maxval);
  418.     }
  419.   }
  420. }
  421.  
  422.  
  423. /*
  424.  * Finish up at the end of the file.
  425.  */
  426.  
  427. METHODDEF void
  428. finish_input_ppm (j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
  429. {
  430.   /* no work */
  431. }
  432.  
  433.  
  434. /*
  435.  * The module selection routine for PPM format input.
  436.  */
  437.  
  438. GLOBAL cjpeg_source_ptr
  439. jinit_read_ppm (j_compress_ptr cinfo)
  440. {
  441.   ppm_source_ptr source;
  442.  
  443.   /* Create module interface object */
  444.   source = (ppm_source_ptr)
  445.       (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  446.                   SIZEOF(ppm_source_struct));
  447.   /* Fill in method ptrs, except get_pixel_rows which start_input sets */
  448.   source->pub.start_input = start_input_ppm;
  449.   source->pub.finish_input = finish_input_ppm;
  450.  
  451.   return (cjpeg_source_ptr) source;
  452. }
  453.  
  454. #endif /* PPM_SUPPORTED */
  455.