Final 350 project
Dependencies: uzair Camera_LS_Y201 F7_Ethernet LCD_DISCO_F746NG NetworkAPI SDFileSystem mbed
includes/rdcolmap.c@0:791a779d6220, 2017-07-31 (annotated)
- Committer:
- shoaib_ahmed
- Date:
- Mon Jul 31 09:16:35 2017 +0000
- Revision:
- 0:791a779d6220
final project;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
shoaib_ahmed | 0:791a779d6220 | 1 | /* |
shoaib_ahmed | 0:791a779d6220 | 2 | * rdcolmap.c |
shoaib_ahmed | 0:791a779d6220 | 3 | * |
shoaib_ahmed | 0:791a779d6220 | 4 | * Copyright (C) 1994-1996, Thomas G. Lane. |
shoaib_ahmed | 0:791a779d6220 | 5 | * This file is part of the Independent JPEG Group's software. |
shoaib_ahmed | 0:791a779d6220 | 6 | * For conditions of distribution and use, see the accompanying README file. |
shoaib_ahmed | 0:791a779d6220 | 7 | * |
shoaib_ahmed | 0:791a779d6220 | 8 | * This file implements djpeg's "-map file" switch. It reads a source image |
shoaib_ahmed | 0:791a779d6220 | 9 | * and constructs a colormap to be supplied to the JPEG decompressor. |
shoaib_ahmed | 0:791a779d6220 | 10 | * |
shoaib_ahmed | 0:791a779d6220 | 11 | * Currently, these file formats are supported for the map file: |
shoaib_ahmed | 0:791a779d6220 | 12 | * GIF: the contents of the GIF's global colormap are used. |
shoaib_ahmed | 0:791a779d6220 | 13 | * PPM (either text or raw flavor): the entire file is read and |
shoaib_ahmed | 0:791a779d6220 | 14 | * each unique pixel value is entered in the map. |
shoaib_ahmed | 0:791a779d6220 | 15 | * Note that reading a large PPM file will be horrendously slow. |
shoaib_ahmed | 0:791a779d6220 | 16 | * Typically, a PPM-format map file should contain just one pixel |
shoaib_ahmed | 0:791a779d6220 | 17 | * of each desired color. Such a file can be extracted from an |
shoaib_ahmed | 0:791a779d6220 | 18 | * ordinary image PPM file with ppmtomap(1). |
shoaib_ahmed | 0:791a779d6220 | 19 | * |
shoaib_ahmed | 0:791a779d6220 | 20 | * Rescaling a PPM that has a maxval unequal to MAXJSAMPLE is not |
shoaib_ahmed | 0:791a779d6220 | 21 | * currently implemented. |
shoaib_ahmed | 0:791a779d6220 | 22 | */ |
shoaib_ahmed | 0:791a779d6220 | 23 | |
shoaib_ahmed | 0:791a779d6220 | 24 | #include "cdjpeg.h" /* Common decls for cjpeg/djpeg applications */ |
shoaib_ahmed | 0:791a779d6220 | 25 | |
shoaib_ahmed | 0:791a779d6220 | 26 | #ifdef QUANT_2PASS_SUPPORTED /* otherwise can't quantize to supplied map */ |
shoaib_ahmed | 0:791a779d6220 | 27 | |
shoaib_ahmed | 0:791a779d6220 | 28 | /* Portions of this code are based on the PBMPLUS library, which is: |
shoaib_ahmed | 0:791a779d6220 | 29 | ** |
shoaib_ahmed | 0:791a779d6220 | 30 | ** Copyright (C) 1988 by Jef Poskanzer. |
shoaib_ahmed | 0:791a779d6220 | 31 | ** |
shoaib_ahmed | 0:791a779d6220 | 32 | ** Permission to use, copy, modify, and distribute this software and its |
shoaib_ahmed | 0:791a779d6220 | 33 | ** documentation for any purpose and without fee is hereby granted, provided |
shoaib_ahmed | 0:791a779d6220 | 34 | ** that the above copyright notice appear in all copies and that both that |
shoaib_ahmed | 0:791a779d6220 | 35 | ** copyright notice and this permission notice appear in supporting |
shoaib_ahmed | 0:791a779d6220 | 36 | ** documentation. This software is provided "as is" without express or |
shoaib_ahmed | 0:791a779d6220 | 37 | ** implied warranty. |
shoaib_ahmed | 0:791a779d6220 | 38 | */ |
shoaib_ahmed | 0:791a779d6220 | 39 | |
shoaib_ahmed | 0:791a779d6220 | 40 | |
shoaib_ahmed | 0:791a779d6220 | 41 | /* |
shoaib_ahmed | 0:791a779d6220 | 42 | * Add a (potentially) new color to the color map. |
shoaib_ahmed | 0:791a779d6220 | 43 | */ |
shoaib_ahmed | 0:791a779d6220 | 44 | |
shoaib_ahmed | 0:791a779d6220 | 45 | LOCAL(void) |
shoaib_ahmed | 0:791a779d6220 | 46 | add_map_entry (j_decompress_ptr cinfo, int R, int G, int B) |
shoaib_ahmed | 0:791a779d6220 | 47 | { |
shoaib_ahmed | 0:791a779d6220 | 48 | JSAMPROW colormap0 = cinfo->colormap[0]; |
shoaib_ahmed | 0:791a779d6220 | 49 | JSAMPROW colormap1 = cinfo->colormap[1]; |
shoaib_ahmed | 0:791a779d6220 | 50 | JSAMPROW colormap2 = cinfo->colormap[2]; |
shoaib_ahmed | 0:791a779d6220 | 51 | int ncolors = cinfo->actual_number_of_colors; |
shoaib_ahmed | 0:791a779d6220 | 52 | int index; |
shoaib_ahmed | 0:791a779d6220 | 53 | |
shoaib_ahmed | 0:791a779d6220 | 54 | /* Check for duplicate color. */ |
shoaib_ahmed | 0:791a779d6220 | 55 | for (index = 0; index < ncolors; index++) { |
shoaib_ahmed | 0:791a779d6220 | 56 | if (GETJSAMPLE(colormap0[index]) == R && |
shoaib_ahmed | 0:791a779d6220 | 57 | GETJSAMPLE(colormap1[index]) == G && |
shoaib_ahmed | 0:791a779d6220 | 58 | GETJSAMPLE(colormap2[index]) == B) |
shoaib_ahmed | 0:791a779d6220 | 59 | return; /* color is already in map */ |
shoaib_ahmed | 0:791a779d6220 | 60 | } |
shoaib_ahmed | 0:791a779d6220 | 61 | |
shoaib_ahmed | 0:791a779d6220 | 62 | /* Check for map overflow. */ |
shoaib_ahmed | 0:791a779d6220 | 63 | if (ncolors >= (MAXJSAMPLE+1)) |
shoaib_ahmed | 0:791a779d6220 | 64 | ERREXIT1(cinfo, JERR_QUANT_MANY_COLORS, (MAXJSAMPLE+1)); |
shoaib_ahmed | 0:791a779d6220 | 65 | |
shoaib_ahmed | 0:791a779d6220 | 66 | /* OK, add color to map. */ |
shoaib_ahmed | 0:791a779d6220 | 67 | colormap0[ncolors] = (JSAMPLE) R; |
shoaib_ahmed | 0:791a779d6220 | 68 | colormap1[ncolors] = (JSAMPLE) G; |
shoaib_ahmed | 0:791a779d6220 | 69 | colormap2[ncolors] = (JSAMPLE) B; |
shoaib_ahmed | 0:791a779d6220 | 70 | cinfo->actual_number_of_colors++; |
shoaib_ahmed | 0:791a779d6220 | 71 | } |
shoaib_ahmed | 0:791a779d6220 | 72 | |
shoaib_ahmed | 0:791a779d6220 | 73 | |
shoaib_ahmed | 0:791a779d6220 | 74 | /* |
shoaib_ahmed | 0:791a779d6220 | 75 | * Extract color map from a GIF file. |
shoaib_ahmed | 0:791a779d6220 | 76 | */ |
shoaib_ahmed | 0:791a779d6220 | 77 | |
shoaib_ahmed | 0:791a779d6220 | 78 | LOCAL(void) |
shoaib_ahmed | 0:791a779d6220 | 79 | read_gif_map (j_decompress_ptr cinfo, FILE * infile) |
shoaib_ahmed | 0:791a779d6220 | 80 | { |
shoaib_ahmed | 0:791a779d6220 | 81 | int header[13]; |
shoaib_ahmed | 0:791a779d6220 | 82 | int i, colormaplen; |
shoaib_ahmed | 0:791a779d6220 | 83 | int R, G, B; |
shoaib_ahmed | 0:791a779d6220 | 84 | |
shoaib_ahmed | 0:791a779d6220 | 85 | /* Initial 'G' has already been read by read_color_map */ |
shoaib_ahmed | 0:791a779d6220 | 86 | /* Read the rest of the GIF header and logical screen descriptor */ |
shoaib_ahmed | 0:791a779d6220 | 87 | for (i = 1; i < 13; i++) { |
shoaib_ahmed | 0:791a779d6220 | 88 | if ((header[i] = getc(infile)) == EOF) |
shoaib_ahmed | 0:791a779d6220 | 89 | ERREXIT(cinfo, JERR_BAD_CMAP_FILE); |
shoaib_ahmed | 0:791a779d6220 | 90 | } |
shoaib_ahmed | 0:791a779d6220 | 91 | |
shoaib_ahmed | 0:791a779d6220 | 92 | /* Verify GIF Header */ |
shoaib_ahmed | 0:791a779d6220 | 93 | if (header[1] != 'I' || header[2] != 'F') |
shoaib_ahmed | 0:791a779d6220 | 94 | ERREXIT(cinfo, JERR_BAD_CMAP_FILE); |
shoaib_ahmed | 0:791a779d6220 | 95 | |
shoaib_ahmed | 0:791a779d6220 | 96 | /* There must be a global color map. */ |
shoaib_ahmed | 0:791a779d6220 | 97 | if ((header[10] & 0x80) == 0) |
shoaib_ahmed | 0:791a779d6220 | 98 | ERREXIT(cinfo, JERR_BAD_CMAP_FILE); |
shoaib_ahmed | 0:791a779d6220 | 99 | |
shoaib_ahmed | 0:791a779d6220 | 100 | /* OK, fetch it. */ |
shoaib_ahmed | 0:791a779d6220 | 101 | colormaplen = 2 << (header[10] & 0x07); |
shoaib_ahmed | 0:791a779d6220 | 102 | |
shoaib_ahmed | 0:791a779d6220 | 103 | for (i = 0; i < colormaplen; i++) { |
shoaib_ahmed | 0:791a779d6220 | 104 | R = getc(infile); |
shoaib_ahmed | 0:791a779d6220 | 105 | G = getc(infile); |
shoaib_ahmed | 0:791a779d6220 | 106 | B = getc(infile); |
shoaib_ahmed | 0:791a779d6220 | 107 | if (R == EOF || G == EOF || B == EOF) |
shoaib_ahmed | 0:791a779d6220 | 108 | ERREXIT(cinfo, JERR_BAD_CMAP_FILE); |
shoaib_ahmed | 0:791a779d6220 | 109 | add_map_entry(cinfo, |
shoaib_ahmed | 0:791a779d6220 | 110 | R << (BITS_IN_JSAMPLE-8), |
shoaib_ahmed | 0:791a779d6220 | 111 | G << (BITS_IN_JSAMPLE-8), |
shoaib_ahmed | 0:791a779d6220 | 112 | B << (BITS_IN_JSAMPLE-8)); |
shoaib_ahmed | 0:791a779d6220 | 113 | } |
shoaib_ahmed | 0:791a779d6220 | 114 | } |
shoaib_ahmed | 0:791a779d6220 | 115 | |
shoaib_ahmed | 0:791a779d6220 | 116 | |
shoaib_ahmed | 0:791a779d6220 | 117 | /* Support routines for reading PPM */ |
shoaib_ahmed | 0:791a779d6220 | 118 | |
shoaib_ahmed | 0:791a779d6220 | 119 | |
shoaib_ahmed | 0:791a779d6220 | 120 | LOCAL(int) |
shoaib_ahmed | 0:791a779d6220 | 121 | pbm_getc (FILE * infile) |
shoaib_ahmed | 0:791a779d6220 | 122 | /* Read next char, skipping over any comments */ |
shoaib_ahmed | 0:791a779d6220 | 123 | /* A comment/newline sequence is returned as a newline */ |
shoaib_ahmed | 0:791a779d6220 | 124 | { |
shoaib_ahmed | 0:791a779d6220 | 125 | register int ch; |
shoaib_ahmed | 0:791a779d6220 | 126 | |
shoaib_ahmed | 0:791a779d6220 | 127 | ch = getc(infile); |
shoaib_ahmed | 0:791a779d6220 | 128 | if (ch == '#') { |
shoaib_ahmed | 0:791a779d6220 | 129 | do { |
shoaib_ahmed | 0:791a779d6220 | 130 | ch = getc(infile); |
shoaib_ahmed | 0:791a779d6220 | 131 | } while (ch != '\n' && ch != EOF); |
shoaib_ahmed | 0:791a779d6220 | 132 | } |
shoaib_ahmed | 0:791a779d6220 | 133 | return ch; |
shoaib_ahmed | 0:791a779d6220 | 134 | } |
shoaib_ahmed | 0:791a779d6220 | 135 | |
shoaib_ahmed | 0:791a779d6220 | 136 | |
shoaib_ahmed | 0:791a779d6220 | 137 | LOCAL(unsigned int) |
shoaib_ahmed | 0:791a779d6220 | 138 | read_pbm_integer (j_decompress_ptr cinfo, FILE * infile) |
shoaib_ahmed | 0:791a779d6220 | 139 | /* Read an unsigned decimal integer from the PPM file */ |
shoaib_ahmed | 0:791a779d6220 | 140 | /* Swallows one trailing character after the integer */ |
shoaib_ahmed | 0:791a779d6220 | 141 | /* Note that on a 16-bit-int machine, only values up to 64k can be read. */ |
shoaib_ahmed | 0:791a779d6220 | 142 | /* This should not be a problem in practice. */ |
shoaib_ahmed | 0:791a779d6220 | 143 | { |
shoaib_ahmed | 0:791a779d6220 | 144 | register int ch; |
shoaib_ahmed | 0:791a779d6220 | 145 | register unsigned int val; |
shoaib_ahmed | 0:791a779d6220 | 146 | |
shoaib_ahmed | 0:791a779d6220 | 147 | /* Skip any leading whitespace */ |
shoaib_ahmed | 0:791a779d6220 | 148 | do { |
shoaib_ahmed | 0:791a779d6220 | 149 | ch = pbm_getc(infile); |
shoaib_ahmed | 0:791a779d6220 | 150 | if (ch == EOF) |
shoaib_ahmed | 0:791a779d6220 | 151 | ERREXIT(cinfo, JERR_BAD_CMAP_FILE); |
shoaib_ahmed | 0:791a779d6220 | 152 | } while (ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r'); |
shoaib_ahmed | 0:791a779d6220 | 153 | |
shoaib_ahmed | 0:791a779d6220 | 154 | if (ch < '0' || ch > '9') |
shoaib_ahmed | 0:791a779d6220 | 155 | ERREXIT(cinfo, JERR_BAD_CMAP_FILE); |
shoaib_ahmed | 0:791a779d6220 | 156 | |
shoaib_ahmed | 0:791a779d6220 | 157 | val = ch - '0'; |
shoaib_ahmed | 0:791a779d6220 | 158 | while ((ch = pbm_getc(infile)) >= '0' && ch <= '9') { |
shoaib_ahmed | 0:791a779d6220 | 159 | val *= 10; |
shoaib_ahmed | 0:791a779d6220 | 160 | val += ch - '0'; |
shoaib_ahmed | 0:791a779d6220 | 161 | } |
shoaib_ahmed | 0:791a779d6220 | 162 | return val; |
shoaib_ahmed | 0:791a779d6220 | 163 | } |
shoaib_ahmed | 0:791a779d6220 | 164 | |
shoaib_ahmed | 0:791a779d6220 | 165 | |
shoaib_ahmed | 0:791a779d6220 | 166 | /* |
shoaib_ahmed | 0:791a779d6220 | 167 | * Extract color map from a PPM file. |
shoaib_ahmed | 0:791a779d6220 | 168 | */ |
shoaib_ahmed | 0:791a779d6220 | 169 | |
shoaib_ahmed | 0:791a779d6220 | 170 | LOCAL(void) |
shoaib_ahmed | 0:791a779d6220 | 171 | read_ppm_map (j_decompress_ptr cinfo, FILE * infile) |
shoaib_ahmed | 0:791a779d6220 | 172 | { |
shoaib_ahmed | 0:791a779d6220 | 173 | int c; |
shoaib_ahmed | 0:791a779d6220 | 174 | unsigned int w, h, maxval, row, col; |
shoaib_ahmed | 0:791a779d6220 | 175 | int R, G, B; |
shoaib_ahmed | 0:791a779d6220 | 176 | |
shoaib_ahmed | 0:791a779d6220 | 177 | /* Initial 'P' has already been read by read_color_map */ |
shoaib_ahmed | 0:791a779d6220 | 178 | c = getc(infile); /* save format discriminator for a sec */ |
shoaib_ahmed | 0:791a779d6220 | 179 | |
shoaib_ahmed | 0:791a779d6220 | 180 | /* while we fetch the remaining header info */ |
shoaib_ahmed | 0:791a779d6220 | 181 | w = read_pbm_integer(cinfo, infile); |
shoaib_ahmed | 0:791a779d6220 | 182 | h = read_pbm_integer(cinfo, infile); |
shoaib_ahmed | 0:791a779d6220 | 183 | maxval = read_pbm_integer(cinfo, infile); |
shoaib_ahmed | 0:791a779d6220 | 184 | |
shoaib_ahmed | 0:791a779d6220 | 185 | if (w <= 0 || h <= 0 || maxval <= 0) /* error check */ |
shoaib_ahmed | 0:791a779d6220 | 186 | ERREXIT(cinfo, JERR_BAD_CMAP_FILE); |
shoaib_ahmed | 0:791a779d6220 | 187 | |
shoaib_ahmed | 0:791a779d6220 | 188 | /* For now, we don't support rescaling from an unusual maxval. */ |
shoaib_ahmed | 0:791a779d6220 | 189 | if (maxval != (unsigned int) MAXJSAMPLE) |
shoaib_ahmed | 0:791a779d6220 | 190 | ERREXIT(cinfo, JERR_BAD_CMAP_FILE); |
shoaib_ahmed | 0:791a779d6220 | 191 | |
shoaib_ahmed | 0:791a779d6220 | 192 | switch (c) { |
shoaib_ahmed | 0:791a779d6220 | 193 | case '3': /* it's a text-format PPM file */ |
shoaib_ahmed | 0:791a779d6220 | 194 | for (row = 0; row < h; row++) { |
shoaib_ahmed | 0:791a779d6220 | 195 | for (col = 0; col < w; col++) { |
shoaib_ahmed | 0:791a779d6220 | 196 | R = read_pbm_integer(cinfo, infile); |
shoaib_ahmed | 0:791a779d6220 | 197 | G = read_pbm_integer(cinfo, infile); |
shoaib_ahmed | 0:791a779d6220 | 198 | B = read_pbm_integer(cinfo, infile); |
shoaib_ahmed | 0:791a779d6220 | 199 | add_map_entry(cinfo, R, G, B); |
shoaib_ahmed | 0:791a779d6220 | 200 | } |
shoaib_ahmed | 0:791a779d6220 | 201 | } |
shoaib_ahmed | 0:791a779d6220 | 202 | break; |
shoaib_ahmed | 0:791a779d6220 | 203 | |
shoaib_ahmed | 0:791a779d6220 | 204 | case '6': /* it's a raw-format PPM file */ |
shoaib_ahmed | 0:791a779d6220 | 205 | for (row = 0; row < h; row++) { |
shoaib_ahmed | 0:791a779d6220 | 206 | for (col = 0; col < w; col++) { |
shoaib_ahmed | 0:791a779d6220 | 207 | R = getc(infile); |
shoaib_ahmed | 0:791a779d6220 | 208 | G = getc(infile); |
shoaib_ahmed | 0:791a779d6220 | 209 | B = getc(infile); |
shoaib_ahmed | 0:791a779d6220 | 210 | if (R == EOF || G == EOF || B == EOF) |
shoaib_ahmed | 0:791a779d6220 | 211 | ERREXIT(cinfo, JERR_BAD_CMAP_FILE); |
shoaib_ahmed | 0:791a779d6220 | 212 | add_map_entry(cinfo, R, G, B); |
shoaib_ahmed | 0:791a779d6220 | 213 | } |
shoaib_ahmed | 0:791a779d6220 | 214 | } |
shoaib_ahmed | 0:791a779d6220 | 215 | break; |
shoaib_ahmed | 0:791a779d6220 | 216 | |
shoaib_ahmed | 0:791a779d6220 | 217 | default: |
shoaib_ahmed | 0:791a779d6220 | 218 | ERREXIT(cinfo, JERR_BAD_CMAP_FILE); |
shoaib_ahmed | 0:791a779d6220 | 219 | break; |
shoaib_ahmed | 0:791a779d6220 | 220 | } |
shoaib_ahmed | 0:791a779d6220 | 221 | } |
shoaib_ahmed | 0:791a779d6220 | 222 | |
shoaib_ahmed | 0:791a779d6220 | 223 | |
shoaib_ahmed | 0:791a779d6220 | 224 | /* |
shoaib_ahmed | 0:791a779d6220 | 225 | * Main entry point from djpeg.c. |
shoaib_ahmed | 0:791a779d6220 | 226 | * Input: opened input file (from file name argument on command line). |
shoaib_ahmed | 0:791a779d6220 | 227 | * Output: colormap and actual_number_of_colors fields are set in cinfo. |
shoaib_ahmed | 0:791a779d6220 | 228 | */ |
shoaib_ahmed | 0:791a779d6220 | 229 | |
shoaib_ahmed | 0:791a779d6220 | 230 | GLOBAL(void) |
shoaib_ahmed | 0:791a779d6220 | 231 | read_color_map (j_decompress_ptr cinfo, FILE * infile) |
shoaib_ahmed | 0:791a779d6220 | 232 | { |
shoaib_ahmed | 0:791a779d6220 | 233 | /* Allocate space for a color map of maximum supported size. */ |
shoaib_ahmed | 0:791a779d6220 | 234 | cinfo->colormap = (*cinfo->mem->alloc_sarray) |
shoaib_ahmed | 0:791a779d6220 | 235 | ((j_common_ptr) cinfo, JPOOL_IMAGE, |
shoaib_ahmed | 0:791a779d6220 | 236 | (JDIMENSION) (MAXJSAMPLE+1), (JDIMENSION) 3); |
shoaib_ahmed | 0:791a779d6220 | 237 | cinfo->actual_number_of_colors = 0; /* initialize map to empty */ |
shoaib_ahmed | 0:791a779d6220 | 238 | |
shoaib_ahmed | 0:791a779d6220 | 239 | /* Read first byte to determine file format */ |
shoaib_ahmed | 0:791a779d6220 | 240 | switch (getc(infile)) { |
shoaib_ahmed | 0:791a779d6220 | 241 | case 'G': |
shoaib_ahmed | 0:791a779d6220 | 242 | read_gif_map(cinfo, infile); |
shoaib_ahmed | 0:791a779d6220 | 243 | break; |
shoaib_ahmed | 0:791a779d6220 | 244 | case 'P': |
shoaib_ahmed | 0:791a779d6220 | 245 | read_ppm_map(cinfo, infile); |
shoaib_ahmed | 0:791a779d6220 | 246 | break; |
shoaib_ahmed | 0:791a779d6220 | 247 | default: |
shoaib_ahmed | 0:791a779d6220 | 248 | ERREXIT(cinfo, JERR_BAD_CMAP_FILE); |
shoaib_ahmed | 0:791a779d6220 | 249 | break; |
shoaib_ahmed | 0:791a779d6220 | 250 | } |
shoaib_ahmed | 0:791a779d6220 | 251 | } |
shoaib_ahmed | 0:791a779d6220 | 252 | |
shoaib_ahmed | 0:791a779d6220 | 253 | #endif /* QUANT_2PASS_SUPPORTED */ |