about summary refs log tree commit diff
path: root/editor/specialty/pnmmercator.c
blob: 0c5c790fb86c12c94a949bc74fa5987dab8e0c5f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
/* pammercator.c - convert a map in PNM image format from 360x180 degrees
**                 to Mercator projection or vice versa
**
** This program borrowed a lot of code from PnmScale which again was derived
** from PpmScale.
**
** Copyright (C) 2009 by Willem van Schaik <willem@schaik.com>
** Copyright (C) 1989, 1991 by Jef Poskanzer.
**
** Permission to use, copy, modify, and distribute this software and its
** documentation for any purpose and without fee is hereby granted, provided
** that the above copyright notice appear in all copies and that both that
** copyright notice and this permission notice appear in supporting
** documentation.  This software is provided "as is" without express or
** implied warranty.
**
*/

#define _XOPEN_SOURCE 500  /* get M_PI in math.h */
#include <math.h>
#include <string.h>

#include "pm_c_util.h"
#include "mallocvar.h"
#include "shhopt.h"
#include "pnm.h"

/* The pnm library allows us to code this program without branching cases for
   PGM and PPM, but we do the branch anyway to speed up processing of PGM
   images.
*/



struct cmdlineInfo
{
    /*
    All the information the user supplied in the command line,
    in a form easy for the program to use.
    */

    const char * input_filespec;  /* Filespecs of input files */
    const char * inputFileName;  /* Filespec of input file */
    unsigned int inverse; /* from Mercator to Degrees */
    unsigned int nomix;
    unsigned int verbose;
    unsigned int vverbose;
};



static void
parseCommandLine(int                        argc,
                 const char **              argv,
                 struct cmdlineInfo * const cmdlineP ) {

    optEntry * option_def;
    optStruct3 opt;
        /* Instructions to pm_optParseOptions3 on how to parse our options. */

    unsigned int option_def_index;

    MALLOCARRAY_NOFAIL(option_def, 100);

    option_def_index = 0;   /* incremented by OPTENT3 */
    OPTENT3(0, "inverse",   OPT_FLAG,    NULL,       &cmdlineP->inverse,   0);
    OPTENT3(0, "nomix",     OPT_FLAG,    NULL,       &cmdlineP->nomix,     0);
    OPTENT3(0, "verbose",   OPT_FLAG,    NULL,       &cmdlineP->verbose,   0);
    OPTENT3(0, "vverbose",  OPT_FLAG,    NULL,       &cmdlineP->vverbose,  0);

    opt.opt_table = option_def;
    opt.short_allowed = FALSE;  /* We have no short (old-fashioned) options */
    opt.allowNegNum = FALSE;   /* We have no parms that are negative numbers */

    /* Uses and sets argc, argv, and some of *cmdlineP and others. */
    pm_optParseOptions3( &argc, (char **)argv, opt, sizeof(opt), 0 );

    /* Only parameter allowed is optional filespec */
    if (argc-1 < 1)
        cmdlineP->input_filespec = "-";
    else
        cmdlineP->input_filespec = argv[1];
}



static void
computeOutputDimensions(const struct cmdlineInfo cmdline,
                        const int rows, const int cols,
                        int * newrowsP, int * newcolsP)
{
    *newcolsP = cols;
    if (!cmdline.inverse)
        *newrowsP = 2 * rows;
    else
        *newrowsP = rows / 2;

    if (*newcolsP < 1) *newcolsP = 1;
    if (*newrowsP < 1) *newrowsP = 1;

    if (cmdline.verbose) {
        if (!cmdline.inverse)
            pm_message("Creating Mercator map, new size is %dx%d",
                       *newcolsP, *newrowsP);
        else
            pm_message("Creating Degrees map, new size is %dx%d",
                       *newcolsP, *newrowsP);
    }
}



