|
Packit |
78deda |
/*
|
|
Packit |
78deda |
* This is derived from the file of the same name dated June 5, 1995,
|
|
Packit |
78deda |
* copied from the Army High Performance Computing Research Center's
|
|
Packit |
78deda |
* media-tools.tar.gz package, received from
|
|
Packit |
78deda |
* http://www.arc.umn.edu/gvl-software/media-tools.tar.gz on 2000.04.13.
|
|
Packit |
78deda |
*
|
|
Packit |
78deda |
* This software is copyrighted as noted below. It may be freely copied,
|
|
Packit |
78deda |
* modified, and redistributed, provided that the copyright notice is
|
|
Packit |
78deda |
* preserved on all copies.
|
|
Packit |
78deda |
*
|
|
Packit |
78deda |
* There is no warranty or other guarantee of fitness for this software,
|
|
Packit |
78deda |
* it is provided solely "as is". Bug reports or fixes may be sent
|
|
Packit |
78deda |
* to the author, who may or may not act on them as he desires.
|
|
Packit |
78deda |
*
|
|
Packit |
78deda |
* You may not include this software in a program or other software product
|
|
Packit |
78deda |
* without supplying the source, or without informing the end-user that the
|
|
Packit |
78deda |
* source is available for no extra charge.
|
|
Packit |
78deda |
*
|
|
Packit |
78deda |
* If you modify this software, you should include a notice giving the
|
|
Packit |
78deda |
* name of the person performing the modification, the date of modification,
|
|
Packit |
78deda |
* and the reason for such modification.
|
|
Packit |
78deda |
*/
|
|
Packit |
78deda |
/*
|
|
Packit |
78deda |
* rletopnm - A conversion program to convert from Utah's "rle" image format
|
|
Packit |
78deda |
* to pbmplus ppm or pgm image formats.
|
|
Packit |
78deda |
*
|
|
Packit |
78deda |
* Author: Wes Barris (wes@msc.edu)
|
|
Packit |
78deda |
* AHPCRC
|
|
Packit |
78deda |
* Minnesota Supercomputer Center, Inc.
|
|
Packit |
78deda |
* Date: March 30, 1994
|
|
Packit |
78deda |
* Copyright (c) Minnesota Supercomputer Center 1994
|
|
Packit |
78deda |
*
|
|
Packit |
78deda |
* 2000.04.13 adapted for Netpbm by Bryan Henderson. Quieted compiler
|
|
Packit |
78deda |
* warnings. Added --alpha option. Accept input on stdin
|
|
Packit |
78deda |
*
|
|
Packit |
78deda |
*/
|
|
Packit |
78deda |
|
|
Packit |
78deda |
#define _DEFAULT_SOURCE 1 /* New name for SVID & BSD source defines */
|
|
Packit |
78deda |
#define _BSD_SOURCE 1 /* Make sure strdup() is in string.h */
|
|
Packit |
78deda |
#define _XOPEN_SOURCE 500 /* Make sure strdup() is in string.h */
|
|
Packit |
78deda |
|
|
Packit |
78deda |
/*-----------------------------------------------------------------------------
|
|
Packit |
78deda |
* System includes.
|
|
Packit |
78deda |
*/
|
|
Packit |
78deda |
#include <string.h>
|
|
Packit |
78deda |
#include <stdio.h>
|
|
Packit |
78deda |
#define NO_DECLARE_MALLOC
|
|
Packit |
78deda |
#include <rle.h>
|
|
Packit |
78deda |
|
|
Packit |
78deda |
#include "pm_c_util.h"
|
|
Packit |
78deda |
#include "pnm.h"
|
|
Packit |
78deda |
#include "shhopt.h"
|
|
Packit |
78deda |
#include "mallocvar.h"
|
|
Packit |
78deda |
#include "nstring.h"
|
|
Packit |
78deda |
|
|
Packit |
78deda |
#define HMSG if (headerDump) pm_message
|
|
Packit |
78deda |
#define GRAYSCALE 001 /* 8 bits, no colormap */
|
|
Packit |
78deda |
#define PSEUDOCOLOR 010 /* 8 bits, colormap */
|
|
Packit |
78deda |
#define TRUECOLOR 011 /* 24 bits, colormap */
|
|
Packit |
78deda |
#define DIRECTCOLOR 100 /* 24 bits, no colormap */
|
|
Packit |
78deda |
#define RLE_MAXVAL 255
|
|
Packit |
78deda |
/*
|
|
Packit |
78deda |
* Utah type declarations.
|
|
Packit |
78deda |
*/
|
|
Packit |
78deda |
static rle_hdr hdr;
|
|
Packit |
78deda |
static rle_map * colormap;
|
|
Packit |
78deda |
|
|
Packit |
78deda |
|
|
Packit |
78deda |
struct CmdlineInfo {
|
|
Packit |
78deda |
/* All the information the user supplied in the command line,
|
|
Packit |
78deda |
in a form easy for the program to use.
|
|
Packit |
78deda |
*/
|
|
Packit |
78deda |
char * inputFilename;
|
|
Packit |
78deda |
unsigned int headerdump;
|
|
Packit |
78deda |
unsigned int verbose;
|
|
Packit |
78deda |
char * alphaout;
|
|
Packit |
78deda |
bool alphaStdout;
|
|
Packit |
78deda |
};
|
|
Packit |
78deda |
|
|
Packit |
78deda |
|
|
Packit |
78deda |
|
|
Packit |
78deda |
/*
|
|
Packit |
78deda |
* Other declarations.
|
|
Packit |
78deda |
*/
|
|
Packit |
78deda |
static int visual, maplen;
|
|
Packit |
78deda |
static int width, height;
|
|
Packit |
78deda |
|
|
Packit |
78deda |
|
|
Packit |
78deda |
|
|
Packit |
78deda |
static void
|
|
Packit |
78deda |
parseCommandLine(int argc, char ** argv,
|
|
Packit |
78deda |
struct CmdlineInfo * const cmdlineP) {
|
|
Packit |
78deda |
/*----------------------------------------------------------------------------
|
|
Packit |
78deda |
Note that many of the strings that this function returns in the
|
|
Packit |
78deda |
*cmdlineP structure are actually in the supplied argv array. And
|
|
Packit |
78deda |
sometimes, one of these strings is actually just a suffix of an entry
|
|
Packit |
78deda |
in argv!
|
|
Packit |
78deda |
-----------------------------------------------------------------------------*/
|
|
Packit |
78deda |
optEntry * option_def; /* malloc'ed */
|
|
Packit |
78deda |
optStruct3 opt;
|
|
Packit |
78deda |
unsigned int option_def_index;
|
|
Packit |
78deda |
|
|
Packit |
78deda |
unsigned int alphaoutSpec;
|
|
Packit |
78deda |
|
|
Packit |
78deda |
MALLOCARRAY(option_def, 100);
|
|
Packit |
78deda |
|
|
Packit |
78deda |
option_def_index = 0; /* incremented by OPTENT3 */
|
|
Packit |
78deda |
OPTENT3('h', "headerdump", OPT_FLAG,
|
|
Packit |
78deda |
NULL, &cmdlineP->headerdump, 0);
|
|
Packit |
78deda |
OPTENT3('v', "verbose", OPT_FLAG,
|
|
Packit |
78deda |
NULL, &cmdlineP->verbose, 0);
|
|
Packit |
78deda |
OPTENT3(0, "alphaout", OPT_STRING,
|
|
Packit |
78deda |
&cmdlineP->alphaout, &alphaoutSpec, 0);
|
|
Packit |
78deda |
|
|
Packit |
78deda |
opt.opt_table = option_def;
|
|
Packit |
78deda |
opt.short_allowed = TRUE; /* We have short (old-fashioned) options */
|
|
Packit |
78deda |
opt.allowNegNum = FALSE; /* We have no parms that are negative numbers */
|
|
Packit |
78deda |
|
|
Packit |
78deda |
pm_optParseOptions3(&argc, argv, opt, sizeof(opt), 0);
|
|
Packit |
78deda |
/* Uses and sets argc, argv, and all of *cmdlineP. */
|
|
Packit |
78deda |
|
|
Packit |
78deda |
if (!alphaoutSpec)
|
|
Packit |
78deda |
cmdlineP->alphaout = NULL;
|
|
Packit |
78deda |
|
|
Packit |
78deda |
if (argc - 1 == 0)
|
|
Packit |
78deda |
cmdlineP->inputFilename = NULL; /* he wants stdin */
|
|
Packit |
78deda |
else if (argc - 1 == 1) {
|
|
Packit |
78deda |
if (streq(argv[1], "-"))
|
|
Packit |
78deda |
cmdlineP->inputFilename = NULL; /* he wants stdin */
|
|
Packit |
78deda |
else
|
|
Packit |
78deda |
cmdlineP->inputFilename = strdup(argv[1]);
|
|
Packit |
78deda |
} else
|
|
Packit |
78deda |
pm_error("Too many arguments. The only argument accepted "
|
|
Packit |
78deda |
"is the input file specification");
|
|
Packit |
78deda |
|
|
Packit |
78deda |
if (cmdlineP->alphaout &&
|
|
Packit |
78deda |
streq(cmdlineP->alphaout, "-"))
|
|
Packit |
78deda |
cmdlineP->alphaStdout = TRUE;
|
|
Packit |
78deda |
else
|
|
Packit |
78deda |
cmdlineP->alphaStdout = FALSE;
|
|
Packit |
78deda |
}
|
|
Packit |
78deda |
|
|
Packit |
78deda |
|
|
Packit |
78deda |
|
|
Packit |
78deda |
static void
|
|
Packit |
78deda |
reportRleGetSetupError(int const rleGetSetupRc) {
|
|
Packit |
78deda |
|
|
Packit |
78deda |
switch (rleGetSetupRc) {
|
|
Packit |
78deda |
case -1:
|
|
Packit |
78deda |
pm_error("According to the URT library, the input is not "
|
|
Packit |
78deda |
"an RLE file. rle_get_setup() failed.");
|
|
Packit |
78deda |
break;
|
|
Packit |
78deda |
case -2:
|
|
Packit |
78deda |
pm_error("Unable to get memory for the color map. "
|
|
Packit |
78deda |
"rle_get_setup() failed.");
|
|
Packit |
78deda |
break;
|
|
Packit |
78deda |
case -3:
|
|
Packit |
78deda |
pm_error("Input file is empty. rle_get_setup() failed.");
|
|
Packit |
78deda |
break;
|
|
Packit |
78deda |
case -4:
|
|
Packit |
78deda |
pm_error("End of file in the middle of where the RLE header should "
|
|
Packit |
78deda |
"be. rle_get_setup() failed.");
|
|
Packit |
78deda |
break;
|
|
Packit |
78deda |
default:
|
|
Packit |
78deda |
pm_error("rle_get_setup() failed for an unknown reason");
|
|
Packit |
78deda |
}
|
|
Packit |
78deda |
}
|
|
Packit |
78deda |
|
|
Packit |
78deda |
|
|
Packit |
78deda |
|
|
Packit |
78deda |
static void
|
|
Packit |
78deda |
readRleHeader(FILE * const ifP,
|
|
Packit |
78deda |
bool const headerDump) {
|
|
Packit |
78deda |
|
|
Packit |
78deda |
int rc;
|
|
Packit |
78deda |
int i;
|
|
Packit |
78deda |
hdr.rle_file = ifP;
|
|
Packit |
78deda |
rc = rle_get_setup(&hdr);
|
|
Packit |
78deda |
if (rc != 0)
|
|
Packit |
78deda |
reportRleGetSetupError(rc);
|
|
Packit |
78deda |
|
|
Packit |
78deda |
width = hdr.xmax - hdr.xmin + 1;
|
|
Packit |
78deda |
height = hdr.ymax - hdr.ymin + 1;
|
|
Packit |
78deda |
HMSG("Image size: %dx%d", width, height);
|
|
Packit |
78deda |
if (hdr.ncolors == 1 && hdr.ncmap == 3) {
|
|
Packit |
78deda |
visual = PSEUDOCOLOR;
|
|
Packit |
78deda |
colormap = hdr.cmap;
|
|
Packit |
78deda |
maplen = (1 << hdr.cmaplen);
|
|
Packit |
78deda |
HMSG("Mapped color image with a map of length %d.",
|
|
Packit |
78deda |
maplen);
|
|
Packit |
78deda |
}
|
|
Packit |
78deda |
else if (hdr.ncolors == 3 && hdr.ncmap == 0) {
|
|
Packit |
78deda |
visual = DIRECTCOLOR;
|
|
Packit |
78deda |
HMSG("24 bit color image, no colormap.");
|
|
Packit |
78deda |
}
|
|
Packit |
78deda |
else if (hdr.ncolors == 3 && hdr.ncmap == 3) {
|
|
Packit |
78deda |
visual = TRUECOLOR;
|
|
Packit |
78deda |
colormap = hdr.cmap;
|
|
Packit |
78deda |
maplen = (1 << hdr.cmaplen);
|
|
Packit |
78deda |
HMSG(
|
|
Packit |
78deda |
"24 bit color image with color map of length %d" ,maplen);
|
|
Packit |
78deda |
}
|
|
Packit |
78deda |
else if (hdr.ncolors == 1 && hdr.ncmap == 0) {
|
|
Packit |
78deda |
visual = GRAYSCALE;
|
|
Packit |
78deda |
HMSG("Grayscale image.");
|
|
Packit |
78deda |
}
|
|
Packit |
78deda |
else {
|
|
Packit |
78deda |
fprintf(stderr,
|
|
Packit |
78deda |
"ncolors = %d, ncmap = %d, I don't know how to handle this!",
|
|
Packit |
78deda |
hdr.ncolors, hdr.ncmap);
|
|
Packit |
78deda |
exit(-1);
|
|
Packit |
78deda |
}
|
|
Packit |
78deda |
if (hdr.alpha == 0) {
|
|
Packit |
78deda |
HMSG("No alpha channel.");
|
|
Packit |
78deda |
} else if (hdr.alpha == 1) {
|
|
Packit |
78deda |
HMSG("Alpha channel exists!");
|
|
Packit |
78deda |
} else {
|
|
Packit |
78deda |
fprintf(stderr, "alpha = %d, I don't know how to handle this!\n",
|
|
Packit |
78deda |
hdr.alpha);
|
|
Packit |
78deda |
exit(-1);
|
|
Packit |
78deda |
}
|
|
Packit |
78deda |
switch (hdr.background) {
|
|
Packit |
78deda |
case 0:
|
|
Packit |
78deda |
HMSG("Use all pixels, ignore background color.");
|
|
Packit |
78deda |
break;
|
|
Packit |
78deda |
case 1:
|
|
Packit |
78deda |
HMSG("Use only non-background pixels, ignore background color.");
|
|
Packit |
78deda |
break;
|
|
Packit |
78deda |
case 2:
|
|
Packit |
78deda |
HMSG("Use only non-background pixels, "
|
|
Packit |
78deda |
"clear to background color (default).");
|
|
Packit |
78deda |
break;
|
|
Packit |
78deda |
default:
|
|
Packit |
78deda |
HMSG("Unknown background flag!");
|
|
Packit |
78deda |
break;
|
|
Packit |
78deda |
}
|
|
Packit |
78deda |
if (hdr.background == 2)
|
|
Packit |
78deda |
for (i = 0; i < hdr.ncolors; i++)
|
|
Packit |
78deda |
HMSG(" %d", hdr.bg_color[i]);
|
|
Packit |
78deda |
if (hdr.ncolors == 1 && hdr.ncmap == 3) {
|
|
Packit |
78deda |
HMSG(" (%d %d %d)",
|
|
Packit |
78deda |
hdr.cmap[hdr.bg_color[0]]>>8,
|
|
Packit |
78deda |
hdr.cmap[hdr.bg_color[0]+256]>>8,
|
|
Packit |
78deda |
hdr.cmap[hdr.bg_color[0]+512]>>8);
|
|
Packit |
78deda |
}
|
|
Packit |
78deda |
else if (hdr.ncolors == 3 && hdr.ncmap == 3) {
|
|
Packit |
78deda |
HMSG(" (%d %d %d)",
|
|
Packit |
78deda |
hdr.cmap[hdr.bg_color[0]]>>8,
|
|
Packit |
78deda |
hdr.cmap[hdr.bg_color[1]+256]>>8,
|
|
Packit |
78deda |
hdr.cmap[hdr.bg_color[2]+512]>>8);
|
|
Packit |
78deda |
}
|
|
Packit |
78deda |
if (hdr.comments)
|
|
Packit |
78deda |
for (i = 0; hdr.comments[i] != NULL; i++)
|
|
Packit |
78deda |
HMSG("%s", hdr.comments[i]);
|
|
Packit |
78deda |
}
|
|
Packit |
78deda |
|
|
Packit |
78deda |
|
|
Packit |
78deda |
|
|
Packit |
78deda |
static void
|
|
Packit |
78deda |
writePpmRaster(FILE * const imageoutFileP,
|
|
Packit |
78deda |
FILE * const alphaFileP) {
|
|
Packit |
78deda |
|
|
Packit |
78deda |
rle_pixel ***scanlines, **scanline;
|
|
Packit |
78deda |
pixval r, g, b;
|
|
Packit |
78deda |
pixel *pixelrow;
|
|
Packit |
78deda |
gray *alpharow;
|
|
Packit |
78deda |
|
|
Packit |
78deda |
int scan;
|
|
Packit |
78deda |
int x;
|
|
Packit |
78deda |
/*
|
|
Packit |
78deda |
* Allocate some stuff.
|
|
Packit |
78deda |
*/
|
|
Packit |
78deda |
pixelrow = ppm_allocrow(width);
|
|
Packit |
78deda |
alpharow = pgm_allocrow(width);
|
|
Packit |
78deda |
|
|
Packit |
78deda |
MALLOCARRAY(scanlines, height);
|
|
Packit |
78deda |
RLE_CHECK_ALLOC( hdr.cmd, scanlines, "scanline pointers" );
|
|
Packit |
78deda |
|
|
Packit |
78deda |
for ( scan = 0; scan < height; scan++ )
|
|
Packit |
78deda |
RLE_CHECK_ALLOC( hdr.cmd, (rle_row_alloc(&hdr, &scanlines[scan]) >= 0),
|
|
Packit |
78deda |
"pixel memory" );
|
|
Packit |
78deda |
/*
|
|
Packit |
78deda |
* Loop through those scan lines.
|
|
Packit |
78deda |
*/
|
|
Packit |
78deda |
for (scan = 0; scan < height; ++scan)
|
|
Packit |
78deda |
rle_getrow(&hdr, scanlines[height - scan - 1]);
|
|
Packit |
78deda |
for (scan = 0; scan < height; ++scan) {
|
|
Packit |
78deda |
scanline = scanlines[scan];
|
|
Packit |
78deda |
switch (visual) {
|
|
Packit |
78deda |
case GRAYSCALE: /* 8 bits without colormap */
|
|
Packit |
78deda |
for (x = 0; x < width; x++) {
|
|
Packit |
78deda |
r = scanline[0][x];
|
|
Packit |
78deda |
g = scanline[0][x];
|
|
Packit |
78deda |
b = scanline[0][x];
|
|
Packit |
78deda |
PPM_ASSIGN(pixelrow[x], r, g, b);
|
|
Packit |
78deda |
if (hdr.alpha)
|
|
Packit |
78deda |
alpharow[x] = scanline[-1][x];
|
|
Packit |
78deda |
else
|
|
Packit |
78deda |
alpharow[x] = 0;
|
|
Packit |
78deda |
}
|
|
Packit |
78deda |
break;
|
|
Packit |
78deda |
case TRUECOLOR: /* 24 bits with colormap */
|
|
Packit |
78deda |
for (x = 0; x < width; x++) {
|
|
Packit |
78deda |
r = colormap[scanline[0][x]]>>8;
|
|
Packit |
78deda |
g = colormap[scanline[1][x]+256]>>8;
|
|
Packit |
78deda |
b = colormap[scanline[2][x]+512]>>8;
|
|
Packit |
78deda |
PPM_ASSIGN(pixelrow[x], r, g, b);
|
|
Packit |
78deda |
if (hdr.alpha)
|
|
Packit |
78deda |
alpharow[x] = colormap[scanline[-1][x]];
|
|
Packit |
78deda |
else
|
|
Packit |
78deda |
alpharow[x] = 0;
|
|
Packit |
78deda |
}
|
|
Packit |
78deda |
break;
|
|
Packit |
78deda |
case DIRECTCOLOR: /* 24 bits without colormap */
|
|
Packit |
78deda |
for (x = 0; x < width; x++) {
|
|
Packit |
78deda |
r = scanline[0][x];
|
|
Packit |
78deda |
g = scanline[1][x];
|
|
Packit |
78deda |
b = scanline[2][x];
|
|
Packit |
78deda |
PPM_ASSIGN(pixelrow[x], r, g, b);
|
|
Packit |
78deda |
if (hdr.alpha)
|
|
Packit |
78deda |
alpharow[x] = scanline[-1][x];
|
|
Packit |
78deda |
else
|
|
Packit |
78deda |
alpharow[x] = 0;
|
|
Packit |
78deda |
}
|
|
Packit |
78deda |
break;
|
|
Packit |
78deda |
case PSEUDOCOLOR: /* 8 bits with colormap */
|
|
Packit |
78deda |
for (x = 0; x < width; x++) {
|
|
Packit |
78deda |
r = colormap[scanline[0][x]]>>8;
|
|
Packit |
78deda |
g = colormap[scanline[0][x]+256]>>8;
|
|
Packit |
78deda |
b = colormap[scanline[0][x]+512]>>8;
|
|
Packit |
78deda |
PPM_ASSIGN(pixelrow[x], r, g, b);
|
|
Packit |
78deda |
if (hdr.alpha)
|
|
Packit |
78deda |
alpharow[x] = colormap[scanline[-1][x]];
|
|
Packit |
78deda |
else
|
|
Packit |
78deda |
alpharow[x] = 0;
|
|
Packit |
78deda |
}
|
|
Packit |
78deda |
break;
|
|
Packit |
78deda |
default:
|
|
Packit |
78deda |
break;
|
|
Packit |
78deda |
}
|
|
Packit |
78deda |
/*
|
|
Packit |
78deda |
* Write the scan line.
|
|
Packit |
78deda |
*/
|
|
Packit |
78deda |
if (imageoutFileP)
|
|
Packit |
78deda |
ppm_writeppmrow(imageoutFileP, pixelrow, width, RLE_MAXVAL, 0);
|
|
Packit |
78deda |
if (alphaFileP)
|
|
Packit |
78deda |
pgm_writepgmrow(alphaFileP, alpharow, width, RLE_MAXVAL, 0);
|
|
Packit |
78deda |
|
|
Packit |
78deda |
} /* end of for scan = 0 to height */
|
|
Packit |
78deda |
|
|
Packit |
78deda |
/* Free scanline memory. */
|
|
Packit |
78deda |
for (scan = 0; scan < height; ++scan)
|
|
Packit |
78deda |
rle_row_free(&hdr, scanlines[scan]);
|
|
Packit |
78deda |
free (scanlines);
|
|
Packit |
78deda |
ppm_freerow(pixelrow);
|
|
Packit |
78deda |
pgm_freerow(alpharow);
|
|
Packit |
78deda |
}
|
|
Packit |
78deda |
|
|
Packit |
78deda |
|
|
Packit |
78deda |
|
|
Packit |
78deda |
static void
|
|
Packit |
78deda |
writePgmRaster(FILE * const imageoutFileP,
|
|
Packit |
78deda |
FILE * const alphaFileP) {
|
|
Packit |
78deda |
/*----------------------------------------------------------------------------
|
|
Packit |
78deda |
Write the PGM image data
|
|
Packit |
78deda |
-----------------------------------------------------------------------------*/
|
|
Packit |
78deda |
rle_pixel ***scanlines, **scanline;
|
|
Packit |
78deda |
gray * pixelrow;
|
|
Packit |
78deda |
gray * alpharow;
|
|
Packit |
78deda |
int scan;
|
|
Packit |
78deda |
/*
|
|
Packit |
78deda |
* Allocate some stuff.
|
|
Packit |
78deda |
*/
|
|
Packit |
78deda |
pixelrow = pgm_allocrow(width);
|
|
Packit |
78deda |
alpharow = pgm_allocrow(width);
|
|
Packit |
78deda |
|
|
Packit |
78deda |
MALLOCARRAY(scanlines, height);
|
|
Packit |
78deda |
RLE_CHECK_ALLOC( hdr.cmd, scanlines, "scanline pointers" );
|
|
Packit |
78deda |
|
|
Packit |
78deda |
for (scan = 0; scan < height; ++scan)
|
|
Packit |
78deda |
RLE_CHECK_ALLOC(hdr.cmd, (rle_row_alloc(&hdr, &scanlines[scan]) >= 0),
|
|
Packit |
78deda |
"pixel memory" );
|
|
Packit |
78deda |
/*
|
|
Packit |
78deda |
* Loop through those scan lines.
|
|
Packit |
78deda |
*/
|
|
Packit |
78deda |
for (scan = 0; scan < height; ++scan)
|
|
Packit |
78deda |
rle_getrow(&hdr, scanlines[height - scan - 1]);
|
|
Packit |
78deda |
|
|
Packit |
78deda |
for (scan = 0; scan < height; ++scan) {
|
|
Packit |
78deda |
int x;
|
|
Packit |
78deda |
scanline = scanlines[scan];
|
|
Packit |
78deda |
for (x = 0; x < width; ++x) {
|
|
Packit |
78deda |
pixelrow[x] = scanline[0][x];
|
|
Packit |
78deda |
if (hdr.alpha)
|
|
Packit |
78deda |
alpharow[x] = scanline[1][x];
|
|
Packit |
78deda |
else
|
|
Packit |
78deda |
alpharow[x] = 0;
|
|
Packit |
78deda |
}
|
|
Packit |
78deda |
if (imageoutFileP)
|
|
Packit |
78deda |
pgm_writepgmrow(imageoutFileP, pixelrow, width, RLE_MAXVAL, 0);
|
|
Packit |
78deda |
if (alphaFileP)
|
|
Packit |
78deda |
pgm_writepgmrow(alphaFileP, alpharow, width, RLE_MAXVAL, 0);
|
|
Packit |
78deda |
|
|
Packit |
78deda |
} /* end of for scan = 0 to height */
|
|
Packit |
78deda |
|
|
Packit |
78deda |
/* Free scanline memory. */
|
|
Packit |
78deda |
for (scan = 0; scan < height; ++scan)
|
|
Packit |
78deda |
rle_row_free(&hdr, scanlines[scan]);
|
|
Packit |
78deda |
free(scanlines);
|
|
Packit |
78deda |
pgm_freerow(pixelrow);
|
|
Packit |
78deda |
pgm_freerow(alpharow);
|
|
Packit |
78deda |
}
|
|
Packit |
78deda |
|
|
Packit |
78deda |
|
|
Packit |
78deda |
|
|
Packit |
78deda |
int
|
|
Packit |
78deda |
main(int argc, char ** argv) {
|
|
Packit |
78deda |
|
|
Packit |
78deda |
struct CmdlineInfo cmdline;
|
|
Packit |
78deda |
FILE * ifP;
|
|
Packit |
78deda |
FILE * imageoutFileP;
|
|
Packit |
78deda |
FILE * alphaFileP;
|
|
Packit |
78deda |
char * fname;
|
|
Packit |
78deda |
|
|
Packit |
78deda |
pnm_init( &argc, argv );
|
|
Packit |
78deda |
|
|
Packit |
78deda |
parseCommandLine(argc, argv, &cmdline);
|
|
Packit |
78deda |
|
|
Packit |
78deda |
fname = NULL; /* initial value */
|
|
Packit |
78deda |
|
|
Packit |
78deda |
if (cmdline.inputFilename != NULL )
|
|
Packit |
78deda |
ifP = pm_openr(cmdline.inputFilename);
|
|
Packit |
78deda |
else
|
|
Packit |
78deda |
ifP = stdin;
|
|
Packit |
78deda |
|
|
Packit |
78deda |
if (cmdline.alphaStdout)
|
|
Packit |
78deda |
alphaFileP = stdout;
|
|
Packit |
78deda |
else if (cmdline.alphaout == NULL)
|
|
Packit |
78deda |
alphaFileP = NULL;
|
|
Packit |
78deda |
else {
|
|
Packit |
78deda |
alphaFileP = pm_openw(cmdline.alphaout);
|
|
Packit |
78deda |
}
|
|
Packit |
78deda |
|
|
Packit |
78deda |
if (cmdline.alphaStdout)
|
|
Packit |
78deda |
imageoutFileP = NULL;
|
|
Packit |
78deda |
else
|
|
Packit |
78deda |
imageoutFileP = stdout;
|
|
Packit |
78deda |
|
|
Packit |
78deda |
|
|
Packit |
78deda |
/*
|
|
Packit |
78deda |
* Open the file.
|
|
Packit |
78deda |
*/
|
|
Packit |
78deda |
/* Initialize header. */
|
|
Packit |
78deda |
hdr = *rle_hdr_init(NULL);
|
|
Packit |
78deda |
rle_names(&hdr, cmd_name( argv ), fname, 0);
|
|
Packit |
78deda |
|
|
Packit |
78deda |
/*
|
|
Packit |
78deda |
* Read the rle file header.
|
|
Packit |
78deda |
*/
|
|
Packit |
78deda |
readRleHeader(ifP, cmdline.headerdump || cmdline.verbose);
|
|
Packit |
78deda |
if (cmdline.headerdump)
|
|
Packit |
78deda |
exit(0);
|
|
Packit |
78deda |
|
|
Packit |
78deda |
/*
|
|
Packit |
78deda |
* Write the alpha file header
|
|
Packit |
78deda |
*/
|
|
Packit |
78deda |
if (alphaFileP)
|
|
Packit |
78deda |
pgm_writepgminit(alphaFileP, width, height, RLE_MAXVAL, 0);
|
|
Packit |
78deda |
|
|
Packit |
78deda |
/*
|
|
Packit |
78deda |
* Write the pnm file header.
|
|
Packit |
78deda |
*/
|
|
Packit |
78deda |
switch (visual) {
|
|
Packit |
78deda |
case GRAYSCALE: /* 8 bits without colormap -> pgm */
|
|
Packit |
78deda |
if (cmdline.verbose)
|
|
Packit |
78deda |
pm_message("Writing pgm file.");
|
|
Packit |
78deda |
if (imageoutFileP)
|
|
Packit |
78deda |
pgm_writepgminit(imageoutFileP, width, height, RLE_MAXVAL, 0);
|
|
Packit |
78deda |
writePgmRaster(imageoutFileP, alphaFileP);
|
|
Packit |
78deda |
break;
|
|
Packit |
78deda |
default: /* anything else -> ppm */
|
|
Packit |
78deda |
if (cmdline.verbose)
|
|
Packit |
78deda |
pm_message("Writing ppm file.");
|
|
Packit |
78deda |
if (imageoutFileP)
|
|
Packit |
78deda |
ppm_writeppminit(imageoutFileP, width, height, RLE_MAXVAL, 0);
|
|
Packit |
78deda |
writePpmRaster(imageoutFileP, alphaFileP);
|
|
Packit |
78deda |
break;
|
|
Packit |
78deda |
}
|
|
Packit |
78deda |
|
|
Packit |
78deda |
pm_close(ifP);
|
|
Packit |
78deda |
if (imageoutFileP)
|
|
Packit |
78deda |
pm_close(imageoutFileP);
|
|
Packit |
78deda |
if (alphaFileP)
|
|
Packit |
78deda |
pm_close(alphaFileP);
|
|
Packit |
78deda |
|
|
Packit |
78deda |
return 0;
|
|
Packit |
78deda |
}
|