|
Packit |
78deda |
/* ppmtopgm.c - convert a portable pixmap to a portable graymap
|
|
Packit |
78deda |
**
|
|
Packit |
78deda |
** Copyright (C) 1989 by Jef Poskanzer.
|
|
Packit |
78deda |
**
|
|
Packit |
78deda |
** Permission to use, copy, modify, and distribute this software and its
|
|
Packit |
78deda |
** documentation for any purpose and without fee is hereby granted, provided
|
|
Packit |
78deda |
** that the above copyright notice appear in all copies and that both that
|
|
Packit |
78deda |
** copyright notice and this permission notice appear in supporting
|
|
Packit |
78deda |
** documentation. This software is provided "as is" without express or
|
|
Packit |
78deda |
** implied warranty.
|
|
Packit |
78deda |
*/
|
|
Packit |
78deda |
|
|
Packit |
78deda |
#include "pm_c_util.h"
|
|
Packit |
78deda |
#include "ppm.h"
|
|
Packit |
78deda |
#include "pgm.h"
|
|
Packit |
78deda |
#include "lum.h"
|
|
Packit |
78deda |
|
|
Packit |
78deda |
static void
|
|
Packit |
78deda |
convertRaster(FILE * const ifP,
|
|
Packit |
78deda |
unsigned int const cols,
|
|
Packit |
78deda |
unsigned int const rows,
|
|
Packit |
78deda |
pixval const maxval,
|
|
Packit |
78deda |
int const format,
|
|
Packit |
78deda |
pixel * const inputRow,
|
|
Packit |
78deda |
gray * const outputRow,
|
|
Packit |
78deda |
FILE * const ofP) {
|
|
Packit |
78deda |
|
|
Packit |
78deda |
unsigned int row;
|
|
Packit |
78deda |
|
|
Packit |
78deda |
for (row = 0; row < rows; ++row) {
|
|
Packit |
78deda |
ppm_readppmrow( ifP, inputRow, cols, maxval, format );
|
|
Packit |
78deda |
if (maxval <= 255) {
|
|
Packit |
78deda |
/* Use fast approximation to 0.299 r + 0.587 g + 0.114 b */
|
|
Packit |
78deda |
unsigned int col;
|
|
Packit |
78deda |
for (col = 0; col < cols; ++col)
|
|
Packit |
78deda |
outputRow[col] = (gray) ppm_fastlumin(inputRow[col]);
|
|
Packit |
78deda |
} else {
|
|
Packit |
78deda |
/* Can't use fast approximation, so fall back on floats. */
|
|
Packit |
78deda |
unsigned int col;
|
|
Packit |
78deda |
for (col = 0; col < cols; ++col)
|
|
Packit |
78deda |
outputRow[col] = ppm_luminosity(inputRow[col]);
|
|
Packit |
78deda |
}
|
|
Packit |
78deda |
pgm_writepgmrow(ofP, outputRow, cols, maxval, 0);
|
|
Packit |
78deda |
}
|
|
Packit |
78deda |
}
|
|
Packit |
78deda |
|
|
Packit |
78deda |
|
|
Packit |
78deda |
|
|
Packit |
78deda |
int
|
|
Packit |
78deda |
main(int argc, char *argv[]) {
|
|
Packit |
78deda |
|
|
Packit |
78deda |
FILE* ifP;
|
|
Packit |
78deda |
const char * inputFilespec;
|
|
Packit |
78deda |
int eof;
|
|
Packit |
78deda |
|
|
Packit |
78deda |
ppm_init( &argc, argv );
|
|
Packit |
78deda |
|
|
Packit |
78deda |
if (argc-1 > 1)
|
|
Packit |
78deda |
pm_error("The only argument is the (optional) input filename");
|
|
Packit |
78deda |
|
|
Packit |
78deda |
if (argc == 2)
|
|
Packit |
78deda |
inputFilespec = argv[1];
|
|
Packit |
78deda |
else
|
|
Packit |
78deda |
inputFilespec = "-";
|
|
Packit |
78deda |
|
|
Packit |
78deda |
ifP = pm_openr(inputFilespec);
|
|
Packit |
78deda |
|
|
Packit |
78deda |
eof = FALSE; /* initial assumption */
|
|
Packit |
78deda |
|
|
Packit |
78deda |
while (!eof) {
|
|
Packit |
78deda |
ppm_nextimage(ifP, &eof;;
|
|
Packit |
78deda |
if (!eof) {
|
|
Packit |
78deda |
int rows, cols, format;
|
|
Packit |
78deda |
pixval maxval;
|
|
Packit |
78deda |
pixel* inputRow;
|
|
Packit |
78deda |
gray* outputRow;
|
|
Packit |
78deda |
|
|
Packit |
78deda |
ppm_readppminit(ifP, &cols, &rows, &maxval, &format);
|
|
Packit |
78deda |
pgm_writepgminit(stdout, cols, rows, maxval, 0);
|
|
Packit |
78deda |
|
|
Packit |
78deda |
inputRow = ppm_allocrow(cols);
|
|
Packit |
78deda |
outputRow = pgm_allocrow(cols);
|
|
Packit |
78deda |
|
|
Packit |
78deda |
convertRaster(ifP, cols, rows, maxval, format,
|
|
Packit |
78deda |
inputRow, outputRow, stdout);
|
|
Packit |
78deda |
|
|
Packit |
78deda |
ppm_freerow(inputRow);
|
|
Packit |
78deda |
pgm_freerow(outputRow);
|
|
Packit |
78deda |
}
|
|
Packit |
78deda |
}
|
|
Packit |
78deda |
pm_close(ifP);
|
|
Packit |
78deda |
pm_close(stdout);
|
|
Packit |
78deda |
|
|
Packit |
78deda |
return 0;
|
|
Packit |
78deda |
}
|