Debian Codegraph @

editor/specialty/pnmmercator.c

raw
1 /* pammercator.c - convert a map in PNM image format from 360x180 degrees
2 ** to Mercator projection or vice versa
3 **
4 ** This program borrowed a lot of code from PnmScale which again was derived
5 ** from PpmScale.
6 **
7 ** Copyright (C) 2009 by Willem van Schaik <[email protected]>
8 ** Copyright (C) 1989, 1991 by Jef Poskanzer.
9 **
10 ** Permission to use, copy, modify, and distribute this software and its
11 ** documentation for any purpose and without fee is hereby granted, provided
12 ** that the above copyright notice appear in all copies and that both that
13 ** copyright notice and this permission notice appear in supporting
14 ** documentation. This software is provided "as is" without express or
15 ** implied warranty.
16 **
17 */
18
19 #define _XOPEN_SOURCE 500 /* get M_PI in math.h */
20 #include <math.h>
21 #include <string.h>
22
23 #include "pm_c_util.h"
24 #include "mallocvar.h"
25 #include "shhopt.h"
26 #include "pnm.h"
27
28 /* The pnm library allows us to code this program without branching cases for
29 PGM and PPM, but we do the branch anyway to speed up processing of PGM
30 images.
31 */
32
33
34
35 struct cmdlineInfo
36 {
37 /*
38 All the information the user supplied in the command line,
39 in a form easy for the program to use.
40 */
41
42 const char * input_filespec; /* Filespecs of input files */
43 const char * inputFileName; /* Filespec of input file */
44 unsigned int inverse; /* from Mercator to Degrees */
45 unsigned int nomix;
46 unsigned int verbose;
47 unsigned int vverbose;
48 };
49
50
51
52 static void
53 parseCommandLine(int argc,
54 const char ** argv,
55 struct cmdlineInfo * const cmdlineP ) {
56
57 optEntry * option_def;
58 optStruct3 opt;
59 /* Instructions to pm_optParseOptions3 on how to parse our options. */
60
61 unsigned int option_def_index;
62
63 MALLOCARRAY_NOFAIL(option_def, 100);
64
65 option_def_index = 0; /* incremented by OPTENT3 */
66 OPTENT3(0, "inverse", OPT_FLAG, NULL, &cmdlineP->inverse, 0);
67 OPTENT3(0, "nomix", OPT_FLAG, NULL, &cmdlineP->nomix, 0);
68 OPTENT3(0, "verbose", OPT_FLAG, NULL, &cmdlineP->verbose, 0);
69 OPTENT3(0, "vverbose", OPT_FLAG, NULL, &cmdlineP->vverbose, 0);
70
71 opt.opt_table = option_def;
72 opt.short_allowed = FALSE; /* We have no short (old-fashioned) options */
73 opt.allowNegNum = FALSE; /* We have no parms that are negative numbers */
74
75 /* Uses and sets argc, argv, and some of *cmdlineP and others. */
76 pm_optParseOptions3( &argc, (char **)argv, opt, sizeof(opt), 0 );
77
78 /* Only parameter allowed is optional filespec */
79 if (argc-1 < 1)
80 cmdlineP->input_filespec = "-";
81 else
82 cmdlineP->input_filespec = argv[1];
83 }
84
85
86
87 static void
88 computeOutputDimensions(const struct cmdlineInfo cmdline,
89 const int rows, const int cols,
90 int * newrowsP, int * newcolsP)
91 {
92 *newcolsP = cols;
93 if (!cmdline.inverse)
94 *newrowsP = 2 * rows;
95 else
96 *newrowsP = rows / 2;
97
98 if (*newcolsP < 1) *newcolsP = 1;
99 if (*newrowsP < 1) *newrowsP = 1;
100
101 if (cmdline.verbose) {
102 if (!cmdline.inverse)
103 pm_message("Creating Mercator map, new size is %dx%d",
104 *newcolsP, *newrowsP);
105 else
106 pm_message("Creating Degrees map, new size is %dx%d",
107 *newcolsP, *newrowsP);
108 }
109 }
110
111
112
113 static void
114 transformWithMixing(FILE * const ifP,
115 int const cols, int const rows,
116 xelval const maxval, int const format,
117 int const newcols, int const newrows,
118 xelval const newmaxval, int const newformat,
119 bool const inverse,
120 bool const verbose, bool const vverbose)
121 {
122 /*
123 Transform the map image on input file 'ifP' (which is described by
124 'cols', 'rows', 'format', and 'maxval') to a Mercator projection
125 and write the result to standard output as format 'newformat' and
126 with maxval 'newmaxval'.
127
128 We'll blend colors from subsequent rows in the output pixels.
129 */
130
131 xel* oddxelrow; /* an input row */
132 xel* evenxelrow;
133 xel* newxelrow; /* the output row */
134
135 int row;
136 double fRow;
137 int rowInXelrow;
138 int inputRow;
139 int col;
140
141 double fFract;
142 double fLatRad;
143 double fLatMerc;
144
145 oddxelrow = pnm_allocrow(cols);
146 evenxelrow = pnm_allocrow(cols);
147 rowInXelrow = 0;
148
149 newxelrow = pnm_allocrow(newcols);
150
151 for (row = 0; row < newrows; ++row) {
152
153 fRow = (double)row + 0.5; /* center on half the pixel */
154 if (!inverse) {
155 /* the result is mercator, calculate back to degrees */
156 fLatMerc = 2.0 * M_PI * (fRow / (double)newrows - 0.5);
157 /* merc goes from -pi to +pi */
158 fLatRad = 2.0 * (atan(exp(fLatMerc)) - M_PI / 4.0);
159 fRow = ((fLatRad / M_PI) + 0.5) * (double)rows;
160 /* lat goes from -pi/2 to +pi/2 */
161 } else {
162 /* the result is in degrees, calculate back to mercator */
163 fLatRad = M_PI * (fRow / (double)newrows - 0.5);
164 /* lat goes from -pi/2 to +pi/2 */
165 fLatMerc = log(tan((M_PI / 4.0 + fLatRad / 2.0)));
166 fRow = ((fLatMerc / M_PI / 2.0) + 0.5) * (double)rows;
167 /* merc goes from -pi to +pi */
168 }
169 fRow -= 0.5;
170
171 inputRow = (int) floor(fRow);
172 if (inputRow < 0)
173 inputRow = 0;
174 if (inputRow > rows - 1)
175 inputRow = rows - 1;
176
177 /* calculate the fraction */
178 fFract = 1.0 - ((fRow) - (double) inputRow);
179
180 if (vverbose) {
181 if (!inverse) {
182 #ifdef DBG_EDGES
183 if ((row < 10) || (row > (newrows - 10)))
184 #else
185 #ifdef DBG_MIDDLE
186 if ((row > (newrows/2 - 10)) && (row < (newrows/2 + 10)))
187 #else
188 if (row % 20 == 0)
189 #endif
190 #endif
191 pm_message("outputRow=%d latMerc=%f latRad=%f inputRow=%d "
192 "fRow=%f fFract=%f",
193 row, fLatMerc, fLatRad, inputRow, fRow, fFract);
194 } else {
195 #ifdef DBG_EDGES
196 if ((row < 10) || (row > (newrows - 10)))
197 #else
198 #ifdef DBG_MIDDLE
199 if ((row > (newrows/2 - 10)) && (row < (newrows/2 + 10)))
200 #else
201 if (row % 10 == 0)
202 #endif
203 #endif
204 pm_message("outputRow=%d latRad=%f latMerc=%f inputRow=%d"
205 "fRow=%f fFract=%f",
206 row, fLatRad, fLatMerc, inputRow, fRow, fFract);
207 }
208 }
209
210 while ((rowInXelrow <= inputRow + 1) &&
211 (rowInXelrow < rows)) {
212 /* we need to read one row ahead */
213 if (rowInXelrow % 2 == 0) {
214 #ifdef DBG_EDGES
215 if ((row < 10) || (row > (newrows - 10)))
216 pm_message("read even row - rowInXelrow=%d inputRow=%d",
217 rowInXelrow, inputRow);
218 #endif
219 pnm_readpnmrow(ifP, evenxelrow, cols, newmaxval, format);
220 } else {
221 #ifdef DBG_EDGES
222 if ((row < 10) || (row > (newrows - 10)))
223 pm_message("read odd row - rowInXelrow=%d inputRow=%d",
224 rowInXelrow, inputRow);
225 #endif
226 pnm_readpnmrow(ifP, oddxelrow, cols, newmaxval, format);
227 }
228 ++rowInXelrow;
229 }
230
231 for (col = 0; col < newcols; ++col) {
232 if (inputRow == 0)
233 newxelrow[col] = evenxelrow[col];
234 else if (inputRow == rows - 1)
235 newxelrow[col] = oddxelrow[col];
236 else if (inputRow % 2 == 0) {
237 /* the even row is the low one, the odd the high one */
238 newxelrow[col].r = fFract * evenxelrow[col].r +
239 (1.0 - fFract) * oddxelrow[col].r;
240 newxelrow[col].g = fFract * evenxelrow[col].g +
241 (1.0 - fFract) * oddxelrow[col].g;
242 newxelrow[col].b = fFract * evenxelrow[col].b +
243 (1.0 - fFract) * oddxelrow[col].b;
244 } else {
245 newxelrow[col].r = fFract * oddxelrow[col].r +
246 (1.0 - fFract) * evenxelrow[col].r;
247 newxelrow[col].g = fFract * oddxelrow[col].g +
248 (1.0 - fFract) * evenxelrow[col].g;
249 newxelrow[col].b = fFract * oddxelrow[col].b +
250 (1.0 - fFract) * evenxelrow[col].b;
251 }
252 }
253
254 pnm_writepnmrow(stdout, newxelrow, newcols, newmaxval, newformat, 0 );
255 }
256
257 pnm_freerow(oddxelrow);
258 pnm_freerow(evenxelrow);
259 pnm_freerow(newxelrow);
260 }
261
262
263
264 static void
265 transformNoneMixing(FILE * const ifP,
266 int const cols, int const rows,
267 xelval const maxval, int const format,
268 int const newcols, int const newrows,
269 xelval const newmaxval, int const newformat,
270 bool const inverse,
271 bool const verbose, bool const vverbose)
272 {
273 /*
274 Transform the map image on input file 'ifP' (which is described by
275 'cols', 'rows', 'format', and 'maxval') to a Mercator projection and
276 write the result to standard output as format 'newformat' and with
277 maxval 'newmaxval'.
278
279 Don't mix colors from different input pixels together in the output
280 pixels. Each output pixel is an exact copy of some corresponding
281 input pixel.
282 */
283
284 xel* xelrow; /* an input row */
285 xel* newxelrow; /* the output row */
286
287 int row;
288 double fRow;
289 int rowInXelrow;
290 int inputRow;
291 int col;
292
293 double fLatRad;
294 double fLatMerc;
295
296 xelrow = pnm_allocrow(cols);
297 rowInXelrow = 0;
298
299 newxelrow = pnm_allocrow(newcols);
300
301 for (row = 0; row < newrows; ++row) {
302
303 fRow = (double)row + 0.5; /* center on half the pixel */
304 if (!inverse) {
305 /* the result is mercator, calculate back to degrees */
306 fLatMerc = 2.0 * M_PI * (fRow / (double)newrows - 0.5);
307 /* merc goes from -pi to +pi */
308 fLatRad = 2.0 * (atan(exp(fLatMerc)) - M_PI / 4.0);
309 fRow = ((fLatRad / M_PI) + 0.5) * (double)rows;
310 /* lat goes from -pi/2 to +pi/2 */
311 } else {
312 /* the result is in degrees, calculate back to mercator */
313 fLatRad = M_PI * (fRow / (double)newrows - 0.5);
314 /* lat goes from -pi/2 to +pi/2 */
315 fLatMerc = log(tan((M_PI / 4.0 + fLatRad / 2.0)));
316 fRow = ((fLatMerc / M_PI / 2.0) + 0.5) * (double)rows;
317 /* merc goes from -pi to +pi */
318 }
319 /* fRow -= 0.5; */ /* it's weird that this isn't needed */
320
321 inputRow = (int) floor(fRow);
322 if (inputRow < 0)
323 inputRow = 0;
324 if (inputRow > rows - 1)
325 inputRow = rows - 1;
326
327 if (vverbose) {
328 if (!inverse) {
329 #ifdef DBG_EDGES
330 if ((row < 10) || (row > (newrows - 10)))
331 #else
332 #ifdef DBG_MIDDLE
333 if ((row > (newrows/2 - 10)) && (row < (newrows/2 + 10)))
334 #else
335 if (row % 20 == 0)
336 #endif
337 #endif
338 pm_message("outputRow=%d latMerc=%f latRad=%f inputRow=%d"
339 "fRow=%f",
340 row, fLatMerc, fLatRad, inputRow, fRow);
341 } else {
342 #ifdef DBG_EDGES
343 if ((row < 10) || (row > (newrows - 10)))
344 #else
345 #ifdef DBG_MIDDLE
346 if ((row > (newrows/2 - 10)) && (row < (newrows/2 + 10)))
347 #else
348 if (row % 10 == 0)
349 #endif
350 #endif
351 pm_message("outputRow=%d latRad=%f latMerc=%f inputRow=%d"
352 "fRow=%f",
353 row, fLatRad, fLatMerc, inputRow, fRow);
354 }
355 }
356
357 while ((rowInXelrow <= inputRow) && (rowInXelrow < rows)) {
358 #ifdef DBG_EDGES
359 if ((row < 10) || (row > (newrows - 10)))
360 pm_message("read row - rowInXelrow=%d inputRow=%d",
361 rowInXelrow, inputRow);
362 #endif
363 pnm_readpnmrow(ifP, xelrow, cols, newmaxval, format);
364 ++rowInXelrow;
365 }
366 for (col = 0; col < newcols; ++col) {
367 newxelrow[col] = xelrow[col];
368 }
369
370 pnm_writepnmrow(stdout, newxelrow, newcols, newmaxval, newformat, 0 );
371 }
372
373 pnm_freerow(xelrow);
374 pnm_freerow(newxelrow);
375 }
376
377
378
379 int
380 main(int argc, const char ** argv )
381 {
382 struct cmdlineInfo cmdline;
383 FILE* ifP;
384 int rows, cols, format, newformat, newrows, newcols;
385 xelval maxval, newmaxval;
386 bool verbose;
387
388 pm_proginit( &argc, argv );
389
390 parseCommandLine(argc, argv, &cmdline);
391
392 verbose = cmdline.verbose || cmdline.vverbose;
393
394 ifP = pm_openr(cmdline.input_filespec);
395
396 pnm_readpnminit( ifP, &cols, &rows, &maxval, &format );
397
398 /* Promote PBM file to PGM. */
399 if ( PNM_FORMAT_TYPE(format) == PBM_TYPE ) {
400 newformat = PGM_TYPE;
401 newmaxval = PGM_MAXMAXVAL;
402 pm_message( "promoting from PBM to PGM" );
403 } else {
404 newformat = format;
405 newmaxval = maxval;
406 }
407
408 computeOutputDimensions(cmdline, rows, cols, &newrows, &newcols);
409
410 pnm_writepnminit(stdout, newcols, newrows, newmaxval, newformat, 0);
411
412 if (cmdline.nomix) {
413 if (verbose)
414 pm_message("Transforming map without mixing/blending colors");
415 transformNoneMixing(ifP, cols, rows, maxval, format,
416 newcols, newrows, newmaxval, newformat,
417 cmdline.inverse, verbose, cmdline.vverbose);
418 } else {
419 if (verbose)
420 pm_message("Transforming map while using intermediate colors");
421 transformWithMixing(ifP, cols, rows, maxval, format,
422 newcols, newrows, newmaxval, newformat,
423 cmdline.inverse, verbose, cmdline.vverbose);
424 }
425
426 pm_close(ifP);
427 pm_close(stdout);
428
429 return 0;
430 }