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 * jdatadst.c
shoaib_ahmed 0:791a779d6220 3 *
shoaib_ahmed 0:791a779d6220 4 * Copyright (C) 1994-1996, Thomas G. Lane.
shoaib_ahmed 0:791a779d6220 5 * Modified 2009-2012 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 compression data destination routines for the case of
shoaib_ahmed 0:791a779d6220 10 * emitting JPEG data to memory or to a file (or any stdio stream).
shoaib_ahmed 0:791a779d6220 11 * While these routines are sufficient for most applications,
shoaib_ahmed 0:791a779d6220 12 * some will want to use a different destination manager.
shoaib_ahmed 0:791a779d6220 13 * IMPORTANT: we assume that fwrite() will correctly transcribe an array of
shoaib_ahmed 0:791a779d6220 14 * JOCTETs into 8-bit-wide elements on external storage. If char is wider
shoaib_ahmed 0:791a779d6220 15 * than 8 bits on your machine, you may need to do some tweaking.
shoaib_ahmed 0:791a779d6220 16 */
shoaib_ahmed 0:791a779d6220 17
shoaib_ahmed 0:791a779d6220 18 /* this is not a core library module, so it doesn't define JPEG_INTERNALS */
shoaib_ahmed 0:791a779d6220 19 #include "jinclude.h"
shoaib_ahmed 0:791a779d6220 20 #include "jpeglib.h"
shoaib_ahmed 0:791a779d6220 21 #include "jerror.h"
shoaib_ahmed 0:791a779d6220 22
shoaib_ahmed 0:791a779d6220 23 #ifndef HAVE_STDLIB_H /* <stdlib.h> should declare malloc(),free() */
shoaib_ahmed 0:791a779d6220 24 extern void * malloc JPP((size_t size));
shoaib_ahmed 0:791a779d6220 25 extern void free JPP((void *ptr));
shoaib_ahmed 0:791a779d6220 26 #endif
shoaib_ahmed 0:791a779d6220 27
shoaib_ahmed 0:791a779d6220 28
shoaib_ahmed 0:791a779d6220 29 /* Expanded data destination object for stdio output */
shoaib_ahmed 0:791a779d6220 30
shoaib_ahmed 0:791a779d6220 31 typedef struct {
shoaib_ahmed 0:791a779d6220 32 struct jpeg_destination_mgr pub; /* public fields */
shoaib_ahmed 0:791a779d6220 33
shoaib_ahmed 0:791a779d6220 34 FILE * outfile; /* target stream */
shoaib_ahmed 0:791a779d6220 35 JOCTET * buffer; /* start of buffer */
shoaib_ahmed 0:791a779d6220 36 } my_destination_mgr;
shoaib_ahmed 0:791a779d6220 37
shoaib_ahmed 0:791a779d6220 38 typedef my_destination_mgr * my_dest_ptr;
shoaib_ahmed 0:791a779d6220 39
shoaib_ahmed 0:791a779d6220 40 #define OUTPUT_BUF_SIZE 4096 /* choose an efficiently fwrite'able size */
shoaib_ahmed 0:791a779d6220 41
shoaib_ahmed 0:791a779d6220 42
shoaib_ahmed 0:791a779d6220 43 /* Expanded data destination object for memory output */
shoaib_ahmed 0:791a779d6220 44
shoaib_ahmed 0:791a779d6220 45 typedef struct {
shoaib_ahmed 0:791a779d6220 46 struct jpeg_destination_mgr pub; /* public fields */
shoaib_ahmed 0:791a779d6220 47
shoaib_ahmed 0:791a779d6220 48 unsigned char ** outbuffer; /* target buffer */
shoaib_ahmed 0:791a779d6220 49 unsigned long * outsize;
shoaib_ahmed 0:791a779d6220 50 unsigned char * newbuffer; /* newly allocated buffer */
shoaib_ahmed 0:791a779d6220 51 JOCTET * buffer; /* start of buffer */
shoaib_ahmed 0:791a779d6220 52 size_t bufsize;
shoaib_ahmed 0:791a779d6220 53 } my_mem_destination_mgr;
shoaib_ahmed 0:791a779d6220 54
shoaib_ahmed 0:791a779d6220 55 typedef my_mem_destination_mgr * my_mem_dest_ptr;
shoaib_ahmed 0:791a779d6220 56
shoaib_ahmed 0:791a779d6220 57
shoaib_ahmed 0:791a779d6220 58 /*
shoaib_ahmed 0:791a779d6220 59 * Initialize destination --- called by jpeg_start_compress
shoaib_ahmed 0:791a779d6220 60 * before any data is actually written.
shoaib_ahmed 0:791a779d6220 61 */
shoaib_ahmed 0:791a779d6220 62
shoaib_ahmed 0:791a779d6220 63 METHODDEF(void)
shoaib_ahmed 0:791a779d6220 64 init_destination (j_compress_ptr cinfo)
shoaib_ahmed 0:791a779d6220 65 {
shoaib_ahmed 0:791a779d6220 66 my_dest_ptr dest = (my_dest_ptr) cinfo->dest;
shoaib_ahmed 0:791a779d6220 67
shoaib_ahmed 0:791a779d6220 68 /* Allocate the output buffer --- it will be released when done with image */
shoaib_ahmed 0:791a779d6220 69 dest->buffer = (JOCTET *)
shoaib_ahmed 0:791a779d6220 70 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
shoaib_ahmed 0:791a779d6220 71 OUTPUT_BUF_SIZE * SIZEOF(JOCTET));
shoaib_ahmed 0:791a779d6220 72
shoaib_ahmed 0:791a779d6220 73 dest->pub.next_output_byte = dest->buffer;
shoaib_ahmed 0:791a779d6220 74 dest->pub.free_in_buffer = OUTPUT_BUF_SIZE;
shoaib_ahmed 0:791a779d6220 75 }
shoaib_ahmed 0:791a779d6220 76
shoaib_ahmed 0:791a779d6220 77 METHODDEF(void)
shoaib_ahmed 0:791a779d6220 78 init_mem_destination (j_compress_ptr cinfo)
shoaib_ahmed 0:791a779d6220 79 {
shoaib_ahmed 0:791a779d6220 80 /* no work necessary here */
shoaib_ahmed 0:791a779d6220 81 }
shoaib_ahmed 0:791a779d6220 82
shoaib_ahmed 0:791a779d6220 83
shoaib_ahmed 0:791a779d6220 84 /*
shoaib_ahmed 0:791a779d6220 85 * Empty the output buffer --- called whenever buffer fills up.
shoaib_ahmed 0:791a779d6220 86 *
shoaib_ahmed 0:791a779d6220 87 * In typical applications, this should write the entire output buffer
shoaib_ahmed 0:791a779d6220 88 * (ignoring the current state of next_output_byte & free_in_buffer),
shoaib_ahmed 0:791a779d6220 89 * reset the pointer & count to the start of the buffer, and return TRUE
shoaib_ahmed 0:791a779d6220 90 * indicating that the buffer has been dumped.
shoaib_ahmed 0:791a779d6220 91 *
shoaib_ahmed 0:791a779d6220 92 * In applications that need to be able to suspend compression due to output
shoaib_ahmed 0:791a779d6220 93 * overrun, a FALSE return indicates that the buffer cannot be emptied now.
shoaib_ahmed 0:791a779d6220 94 * In this situation, the compressor will return to its caller (possibly with
shoaib_ahmed 0:791a779d6220 95 * an indication that it has not accepted all the supplied scanlines). The
shoaib_ahmed 0:791a779d6220 96 * application should resume compression after it has made more room in the
shoaib_ahmed 0:791a779d6220 97 * output buffer. Note that there are substantial restrictions on the use of
shoaib_ahmed 0:791a779d6220 98 * suspension --- see the documentation.
shoaib_ahmed 0:791a779d6220 99 *
shoaib_ahmed 0:791a779d6220 100 * When suspending, the compressor will back up to a convenient restart point
shoaib_ahmed 0:791a779d6220 101 * (typically the start of the current MCU). next_output_byte & free_in_buffer
shoaib_ahmed 0:791a779d6220 102 * indicate where the restart point will be if the current call returns FALSE.
shoaib_ahmed 0:791a779d6220 103 * Data beyond this point will be regenerated after resumption, so do not
shoaib_ahmed 0:791a779d6220 104 * write it out when emptying the buffer externally.
shoaib_ahmed 0:791a779d6220 105 */
shoaib_ahmed 0:791a779d6220 106
shoaib_ahmed 0:791a779d6220 107 METHODDEF(boolean)
shoaib_ahmed 0:791a779d6220 108 empty_output_buffer (j_compress_ptr cinfo)
shoaib_ahmed 0:791a779d6220 109 {
shoaib_ahmed 0:791a779d6220 110 my_dest_ptr dest = (my_dest_ptr) cinfo->dest;
shoaib_ahmed 0:791a779d6220 111
shoaib_ahmed 0:791a779d6220 112 if (JFWRITE(dest->outfile, dest->buffer, OUTPUT_BUF_SIZE) !=
shoaib_ahmed 0:791a779d6220 113 (size_t) OUTPUT_BUF_SIZE)
shoaib_ahmed 0:791a779d6220 114 ERREXIT(cinfo, JERR_FILE_WRITE);
shoaib_ahmed 0:791a779d6220 115
shoaib_ahmed 0:791a779d6220 116 dest->pub.next_output_byte = dest->buffer;
shoaib_ahmed 0:791a779d6220 117 dest->pub.free_in_buffer = OUTPUT_BUF_SIZE;
shoaib_ahmed 0:791a779d6220 118
shoaib_ahmed 0:791a779d6220 119 return TRUE;
shoaib_ahmed 0:791a779d6220 120 }
shoaib_ahmed 0:791a779d6220 121
shoaib_ahmed 0:791a779d6220 122 METHODDEF(boolean)
shoaib_ahmed 0:791a779d6220 123 empty_mem_output_buffer (j_compress_ptr cinfo)
shoaib_ahmed 0:791a779d6220 124 {
shoaib_ahmed 0:791a779d6220 125 size_t nextsize;
shoaib_ahmed 0:791a779d6220 126 JOCTET * nextbuffer;
shoaib_ahmed 0:791a779d6220 127 my_mem_dest_ptr dest = (my_mem_dest_ptr) cinfo->dest;
shoaib_ahmed 0:791a779d6220 128
shoaib_ahmed 0:791a779d6220 129 /* Try to allocate new buffer with double size */
shoaib_ahmed 0:791a779d6220 130 nextsize = dest->bufsize * 2;
shoaib_ahmed 0:791a779d6220 131 nextbuffer = (JOCTET *) malloc(nextsize);
shoaib_ahmed 0:791a779d6220 132
shoaib_ahmed 0:791a779d6220 133 if (nextbuffer == NULL)
shoaib_ahmed 0:791a779d6220 134 ERREXIT1(cinfo, JERR_OUT_OF_MEMORY, 10);
shoaib_ahmed 0:791a779d6220 135
shoaib_ahmed 0:791a779d6220 136 MEMCOPY(nextbuffer, dest->buffer, dest->bufsize);
shoaib_ahmed 0:791a779d6220 137
shoaib_ahmed 0:791a779d6220 138 if (dest->newbuffer != NULL)
shoaib_ahmed 0:791a779d6220 139 free(dest->newbuffer);
shoaib_ahmed 0:791a779d6220 140
shoaib_ahmed 0:791a779d6220 141 dest->newbuffer = nextbuffer;
shoaib_ahmed 0:791a779d6220 142
shoaib_ahmed 0:791a779d6220 143 dest->pub.next_output_byte = nextbuffer + dest->bufsize;
shoaib_ahmed 0:791a779d6220 144 dest->pub.free_in_buffer = dest->bufsize;
shoaib_ahmed 0:791a779d6220 145
shoaib_ahmed 0:791a779d6220 146 dest->buffer = nextbuffer;
shoaib_ahmed 0:791a779d6220 147 dest->bufsize = nextsize;
shoaib_ahmed 0:791a779d6220 148
shoaib_ahmed 0:791a779d6220 149 return TRUE;
shoaib_ahmed 0:791a779d6220 150 }
shoaib_ahmed 0:791a779d6220 151
shoaib_ahmed 0:791a779d6220 152
shoaib_ahmed 0:791a779d6220 153 /*
shoaib_ahmed 0:791a779d6220 154 * Terminate destination --- called by jpeg_finish_compress
shoaib_ahmed 0:791a779d6220 155 * after all data has been written. Usually needs to flush buffer.
shoaib_ahmed 0:791a779d6220 156 *
shoaib_ahmed 0:791a779d6220 157 * NB: *not* called by jpeg_abort or jpeg_destroy; surrounding
shoaib_ahmed 0:791a779d6220 158 * application must deal with any cleanup that should happen even
shoaib_ahmed 0:791a779d6220 159 * for error exit.
shoaib_ahmed 0:791a779d6220 160 */
shoaib_ahmed 0:791a779d6220 161
shoaib_ahmed 0:791a779d6220 162 METHODDEF(void)
shoaib_ahmed 0:791a779d6220 163 term_destination (j_compress_ptr cinfo)
shoaib_ahmed 0:791a779d6220 164 {
shoaib_ahmed 0:791a779d6220 165 my_dest_ptr dest = (my_dest_ptr) cinfo->dest;
shoaib_ahmed 0:791a779d6220 166 size_t datacount = OUTPUT_BUF_SIZE - dest->pub.free_in_buffer;
shoaib_ahmed 0:791a779d6220 167
shoaib_ahmed 0:791a779d6220 168 /* Write any data remaining in the buffer */
shoaib_ahmed 0:791a779d6220 169 if (datacount > 0) {
shoaib_ahmed 0:791a779d6220 170 if (JFWRITE(dest->outfile, dest->buffer, datacount) != datacount)
shoaib_ahmed 0:791a779d6220 171 ERREXIT(cinfo, JERR_FILE_WRITE);
shoaib_ahmed 0:791a779d6220 172 }
shoaib_ahmed 0:791a779d6220 173 fflush(dest->outfile);
shoaib_ahmed 0:791a779d6220 174 /* Make sure we wrote the output file OK */
shoaib_ahmed 0:791a779d6220 175 if (ferror(dest->outfile))
shoaib_ahmed 0:791a779d6220 176 ERREXIT(cinfo, JERR_FILE_WRITE);
shoaib_ahmed 0:791a779d6220 177 }
shoaib_ahmed 0:791a779d6220 178
shoaib_ahmed 0:791a779d6220 179 METHODDEF(void)
shoaib_ahmed 0:791a779d6220 180 term_mem_destination (j_compress_ptr cinfo)
shoaib_ahmed 0:791a779d6220 181 {
shoaib_ahmed 0:791a779d6220 182 my_mem_dest_ptr dest = (my_mem_dest_ptr) cinfo->dest;
shoaib_ahmed 0:791a779d6220 183
shoaib_ahmed 0:791a779d6220 184 *dest->outbuffer = dest->buffer;
shoaib_ahmed 0:791a779d6220 185 *dest->outsize = dest->bufsize - dest->pub.free_in_buffer;
shoaib_ahmed 0:791a779d6220 186 }
shoaib_ahmed 0:791a779d6220 187
shoaib_ahmed 0:791a779d6220 188
shoaib_ahmed 0:791a779d6220 189 /*
shoaib_ahmed 0:791a779d6220 190 * Prepare for output to a stdio stream.
shoaib_ahmed 0:791a779d6220 191 * The caller must have already opened the stream, and is responsible
shoaib_ahmed 0:791a779d6220 192 * for closing it after finishing compression.
shoaib_ahmed 0:791a779d6220 193 */
shoaib_ahmed 0:791a779d6220 194
shoaib_ahmed 0:791a779d6220 195 GLOBAL(void)
shoaib_ahmed 0:791a779d6220 196 jpeg_stdio_dest (j_compress_ptr cinfo, FILE * outfile)
shoaib_ahmed 0:791a779d6220 197 {
shoaib_ahmed 0:791a779d6220 198 my_dest_ptr dest;
shoaib_ahmed 0:791a779d6220 199
shoaib_ahmed 0:791a779d6220 200 /* The destination object is made permanent so that multiple JPEG images
shoaib_ahmed 0:791a779d6220 201 * can be written to the same file without re-executing jpeg_stdio_dest.
shoaib_ahmed 0:791a779d6220 202 * This makes it dangerous to use this manager and a different destination
shoaib_ahmed 0:791a779d6220 203 * manager serially with the same JPEG object, because their private object
shoaib_ahmed 0:791a779d6220 204 * sizes may be different. Caveat programmer.
shoaib_ahmed 0:791a779d6220 205 */
shoaib_ahmed 0:791a779d6220 206 if (cinfo->dest == NULL) { /* first time for this JPEG object? */
shoaib_ahmed 0:791a779d6220 207 cinfo->dest = (struct jpeg_destination_mgr *)
shoaib_ahmed 0:791a779d6220 208 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
shoaib_ahmed 0:791a779d6220 209 SIZEOF(my_destination_mgr));
shoaib_ahmed 0:791a779d6220 210 }
shoaib_ahmed 0:791a779d6220 211
shoaib_ahmed 0:791a779d6220 212 dest = (my_dest_ptr) cinfo->dest;
shoaib_ahmed 0:791a779d6220 213 dest->pub.init_destination = init_destination;
shoaib_ahmed 0:791a779d6220 214 dest->pub.empty_output_buffer = empty_output_buffer;
shoaib_ahmed 0:791a779d6220 215 dest->pub.term_destination = term_destination;
shoaib_ahmed 0:791a779d6220 216 dest->outfile = outfile;
shoaib_ahmed 0:791a779d6220 217 }
shoaib_ahmed 0:791a779d6220 218
shoaib_ahmed 0:791a779d6220 219
shoaib_ahmed 0:791a779d6220 220 /*
shoaib_ahmed 0:791a779d6220 221 * Prepare for output to a memory buffer.
shoaib_ahmed 0:791a779d6220 222 * The caller may supply an own initial buffer with appropriate size.
shoaib_ahmed 0:791a779d6220 223 * Otherwise, or when the actual data output exceeds the given size,
shoaib_ahmed 0:791a779d6220 224 * the library adapts the buffer size as necessary.
shoaib_ahmed 0:791a779d6220 225 * The standard library functions malloc/free are used for allocating
shoaib_ahmed 0:791a779d6220 226 * larger memory, so the buffer is available to the application after
shoaib_ahmed 0:791a779d6220 227 * finishing compression, and then the application is responsible for
shoaib_ahmed 0:791a779d6220 228 * freeing the requested memory.
shoaib_ahmed 0:791a779d6220 229 * Note: An initial buffer supplied by the caller is expected to be
shoaib_ahmed 0:791a779d6220 230 * managed by the application. The library does not free such buffer
shoaib_ahmed 0:791a779d6220 231 * when allocating a larger buffer.
shoaib_ahmed 0:791a779d6220 232 */
shoaib_ahmed 0:791a779d6220 233
shoaib_ahmed 0:791a779d6220 234 GLOBAL(void)
shoaib_ahmed 0:791a779d6220 235 jpeg_mem_dest (j_compress_ptr cinfo,
shoaib_ahmed 0:791a779d6220 236 unsigned char ** outbuffer, unsigned long * outsize)
shoaib_ahmed 0:791a779d6220 237 {
shoaib_ahmed 0:791a779d6220 238 my_mem_dest_ptr dest;
shoaib_ahmed 0:791a779d6220 239
shoaib_ahmed 0:791a779d6220 240 if (outbuffer == NULL || outsize == NULL) /* sanity check */
shoaib_ahmed 0:791a779d6220 241 ERREXIT(cinfo, JERR_BUFFER_SIZE);
shoaib_ahmed 0:791a779d6220 242
shoaib_ahmed 0:791a779d6220 243 /* The destination object is made permanent so that multiple JPEG images
shoaib_ahmed 0:791a779d6220 244 * can be written to the same buffer without re-executing jpeg_mem_dest.
shoaib_ahmed 0:791a779d6220 245 */
shoaib_ahmed 0:791a779d6220 246 if (cinfo->dest == NULL) { /* first time for this JPEG object? */
shoaib_ahmed 0:791a779d6220 247 cinfo->dest = (struct jpeg_destination_mgr *)
shoaib_ahmed 0:791a779d6220 248 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
shoaib_ahmed 0:791a779d6220 249 SIZEOF(my_mem_destination_mgr));
shoaib_ahmed 0:791a779d6220 250 }
shoaib_ahmed 0:791a779d6220 251
shoaib_ahmed 0:791a779d6220 252 dest = (my_mem_dest_ptr) cinfo->dest;
shoaib_ahmed 0:791a779d6220 253 dest->pub.init_destination = init_mem_destination;
shoaib_ahmed 0:791a779d6220 254 dest->pub.empty_output_buffer = empty_mem_output_buffer;
shoaib_ahmed 0:791a779d6220 255 dest->pub.term_destination = term_mem_destination;
shoaib_ahmed 0:791a779d6220 256 dest->outbuffer = outbuffer;
shoaib_ahmed 0:791a779d6220 257 dest->outsize = outsize;
shoaib_ahmed 0:791a779d6220 258 dest->newbuffer = NULL;
shoaib_ahmed 0:791a779d6220 259
shoaib_ahmed 0:791a779d6220 260 if (*outbuffer == NULL || *outsize == 0) {
shoaib_ahmed 0:791a779d6220 261 /* Allocate initial buffer */
shoaib_ahmed 0:791a779d6220 262 dest->newbuffer = *outbuffer = (unsigned char *) malloc(OUTPUT_BUF_SIZE);
shoaib_ahmed 0:791a779d6220 263 if (dest->newbuffer == NULL)
shoaib_ahmed 0:791a779d6220 264 ERREXIT1(cinfo, JERR_OUT_OF_MEMORY, 10);
shoaib_ahmed 0:791a779d6220 265 *outsize = OUTPUT_BUF_SIZE;
shoaib_ahmed 0:791a779d6220 266 }
shoaib_ahmed 0:791a779d6220 267
shoaib_ahmed 0:791a779d6220 268 dest->pub.next_output_byte = dest->buffer = *outbuffer;
shoaib_ahmed 0:791a779d6220 269 dest->pub.free_in_buffer = dest->bufsize = *outsize;
shoaib_ahmed 0:791a779d6220 270 }