read_pbm_integer (j_compress_ptr cinfo, FILE * infile, int maxval) /* Read an unsigned decimal integer from the PPM file */ /* Swallows one trailing character after the integer */ /* Note that on a 16-bit-int machine, only values up to 64k can be read. */ /* This should not be a problem in practice. */ { register int ch; register unsigned int val; /* Skip any leading whitespace */ do { ch = pbm_getc(infile); if (ch == EOF) ERREXIT(cinfo, JERR_INPUT_EOF); } while (ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r'); if (ch < '0' || ch > '9') ERREXIT(cinfo, JERR_PPM_NONNUMERIC); val = ch - '0'; while ((ch = pbm_getc(infile)) >= '0' && ch <= '9') { val *= 10; val += ch - '0'; } if (val > maxval) ERREXIT(cinfo, JERR_PPM_TOOLARGE); return val; }
LOCAL void read_ppm_map (j_decompress_ptr cinfo, FILE * infile) { int c; unsigned int w, h, maxval, row, col; int R, G, B; /* Initial 'P' has already been read by read_color_map */ c = getc(infile); /* save format discriminator for a sec */ /* while we fetch the remaining header info */ w = read_pbm_integer(cinfo, infile); h = read_pbm_integer(cinfo, infile); maxval = read_pbm_integer(cinfo, infile); if (w <= 0 || h <= 0 || maxval <= 0) /* error check */ ERREXIT(cinfo, JERR_BAD_CMAP_FILE); /* For now, we don't support rescaling from an unusual maxval. */ if (maxval != (unsigned int) MAXJSAMPLE) ERREXIT(cinfo, JERR_BAD_CMAP_FILE); switch (c) { case '3': /* it's a text-format PPM file */ for (row = 0; row < h; row++) { for (col = 0; col < w; col++) { R = read_pbm_integer(cinfo, infile); G = read_pbm_integer(cinfo, infile); B = read_pbm_integer(cinfo, infile); add_map_entry(cinfo, R, G, B); } } break; case '6': /* it's a raw-format PPM file */ for (row = 0; row < h; row++) { for (col = 0; col < w; col++) { R = pbm_getc(infile); G = pbm_getc(infile); B = pbm_getc(infile); if (R == EOF || G == EOF || B == EOF) ERREXIT(cinfo, JERR_BAD_CMAP_FILE); add_map_entry(cinfo, R, G, B); } } break; default: ERREXIT(cinfo, JERR_BAD_CMAP_FILE); break; } }
unsigned int read_pbm_integer (FILE * infile) /* Read an unsigned decimal integer from the PPM file */ /* Swallows one trailing character after the integer */ { char ch; int val; /* Skip any leading whitespace */ do { ch=pbm_getc(infile); } while (ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r'); val=(ch - '0'); while(((ch=pbm_getc(infile))!=' ')&&(ch != '\t') && (ch != '\n') && (ch != '\r')/*&&(feof(infile)==0)*/) { val*=10; val+=ch - '0'; } return val; }
read_ppm_map (j_decompress_ptr cinfo, FILE * infile) { int c; __boundcheck_metadata_store((void *)(&c),(void *)((size_t)(&c)+sizeof(c)*8-1)); unsigned int w; __boundcheck_metadata_store((void *)(&w),(void *)((size_t)(&w)+sizeof(w)*8-1)); unsigned int h; __boundcheck_metadata_store((void *)(&h),(void *)((size_t)(&h)+sizeof(h)*8-1)); unsigned int maxval; __boundcheck_metadata_store((void *)(&maxval),(void *)((size_t)(&maxval)+sizeof(maxval)*8-1)); unsigned int row; __boundcheck_metadata_store((void *)(&row),(void *)((size_t)(&row)+sizeof(row)*8-1)); unsigned int col; __boundcheck_metadata_store((void *)(&col),(void *)((size_t)(&col)+sizeof(col)*8-1)); int R; __boundcheck_metadata_store((void *)(&R),(void *)((size_t)(&R)+sizeof(R)*8-1)); int G; __boundcheck_metadata_store((void *)(&G),(void *)((size_t)(&G)+sizeof(G)*8-1)); int B; __boundcheck_metadata_store((void *)(&B),(void *)((size_t)(&B)+sizeof(B)*8-1)); /* Initial 'P' has already been read by read_color_map */ c = getc(infile); /* save format discriminator for a sec */ /* while we fetch the remaining header info */ w = read_pbm_integer(cinfo, infile); h = read_pbm_integer(cinfo, infile); maxval = read_pbm_integer(cinfo, infile); if (w <= 0 || h <= 0 || maxval <= 0) /* error check */ ERREXIT(cinfo, JERR_BAD_CMAP_FILE); /* For now, we don't support rescaling from an unusual maxval. */ if (maxval != (unsigned int) MAXJSAMPLE) ERREXIT(cinfo, JERR_BAD_CMAP_FILE); switch (c) { case '3': /* it's a text-format PPM file */ for (row = 0; row < h; row++) { for (col = 0; col < w; col++) { R = read_pbm_integer(cinfo, infile); G = read_pbm_integer(cinfo, infile); B = read_pbm_integer(cinfo, infile); add_map_entry(cinfo, R, G, B); } } break; case '6': /* it's a raw-format PPM file */ for (row = 0; row < h; row++) { for (col = 0; col < w; col++) { R = pbm_getc(infile); G = pbm_getc(infile); B = pbm_getc(infile); if (R == EOF || G == EOF || B == EOF) ERREXIT(cinfo, JERR_BAD_CMAP_FILE); add_map_entry(cinfo, R, G, B); } } break; default: ERREXIT(cinfo, JERR_BAD_CMAP_FILE); break; } }