Hash :
2cbeb8ab
Author :
Date :
1991-10-07T00:00:00
The Independent JPEG Group's JPEG software v1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
/*
* jrdppm.c
*
* Copyright (C) 1991, Thomas G. Lane.
* This file is part of the Independent JPEG Group's software.
* For conditions of distribution and use, see the accompanying README file.
*
* This file contains routines to read input images in PPM format.
* The PBMPLUS library is required (well, it will be in the real version).
*
* These routines may need modification for non-Unix environments or
* specialized applications. As they stand, they assume input from
* an ordinary stdio stream. They further assume that reading begins
* at the start of the file; input_init may need work if the
* user interface has already read some data (e.g., to determine that
* the file is indeed PPM format).
*
* These routines are invoked via the methods get_input_row
* and input_init/term.
*/
#include "jinclude.h"
#ifdef PPM_SUPPORTED
/*
* Read the file header; return image size and component count.
*/
METHODDEF void
input_init (compress_info_ptr cinfo)
{
int c, w, h, prec;
if (getc(cinfo->input_file) != 'P')
ERREXIT(cinfo->emethods, "Not a PPM file");
c = getc(cinfo->input_file);
switch (c) {
case '5': /* it's a PGM file */
cinfo->input_components = 1;
cinfo->in_color_space = CS_GRAYSCALE;
break;
case '6': /* it's a PPM file */
cinfo->input_components = 3;
cinfo->in_color_space = CS_RGB;
break;
default:
ERREXIT(cinfo->emethods, "Not a PPM file");
break;
}
if (fscanf(cinfo->input_file, " %d %d %d", &w, &h, &prec) != 3)
ERREXIT(cinfo->emethods, "Not a PPM file");
if (getc(cinfo->input_file) != '\n' || w <= 0 || h <= 0 || prec != 255)
ERREXIT(cinfo->emethods, "Not a PPM file");
cinfo->image_width = w;
cinfo->image_height = h;
cinfo->data_precision = 8;
}
/*
* Read one row of pixels.
*/
METHODDEF void
get_input_row (compress_info_ptr cinfo, JSAMPARRAY pixel_row)
{
register FILE * infile = cinfo->input_file;
register JSAMPROW ptr0, ptr1, ptr2;
register long col;
if (cinfo->input_components == 1) {
ptr0 = pixel_row[0];
for (col = cinfo->image_width; col > 0; col--) {
*ptr0++ = getc(infile);
}
} else {
ptr0 = pixel_row[0];
ptr1 = pixel_row[1];
ptr2 = pixel_row[2];
for (col = cinfo->image_width; col > 0; col--) {
*ptr0++ = getc(infile);
*ptr1++ = getc(infile);
*ptr2++ = getc(infile);
}
}
}
/*
* Finish up at the end of the file.
*/
METHODDEF void
input_term (compress_info_ptr cinfo)
{
/* no work required */
}
/*
* The method selection routine for PPM format input.
* Note that this must be called by the user interface before calling
* jpeg_compress. If multiple input formats are supported, the
* user interface is responsible for discovering the file format and
* calling the appropriate method selection routine.
*/
GLOBAL void
jselrppm (compress_info_ptr cinfo)
{
cinfo->methods->input_init = input_init;
cinfo->methods->get_input_row = get_input_row;
cinfo->methods->input_term = input_term;
}
#endif /* PPM_SUPPORTED */