| 1 |
/* |
| 2 |
* This code written 2003 |
| 3 |
* by Max Gensthaler <[email protected]> |
| 4 |
* Distributed under the Gnu Public License (GPL) |
| 5 |
* |
| 6 |
* Gensthaler called it 'ppmglobemap'. |
| 7 |
* |
| 8 |
* Translations of comments and C dialect by Bryan Henderson May 2003. |
| 9 |
*/ |
| 10 |
|
| 11 |
|
| 12 |
#define _XOPEN_SOURCE 500 /* get M_PI in math.h */ |
| 13 |
#include <stdio.h> |
| 14 |
#include <stdlib.h> |
| 15 |
#include <unistd.h> |
| 16 |
#include <math.h> |
| 17 |
|
| 18 |
#include "pm_c_util.h" |
| 19 |
#include "ppm.h" |
| 20 |
#include "colorname.h" |
| 21 |
#include "shhopt.h" |
| 22 |
#include "mallocvar.h" |
| 23 |
|
| 24 |
|
| 25 |
struct cmdlineInfo { |
| 26 |
/* All the information the user supplied in the command line, |
| 27 |
in a form easy for the program to use. |
| 28 |
*/ |
| 29 |
const char * inputFileName; /* Filename of input files */ |
| 30 |
unsigned int stripcount; |
| 31 |
const char * background; |
| 32 |
unsigned int closeok; |
| 33 |
}; |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
static void |
| 38 |
parseCommandLine(int argc, char ** argv, |
| 39 |
struct cmdlineInfo * const cmdlineP) { |
| 40 |
/*---------------------------------------------------------------------------- |
| 41 |
Note that the file spec array we return is stored in the storage that |
| 42 |
was passed to us as the argv array. |
| 43 |
-----------------------------------------------------------------------------*/ |
| 44 |
optEntry *option_def; |
| 45 |
/* Instructions to pm_optParseOptions3 on how to parse our options. |
| 46 |
*/ |
| 47 |
optStruct3 opt; |
| 48 |
|
| 49 |
unsigned int option_def_index; |
| 50 |
|
| 51 |
unsigned int backgroundSpec; |
| 52 |
|
| 53 |
MALLOCARRAY_NOFAIL(option_def, 100); |
| 54 |
|
| 55 |
option_def_index = 0; /* incremented by OPTENT3 */ |
| 56 |
OPTENT3(0, "background", OPT_STRING, &cmdlineP->background, |
| 57 |
&backgroundSpec, 0); |
| 58 |
OPTENT3(0, "closeok", OPT_FLAG, NULL, |
| 59 |
&cmdlineP->closeok, 0); |
| 60 |
|
| 61 |
opt.opt_table = option_def; |
| 62 |
opt.short_allowed = FALSE; /* We have no short (old-fashioned) options */ |
| 63 |
opt.allowNegNum = FALSE; /* We have no parms that are negative numbers */ |
| 64 |
|
| 65 |
pm_optParseOptions3(&argc, argv, opt, sizeof(opt), 0); |
| 66 |
/* Uses and sets argc, argv, and some of *cmdlineP and others. */ |
| 67 |
|
| 68 |
if (!backgroundSpec) |
| 69 |
cmdlineP->background = NULL; |
| 70 |
|
| 71 |
if (argc - 1 < 1) |
| 72 |
pm_error("You must specify at least one argument: the strip count"); |
| 73 |
else { |
| 74 |
int const stripcount = atoi(argv[1]); |
| 75 |
if (stripcount <= 0) |
| 76 |
pm_error("The strip count must be positive. You specified %d", |
| 77 |
stripcount); |
| 78 |
|
| 79 |
cmdlineP->stripcount = stripcount; |
| 80 |
|
| 81 |
if (argc-1 < 2) |
| 82 |
cmdlineP->inputFileName = "-"; |
| 83 |
else |
| 84 |
cmdlineP->inputFileName = argv[2]; |
| 85 |
|
| 86 |
if (argc - 1 > 2) |
| 87 |
pm_error("There are at most two arguments: strip count " |
| 88 |
"and input file name. " |
| 89 |
"You specified %u", argc-1); |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
int |
| 96 |
main(int argc, char *argv[]) { |
| 97 |
|
| 98 |
struct cmdlineInfo cmdline; |
| 99 |
FILE * ifP; |
| 100 |
pixel ** srcPixels; |
| 101 |
pixel ** dstPixels; |
| 102 |
int srcCols, srcRows; |
| 103 |
unsigned int dstCols, dstRows; |
| 104 |
pixval srcMaxval, dstMaxval; |
| 105 |
unsigned int stripWidth; |
| 106 |
/* Width in pixels of each strip. In the output image, this means |
| 107 |
the rectangular strip in which the lens-shaped foreground strip |
| 108 |
is placed.. |
| 109 |
*/ |
| 110 |
unsigned int row; |
| 111 |
pixel backgroundColor; |
| 112 |
|
| 113 |
ppm_init(&argc, argv); |
| 114 |
|
| 115 |
parseCommandLine(argc, argv, &cmdline); |
| 116 |
|
| 117 |
ifP = pm_openr(cmdline.inputFileName); |
| 118 |
|
| 119 |
srcPixels = ppm_readppm(ifP, &srcCols, &srcRows, &srcMaxval); |
| 120 |
|
| 121 |
pm_close(ifP); |
| 122 |
|
| 123 |
stripWidth = srcCols / cmdline.stripcount; |
| 124 |
|
| 125 |
if (stripWidth < 1) |
| 126 |
pm_error("You asked for %u strips, but the image is only " |
| 127 |
"%u pixels wide, so that is impossible.", |
| 128 |
cmdline.stripcount, srcCols); |
| 129 |
|
| 130 |
dstCols = stripWidth * cmdline.stripcount; |
| 131 |
dstRows = srcRows; |
| 132 |
dstMaxval = srcMaxval; |
| 133 |
|
| 134 |
if (cmdline.background == NULL) |
| 135 |
PPM_ASSIGN(backgroundColor, 0, 0, 0); |
| 136 |
else |
| 137 |
pm_parse_dictionary_name(cmdline.background, |
| 138 |
dstMaxval, cmdline.closeok, |
| 139 |
&backgroundColor); |
| 140 |
|
| 141 |
dstPixels = ppm_allocarray(dstCols, dstRows); |
| 142 |
|
| 143 |
for (row = 0; row < dstRows; ++row) { |
| 144 |
double const factor = sin(M_PI * row / dstRows); |
| 145 |
/* Amount by which we squeeze the foreground image of each |
| 146 |
strip in this row. |
| 147 |
*/ |
| 148 |
int const stripBorder = (int)((stripWidth*(1.0-factor)/2.0) + 0.5); |
| 149 |
/* Distance from the edge (either one) of a strip to the |
| 150 |
foreground image within that strip -- i.e. number of pixels |
| 151 |
of background color, which User will cut out with scissors |
| 152 |
after he prints the image. |
| 153 |
*/ |
| 154 |
unsigned int dstCol; |
| 155 |
|
| 156 |
for (dstCol = 0; dstCol < dstCols; ++dstCol) { |
| 157 |
if (dstCol % stripWidth < stripBorder |
| 158 |
|| dstCol % stripWidth >= stripWidth - stripBorder) |
| 159 |
dstPixels[row][dstCol] = backgroundColor; |
| 160 |
else { |
| 161 |
unsigned int const leftEdge = |
| 162 |
(dstCol / stripWidth) * stripWidth; |
| 163 |
unsigned int const srcCol = leftEdge + |
| 164 |
(int)((dstCol % stripWidth - stripBorder) / factor + 0.5); |
| 165 |
dstPixels[row][dstCol] = srcPixels[row][srcCol]; |
| 166 |
} |
| 167 |
} |
| 168 |
} |
| 169 |
|
| 170 |
ppm_writeppm(stdout, dstPixels, dstCols, dstRows, dstMaxval, 0); |
| 171 |
|
| 172 |
return 0; |
| 173 |
} |