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 * wrppm.c
shoaib_ahmed 0:791a779d6220 3 *
shoaib_ahmed 0:791a779d6220 4 * Copyright (C) 1991-1996, Thomas G. Lane.
shoaib_ahmed 0:791a779d6220 5 * Modified 2009 by 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 write output 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 output to
shoaib_ahmed 0:791a779d6220 16 * an ordinary stdio stream.
shoaib_ahmed 0:791a779d6220 17 */
shoaib_ahmed 0:791a779d6220 18
shoaib_ahmed 0:791a779d6220 19 #include "cdjpeg.h" /* Common decls for cjpeg/djpeg applications */
shoaib_ahmed 0:791a779d6220 20
shoaib_ahmed 0:791a779d6220 21 #ifdef PPM_SUPPORTED
shoaib_ahmed 0:791a779d6220 22
shoaib_ahmed 0:791a779d6220 23
shoaib_ahmed 0:791a779d6220 24 /*
shoaib_ahmed 0:791a779d6220 25 * For 12-bit JPEG data, we either downscale the values to 8 bits
shoaib_ahmed 0:791a779d6220 26 * (to write standard byte-per-sample PPM/PGM files), or output
shoaib_ahmed 0:791a779d6220 27 * nonstandard word-per-sample PPM/PGM files. Downscaling is done
shoaib_ahmed 0:791a779d6220 28 * if PPM_NORAWWORD is defined (this can be done in the Makefile
shoaib_ahmed 0:791a779d6220 29 * or in jconfig.h).
shoaib_ahmed 0:791a779d6220 30 * (When the core library supports data precision reduction, a cleaner
shoaib_ahmed 0:791a779d6220 31 * implementation will be to ask for that instead.)
shoaib_ahmed 0:791a779d6220 32 */
shoaib_ahmed 0:791a779d6220 33
shoaib_ahmed 0:791a779d6220 34 #if BITS_IN_JSAMPLE == 8
shoaib_ahmed 0:791a779d6220 35 #define PUTPPMSAMPLE(ptr,v) *ptr++ = (char) (v)
shoaib_ahmed 0:791a779d6220 36 #define BYTESPERSAMPLE 1
shoaib_ahmed 0:791a779d6220 37 #define PPM_MAXVAL 255
shoaib_ahmed 0:791a779d6220 38 #else
shoaib_ahmed 0:791a779d6220 39 #ifdef PPM_NORAWWORD
shoaib_ahmed 0:791a779d6220 40 #define PUTPPMSAMPLE(ptr,v) *ptr++ = (char) ((v) >> (BITS_IN_JSAMPLE-8))
shoaib_ahmed 0:791a779d6220 41 #define BYTESPERSAMPLE 1
shoaib_ahmed 0:791a779d6220 42 #define PPM_MAXVAL 255
shoaib_ahmed 0:791a779d6220 43 #else
shoaib_ahmed 0:791a779d6220 44 /* The word-per-sample format always puts the MSB first. */
shoaib_ahmed 0:791a779d6220 45 #define PUTPPMSAMPLE(ptr,v) \
shoaib_ahmed 0:791a779d6220 46 { register int val_ = v; \
shoaib_ahmed 0:791a779d6220 47 *ptr++ = (char) ((val_ >> 8) & 0xFF); \
shoaib_ahmed 0:791a779d6220 48 *ptr++ = (char) (val_ & 0xFF); \
shoaib_ahmed 0:791a779d6220 49 }
shoaib_ahmed 0:791a779d6220 50 #define BYTESPERSAMPLE 2
shoaib_ahmed 0:791a779d6220 51 #define PPM_MAXVAL ((1<<BITS_IN_JSAMPLE)-1)
shoaib_ahmed 0:791a779d6220 52 #endif
shoaib_ahmed 0:791a779d6220 53 #endif
shoaib_ahmed 0:791a779d6220 54
shoaib_ahmed 0:791a779d6220 55
shoaib_ahmed 0:791a779d6220 56 /*
shoaib_ahmed 0:791a779d6220 57 * When JSAMPLE is the same size as char, we can just fwrite() the
shoaib_ahmed 0:791a779d6220 58 * decompressed data to the PPM or PGM file. On PCs, in order to make this
shoaib_ahmed 0:791a779d6220 59 * work the output buffer must be allocated in near data space, because we are
shoaib_ahmed 0:791a779d6220 60 * assuming small-data memory model wherein fwrite() can't reach far memory.
shoaib_ahmed 0:791a779d6220 61 * If you need to process very wide images on a PC, you might have to compile
shoaib_ahmed 0:791a779d6220 62 * in large-memory model, or else replace fwrite() with a putc() loop ---
shoaib_ahmed 0:791a779d6220 63 * which will be much slower.
shoaib_ahmed 0:791a779d6220 64 */
shoaib_ahmed 0:791a779d6220 65
shoaib_ahmed 0:791a779d6220 66
shoaib_ahmed 0:791a779d6220 67 /* Private version of data destination object */
shoaib_ahmed 0:791a779d6220 68
shoaib_ahmed 0:791a779d6220 69 typedef struct {
shoaib_ahmed 0:791a779d6220 70 struct djpeg_dest_struct pub; /* public fields */
shoaib_ahmed 0:791a779d6220 71
shoaib_ahmed 0:791a779d6220 72 /* Usually these two pointers point to the same place: */
shoaib_ahmed 0:791a779d6220 73 char *iobuffer; /* fwrite's I/O buffer */
shoaib_ahmed 0:791a779d6220 74 JSAMPROW pixrow; /* decompressor output buffer */
shoaib_ahmed 0:791a779d6220 75 size_t buffer_width; /* width of I/O buffer */
shoaib_ahmed 0:791a779d6220 76 JDIMENSION samples_per_row; /* JSAMPLEs per output row */
shoaib_ahmed 0:791a779d6220 77 } ppm_dest_struct;
shoaib_ahmed 0:791a779d6220 78
shoaib_ahmed 0:791a779d6220 79 typedef ppm_dest_struct * ppm_dest_ptr;
shoaib_ahmed 0:791a779d6220 80
shoaib_ahmed 0:791a779d6220 81
shoaib_ahmed 0:791a779d6220 82 /*
shoaib_ahmed 0:791a779d6220 83 * Write some pixel data.
shoaib_ahmed 0:791a779d6220 84 * In this module rows_supplied will always be 1.
shoaib_ahmed 0:791a779d6220 85 *
shoaib_ahmed 0:791a779d6220 86 * put_pixel_rows handles the "normal" 8-bit case where the decompressor
shoaib_ahmed 0:791a779d6220 87 * output buffer is physically the same as the fwrite buffer.
shoaib_ahmed 0:791a779d6220 88 */
shoaib_ahmed 0:791a779d6220 89
shoaib_ahmed 0:791a779d6220 90 METHODDEF(void)
shoaib_ahmed 0:791a779d6220 91 put_pixel_rows (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo,
shoaib_ahmed 0:791a779d6220 92 JDIMENSION rows_supplied)
shoaib_ahmed 0:791a779d6220 93 {
shoaib_ahmed 0:791a779d6220 94 ppm_dest_ptr dest = (ppm_dest_ptr) dinfo;
shoaib_ahmed 0:791a779d6220 95
shoaib_ahmed 0:791a779d6220 96 (void) JFWRITE(dest->pub.output_file, dest->iobuffer, dest->buffer_width);
shoaib_ahmed 0:791a779d6220 97 }
shoaib_ahmed 0:791a779d6220 98
shoaib_ahmed 0:791a779d6220 99
shoaib_ahmed 0:791a779d6220 100 /*
shoaib_ahmed 0:791a779d6220 101 * This code is used when we have to copy the data and apply a pixel
shoaib_ahmed 0:791a779d6220 102 * format translation. Typically this only happens in 12-bit mode.
shoaib_ahmed 0:791a779d6220 103 */
shoaib_ahmed 0:791a779d6220 104
shoaib_ahmed 0:791a779d6220 105 METHODDEF(void)
shoaib_ahmed 0:791a779d6220 106 copy_pixel_rows (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo,
shoaib_ahmed 0:791a779d6220 107 JDIMENSION rows_supplied)
shoaib_ahmed 0:791a779d6220 108 {
shoaib_ahmed 0:791a779d6220 109 ppm_dest_ptr dest = (ppm_dest_ptr) dinfo;
shoaib_ahmed 0:791a779d6220 110 register char * bufferptr;
shoaib_ahmed 0:791a779d6220 111 register JSAMPROW ptr;
shoaib_ahmed 0:791a779d6220 112 register JDIMENSION col;
shoaib_ahmed 0:791a779d6220 113
shoaib_ahmed 0:791a779d6220 114 ptr = dest->pub.buffer[0];
shoaib_ahmed 0:791a779d6220 115 bufferptr = dest->iobuffer;
shoaib_ahmed 0:791a779d6220 116 for (col = dest->samples_per_row; col > 0; col--) {
shoaib_ahmed 0:791a779d6220 117 PUTPPMSAMPLE(bufferptr, GETJSAMPLE(*ptr++));
shoaib_ahmed 0:791a779d6220 118 }
shoaib_ahmed 0:791a779d6220 119 (void) JFWRITE(dest->pub.output_file, dest->iobuffer, dest->buffer_width);
shoaib_ahmed 0:791a779d6220 120 }
shoaib_ahmed 0:791a779d6220 121
shoaib_ahmed 0:791a779d6220 122
shoaib_ahmed 0:791a779d6220 123 /*
shoaib_ahmed 0:791a779d6220 124 * Write some pixel data when color quantization is in effect.
shoaib_ahmed 0:791a779d6220 125 * We have to demap the color index values to straight data.
shoaib_ahmed 0:791a779d6220 126 */
shoaib_ahmed 0:791a779d6220 127
shoaib_ahmed 0:791a779d6220 128 METHODDEF(void)
shoaib_ahmed 0:791a779d6220 129 put_demapped_rgb (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo,
shoaib_ahmed 0:791a779d6220 130 JDIMENSION rows_supplied)
shoaib_ahmed 0:791a779d6220 131 {
shoaib_ahmed 0:791a779d6220 132 ppm_dest_ptr dest = (ppm_dest_ptr) dinfo;
shoaib_ahmed 0:791a779d6220 133 register char * bufferptr;
shoaib_ahmed 0:791a779d6220 134 register int pixval;
shoaib_ahmed 0:791a779d6220 135 register JSAMPROW ptr;
shoaib_ahmed 0:791a779d6220 136 register JSAMPROW color_map0 = cinfo->colormap[0];
shoaib_ahmed 0:791a779d6220 137 register JSAMPROW color_map1 = cinfo->colormap[1];
shoaib_ahmed 0:791a779d6220 138 register JSAMPROW color_map2 = cinfo->colormap[2];
shoaib_ahmed 0:791a779d6220 139 register JDIMENSION col;
shoaib_ahmed 0:791a779d6220 140
shoaib_ahmed 0:791a779d6220 141 ptr = dest->pub.buffer[0];
shoaib_ahmed 0:791a779d6220 142 bufferptr = dest->iobuffer;
shoaib_ahmed 0:791a779d6220 143 for (col = cinfo->output_width; col > 0; col--) {
shoaib_ahmed 0:791a779d6220 144 pixval = GETJSAMPLE(*ptr++);
shoaib_ahmed 0:791a779d6220 145 PUTPPMSAMPLE(bufferptr, GETJSAMPLE(color_map0[pixval]));
shoaib_ahmed 0:791a779d6220 146 PUTPPMSAMPLE(bufferptr, GETJSAMPLE(color_map1[pixval]));
shoaib_ahmed 0:791a779d6220 147 PUTPPMSAMPLE(bufferptr, GETJSAMPLE(color_map2[pixval]));
shoaib_ahmed 0:791a779d6220 148 }
shoaib_ahmed 0:791a779d6220 149 (void) JFWRITE(dest->pub.output_file, dest->iobuffer, dest->buffer_width);
shoaib_ahmed 0:791a779d6220 150 }
shoaib_ahmed 0:791a779d6220 151
shoaib_ahmed 0:791a779d6220 152
shoaib_ahmed 0:791a779d6220 153 METHODDEF(void)
shoaib_ahmed 0:791a779d6220 154 put_demapped_gray (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo,
shoaib_ahmed 0:791a779d6220 155 JDIMENSION rows_supplied)
shoaib_ahmed 0:791a779d6220 156 {
shoaib_ahmed 0:791a779d6220 157 ppm_dest_ptr dest = (ppm_dest_ptr) dinfo;
shoaib_ahmed 0:791a779d6220 158 register char * bufferptr;
shoaib_ahmed 0:791a779d6220 159 register JSAMPROW ptr;
shoaib_ahmed 0:791a779d6220 160 register JSAMPROW color_map = cinfo->colormap[0];
shoaib_ahmed 0:791a779d6220 161 register JDIMENSION col;
shoaib_ahmed 0:791a779d6220 162
shoaib_ahmed 0:791a779d6220 163 ptr = dest->pub.buffer[0];
shoaib_ahmed 0:791a779d6220 164 bufferptr = dest->iobuffer;
shoaib_ahmed 0:791a779d6220 165 for (col = cinfo->output_width; col > 0; col--) {
shoaib_ahmed 0:791a779d6220 166 PUTPPMSAMPLE(bufferptr, GETJSAMPLE(color_map[GETJSAMPLE(*ptr++)]));
shoaib_ahmed 0:791a779d6220 167 }
shoaib_ahmed 0:791a779d6220 168 (void) JFWRITE(dest->pub.output_file, dest->iobuffer, dest->buffer_width);
shoaib_ahmed 0:791a779d6220 169 }
shoaib_ahmed 0:791a779d6220 170
shoaib_ahmed 0:791a779d6220 171
shoaib_ahmed 0:791a779d6220 172 /*
shoaib_ahmed 0:791a779d6220 173 * Startup: write the file header.
shoaib_ahmed 0:791a779d6220 174 */
shoaib_ahmed 0:791a779d6220 175
shoaib_ahmed 0:791a779d6220 176 METHODDEF(void)
shoaib_ahmed 0:791a779d6220 177 start_output_ppm (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo)
shoaib_ahmed 0:791a779d6220 178 {
shoaib_ahmed 0:791a779d6220 179 ppm_dest_ptr dest = (ppm_dest_ptr) dinfo;
shoaib_ahmed 0:791a779d6220 180
shoaib_ahmed 0:791a779d6220 181 /* Emit file header */
shoaib_ahmed 0:791a779d6220 182 switch (cinfo->out_color_space) {
shoaib_ahmed 0:791a779d6220 183 case JCS_GRAYSCALE:
shoaib_ahmed 0:791a779d6220 184 /* emit header for raw PGM format */
shoaib_ahmed 0:791a779d6220 185 fprintf(dest->pub.output_file, "P5\n%ld %ld\n%d\n",
shoaib_ahmed 0:791a779d6220 186 (long) cinfo->output_width, (long) cinfo->output_height,
shoaib_ahmed 0:791a779d6220 187 PPM_MAXVAL);
shoaib_ahmed 0:791a779d6220 188 break;
shoaib_ahmed 0:791a779d6220 189 case JCS_RGB:
shoaib_ahmed 0:791a779d6220 190 /* emit header for raw PPM format */
shoaib_ahmed 0:791a779d6220 191 fprintf(dest->pub.output_file, "P6\n%ld %ld\n%d\n",
shoaib_ahmed 0:791a779d6220 192 (long) cinfo->output_width, (long) cinfo->output_height,
shoaib_ahmed 0:791a779d6220 193 PPM_MAXVAL);
shoaib_ahmed 0:791a779d6220 194 break;
shoaib_ahmed 0:791a779d6220 195 default:
shoaib_ahmed 0:791a779d6220 196 ERREXIT(cinfo, JERR_PPM_COLORSPACE);
shoaib_ahmed 0:791a779d6220 197 }
shoaib_ahmed 0:791a779d6220 198 }
shoaib_ahmed 0:791a779d6220 199
shoaib_ahmed 0:791a779d6220 200
shoaib_ahmed 0:791a779d6220 201 /*
shoaib_ahmed 0:791a779d6220 202 * Finish up at the end of the file.
shoaib_ahmed 0:791a779d6220 203 */
shoaib_ahmed 0:791a779d6220 204
shoaib_ahmed 0:791a779d6220 205 METHODDEF(void)
shoaib_ahmed 0:791a779d6220 206 finish_output_ppm (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo)
shoaib_ahmed 0:791a779d6220 207 {
shoaib_ahmed 0:791a779d6220 208 /* Make sure we wrote the output file OK */
shoaib_ahmed 0:791a779d6220 209 fflush(dinfo->output_file);
shoaib_ahmed 0:791a779d6220 210 if (ferror(dinfo->output_file))
shoaib_ahmed 0:791a779d6220 211 ERREXIT(cinfo, JERR_FILE_WRITE);
shoaib_ahmed 0:791a779d6220 212 }
shoaib_ahmed 0:791a779d6220 213
shoaib_ahmed 0:791a779d6220 214
shoaib_ahmed 0:791a779d6220 215 /*
shoaib_ahmed 0:791a779d6220 216 * The module selection routine for PPM format output.
shoaib_ahmed 0:791a779d6220 217 */
shoaib_ahmed 0:791a779d6220 218
shoaib_ahmed 0:791a779d6220 219 GLOBAL(djpeg_dest_ptr)
shoaib_ahmed 0:791a779d6220 220 jinit_write_ppm (j_decompress_ptr cinfo)
shoaib_ahmed 0:791a779d6220 221 {
shoaib_ahmed 0:791a779d6220 222 ppm_dest_ptr dest;
shoaib_ahmed 0:791a779d6220 223
shoaib_ahmed 0:791a779d6220 224 /* Create module interface object, fill in method pointers */
shoaib_ahmed 0:791a779d6220 225 dest = (ppm_dest_ptr)
shoaib_ahmed 0:791a779d6220 226 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
shoaib_ahmed 0:791a779d6220 227 SIZEOF(ppm_dest_struct));
shoaib_ahmed 0:791a779d6220 228 dest->pub.start_output = start_output_ppm;
shoaib_ahmed 0:791a779d6220 229 dest->pub.finish_output = finish_output_ppm;
shoaib_ahmed 0:791a779d6220 230
shoaib_ahmed 0:791a779d6220 231 /* Calculate output image dimensions so we can allocate space */
shoaib_ahmed 0:791a779d6220 232 jpeg_calc_output_dimensions(cinfo);
shoaib_ahmed 0:791a779d6220 233
shoaib_ahmed 0:791a779d6220 234 /* Create physical I/O buffer. Note we make this near on a PC. */
shoaib_ahmed 0:791a779d6220 235 dest->samples_per_row = cinfo->output_width * cinfo->out_color_components;
shoaib_ahmed 0:791a779d6220 236 dest->buffer_width = dest->samples_per_row * (BYTESPERSAMPLE * SIZEOF(char));
shoaib_ahmed 0:791a779d6220 237 dest->iobuffer = (char *) (*cinfo->mem->alloc_small)
shoaib_ahmed 0:791a779d6220 238 ((j_common_ptr) cinfo, JPOOL_IMAGE, dest->buffer_width);
shoaib_ahmed 0:791a779d6220 239
shoaib_ahmed 0:791a779d6220 240 if (cinfo->quantize_colors || BITS_IN_JSAMPLE != 8 ||
shoaib_ahmed 0:791a779d6220 241 SIZEOF(JSAMPLE) != SIZEOF(char)) {
shoaib_ahmed 0:791a779d6220 242 /* When quantizing, we need an output buffer for colormap indexes
shoaib_ahmed 0:791a779d6220 243 * that's separate from the physical I/O buffer. We also need a
shoaib_ahmed 0:791a779d6220 244 * separate buffer if pixel format translation must take place.
shoaib_ahmed 0:791a779d6220 245 */
shoaib_ahmed 0:791a779d6220 246 dest->pub.buffer = (*cinfo->mem->alloc_sarray)
shoaib_ahmed 0:791a779d6220 247 ((j_common_ptr) cinfo, JPOOL_IMAGE,
shoaib_ahmed 0:791a779d6220 248 cinfo->output_width * cinfo->output_components, (JDIMENSION) 1);
shoaib_ahmed 0:791a779d6220 249 dest->pub.buffer_height = 1;
shoaib_ahmed 0:791a779d6220 250 if (! cinfo->quantize_colors)
shoaib_ahmed 0:791a779d6220 251 dest->pub.put_pixel_rows = copy_pixel_rows;
shoaib_ahmed 0:791a779d6220 252 else if (cinfo->out_color_space == JCS_GRAYSCALE)
shoaib_ahmed 0:791a779d6220 253 dest->pub.put_pixel_rows = put_demapped_gray;
shoaib_ahmed 0:791a779d6220 254 else
shoaib_ahmed 0:791a779d6220 255 dest->pub.put_pixel_rows = put_demapped_rgb;
shoaib_ahmed 0:791a779d6220 256 } else {
shoaib_ahmed 0:791a779d6220 257 /* We will fwrite() directly from decompressor output buffer. */
shoaib_ahmed 0:791a779d6220 258 /* Synthesize a JSAMPARRAY pointer structure */
shoaib_ahmed 0:791a779d6220 259 /* Cast here implies near->far pointer conversion on PCs */
shoaib_ahmed 0:791a779d6220 260 dest->pixrow = (JSAMPROW) dest->iobuffer;
shoaib_ahmed 0:791a779d6220 261 dest->pub.buffer = & dest->pixrow;
shoaib_ahmed 0:791a779d6220 262 dest->pub.buffer_height = 1;
shoaib_ahmed 0:791a779d6220 263 dest->pub.put_pixel_rows = put_pixel_rows;
shoaib_ahmed 0:791a779d6220 264 }
shoaib_ahmed 0:791a779d6220 265
shoaib_ahmed 0:791a779d6220 266 return (djpeg_dest_ptr) dest;
shoaib_ahmed 0:791a779d6220 267 }
shoaib_ahmed 0:791a779d6220 268
shoaib_ahmed 0:791a779d6220 269 #endif /* PPM_SUPPORTED */