static void
transformWithMixing(FILE * const ifP,
                    int const cols, int const rows,
                    xelval const maxval, int const format,
                    int const newcols, int const newrows,
                    xelval const newmaxval, int const newformat,
                    bool const inverse,
                    bool const verbose, bool const vverbose)
{
    /*
    Transform the map image on input file 'ifP' (which is described by
    'cols', 'rows', 'format', and 'maxval') to a Mercator projection
    and write the result to standard output as format 'newformat' and
    with maxval 'newmaxval'.

    We'll blend colors from subsequent rows in the output pixels.
    */

    xel* oddxelrow;  /* an input row */
    xel* evenxelrow;
    xel* newxelrow;  /* the output row */

    int row;
    double fRow;
    int rowInXelrow;
    int inputRow;
    int col;

    double fFract;
    double fLatRad;
    double fLatMerc;

    oddxelrow = pnm_allocrow(cols);
    evenxelrow = pnm_allocrow(cols);
    rowInXelrow = 0;

    newxelrow = pnm_allocrow(newcols);

    for (row = 0; row < newrows; ++row) {

        fRow = (double)row + 0.5; /* center on half the pixel */
        if (!inverse) {
            /* the result is mercator, calculate back to degrees */
            fLatMerc = 2.0 * M_PI * (fRow / (double)newrows - 0.5);
                /* merc goes from -pi to +pi */
            fLatRad = 2.0 * (atan(exp(fLatMerc)) - M_PI / 4.0);
            fRow = ((fLatRad / M_PI) + 0.5) * (double)rows;
                /* lat goes from -pi/2 to +pi/2 */
        } else {
            /* the result is in degrees, calculate back to mercator */
            fLatRad = M_PI * (fRow / (double)newrows - 0.5);
                /* lat goes from -pi/2 to +pi/2 */
            fLatMerc = log(tan((M_PI / 4.0 + fLatRad / 2.0)));
            fRow = ((fLatMerc / M_PI / 2.0) + 0.5) * (double)rows;
                /* merc goes from -pi to +pi */
        }
        fRow -= 0.5;

        inputRow = (int) floor(fRow);
        if (inputRow < 0)
            inputRow = 0;
        if (inputRow > rows - 1)
            inputRow = rows - 1;

        /* calculate the fraction */
        fFract = 1.0 - ((fRow) - (double) inputRow);

        if (vverbose) {
            if (!inverse) {
#ifdef DBG_EDGES
                if ((row < 10) || (row > (newrows - 10)))
#else
  #ifdef DBG_MIDDLE
                if ((row > (newrows/2 - 10)) && (row < (newrows/2 + 10)))
  #else
                if (row % 20 == 0)
  #endif
#endif
                    pm_message("outputRow=%d latMerc=%f latRad=%f inputRow=%d "
                               "fRow=%f fFract=%f",
                               row, fLatMerc, fLatRad, inputRow, fRow, fFract);
            } else {
#ifdef DBG_EDGES
                if ((row < 10) || (row > (newrows - 10)))
#else
  #ifdef DBG_MIDDLE
                if ((row > (newrows/2 - 10)) && (row < (newrows/2 + 10)))
  #else
                if (row % 10 == 0)
  #endif
#endif
                    pm_message("outputRow=%d latRad=%f latMerc=%f inputRow=%d"
                               "fRow=%f fFract=%f",
                               row, fLatRad, fLatMerc, inputRow, fRow, fFract);
            }
        }

        while ((rowInXelrow <= inputRow + 1) &&
               (rowInXelrow < rows)) {
            /* we need to read one row ahead */
            if (rowInXelrow % 2 == 0) {
#ifdef DBG_EDGES
                if ((row < 10) || (row > (newrows - 10)))
                    pm_message("read even row - rowInXelrow=%d inputRow=%d",
                               rowInXelrow, inputRow);
#endif
                pnm_readpnmrow(ifP, evenxelrow, cols, newmaxval, format);
            } else {
#ifdef DBG_EDGES
                if ((row < 10) || (row > (newrows - 10)))
                    pm_message("read odd row - rowInXelrow=%d inputRow=%d",
                               rowInXelrow, inputRow);
#endif
                pnm_readpnmrow(ifP, oddxelrow, cols, newmaxval, format);
            }
            ++rowInXelrow;
        }

        for (col = 0; col < newcols; ++col) {
            if (inputRow == 0)
                newxelrow[col] = evenxelrow[col];
            else if (inputRow == rows - 1)
                newxelrow[col] = oddxelrow[col];
            else if (inputRow % 2 == 0) {
                /* the even row is the low one, the odd the high one */
                newxelrow[col].r = fFract * evenxelrow[col].r +
                    (1.0 - fFract) * oddxelrow[col].r;
                newxelrow[col].g = fFract * evenxelrow[col].g +
                    (1.0 - fFract) * oddxelrow[col].g;
                newxelrow[col].b = fFract * evenxelrow[col].b +
                    (1.0 - fFract) * oddxelrow[col].b;
            } else {
                newxelrow[col].r = fFract * oddxelrow[col].r +
                    (1.0 - fFract) * evenxelrow[col].r;
                newxelrow[col].g = fFract * oddxelrow[col].g +
                    (1.0 - fFract) * evenxelrow[col].g;
                newxelrow[col].b = fFract * oddxelrow[col].b +
                    (1.0 - fFract) * evenxelrow[col].b;
            }
        }

        pnm_writepnmrow(stdout, newxelrow, newcols, newmaxval, newformat, 0 );
    }

    pnm_freerow(oddxelrow);
    pnm_freerow(evenxelrow);
    pnm_freerow(newxelrow);
}



