|
Packit |
78deda |
/* pnmtile.c - replicate a portable anymap into a specified size
|
|
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 "mallocvar.h"
|
|
Packit |
78deda |
#include "shhopt.h"
|
|
Packit |
78deda |
#include "pnm.h"
|
|
Packit |
78deda |
|
|
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 |
const char * inputFileName;
|
|
Packit |
78deda |
unsigned int width;
|
|
Packit |
78deda |
unsigned int height;
|
|
Packit |
78deda |
};
|
|
Packit |
78deda |
|
|
Packit |
78deda |
|
|
Packit |
78deda |
|
|
Packit |
78deda |
static void
|
|
Packit |
78deda |
parseCommandLine(int argc, const char ** argv,
|
|
Packit |
78deda |
struct cmdlineInfo * const cmdlineP) {
|
|
Packit |
78deda |
/*----------------------------------------------------------------------------
|
|
Packit |
78deda |
Note that the file spec array we return is stored in the storage that
|
|
Packit |
78deda |
was passed to us as the argv array.
|
|
Packit |
78deda |
-----------------------------------------------------------------------------*/
|
|
Packit |
78deda |
optEntry *option_def;
|
|
Packit |
78deda |
/* Instructions to OptParseOptions3 on how to parse our options.
|
|
Packit |
78deda |
*/
|
|
Packit |
78deda |
optStruct3 opt;
|
|
Packit |
78deda |
|
|
Packit |
78deda |
unsigned int option_def_index;
|
|
Packit |
78deda |
|
|
Packit |
78deda |
MALLOCARRAY_NOFAIL(option_def, 100);
|
|
Packit |
78deda |
|
|
Packit |
78deda |
option_def_index = 0; /* incremented by OPTENT3 */
|
|
Packit |
78deda |
|
|
Packit |
78deda |
opt.opt_table = option_def;
|
|
Packit |
78deda |
opt.short_allowed = FALSE; /* We have no short (old-fashioned) options */
|
|
Packit |
78deda |
opt.allowNegNum = FALSE; /* We have no parms that are negative numbers */
|
|
Packit |
78deda |
|
|
Packit |
78deda |
OPTENTINIT;
|
|
Packit |
78deda |
|
|
Packit |
78deda |
pm_optParseOptions3(&argc, (char**)argv, opt, sizeof opt, 0);
|
|
Packit |
78deda |
/* Uses and sets argc, argv, and some of *cmdlineP and others. */
|
|
Packit |
78deda |
|
|
Packit |
78deda |
if (argc-1 < 2)
|
|
Packit |
78deda |
pm_error("You must specify at least two parameters: "
|
|
Packit |
78deda |
"width and height. You specified %u",
|
|
Packit |
78deda |
argc-1);
|
|
Packit |
78deda |
else {
|
|
Packit |
78deda |
cmdlineP->width = pm_parse_width(argv[1]);
|
|
Packit |
78deda |
cmdlineP->height = pm_parse_height(argv[2]);
|
|
Packit |
78deda |
|
|
Packit |
78deda |
if (argc-1 > 2) {
|
|
Packit |
78deda |
cmdlineP->inputFileName = argv[3];
|
|
Packit |
78deda |
|
|
Packit |
78deda |
if (argc-1 > 3)
|
|
Packit |
78deda |
pm_error("There are at most three arguments: "
|
|
Packit |
78deda |
"width, height, file name. You specified %u",
|
|
Packit |
78deda |
argc-1);
|
|
Packit |
78deda |
} else
|
|
Packit |
78deda |
cmdlineP->inputFileName = "-";
|
|
Packit |
78deda |
}
|
|
Packit |
78deda |
}
|
|
Packit |
78deda |
|
|
Packit |
78deda |
|
|
Packit |
78deda |
|
|
Packit |
78deda |
int
|
|
Packit |
78deda |
main(int argc, const char ** argv) {
|
|
Packit |
78deda |
|
|
Packit |
78deda |
struct cmdlineInfo cmdline;
|
|
Packit |
78deda |
FILE * ifP;
|
|
Packit |
78deda |
xel ** xels;
|
|
Packit |
78deda |
xel * xelrow;
|
|
Packit |
78deda |
xelval maxval;
|
|
Packit |
78deda |
int rows, cols;
|
|
Packit |
78deda |
int format;
|
|
Packit |
78deda |
unsigned int row;
|
|
Packit |
78deda |
|
|
Packit |
78deda |
pm_proginit(&argc, argv);
|
|
Packit |
78deda |
|
|
Packit |
78deda |
parseCommandLine(argc, argv, &cmdline);
|
|
Packit |
78deda |
|
|
Packit |
78deda |
ifP = pm_openr(cmdline.inputFileName);
|
|
Packit |
78deda |
|
|
Packit |
78deda |
xels = pnm_readpnm(ifP, &cols, &rows, &maxval, &format);
|
|
Packit |
78deda |
pm_close(ifP);
|
|
Packit |
78deda |
|
|
Packit |
78deda |
xelrow = pnm_allocrow(cmdline.width);
|
|
Packit |
78deda |
|
|
Packit |
78deda |
pnm_writepnminit(stdout, cmdline.width, cmdline.height, maxval, format, 0);
|
|
Packit |
78deda |
for (row = 0; row < cmdline.height; ++row) {
|
|
Packit |
78deda |
unsigned int col;
|
|
Packit |
78deda |
for (col = 0; col < cmdline.width; ++col)
|
|
Packit |
78deda |
xelrow[col] = xels[row % rows][col % cols];
|
|
Packit |
78deda |
pnm_writepnmrow(stdout, xelrow, cmdline.width, maxval, format, 0);
|
|
Packit |
78deda |
}
|
|
Packit |
78deda |
|
|
Packit |
78deda |
pm_close(stdout);
|
|
Packit |
78deda |
|
|
Packit |
78deda |
return 0;
|
|
Packit |
78deda |
}
|