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 * jutils.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-2011 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 tables and miscellaneous utility routines needed
shoaib_ahmed 0:791a779d6220 10 * for both compression and decompression.
shoaib_ahmed 0:791a779d6220 11 * Note we prefix all global names with "j" to minimize conflicts with
shoaib_ahmed 0:791a779d6220 12 * a surrounding application.
shoaib_ahmed 0:791a779d6220 13 */
shoaib_ahmed 0:791a779d6220 14
shoaib_ahmed 0:791a779d6220 15 #define JPEG_INTERNALS
shoaib_ahmed 0:791a779d6220 16 #include "jinclude.h"
shoaib_ahmed 0:791a779d6220 17 #include "jpeglib.h"
shoaib_ahmed 0:791a779d6220 18
shoaib_ahmed 0:791a779d6220 19
shoaib_ahmed 0:791a779d6220 20 /*
shoaib_ahmed 0:791a779d6220 21 * jpeg_zigzag_order[i] is the zigzag-order position of the i'th element
shoaib_ahmed 0:791a779d6220 22 * of a DCT block read in natural order (left to right, top to bottom).
shoaib_ahmed 0:791a779d6220 23 */
shoaib_ahmed 0:791a779d6220 24
shoaib_ahmed 0:791a779d6220 25 #if 0 /* This table is not actually needed in v6a */
shoaib_ahmed 0:791a779d6220 26
shoaib_ahmed 0:791a779d6220 27 const int jpeg_zigzag_order[DCTSIZE2] = {
shoaib_ahmed 0:791a779d6220 28 0, 1, 5, 6, 14, 15, 27, 28,
shoaib_ahmed 0:791a779d6220 29 2, 4, 7, 13, 16, 26, 29, 42,
shoaib_ahmed 0:791a779d6220 30 3, 8, 12, 17, 25, 30, 41, 43,
shoaib_ahmed 0:791a779d6220 31 9, 11, 18, 24, 31, 40, 44, 53,
shoaib_ahmed 0:791a779d6220 32 10, 19, 23, 32, 39, 45, 52, 54,
shoaib_ahmed 0:791a779d6220 33 20, 22, 33, 38, 46, 51, 55, 60,
shoaib_ahmed 0:791a779d6220 34 21, 34, 37, 47, 50, 56, 59, 61,
shoaib_ahmed 0:791a779d6220 35 35, 36, 48, 49, 57, 58, 62, 63
shoaib_ahmed 0:791a779d6220 36 };
shoaib_ahmed 0:791a779d6220 37
shoaib_ahmed 0:791a779d6220 38 #endif
shoaib_ahmed 0:791a779d6220 39
shoaib_ahmed 0:791a779d6220 40 /*
shoaib_ahmed 0:791a779d6220 41 * jpeg_natural_order[i] is the natural-order position of the i'th element
shoaib_ahmed 0:791a779d6220 42 * of zigzag order.
shoaib_ahmed 0:791a779d6220 43 *
shoaib_ahmed 0:791a779d6220 44 * When reading corrupted data, the Huffman decoders could attempt
shoaib_ahmed 0:791a779d6220 45 * to reference an entry beyond the end of this array (if the decoded
shoaib_ahmed 0:791a779d6220 46 * zero run length reaches past the end of the block). To prevent
shoaib_ahmed 0:791a779d6220 47 * wild stores without adding an inner-loop test, we put some extra
shoaib_ahmed 0:791a779d6220 48 * "63"s after the real entries. This will cause the extra coefficient
shoaib_ahmed 0:791a779d6220 49 * to be stored in location 63 of the block, not somewhere random.
shoaib_ahmed 0:791a779d6220 50 * The worst case would be a run-length of 15, which means we need 16
shoaib_ahmed 0:791a779d6220 51 * fake entries.
shoaib_ahmed 0:791a779d6220 52 */
shoaib_ahmed 0:791a779d6220 53
shoaib_ahmed 0:791a779d6220 54 const int jpeg_natural_order[DCTSIZE2+16] = {
shoaib_ahmed 0:791a779d6220 55 0, 1, 8, 16, 9, 2, 3, 10,
shoaib_ahmed 0:791a779d6220 56 17, 24, 32, 25, 18, 11, 4, 5,
shoaib_ahmed 0:791a779d6220 57 12, 19, 26, 33, 40, 48, 41, 34,
shoaib_ahmed 0:791a779d6220 58 27, 20, 13, 6, 7, 14, 21, 28,
shoaib_ahmed 0:791a779d6220 59 35, 42, 49, 56, 57, 50, 43, 36,
shoaib_ahmed 0:791a779d6220 60 29, 22, 15, 23, 30, 37, 44, 51,
shoaib_ahmed 0:791a779d6220 61 58, 59, 52, 45, 38, 31, 39, 46,
shoaib_ahmed 0:791a779d6220 62 53, 60, 61, 54, 47, 55, 62, 63,
shoaib_ahmed 0:791a779d6220 63 63, 63, 63, 63, 63, 63, 63, 63, /* extra entries for safety in decoder */
shoaib_ahmed 0:791a779d6220 64 63, 63, 63, 63, 63, 63, 63, 63
shoaib_ahmed 0:791a779d6220 65 };
shoaib_ahmed 0:791a779d6220 66
shoaib_ahmed 0:791a779d6220 67 const int jpeg_natural_order7[7*7+16] = {
shoaib_ahmed 0:791a779d6220 68 0, 1, 8, 16, 9, 2, 3, 10,
shoaib_ahmed 0:791a779d6220 69 17, 24, 32, 25, 18, 11, 4, 5,
shoaib_ahmed 0:791a779d6220 70 12, 19, 26, 33, 40, 48, 41, 34,
shoaib_ahmed 0:791a779d6220 71 27, 20, 13, 6, 14, 21, 28, 35,
shoaib_ahmed 0:791a779d6220 72 42, 49, 50, 43, 36, 29, 22, 30,
shoaib_ahmed 0:791a779d6220 73 37, 44, 51, 52, 45, 38, 46, 53,
shoaib_ahmed 0:791a779d6220 74 54,
shoaib_ahmed 0:791a779d6220 75 63, 63, 63, 63, 63, 63, 63, 63, /* extra entries for safety in decoder */
shoaib_ahmed 0:791a779d6220 76 63, 63, 63, 63, 63, 63, 63, 63
shoaib_ahmed 0:791a779d6220 77 };
shoaib_ahmed 0:791a779d6220 78
shoaib_ahmed 0:791a779d6220 79 const int jpeg_natural_order6[6*6+16] = {
shoaib_ahmed 0:791a779d6220 80 0, 1, 8, 16, 9, 2, 3, 10,
shoaib_ahmed 0:791a779d6220 81 17, 24, 32, 25, 18, 11, 4, 5,
shoaib_ahmed 0:791a779d6220 82 12, 19, 26, 33, 40, 41, 34, 27,
shoaib_ahmed 0:791a779d6220 83 20, 13, 21, 28, 35, 42, 43, 36,
shoaib_ahmed 0:791a779d6220 84 29, 37, 44, 45,
shoaib_ahmed 0:791a779d6220 85 63, 63, 63, 63, 63, 63, 63, 63, /* extra entries for safety in decoder */
shoaib_ahmed 0:791a779d6220 86 63, 63, 63, 63, 63, 63, 63, 63
shoaib_ahmed 0:791a779d6220 87 };
shoaib_ahmed 0:791a779d6220 88
shoaib_ahmed 0:791a779d6220 89 const int jpeg_natural_order5[5*5+16] = {
shoaib_ahmed 0:791a779d6220 90 0, 1, 8, 16, 9, 2, 3, 10,
shoaib_ahmed 0:791a779d6220 91 17, 24, 32, 25, 18, 11, 4, 12,
shoaib_ahmed 0:791a779d6220 92 19, 26, 33, 34, 27, 20, 28, 35,
shoaib_ahmed 0:791a779d6220 93 36,
shoaib_ahmed 0:791a779d6220 94 63, 63, 63, 63, 63, 63, 63, 63, /* extra entries for safety in decoder */
shoaib_ahmed 0:791a779d6220 95 63, 63, 63, 63, 63, 63, 63, 63
shoaib_ahmed 0:791a779d6220 96 };
shoaib_ahmed 0:791a779d6220 97
shoaib_ahmed 0:791a779d6220 98 const int jpeg_natural_order4[4*4+16] = {
shoaib_ahmed 0:791a779d6220 99 0, 1, 8, 16, 9, 2, 3, 10,
shoaib_ahmed 0:791a779d6220 100 17, 24, 25, 18, 11, 19, 26, 27,
shoaib_ahmed 0:791a779d6220 101 63, 63, 63, 63, 63, 63, 63, 63, /* extra entries for safety in decoder */
shoaib_ahmed 0:791a779d6220 102 63, 63, 63, 63, 63, 63, 63, 63
shoaib_ahmed 0:791a779d6220 103 };
shoaib_ahmed 0:791a779d6220 104
shoaib_ahmed 0:791a779d6220 105 const int jpeg_natural_order3[3*3+16] = {
shoaib_ahmed 0:791a779d6220 106 0, 1, 8, 16, 9, 2, 10, 17,
shoaib_ahmed 0:791a779d6220 107 18,
shoaib_ahmed 0:791a779d6220 108 63, 63, 63, 63, 63, 63, 63, 63, /* extra entries for safety in decoder */
shoaib_ahmed 0:791a779d6220 109 63, 63, 63, 63, 63, 63, 63, 63
shoaib_ahmed 0:791a779d6220 110 };
shoaib_ahmed 0:791a779d6220 111
shoaib_ahmed 0:791a779d6220 112 const int jpeg_natural_order2[2*2+16] = {
shoaib_ahmed 0:791a779d6220 113 0, 1, 8, 9,
shoaib_ahmed 0:791a779d6220 114 63, 63, 63, 63, 63, 63, 63, 63, /* extra entries for safety in decoder */
shoaib_ahmed 0:791a779d6220 115 63, 63, 63, 63, 63, 63, 63, 63
shoaib_ahmed 0:791a779d6220 116 };
shoaib_ahmed 0:791a779d6220 117
shoaib_ahmed 0:791a779d6220 118
shoaib_ahmed 0:791a779d6220 119 /*
shoaib_ahmed 0:791a779d6220 120 * Arithmetic utilities
shoaib_ahmed 0:791a779d6220 121 */
shoaib_ahmed 0:791a779d6220 122
shoaib_ahmed 0:791a779d6220 123 GLOBAL(long)
shoaib_ahmed 0:791a779d6220 124 jdiv_round_up (long a, long b)
shoaib_ahmed 0:791a779d6220 125 /* Compute a/b rounded up to next integer, ie, ceil(a/b) */
shoaib_ahmed 0:791a779d6220 126 /* Assumes a >= 0, b > 0 */
shoaib_ahmed 0:791a779d6220 127 {
shoaib_ahmed 0:791a779d6220 128 return (a + b - 1L) / b;
shoaib_ahmed 0:791a779d6220 129 }
shoaib_ahmed 0:791a779d6220 130
shoaib_ahmed 0:791a779d6220 131
shoaib_ahmed 0:791a779d6220 132 GLOBAL(long)
shoaib_ahmed 0:791a779d6220 133 jround_up (long a, long b)
shoaib_ahmed 0:791a779d6220 134 /* Compute a rounded up to next multiple of b, ie, ceil(a/b)*b */
shoaib_ahmed 0:791a779d6220 135 /* Assumes a >= 0, b > 0 */
shoaib_ahmed 0:791a779d6220 136 {
shoaib_ahmed 0:791a779d6220 137 a += b - 1L;
shoaib_ahmed 0:791a779d6220 138 return a - (a % b);
shoaib_ahmed 0:791a779d6220 139 }
shoaib_ahmed 0:791a779d6220 140
shoaib_ahmed 0:791a779d6220 141
shoaib_ahmed 0:791a779d6220 142 /* On normal machines we can apply MEMCOPY() and MEMZERO() to sample arrays
shoaib_ahmed 0:791a779d6220 143 * and coefficient-block arrays. This won't work on 80x86 because the arrays
shoaib_ahmed 0:791a779d6220 144 * are FAR and we're assuming a small-pointer memory model. However, some
shoaib_ahmed 0:791a779d6220 145 * DOS compilers provide far-pointer versions of memcpy() and memset() even
shoaib_ahmed 0:791a779d6220 146 * in the small-model libraries. These will be used if USE_FMEM is defined.
shoaib_ahmed 0:791a779d6220 147 * Otherwise, the routines below do it the hard way. (The performance cost
shoaib_ahmed 0:791a779d6220 148 * is not all that great, because these routines aren't very heavily used.)
shoaib_ahmed 0:791a779d6220 149 */
shoaib_ahmed 0:791a779d6220 150
shoaib_ahmed 0:791a779d6220 151 #ifndef NEED_FAR_POINTERS /* normal case, same as regular macro */
shoaib_ahmed 0:791a779d6220 152 #define FMEMCOPY(dest,src,size) MEMCOPY(dest,src,size)
shoaib_ahmed 0:791a779d6220 153 #else /* 80x86 case, define if we can */
shoaib_ahmed 0:791a779d6220 154 #ifdef USE_FMEM
shoaib_ahmed 0:791a779d6220 155 #define FMEMCOPY(dest,src,size) _fmemcpy((void FAR *)(dest), (const void FAR *)(src), (size_t)(size))
shoaib_ahmed 0:791a779d6220 156 #else
shoaib_ahmed 0:791a779d6220 157 /* This function is for use by the FMEMZERO macro defined in jpegint.h.
shoaib_ahmed 0:791a779d6220 158 * Do not call this function directly, use the FMEMZERO macro instead.
shoaib_ahmed 0:791a779d6220 159 */
shoaib_ahmed 0:791a779d6220 160 GLOBAL(void)
shoaib_ahmed 0:791a779d6220 161 jzero_far (void FAR * target, size_t bytestozero)
shoaib_ahmed 0:791a779d6220 162 /* Zero out a chunk of FAR memory. */
shoaib_ahmed 0:791a779d6220 163 /* This might be sample-array data, block-array data, or alloc_large data. */
shoaib_ahmed 0:791a779d6220 164 {
shoaib_ahmed 0:791a779d6220 165 register char FAR * ptr = (char FAR *) target;
shoaib_ahmed 0:791a779d6220 166 register size_t count;
shoaib_ahmed 0:791a779d6220 167
shoaib_ahmed 0:791a779d6220 168 for (count = bytestozero; count > 0; count--) {
shoaib_ahmed 0:791a779d6220 169 *ptr++ = 0;
shoaib_ahmed 0:791a779d6220 170 }
shoaib_ahmed 0:791a779d6220 171 }
shoaib_ahmed 0:791a779d6220 172 #endif
shoaib_ahmed 0:791a779d6220 173 #endif
shoaib_ahmed 0:791a779d6220 174
shoaib_ahmed 0:791a779d6220 175
shoaib_ahmed 0:791a779d6220 176 GLOBAL(void)
shoaib_ahmed 0:791a779d6220 177 jcopy_sample_rows (JSAMPARRAY input_array, int source_row,
shoaib_ahmed 0:791a779d6220 178 JSAMPARRAY output_array, int dest_row,
shoaib_ahmed 0:791a779d6220 179 int num_rows, JDIMENSION num_cols)
shoaib_ahmed 0:791a779d6220 180 /* Copy some rows of samples from one place to another.
shoaib_ahmed 0:791a779d6220 181 * num_rows rows are copied from input_array[source_row++]
shoaib_ahmed 0:791a779d6220 182 * to output_array[dest_row++]; these areas may overlap for duplication.
shoaib_ahmed 0:791a779d6220 183 * The source and destination arrays must be at least as wide as num_cols.
shoaib_ahmed 0:791a779d6220 184 */
shoaib_ahmed 0:791a779d6220 185 {
shoaib_ahmed 0:791a779d6220 186 register JSAMPROW inptr, outptr;
shoaib_ahmed 0:791a779d6220 187 #ifdef FMEMCOPY
shoaib_ahmed 0:791a779d6220 188 register size_t count = (size_t) (num_cols * SIZEOF(JSAMPLE));
shoaib_ahmed 0:791a779d6220 189 #else
shoaib_ahmed 0:791a779d6220 190 register JDIMENSION count;
shoaib_ahmed 0:791a779d6220 191 #endif
shoaib_ahmed 0:791a779d6220 192 register int row;
shoaib_ahmed 0:791a779d6220 193
shoaib_ahmed 0:791a779d6220 194 input_array += source_row;
shoaib_ahmed 0:791a779d6220 195 output_array += dest_row;
shoaib_ahmed 0:791a779d6220 196
shoaib_ahmed 0:791a779d6220 197 for (row = num_rows; row > 0; row--) {
shoaib_ahmed 0:791a779d6220 198 inptr = *input_array++;
shoaib_ahmed 0:791a779d6220 199 outptr = *output_array++;
shoaib_ahmed 0:791a779d6220 200 #ifdef FMEMCOPY
shoaib_ahmed 0:791a779d6220 201 FMEMCOPY(outptr, inptr, count);
shoaib_ahmed 0:791a779d6220 202 #else
shoaib_ahmed 0:791a779d6220 203 for (count = num_cols; count > 0; count--)
shoaib_ahmed 0:791a779d6220 204 *outptr++ = *inptr++; /* needn't bother with GETJSAMPLE() here */
shoaib_ahmed 0:791a779d6220 205 #endif
shoaib_ahmed 0:791a779d6220 206 }
shoaib_ahmed 0:791a779d6220 207 }
shoaib_ahmed 0:791a779d6220 208
shoaib_ahmed 0:791a779d6220 209
shoaib_ahmed 0:791a779d6220 210 GLOBAL(void)
shoaib_ahmed 0:791a779d6220 211 jcopy_block_row (JBLOCKROW input_row, JBLOCKROW output_row,
shoaib_ahmed 0:791a779d6220 212 JDIMENSION num_blocks)
shoaib_ahmed 0:791a779d6220 213 /* Copy a row of coefficient blocks from one place to another. */
shoaib_ahmed 0:791a779d6220 214 {
shoaib_ahmed 0:791a779d6220 215 #ifdef FMEMCOPY
shoaib_ahmed 0:791a779d6220 216 FMEMCOPY(output_row, input_row, num_blocks * (DCTSIZE2 * SIZEOF(JCOEF)));
shoaib_ahmed 0:791a779d6220 217 #else
shoaib_ahmed 0:791a779d6220 218 register JCOEFPTR inptr, outptr;
shoaib_ahmed 0:791a779d6220 219 register long count;
shoaib_ahmed 0:791a779d6220 220
shoaib_ahmed 0:791a779d6220 221 inptr = (JCOEFPTR) input_row;
shoaib_ahmed 0:791a779d6220 222 outptr = (JCOEFPTR) output_row;
shoaib_ahmed 0:791a779d6220 223 for (count = (long) num_blocks * DCTSIZE2; count > 0; count--) {
shoaib_ahmed 0:791a779d6220 224 *outptr++ = *inptr++;
shoaib_ahmed 0:791a779d6220 225 }
shoaib_ahmed 0:791a779d6220 226 #endif
shoaib_ahmed 0:791a779d6220 227 }