static void
transformNoneMixing(FILE * const ifP,
                   int const cols, int const rows,
                   xelval const maxval, int const format,
                   int const newcols, int const newrows,
                   xelval const newmaxval, int const newformat,
                   bool const inverse,
                   bool const verbose, bool const vverbose)
{
    /*
    Transform the map image on input file 'ifP' (which is described by
    'cols', 'rows', 'format', and 'maxval') to a Mercator projection and
    write the result to standard output as format 'newformat' and with
    maxval 'newmaxval'.

    Don't mix colors from different input pixels together in the output
    pixels.  Each output pixel is an exact copy of some corresponding
    input pixel.
    */

    xel* xelrow;          /* an input row */
    xel* newxelrow;         /* the output row */

    int row;
    double fRow;
    int rowInXelrow;
    int inputRow;
    int col;

    double fLatRad;
    double fLatMerc;

    xelrow = pnm_allocrow(cols);
    rowInXelrow = 0;

    newxelrow = pnm_allocrow(newcols);

    for (row = 0; row < newrows; ++row) {

        fRow = (double)row + 0.5; /* center on half the pixel */
        if (!inverse) {
            /* the result is mercator, calculate back to degrees */
            fLatMerc = 2.0 * M_PI * (fRow / (double)newrows - 0.5);
                /* merc goes from -pi to +pi */
            fLatRad = 2.0 * (atan(exp(fLatMerc)) - M_PI / 4.0);
            fRow = ((fLatRad / M_PI) + 0.5) * (double)rows;
                /* lat goes from -pi/2 to +pi/2 */
        } else {
            /* the result is in degrees, calculate back to mercator */
            fLatRad = M_PI * (fRow / (double)newrows - 0.5);
                /* lat goes from -pi/2 to +pi/2 */
            fLatMerc = log(tan((M_PI / 4.0 + fLatRad / 2.0)));
            fRow = ((fLatMerc / M_PI / 2.0) + 0.5) * (double)rows;
                /* merc goes from -pi to +pi */
        }
        /* fRow -= 0.5; */        /* it's weird that this isn't needed */

        inputRow = (int) floor(fRow);
        if (inputRow < 0)
            inputRow = 0;
        if (inputRow > rows - 1)
            inputRow = rows - 1;

        if (vverbose) {
            if (!inverse) {
#ifdef DBG_EDGES
                if ((row < 10) || (row > (newrows - 10)))
#else
  #ifdef DBG_MIDDLE
                if ((row > (newrows/2 - 10)) && (row < (newrows/2 + 10)))
  #else
                if (row % 20 == 0)
  #endif
#endif
                    pm_message("outputRow=%d latMerc=%f latRad=%f inputRow=%d"
                               "fRow=%f",
                               row, fLatMerc, fLatRad, inputRow, fRow);
            } else {
#ifdef DBG_EDGES
                if ((row < 10) || (row > (newrows - 10)))
#else
  #ifdef DBG_MIDDLE
                if ((row > (newrows/2 - 10)) && (row < (newrows/2 + 10)))
  #else
                if (row % 10 == 0)
  #endif
#endif
                    pm_message("outputRow=%d latRad=%f latMerc=%f inputRow=%d"
                               "fRow=%f",
                               row, fLatRad, fLatMerc, inputRow, fRow);
            }
        }

        while ((rowInXelrow <= inputRow) && (rowInXelrow < rows)) {
#ifdef DBG_EDGES
            if ((row < 10) || (row > (newrows - 10)))
                pm_message("read row - rowInXelrow=%d inputRow=%d",
                           rowInXelrow, inputRow);
#endif
            pnm_readpnmrow(ifP, xelrow, cols, newmaxval, format);
            ++rowInXelrow;
        }
        for (col = 0; col < newcols; ++col) {
            newxelrow[col] = xelrow[col];
        }

        pnm_writepnmrow(stdout, newxelrow, newcols, newmaxval, newformat, 0 );
    }

    pnm_freerow(xelrow);
    pnm_freerow(newxelrow);
}



int
main(int argc, const char ** argv )
{
    struct cmdlineInfo cmdline;
    FILE* ifP;
    int rows, cols, format, newformat, newrows, newcols;
    xelval maxval, newmaxval;
    bool verbose;

    pm_proginit( &argc, argv );

    parseCommandLine(argc, argv, &cmdline);

    verbose = cmdline.verbose || cmdline.vverbose;

    ifP = pm_openr(cmdline.input_filespec);

    pnm_readpnminit( ifP, &cols, &rows, &maxval, &format );

    /* Promote PBM file to PGM. */
    if ( PNM_FORMAT_TYPE(format) == PBM_TYPE ) {
        newformat = PGM_TYPE;
        newmaxval = PGM_MAXMAXVAL;
        pm_message( "promoting from PBM to PGM" );
    } else {
        newformat = format;
        newmaxval = maxval;
    }

    computeOutputDimensions(cmdline, rows, cols, &newrows, &newcols);

    pnm_writepnminit(stdout, newcols, newrows, newmaxval, newformat, 0);

    if (cmdline.nomix) {
        if (verbose)
            pm_message("Transforming map without mixing/blending colors");
        transformNoneMixing(ifP, cols, rows, maxval, format,
                            newcols, newrows, newmaxval, newformat,
                            cmdline.inverse, verbose, cmdline.vverbose);
    } else {
        if (verbose)
            pm_message("Transforming map while using intermediate colors");
        transformWithMixing(ifP, cols, rows, maxval, format,
                            newcols, newrows, newmaxval, newformat,
                            cmdline.inverse, verbose, cmdline.vverbose);
    }

    pm_close(ifP);
    pm_close(stdout);

    return 0;
}