Final 350 project

Dependencies:   uzair Camera_LS_Y201 F7_Ethernet LCD_DISCO_F746NG NetworkAPI SDFileSystem mbed

Committer:
shoaib_ahmed
Date:
Mon Jul 31 09:16:35 2017 +0000
Revision:
0:791a779d6220
final project;

Who changed what in which revision?

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