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 * wrtarga.c
shoaib_ahmed 0:791a779d6220 3 *
shoaib_ahmed 0:791a779d6220 4 * Copyright (C) 1991-1996, Thomas G. Lane.
shoaib_ahmed 0:791a779d6220 5 * Modified 2015 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 Targa format.
shoaib_ahmed 0:791a779d6220 10 *
shoaib_ahmed 0:791a779d6220 11 * These routines may need modification for non-Unix environments or
shoaib_ahmed 0:791a779d6220 12 * specialized applications. As they stand, they assume output to
shoaib_ahmed 0:791a779d6220 13 * an ordinary stdio stream.
shoaib_ahmed 0:791a779d6220 14 *
shoaib_ahmed 0:791a779d6220 15 * Based on code contributed by Lee Daniel Crocker.
shoaib_ahmed 0:791a779d6220 16 */
shoaib_ahmed 0:791a779d6220 17
shoaib_ahmed 0:791a779d6220 18 #include "cdjpeg.h" /* Common decls for cjpeg/djpeg applications */
shoaib_ahmed 0:791a779d6220 19
shoaib_ahmed 0:791a779d6220 20 #ifdef TARGA_SUPPORTED
shoaib_ahmed 0:791a779d6220 21
shoaib_ahmed 0:791a779d6220 22
shoaib_ahmed 0:791a779d6220 23 /*
shoaib_ahmed 0:791a779d6220 24 * To support 12-bit JPEG data, we'd have to scale output down to 8 bits.
shoaib_ahmed 0:791a779d6220 25 * This is not yet implemented.
shoaib_ahmed 0:791a779d6220 26 */
shoaib_ahmed 0:791a779d6220 27
shoaib_ahmed 0:791a779d6220 28 #if BITS_IN_JSAMPLE != 8
shoaib_ahmed 0:791a779d6220 29 Sorry, this code only copes with 8-bit JSAMPLEs. /* deliberate syntax err */
shoaib_ahmed 0:791a779d6220 30 #endif
shoaib_ahmed 0:791a779d6220 31
shoaib_ahmed 0:791a779d6220 32 /*
shoaib_ahmed 0:791a779d6220 33 * The output buffer needs to be writable by fwrite(). On PCs, we must
shoaib_ahmed 0:791a779d6220 34 * allocate the buffer in near data space, because we are assuming small-data
shoaib_ahmed 0:791a779d6220 35 * memory model, wherein fwrite() can't reach far memory. If you need to
shoaib_ahmed 0:791a779d6220 36 * process very wide images on a PC, you might have to compile in large-memory
shoaib_ahmed 0:791a779d6220 37 * model, or else replace fwrite() with a putc() loop --- which will be much
shoaib_ahmed 0:791a779d6220 38 * slower.
shoaib_ahmed 0:791a779d6220 39 */
shoaib_ahmed 0:791a779d6220 40
shoaib_ahmed 0:791a779d6220 41
shoaib_ahmed 0:791a779d6220 42 /* Private version of data destination object */
shoaib_ahmed 0:791a779d6220 43
shoaib_ahmed 0:791a779d6220 44 typedef struct {
shoaib_ahmed 0:791a779d6220 45 struct djpeg_dest_struct pub; /* public fields */
shoaib_ahmed 0:791a779d6220 46
shoaib_ahmed 0:791a779d6220 47 char *iobuffer; /* physical I/O buffer */
shoaib_ahmed 0:791a779d6220 48 JDIMENSION buffer_width; /* width of one row */
shoaib_ahmed 0:791a779d6220 49 } tga_dest_struct;
shoaib_ahmed 0:791a779d6220 50
shoaib_ahmed 0:791a779d6220 51 typedef tga_dest_struct * tga_dest_ptr;
shoaib_ahmed 0:791a779d6220 52
shoaib_ahmed 0:791a779d6220 53
shoaib_ahmed 0:791a779d6220 54 LOCAL(void)
shoaib_ahmed 0:791a779d6220 55 write_header (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo, int num_colors)
shoaib_ahmed 0:791a779d6220 56 /* Create and write a Targa header */
shoaib_ahmed 0:791a779d6220 57 {
shoaib_ahmed 0:791a779d6220 58 char targaheader[18];
shoaib_ahmed 0:791a779d6220 59
shoaib_ahmed 0:791a779d6220 60 /* Set unused fields of header to 0 */
shoaib_ahmed 0:791a779d6220 61 MEMZERO(targaheader, SIZEOF(targaheader));
shoaib_ahmed 0:791a779d6220 62
shoaib_ahmed 0:791a779d6220 63 if (num_colors > 0) {
shoaib_ahmed 0:791a779d6220 64 targaheader[1] = 1; /* color map type 1 */
shoaib_ahmed 0:791a779d6220 65 targaheader[5] = (char) (num_colors & 0xFF);
shoaib_ahmed 0:791a779d6220 66 targaheader[6] = (char) (num_colors >> 8);
shoaib_ahmed 0:791a779d6220 67 targaheader[7] = 24; /* 24 bits per cmap entry */
shoaib_ahmed 0:791a779d6220 68 }
shoaib_ahmed 0:791a779d6220 69
shoaib_ahmed 0:791a779d6220 70 targaheader[12] = (char) (cinfo->output_width & 0xFF);
shoaib_ahmed 0:791a779d6220 71 targaheader[13] = (char) (cinfo->output_width >> 8);
shoaib_ahmed 0:791a779d6220 72 targaheader[14] = (char) (cinfo->output_height & 0xFF);
shoaib_ahmed 0:791a779d6220 73 targaheader[15] = (char) (cinfo->output_height >> 8);
shoaib_ahmed 0:791a779d6220 74 targaheader[17] = 0x20; /* Top-down, non-interlaced */
shoaib_ahmed 0:791a779d6220 75
shoaib_ahmed 0:791a779d6220 76 if (cinfo->out_color_space == JCS_GRAYSCALE) {
shoaib_ahmed 0:791a779d6220 77 targaheader[2] = 3; /* image type = uncompressed grayscale */
shoaib_ahmed 0:791a779d6220 78 targaheader[16] = 8; /* bits per pixel */
shoaib_ahmed 0:791a779d6220 79 } else { /* must be RGB */
shoaib_ahmed 0:791a779d6220 80 if (num_colors > 0) {
shoaib_ahmed 0:791a779d6220 81 targaheader[2] = 1; /* image type = colormapped RGB */
shoaib_ahmed 0:791a779d6220 82 targaheader[16] = 8;
shoaib_ahmed 0:791a779d6220 83 } else {
shoaib_ahmed 0:791a779d6220 84 targaheader[2] = 2; /* image type = uncompressed RGB */
shoaib_ahmed 0:791a779d6220 85 targaheader[16] = 24;
shoaib_ahmed 0:791a779d6220 86 }
shoaib_ahmed 0:791a779d6220 87 }
shoaib_ahmed 0:791a779d6220 88
shoaib_ahmed 0:791a779d6220 89 if (JFWRITE(dinfo->output_file, targaheader, 18) != (size_t) 18)
shoaib_ahmed 0:791a779d6220 90 ERREXIT(cinfo, JERR_FILE_WRITE);
shoaib_ahmed 0:791a779d6220 91 }
shoaib_ahmed 0:791a779d6220 92
shoaib_ahmed 0:791a779d6220 93
shoaib_ahmed 0:791a779d6220 94 /*
shoaib_ahmed 0:791a779d6220 95 * Write some pixel data.
shoaib_ahmed 0:791a779d6220 96 * In this module rows_supplied will always be 1.
shoaib_ahmed 0:791a779d6220 97 */
shoaib_ahmed 0:791a779d6220 98
shoaib_ahmed 0:791a779d6220 99 METHODDEF(void)
shoaib_ahmed 0:791a779d6220 100 put_pixel_rows (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo,
shoaib_ahmed 0:791a779d6220 101 JDIMENSION rows_supplied)
shoaib_ahmed 0:791a779d6220 102 /* used for unquantized full-color output */
shoaib_ahmed 0:791a779d6220 103 {
shoaib_ahmed 0:791a779d6220 104 tga_dest_ptr dest = (tga_dest_ptr) dinfo;
shoaib_ahmed 0:791a779d6220 105 register JSAMPROW inptr;
shoaib_ahmed 0:791a779d6220 106 register char * outptr;
shoaib_ahmed 0:791a779d6220 107 register JDIMENSION col;
shoaib_ahmed 0:791a779d6220 108
shoaib_ahmed 0:791a779d6220 109 inptr = dest->pub.buffer[0];
shoaib_ahmed 0:791a779d6220 110 outptr = dest->iobuffer;
shoaib_ahmed 0:791a779d6220 111 for (col = cinfo->output_width; col > 0; col--) {
shoaib_ahmed 0:791a779d6220 112 outptr[0] = (char) GETJSAMPLE(inptr[2]); /* RGB to BGR order */
shoaib_ahmed 0:791a779d6220 113 outptr[1] = (char) GETJSAMPLE(inptr[1]);
shoaib_ahmed 0:791a779d6220 114 outptr[2] = (char) GETJSAMPLE(inptr[0]);
shoaib_ahmed 0:791a779d6220 115 inptr += 3, outptr += 3;
shoaib_ahmed 0:791a779d6220 116 }
shoaib_ahmed 0:791a779d6220 117 (void) JFWRITE(dest->pub.output_file, dest->iobuffer, dest->buffer_width);
shoaib_ahmed 0:791a779d6220 118 }
shoaib_ahmed 0:791a779d6220 119
shoaib_ahmed 0:791a779d6220 120 METHODDEF(void)
shoaib_ahmed 0:791a779d6220 121 put_gray_rows (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo,
shoaib_ahmed 0:791a779d6220 122 JDIMENSION rows_supplied)
shoaib_ahmed 0:791a779d6220 123 /* used for grayscale OR quantized color output */
shoaib_ahmed 0:791a779d6220 124 {
shoaib_ahmed 0:791a779d6220 125 tga_dest_ptr dest = (tga_dest_ptr) dinfo;
shoaib_ahmed 0:791a779d6220 126 register JSAMPROW inptr;
shoaib_ahmed 0:791a779d6220 127 register char * outptr;
shoaib_ahmed 0:791a779d6220 128 register JDIMENSION col;
shoaib_ahmed 0:791a779d6220 129
shoaib_ahmed 0:791a779d6220 130 inptr = dest->pub.buffer[0];
shoaib_ahmed 0:791a779d6220 131 outptr = dest->iobuffer;
shoaib_ahmed 0:791a779d6220 132 for (col = cinfo->output_width; col > 0; col--) {
shoaib_ahmed 0:791a779d6220 133 *outptr++ = (char) GETJSAMPLE(*inptr++);
shoaib_ahmed 0:791a779d6220 134 }
shoaib_ahmed 0:791a779d6220 135 (void) JFWRITE(dest->pub.output_file, dest->iobuffer, dest->buffer_width);
shoaib_ahmed 0:791a779d6220 136 }
shoaib_ahmed 0:791a779d6220 137
shoaib_ahmed 0:791a779d6220 138
shoaib_ahmed 0:791a779d6220 139 /*
shoaib_ahmed 0:791a779d6220 140 * Write some demapped pixel data when color quantization is in effect.
shoaib_ahmed 0:791a779d6220 141 * For Targa, this is only applied to grayscale data.
shoaib_ahmed 0:791a779d6220 142 */
shoaib_ahmed 0:791a779d6220 143
shoaib_ahmed 0:791a779d6220 144 METHODDEF(void)
shoaib_ahmed 0:791a779d6220 145 put_demapped_gray (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo,
shoaib_ahmed 0:791a779d6220 146 JDIMENSION rows_supplied)
shoaib_ahmed 0:791a779d6220 147 {
shoaib_ahmed 0:791a779d6220 148 tga_dest_ptr dest = (tga_dest_ptr) dinfo;
shoaib_ahmed 0:791a779d6220 149 register JSAMPROW inptr;
shoaib_ahmed 0:791a779d6220 150 register char * outptr;
shoaib_ahmed 0:791a779d6220 151 register JSAMPROW color_map0 = cinfo->colormap[0];
shoaib_ahmed 0:791a779d6220 152 register JDIMENSION col;
shoaib_ahmed 0:791a779d6220 153
shoaib_ahmed 0:791a779d6220 154 inptr = dest->pub.buffer[0];
shoaib_ahmed 0:791a779d6220 155 outptr = dest->iobuffer;
shoaib_ahmed 0:791a779d6220 156 for (col = cinfo->output_width; col > 0; col--) {
shoaib_ahmed 0:791a779d6220 157 *outptr++ = (char) GETJSAMPLE(color_map0[GETJSAMPLE(*inptr++)]);
shoaib_ahmed 0:791a779d6220 158 }
shoaib_ahmed 0:791a779d6220 159 (void) JFWRITE(dest->pub.output_file, dest->iobuffer, dest->buffer_width);
shoaib_ahmed 0:791a779d6220 160 }
shoaib_ahmed 0:791a779d6220 161
shoaib_ahmed 0:791a779d6220 162
shoaib_ahmed 0:791a779d6220 163 /*
shoaib_ahmed 0:791a779d6220 164 * Startup: write the file header.
shoaib_ahmed 0:791a779d6220 165 */
shoaib_ahmed 0:791a779d6220 166
shoaib_ahmed 0:791a779d6220 167 METHODDEF(void)
shoaib_ahmed 0:791a779d6220 168 start_output_tga (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo)
shoaib_ahmed 0:791a779d6220 169 {
shoaib_ahmed 0:791a779d6220 170 tga_dest_ptr dest = (tga_dest_ptr) dinfo;
shoaib_ahmed 0:791a779d6220 171 int num_colors, i;
shoaib_ahmed 0:791a779d6220 172 FILE *outfile;
shoaib_ahmed 0:791a779d6220 173
shoaib_ahmed 0:791a779d6220 174 if (cinfo->out_color_space == JCS_GRAYSCALE) {
shoaib_ahmed 0:791a779d6220 175 /* Targa doesn't have a mapped grayscale format, so we will */
shoaib_ahmed 0:791a779d6220 176 /* demap quantized gray output. Never emit a colormap. */
shoaib_ahmed 0:791a779d6220 177 write_header(cinfo, dinfo, 0);
shoaib_ahmed 0:791a779d6220 178 if (cinfo->quantize_colors)
shoaib_ahmed 0:791a779d6220 179 dest->pub.put_pixel_rows = put_demapped_gray;
shoaib_ahmed 0:791a779d6220 180 else
shoaib_ahmed 0:791a779d6220 181 dest->pub.put_pixel_rows = put_gray_rows;
shoaib_ahmed 0:791a779d6220 182 } else if (cinfo->out_color_space == JCS_RGB) {
shoaib_ahmed 0:791a779d6220 183 if (cinfo->quantize_colors) {
shoaib_ahmed 0:791a779d6220 184 /* We only support 8-bit colormap indexes, so only 256 colors */
shoaib_ahmed 0:791a779d6220 185 num_colors = cinfo->actual_number_of_colors;
shoaib_ahmed 0:791a779d6220 186 if (num_colors > 256)
shoaib_ahmed 0:791a779d6220 187 ERREXIT1(cinfo, JERR_TOO_MANY_COLORS, num_colors);
shoaib_ahmed 0:791a779d6220 188 write_header(cinfo, dinfo, num_colors);
shoaib_ahmed 0:791a779d6220 189 /* Write the colormap. Note Targa uses BGR byte order */
shoaib_ahmed 0:791a779d6220 190 outfile = dest->pub.output_file;
shoaib_ahmed 0:791a779d6220 191 for (i = 0; i < num_colors; i++) {
shoaib_ahmed 0:791a779d6220 192 putc(GETJSAMPLE(cinfo->colormap[2][i]), outfile);
shoaib_ahmed 0:791a779d6220 193 putc(GETJSAMPLE(cinfo->colormap[1][i]), outfile);
shoaib_ahmed 0:791a779d6220 194 putc(GETJSAMPLE(cinfo->colormap[0][i]), outfile);
shoaib_ahmed 0:791a779d6220 195 }
shoaib_ahmed 0:791a779d6220 196 dest->pub.put_pixel_rows = put_gray_rows;
shoaib_ahmed 0:791a779d6220 197 } else {
shoaib_ahmed 0:791a779d6220 198 write_header(cinfo, dinfo, 0);
shoaib_ahmed 0:791a779d6220 199 dest->pub.put_pixel_rows = put_pixel_rows;
shoaib_ahmed 0:791a779d6220 200 }
shoaib_ahmed 0:791a779d6220 201 } else {
shoaib_ahmed 0:791a779d6220 202 ERREXIT(cinfo, JERR_TGA_COLORSPACE);
shoaib_ahmed 0:791a779d6220 203 }
shoaib_ahmed 0:791a779d6220 204 }
shoaib_ahmed 0:791a779d6220 205
shoaib_ahmed 0:791a779d6220 206
shoaib_ahmed 0:791a779d6220 207 /*
shoaib_ahmed 0:791a779d6220 208 * Finish up at the end of the file.
shoaib_ahmed 0:791a779d6220 209 */
shoaib_ahmed 0:791a779d6220 210
shoaib_ahmed 0:791a779d6220 211 METHODDEF(void)
shoaib_ahmed 0:791a779d6220 212 finish_output_tga (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo)
shoaib_ahmed 0:791a779d6220 213 {
shoaib_ahmed 0:791a779d6220 214 /* Make sure we wrote the output file OK */
shoaib_ahmed 0:791a779d6220 215 fflush(dinfo->output_file);
shoaib_ahmed 0:791a779d6220 216 if (ferror(dinfo->output_file))
shoaib_ahmed 0:791a779d6220 217 ERREXIT(cinfo, JERR_FILE_WRITE);
shoaib_ahmed 0:791a779d6220 218 }
shoaib_ahmed 0:791a779d6220 219
shoaib_ahmed 0:791a779d6220 220
shoaib_ahmed 0:791a779d6220 221 /*
shoaib_ahmed 0:791a779d6220 222 * The module selection routine for Targa format output.
shoaib_ahmed 0:791a779d6220 223 */
shoaib_ahmed 0:791a779d6220 224
shoaib_ahmed 0:791a779d6220 225 GLOBAL(djpeg_dest_ptr)
shoaib_ahmed 0:791a779d6220 226 jinit_write_targa (j_decompress_ptr cinfo)
shoaib_ahmed 0:791a779d6220 227 {
shoaib_ahmed 0:791a779d6220 228 tga_dest_ptr dest;
shoaib_ahmed 0:791a779d6220 229
shoaib_ahmed 0:791a779d6220 230 /* Create module interface object, fill in method pointers */
shoaib_ahmed 0:791a779d6220 231 dest = (tga_dest_ptr)
shoaib_ahmed 0:791a779d6220 232 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
shoaib_ahmed 0:791a779d6220 233 SIZEOF(tga_dest_struct));
shoaib_ahmed 0:791a779d6220 234 dest->pub.start_output = start_output_tga;
shoaib_ahmed 0:791a779d6220 235 dest->pub.finish_output = finish_output_tga;
shoaib_ahmed 0:791a779d6220 236
shoaib_ahmed 0:791a779d6220 237 /* Calculate output image dimensions so we can allocate space */
shoaib_ahmed 0:791a779d6220 238 jpeg_calc_output_dimensions(cinfo);
shoaib_ahmed 0:791a779d6220 239
shoaib_ahmed 0:791a779d6220 240 /* Create I/O buffer. Note we make this near on a PC. */
shoaib_ahmed 0:791a779d6220 241 dest->buffer_width = cinfo->output_width * cinfo->output_components;
shoaib_ahmed 0:791a779d6220 242 dest->iobuffer = (char *)
shoaib_ahmed 0:791a779d6220 243 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
shoaib_ahmed 0:791a779d6220 244 (size_t) (dest->buffer_width * SIZEOF(char)));
shoaib_ahmed 0:791a779d6220 245
shoaib_ahmed 0:791a779d6220 246 /* Create decompressor output buffer. */
shoaib_ahmed 0:791a779d6220 247 dest->pub.buffer = (*cinfo->mem->alloc_sarray)
shoaib_ahmed 0:791a779d6220 248 ((j_common_ptr) cinfo, JPOOL_IMAGE, dest->buffer_width, (JDIMENSION) 1);
shoaib_ahmed 0:791a779d6220 249 dest->pub.buffer_height = 1;
shoaib_ahmed 0:791a779d6220 250
shoaib_ahmed 0:791a779d6220 251 return &dest->pub;
shoaib_ahmed 0:791a779d6220 252 }
shoaib_ahmed 0:791a779d6220 253
shoaib_ahmed 0:791a779d6220 254 #endif /* TARGA_SUPPORTED */