annotate map2ppm.c @ 1771:72adabd8e746

More cleanups.
author Matti Hamalainen <ccr@tnsp.org>
date Fri, 27 Oct 2017 05:10:08 +0300
parents cc59f80b0e78
children 79dd960610cb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
96
3d10542703a9 New utility for converting map files to PPM format images
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1 /*
1063
75a7b2594d93 Add PNG output support to map2ppm.
Matti Hamalainen <ccr@tnsp.org>
parents: 1056
diff changeset
2 * Convert BatMUD ASCII map to PPM or PNG image file
96
3d10542703a9 New utility for converting map files to PPM format images
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3 * Programmed by Matti 'ccr' Hämäläinen (Ggr Pupunen)
1487
e72577821dc6 Update copyrights.
Matti Hamalainen <ccr@tnsp.org>
parents: 1484
diff changeset
4 * (C) Copyright 2006-2015 Tecnic Software productions (TNSP)
96
3d10542703a9 New utility for converting map files to PPM format images
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
5 */
1308
d11f9751e299 Renamed maputils.[ch] -> libmaputils.[ch], adjusted other code accordingly;
Matti Hamalainen <ccr@tnsp.org>
parents: 1249
diff changeset
6 #include "libmaputils.h"
96
3d10542703a9 New utility for converting map files to PPM format images
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
7 #include "th_args.h"
3d10542703a9 New utility for converting map files to PPM format images
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
8 #include "th_string.h"
3d10542703a9 New utility for converting map files to PPM format images
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
9
1063
75a7b2594d93 Add PNG output support to map2ppm.
Matti Hamalainen <ccr@tnsp.org>
parents: 1056
diff changeset
10 #ifdef HAVE_LIBPNG
75a7b2594d93 Add PNG output support to map2ppm.
Matti Hamalainen <ccr@tnsp.org>
parents: 1056
diff changeset
11 #include <png.h>
75a7b2594d93 Add PNG output support to map2ppm.
Matti Hamalainen <ccr@tnsp.org>
parents: 1056
diff changeset
12 #endif
96
3d10542703a9 New utility for converting map files to PPM format images
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
13
714
304a50a9d51f Indentation fixes.
Matti Hamalainen <ccr@tnsp.org>
parents: 574
diff changeset
14 char *srcFilename = NULL,
304a50a9d51f Indentation fixes.
Matti Hamalainen <ccr@tnsp.org>
parents: 574
diff changeset
15 *destFilename = NULL;
96
3d10542703a9 New utility for converting map files to PPM format images
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
16
714
304a50a9d51f Indentation fixes.
Matti Hamalainen <ccr@tnsp.org>
parents: 574
diff changeset
17 BOOL optUseOldFormat = FALSE,
304a50a9d51f Indentation fixes.
Matti Hamalainen <ccr@tnsp.org>
parents: 574
diff changeset
18 optInputIsDiff = FALSE,
304a50a9d51f Indentation fixes.
Matti Hamalainen <ccr@tnsp.org>
parents: 574
diff changeset
19 optUseANSI = FALSE,
304a50a9d51f Indentation fixes.
Matti Hamalainen <ccr@tnsp.org>
parents: 574
diff changeset
20 optCityFormat = FALSE;
784
c80fb894df24 Add scaling of output image size (by integer value).
Matti Hamalainen <ccr@tnsp.org>
parents: 714
diff changeset
21 int optScale = 1;
1063
75a7b2594d93 Add PNG output support to map2ppm.
Matti Hamalainen <ccr@tnsp.org>
parents: 1056
diff changeset
22 #ifdef HAVE_LIBPNG
75a7b2594d93 Add PNG output support to map2ppm.
Matti Hamalainen <ccr@tnsp.org>
parents: 1056
diff changeset
23 int optPNGLevel = -1;
75a7b2594d93 Add PNG output support to map2ppm.
Matti Hamalainen <ccr@tnsp.org>
parents: 1056
diff changeset
24 #endif
96
3d10542703a9 New utility for converting map files to PPM format images
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
25
1543
0da2d8e74583 Cosmetics pass.
Matti Hamalainen <ccr@tnsp.org>
parents: 1487
diff changeset
26
96
3d10542703a9 New utility for converting map files to PPM format images
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
27 /* Arguments
3d10542703a9 New utility for converting map files to PPM format images
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
28 */
1615
582675ccc3e6 Match th_args API change.
Matti Hamalainen <ccr@tnsp.org>
parents: 1552
diff changeset
29 static const th_optarg optList[] =
1484
2350cd3a6feb Update to match the changes in th_args API.
Matti Hamalainen <ccr@tnsp.org>
parents: 1470
diff changeset
30 {
714
304a50a9d51f Indentation fixes.
Matti Hamalainen <ccr@tnsp.org>
parents: 574
diff changeset
31 { 0, '?', "help", "Show this help", OPT_NONE },
304a50a9d51f Indentation fixes.
Matti Hamalainen <ccr@tnsp.org>
parents: 574
diff changeset
32 { 1, 'v', "verbose", "Be more verbose", OPT_NONE },
304a50a9d51f Indentation fixes.
Matti Hamalainen <ccr@tnsp.org>
parents: 574
diff changeset
33 { 2, 'q', "quiet", "Be quiet", OPT_NONE },
304a50a9d51f Indentation fixes.
Matti Hamalainen <ccr@tnsp.org>
parents: 574
diff changeset
34 { 3, 'd', "input-diff", "Input is a diff", OPT_NONE },
304a50a9d51f Indentation fixes.
Matti Hamalainen <ccr@tnsp.org>
parents: 574
diff changeset
35 { 4, 'O', "old-format", "Input using old symbols/colors", OPT_NONE },
304a50a9d51f Indentation fixes.
Matti Hamalainen <ccr@tnsp.org>
parents: 574
diff changeset
36 { 5, 'o', "output", "Output filename", OPT_ARGREQ },
304a50a9d51f Indentation fixes.
Matti Hamalainen <ccr@tnsp.org>
parents: 574
diff changeset
37 { 6, 'A', "ansi-colors", "Use ANSI colors", OPT_NONE },
304a50a9d51f Indentation fixes.
Matti Hamalainen <ccr@tnsp.org>
parents: 574
diff changeset
38 { 7, 'c', "city-format", "Input is a city map", OPT_NONE },
784
c80fb894df24 Add scaling of output image size (by integer value).
Matti Hamalainen <ccr@tnsp.org>
parents: 714
diff changeset
39 { 8, 's', "scale", "Scale value (integer)", OPT_ARGREQ },
1063
75a7b2594d93 Add PNG output support to map2ppm.
Matti Hamalainen <ccr@tnsp.org>
parents: 1056
diff changeset
40
75a7b2594d93 Add PNG output support to map2ppm.
Matti Hamalainen <ccr@tnsp.org>
parents: 1056
diff changeset
41 #ifdef HAVE_LIBPNG
75a7b2594d93 Add PNG output support to map2ppm.
Matti Hamalainen <ccr@tnsp.org>
parents: 1056
diff changeset
42 { 9, 'P', "png", "PNG format output (compression level 0-9)", OPT_ARGREQ },
75a7b2594d93 Add PNG output support to map2ppm.
Matti Hamalainen <ccr@tnsp.org>
parents: 1056
diff changeset
43 #endif
96
3d10542703a9 New utility for converting map files to PPM format images
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
44 };
3d10542703a9 New utility for converting map files to PPM format images
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
45
1484
2350cd3a6feb Update to match the changes in th_args API.
Matti Hamalainen <ccr@tnsp.org>
parents: 1470
diff changeset
46 static const int optListN = sizeof(optList) / sizeof(optList[0]);
96
3d10542703a9 New utility for converting map files to PPM format images
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
47
3d10542703a9 New utility for converting map files to PPM format images
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
48
3d10542703a9 New utility for converting map files to PPM format images
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
49 void argShowHelp(void)
3d10542703a9 New utility for converting map files to PPM format images
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
50 {
1249
0dad7911e251 Sync th-lib changes.
Matti Hamalainen <ccr@tnsp.org>
parents: 1162
diff changeset
51 th_print_banner(stdout, th_prog_name,
714
304a50a9d51f Indentation fixes.
Matti Hamalainen <ccr@tnsp.org>
parents: 574
diff changeset
52 "[options] <input mapfile>");
1249
0dad7911e251 Sync th-lib changes.
Matti Hamalainen <ccr@tnsp.org>
parents: 1162
diff changeset
53
1484
2350cd3a6feb Update to match the changes in th_args API.
Matti Hamalainen <ccr@tnsp.org>
parents: 1470
diff changeset
54 th_args_help(stdout, optList, optListN, 0);
96
3d10542703a9 New utility for converting map files to PPM format images
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
55 }
3d10542703a9 New utility for converting map files to PPM format images
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
56
3d10542703a9 New utility for converting map files to PPM format images
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
57
3d10542703a9 New utility for converting map files to PPM format images
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
58 BOOL argHandleOpt(const int optN, char *optArg, char *currArg)
3d10542703a9 New utility for converting map files to PPM format images
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
59 {
1314
5d6ddd4d6781 Import more changes from Map Mask Designer, and do a cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 1310
diff changeset
60 switch (optN)
5d6ddd4d6781 Import more changes from Map Mask Designer, and do a cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 1310
diff changeset
61 {
714
304a50a9d51f Indentation fixes.
Matti Hamalainen <ccr@tnsp.org>
parents: 574
diff changeset
62 case 0:
304a50a9d51f Indentation fixes.
Matti Hamalainen <ccr@tnsp.org>
parents: 574
diff changeset
63 argShowHelp();
304a50a9d51f Indentation fixes.
Matti Hamalainen <ccr@tnsp.org>
parents: 574
diff changeset
64 exit(0);
304a50a9d51f Indentation fixes.
Matti Hamalainen <ccr@tnsp.org>
parents: 574
diff changeset
65 break;
96
3d10542703a9 New utility for converting map files to PPM format images
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
66
714
304a50a9d51f Indentation fixes.
Matti Hamalainen <ccr@tnsp.org>
parents: 574
diff changeset
67 case 1:
304a50a9d51f Indentation fixes.
Matti Hamalainen <ccr@tnsp.org>
parents: 574
diff changeset
68 th_verbosityLevel++;
304a50a9d51f Indentation fixes.
Matti Hamalainen <ccr@tnsp.org>
parents: 574
diff changeset
69 break;
1543
0da2d8e74583 Cosmetics pass.
Matti Hamalainen <ccr@tnsp.org>
parents: 1487
diff changeset
70
714
304a50a9d51f Indentation fixes.
Matti Hamalainen <ccr@tnsp.org>
parents: 574
diff changeset
71 case 2:
304a50a9d51f Indentation fixes.
Matti Hamalainen <ccr@tnsp.org>
parents: 574
diff changeset
72 th_verbosityLevel = -1;
304a50a9d51f Indentation fixes.
Matti Hamalainen <ccr@tnsp.org>
parents: 574
diff changeset
73 break;
1543
0da2d8e74583 Cosmetics pass.
Matti Hamalainen <ccr@tnsp.org>
parents: 1487
diff changeset
74
714
304a50a9d51f Indentation fixes.
Matti Hamalainen <ccr@tnsp.org>
parents: 574
diff changeset
75 case 3:
304a50a9d51f Indentation fixes.
Matti Hamalainen <ccr@tnsp.org>
parents: 574
diff changeset
76 optInputIsDiff = TRUE;
304a50a9d51f Indentation fixes.
Matti Hamalainen <ccr@tnsp.org>
parents: 574
diff changeset
77 THMSG(2, "Input is a 'diff', handling it as such.\n");
304a50a9d51f Indentation fixes.
Matti Hamalainen <ccr@tnsp.org>
parents: 574
diff changeset
78 break;
1543
0da2d8e74583 Cosmetics pass.
Matti Hamalainen <ccr@tnsp.org>
parents: 1487
diff changeset
79
714
304a50a9d51f Indentation fixes.
Matti Hamalainen <ccr@tnsp.org>
parents: 574
diff changeset
80 case 4:
304a50a9d51f Indentation fixes.
Matti Hamalainen <ccr@tnsp.org>
parents: 574
diff changeset
81 optUseOldFormat = TRUE;
304a50a9d51f Indentation fixes.
Matti Hamalainen <ccr@tnsp.org>
parents: 574
diff changeset
82 THMSG(2, "Input is using old map symbols/colors.\n");
304a50a9d51f Indentation fixes.
Matti Hamalainen <ccr@tnsp.org>
parents: 574
diff changeset
83 break;
1543
0da2d8e74583 Cosmetics pass.
Matti Hamalainen <ccr@tnsp.org>
parents: 1487
diff changeset
84
714
304a50a9d51f Indentation fixes.
Matti Hamalainen <ccr@tnsp.org>
parents: 574
diff changeset
85 case 5:
304a50a9d51f Indentation fixes.
Matti Hamalainen <ccr@tnsp.org>
parents: 574
diff changeset
86 destFilename = optArg;
304a50a9d51f Indentation fixes.
Matti Hamalainen <ccr@tnsp.org>
parents: 574
diff changeset
87 THMSG(2, "Output file set to '%s'.\n", destFilename);
304a50a9d51f Indentation fixes.
Matti Hamalainen <ccr@tnsp.org>
parents: 574
diff changeset
88 break;
1543
0da2d8e74583 Cosmetics pass.
Matti Hamalainen <ccr@tnsp.org>
parents: 1487
diff changeset
89
714
304a50a9d51f Indentation fixes.
Matti Hamalainen <ccr@tnsp.org>
parents: 574
diff changeset
90 case 6:
304a50a9d51f Indentation fixes.
Matti Hamalainen <ccr@tnsp.org>
parents: 574
diff changeset
91 optUseANSI = TRUE;
304a50a9d51f Indentation fixes.
Matti Hamalainen <ccr@tnsp.org>
parents: 574
diff changeset
92 THMSG(2, "Using ANSI colors.\n");
304a50a9d51f Indentation fixes.
Matti Hamalainen <ccr@tnsp.org>
parents: 574
diff changeset
93 break;
1543
0da2d8e74583 Cosmetics pass.
Matti Hamalainen <ccr@tnsp.org>
parents: 1487
diff changeset
94
714
304a50a9d51f Indentation fixes.
Matti Hamalainen <ccr@tnsp.org>
parents: 574
diff changeset
95 case 7:
304a50a9d51f Indentation fixes.
Matti Hamalainen <ccr@tnsp.org>
parents: 574
diff changeset
96 optCityFormat = TRUE;
304a50a9d51f Indentation fixes.
Matti Hamalainen <ccr@tnsp.org>
parents: 574
diff changeset
97 THMSG(2, "Input is handled as a city map\n");
304a50a9d51f Indentation fixes.
Matti Hamalainen <ccr@tnsp.org>
parents: 574
diff changeset
98 break;
401
bc6171789b80 Support for citymap symbols/colors.
Matti Hamalainen <ccr@tnsp.org>
parents: 378
diff changeset
99
784
c80fb894df24 Add scaling of output image size (by integer value).
Matti Hamalainen <ccr@tnsp.org>
parents: 714
diff changeset
100 case 8:
c80fb894df24 Add scaling of output image size (by integer value).
Matti Hamalainen <ccr@tnsp.org>
parents: 714
diff changeset
101 optScale = atoi(optArg);
1314
5d6ddd4d6781 Import more changes from Map Mask Designer, and do a cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 1310
diff changeset
102 if (optScale < 1 || optScale > 50)
5d6ddd4d6781 Import more changes from Map Mask Designer, and do a cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 1310
diff changeset
103 {
784
c80fb894df24 Add scaling of output image size (by integer value).
Matti Hamalainen <ccr@tnsp.org>
parents: 714
diff changeset
104 THERR("Invalid scale value %d, must be 1 < x < 50.\n", optScale);
c80fb894df24 Add scaling of output image size (by integer value).
Matti Hamalainen <ccr@tnsp.org>
parents: 714
diff changeset
105 return FALSE;
c80fb894df24 Add scaling of output image size (by integer value).
Matti Hamalainen <ccr@tnsp.org>
parents: 714
diff changeset
106 }
c80fb894df24 Add scaling of output image size (by integer value).
Matti Hamalainen <ccr@tnsp.org>
parents: 714
diff changeset
107 THMSG(2, "Output scaling set to %d.\n", optScale);
c80fb894df24 Add scaling of output image size (by integer value).
Matti Hamalainen <ccr@tnsp.org>
parents: 714
diff changeset
108 break;
1063
75a7b2594d93 Add PNG output support to map2ppm.
Matti Hamalainen <ccr@tnsp.org>
parents: 1056
diff changeset
109
75a7b2594d93 Add PNG output support to map2ppm.
Matti Hamalainen <ccr@tnsp.org>
parents: 1056
diff changeset
110
75a7b2594d93 Add PNG output support to map2ppm.
Matti Hamalainen <ccr@tnsp.org>
parents: 1056
diff changeset
111 #ifdef HAVE_LIBPNG
75a7b2594d93 Add PNG output support to map2ppm.
Matti Hamalainen <ccr@tnsp.org>
parents: 1056
diff changeset
112 case 9:
75a7b2594d93 Add PNG output support to map2ppm.
Matti Hamalainen <ccr@tnsp.org>
parents: 1056
diff changeset
113 optPNGLevel = atoi(optArg);
1314
5d6ddd4d6781 Import more changes from Map Mask Designer, and do a cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 1310
diff changeset
114 if (optPNGLevel < 0 || optPNGLevel > 9)
5d6ddd4d6781 Import more changes from Map Mask Designer, and do a cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 1310
diff changeset
115 {
1063
75a7b2594d93 Add PNG output support to map2ppm.
Matti Hamalainen <ccr@tnsp.org>
parents: 1056
diff changeset
116 THERR("Invalid PNG compression factor %d, must be 0 < x < 9.\n", optPNGLevel);
75a7b2594d93 Add PNG output support to map2ppm.
Matti Hamalainen <ccr@tnsp.org>
parents: 1056
diff changeset
117 return FALSE;
75a7b2594d93 Add PNG output support to map2ppm.
Matti Hamalainen <ccr@tnsp.org>
parents: 1056
diff changeset
118 }
75a7b2594d93 Add PNG output support to map2ppm.
Matti Hamalainen <ccr@tnsp.org>
parents: 1056
diff changeset
119 THMSG(2, "Output format set to PNG, compression level %d.\n", optPNGLevel);
75a7b2594d93 Add PNG output support to map2ppm.
Matti Hamalainen <ccr@tnsp.org>
parents: 1056
diff changeset
120 break;
75a7b2594d93 Add PNG output support to map2ppm.
Matti Hamalainen <ccr@tnsp.org>
parents: 1056
diff changeset
121 #endif
75a7b2594d93 Add PNG output support to map2ppm.
Matti Hamalainen <ccr@tnsp.org>
parents: 1056
diff changeset
122
714
304a50a9d51f Indentation fixes.
Matti Hamalainen <ccr@tnsp.org>
parents: 574
diff changeset
123 default:
304a50a9d51f Indentation fixes.
Matti Hamalainen <ccr@tnsp.org>
parents: 574
diff changeset
124 THERR("Unknown option '%s'.\n", currArg);
304a50a9d51f Indentation fixes.
Matti Hamalainen <ccr@tnsp.org>
parents: 574
diff changeset
125 return FALSE;
304a50a9d51f Indentation fixes.
Matti Hamalainen <ccr@tnsp.org>
parents: 574
diff changeset
126 }
1543
0da2d8e74583 Cosmetics pass.
Matti Hamalainen <ccr@tnsp.org>
parents: 1487
diff changeset
127
714
304a50a9d51f Indentation fixes.
Matti Hamalainen <ccr@tnsp.org>
parents: 574
diff changeset
128 return TRUE;
96
3d10542703a9 New utility for converting map files to PPM format images
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
129 }
3d10542703a9 New utility for converting map files to PPM format images
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
130
3d10542703a9 New utility for converting map files to PPM format images
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
131
3d10542703a9 New utility for converting map files to PPM format images
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
132 BOOL argHandleFile(char *currArg)
3d10542703a9 New utility for converting map files to PPM format images
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
133 {
714
304a50a9d51f Indentation fixes.
Matti Hamalainen <ccr@tnsp.org>
parents: 574
diff changeset
134 if (!srcFilename)
304a50a9d51f Indentation fixes.
Matti Hamalainen <ccr@tnsp.org>
parents: 574
diff changeset
135 srcFilename = currArg;
1314
5d6ddd4d6781 Import more changes from Map Mask Designer, and do a cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 1310
diff changeset
136 else
5d6ddd4d6781 Import more changes from Map Mask Designer, and do a cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 1310
diff changeset
137 {
714
304a50a9d51f Indentation fixes.
Matti Hamalainen <ccr@tnsp.org>
parents: 574
diff changeset
138 THERR("Too many input map files specified!\n");
304a50a9d51f Indentation fixes.
Matti Hamalainen <ccr@tnsp.org>
parents: 574
diff changeset
139 return FALSE;
304a50a9d51f Indentation fixes.
Matti Hamalainen <ccr@tnsp.org>
parents: 574
diff changeset
140 }
96
3d10542703a9 New utility for converting map files to PPM format images
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
141
714
304a50a9d51f Indentation fixes.
Matti Hamalainen <ccr@tnsp.org>
parents: 574
diff changeset
142 return TRUE;
96
3d10542703a9 New utility for converting map files to PPM format images
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
143 }
3d10542703a9 New utility for converting map files to PPM format images
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
144
3d10542703a9 New utility for converting map files to PPM format images
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
145
1762
a35b392a40ce Use uint8_t instead of unsigned char.
Matti Hamalainen <ccr@tnsp.org>
parents: 1692
diff changeset
146 int writeImageData(MapBlock *map, void *cbdata, BOOL (*writeRowCB)(void *, uint8_t *, size_t), int scale)
1063
75a7b2594d93 Add PNG output support to map2ppm.
Matti Hamalainen <ccr@tnsp.org>
parents: 1056
diff changeset
147 {
1771
72adabd8e746 More cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1770
diff changeset
148 int res = 0;
1762
a35b392a40ce Use uint8_t instead of unsigned char.
Matti Hamalainen <ccr@tnsp.org>
parents: 1692
diff changeset
149 uint8_t *row = NULL;
1063
75a7b2594d93 Add PNG output support to map2ppm.
Matti Hamalainen <ccr@tnsp.org>
parents: 1056
diff changeset
150
1770
cc59f80b0e78 Various cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1762
diff changeset
151 // Allocate memory for row buffer
1314
5d6ddd4d6781 Import more changes from Map Mask Designer, and do a cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 1310
diff changeset
152 if ((row = th_malloc(map->width * 3 * scale + 16)) == NULL)
5d6ddd4d6781 Import more changes from Map Mask Designer, and do a cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 1310
diff changeset
153 {
1094
cdb9db1234be Fix two memory leaks, though they are not significant.
Matti Hamalainen <ccr@tnsp.org>
parents: 1066
diff changeset
154 res = -16;
cdb9db1234be Fix two memory leaks, though they are not significant.
Matti Hamalainen <ccr@tnsp.org>
parents: 1066
diff changeset
155 goto done;
cdb9db1234be Fix two memory leaks, though they are not significant.
Matti Hamalainen <ccr@tnsp.org>
parents: 1066
diff changeset
156 }
1543
0da2d8e74583 Cosmetics pass.
Matti Hamalainen <ccr@tnsp.org>
parents: 1487
diff changeset
157
1771
72adabd8e746 More cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1770
diff changeset
158 for (int y = 0; y < map->height; y++)
1314
5d6ddd4d6781 Import more changes from Map Mask Designer, and do a cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 1310
diff changeset
159 {
1762
a35b392a40ce Use uint8_t instead of unsigned char.
Matti Hamalainen <ccr@tnsp.org>
parents: 1692
diff changeset
160 uint8_t *ptr = row;
1063
75a7b2594d93 Add PNG output support to map2ppm.
Matti Hamalainen <ccr@tnsp.org>
parents: 1056
diff changeset
161
1771
72adabd8e746 More cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1770
diff changeset
162 for (int x = 0; x < map->width; x++)
1314
5d6ddd4d6781 Import more changes from Map Mask Designer, and do a cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 1310
diff changeset
163 {
1063
75a7b2594d93 Add PNG output support to map2ppm.
Matti Hamalainen <ccr@tnsp.org>
parents: 1056
diff changeset
164 int qr, qg, qb, c;
75a7b2594d93 Add PNG output support to map2ppm.
Matti Hamalainen <ccr@tnsp.org>
parents: 1056
diff changeset
165 qr = 255; qg = qb = 0;
1762
a35b392a40ce Use uint8_t instead of unsigned char.
Matti Hamalainen <ccr@tnsp.org>
parents: 1692
diff changeset
166 c = (uint8_t) map->data[(y * map->scansize) + x];
1543
0da2d8e74583 Cosmetics pass.
Matti Hamalainen <ccr@tnsp.org>
parents: 1487
diff changeset
167
1314
5d6ddd4d6781 Import more changes from Map Mask Designer, and do a cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 1310
diff changeset
168 if (optInputIsDiff)
5d6ddd4d6781 Import more changes from Map Mask Designer, and do a cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 1310
diff changeset
169 {
5d6ddd4d6781 Import more changes from Map Mask Designer, and do a cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 1310
diff changeset
170 if (c < nmapPieces)
5d6ddd4d6781 Import more changes from Map Mask Designer, and do a cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 1310
diff changeset
171 {
5d6ddd4d6781 Import more changes from Map Mask Designer, and do a cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 1310
diff changeset
172 if (optUseANSI)
5d6ddd4d6781 Import more changes from Map Mask Designer, and do a cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 1310
diff changeset
173 {
1063
75a7b2594d93 Add PNG output support to map2ppm.
Matti Hamalainen <ccr@tnsp.org>
parents: 1056
diff changeset
174 qr = qg = qb = 255;
1314
5d6ddd4d6781 Import more changes from Map Mask Designer, and do a cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 1310
diff changeset
175 }
5d6ddd4d6781 Import more changes from Map Mask Designer, and do a cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 1310
diff changeset
176 else
5d6ddd4d6781 Import more changes from Map Mask Designer, and do a cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 1310
diff changeset
177 {
1063
75a7b2594d93 Add PNG output support to map2ppm.
Matti Hamalainen <ccr@tnsp.org>
parents: 1056
diff changeset
178 c = c & 63;
75a7b2594d93 Add PNG output support to map2ppm.
Matti Hamalainen <ccr@tnsp.org>
parents: 1056
diff changeset
179 qr = mapPieces[c].r;
75a7b2594d93 Add PNG output support to map2ppm.
Matti Hamalainen <ccr@tnsp.org>
parents: 1056
diff changeset
180 qg = mapPieces[c].g;
75a7b2594d93 Add PNG output support to map2ppm.
Matti Hamalainen <ccr@tnsp.org>
parents: 1056
diff changeset
181 qb = mapPieces[c].b;
75a7b2594d93 Add PNG output support to map2ppm.
Matti Hamalainen <ccr@tnsp.org>
parents: 1056
diff changeset
182 }
75a7b2594d93 Add PNG output support to map2ppm.
Matti Hamalainen <ccr@tnsp.org>
parents: 1056
diff changeset
183 }
1314
5d6ddd4d6781 Import more changes from Map Mask Designer, and do a cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 1310
diff changeset
184 }
1552
a122d4801a15 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 1543
diff changeset
185 else
a122d4801a15 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 1543
diff changeset
186 if (optUseANSI)
1314
5d6ddd4d6781 Import more changes from Map Mask Designer, and do a cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 1310
diff changeset
187 {
1470
e5502fc43a7d Rename some functions, make few static.
Matti Hamalainen <ccr@tnsp.org>
parents: 1360
diff changeset
188 if ((c = muGetMapPieceColor(c, optUseOldFormat, optCityFormat)) >= 0)
1314
5d6ddd4d6781 Import more changes from Map Mask Designer, and do a cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 1310
diff changeset
189 {
1063
75a7b2594d93 Add PNG output support to map2ppm.
Matti Hamalainen <ccr@tnsp.org>
parents: 1056
diff changeset
190 qr = mapColors[c].r;
75a7b2594d93 Add PNG output support to map2ppm.
Matti Hamalainen <ccr@tnsp.org>
parents: 1056
diff changeset
191 qg = mapColors[c].g;
75a7b2594d93 Add PNG output support to map2ppm.
Matti Hamalainen <ccr@tnsp.org>
parents: 1056
diff changeset
192 qb = mapColors[c].b;
75a7b2594d93 Add PNG output support to map2ppm.
Matti Hamalainen <ccr@tnsp.org>
parents: 1056
diff changeset
193 }
1314
5d6ddd4d6781 Import more changes from Map Mask Designer, and do a cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 1310
diff changeset
194 }
5d6ddd4d6781 Import more changes from Map Mask Designer, and do a cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 1310
diff changeset
195 else
5d6ddd4d6781 Import more changes from Map Mask Designer, and do a cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 1310
diff changeset
196 {
1470
e5502fc43a7d Rename some functions, make few static.
Matti Hamalainen <ccr@tnsp.org>
parents: 1360
diff changeset
197 if ((c = muGetMapPieceIndex(c, optUseOldFormat, optCityFormat)) >= 0)
1314
5d6ddd4d6781 Import more changes from Map Mask Designer, and do a cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 1310
diff changeset
198 {
1063
75a7b2594d93 Add PNG output support to map2ppm.
Matti Hamalainen <ccr@tnsp.org>
parents: 1056
diff changeset
199 qr = mapPieces[c].r;
75a7b2594d93 Add PNG output support to map2ppm.
Matti Hamalainen <ccr@tnsp.org>
parents: 1056
diff changeset
200 qg = mapPieces[c].g;
75a7b2594d93 Add PNG output support to map2ppm.
Matti Hamalainen <ccr@tnsp.org>
parents: 1056
diff changeset
201 qb = mapPieces[c].b;
75a7b2594d93 Add PNG output support to map2ppm.
Matti Hamalainen <ccr@tnsp.org>
parents: 1056
diff changeset
202 }
75a7b2594d93 Add PNG output support to map2ppm.
Matti Hamalainen <ccr@tnsp.org>
parents: 1056
diff changeset
203 }
1543
0da2d8e74583 Cosmetics pass.
Matti Hamalainen <ccr@tnsp.org>
parents: 1487
diff changeset
204
1771
72adabd8e746 More cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1770
diff changeset
205 for (int xscale = 0; xscale < scale; xscale++)
1314
5d6ddd4d6781 Import more changes from Map Mask Designer, and do a cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 1310
diff changeset
206 {
1063
75a7b2594d93 Add PNG output support to map2ppm.
Matti Hamalainen <ccr@tnsp.org>
parents: 1056
diff changeset
207 *ptr++ = qr;
75a7b2594d93 Add PNG output support to map2ppm.
Matti Hamalainen <ccr@tnsp.org>
parents: 1056
diff changeset
208 *ptr++ = qg;
75a7b2594d93 Add PNG output support to map2ppm.
Matti Hamalainen <ccr@tnsp.org>
parents: 1056
diff changeset
209 *ptr++ = qb;
75a7b2594d93 Add PNG output support to map2ppm.
Matti Hamalainen <ccr@tnsp.org>
parents: 1056
diff changeset
210 }
75a7b2594d93 Add PNG output support to map2ppm.
Matti Hamalainen <ccr@tnsp.org>
parents: 1056
diff changeset
211 }
75a7b2594d93 Add PNG output support to map2ppm.
Matti Hamalainen <ccr@tnsp.org>
parents: 1056
diff changeset
212
1771
72adabd8e746 More cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1770
diff changeset
213 for (int yscale = 0; yscale < scale; yscale++)
1314
5d6ddd4d6781 Import more changes from Map Mask Designer, and do a cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 1310
diff changeset
214 {
5d6ddd4d6781 Import more changes from Map Mask Designer, and do a cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 1310
diff changeset
215 if (!writeRowCB(cbdata, row, map->width * 3 * scale))
5d6ddd4d6781 Import more changes from Map Mask Designer, and do a cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 1310
diff changeset
216 {
1094
cdb9db1234be Fix two memory leaks, though they are not significant.
Matti Hamalainen <ccr@tnsp.org>
parents: 1066
diff changeset
217 res = -32;
cdb9db1234be Fix two memory leaks, though they are not significant.
Matti Hamalainen <ccr@tnsp.org>
parents: 1066
diff changeset
218 goto done;
cdb9db1234be Fix two memory leaks, though they are not significant.
Matti Hamalainen <ccr@tnsp.org>
parents: 1066
diff changeset
219 }
1063
75a7b2594d93 Add PNG output support to map2ppm.
Matti Hamalainen <ccr@tnsp.org>
parents: 1056
diff changeset
220 }
75a7b2594d93 Add PNG output support to map2ppm.
Matti Hamalainen <ccr@tnsp.org>
parents: 1056
diff changeset
221 }
1094
cdb9db1234be Fix two memory leaks, though they are not significant.
Matti Hamalainen <ccr@tnsp.org>
parents: 1066
diff changeset
222
cdb9db1234be Fix two memory leaks, though they are not significant.
Matti Hamalainen <ccr@tnsp.org>
parents: 1066
diff changeset
223 done:
1543
0da2d8e74583 Cosmetics pass.
Matti Hamalainen <ccr@tnsp.org>
parents: 1487
diff changeset
224 th_free(row);
1094
cdb9db1234be Fix two memory leaks, though they are not significant.
Matti Hamalainen <ccr@tnsp.org>
parents: 1066
diff changeset
225 return res;
1063
75a7b2594d93 Add PNG output support to map2ppm.
Matti Hamalainen <ccr@tnsp.org>
parents: 1056
diff changeset
226 }
75a7b2594d93 Add PNG output support to map2ppm.
Matti Hamalainen <ccr@tnsp.org>
parents: 1056
diff changeset
227
75a7b2594d93 Add PNG output support to map2ppm.
Matti Hamalainen <ccr@tnsp.org>
parents: 1056
diff changeset
228
1762
a35b392a40ce Use uint8_t instead of unsigned char.
Matti Hamalainen <ccr@tnsp.org>
parents: 1692
diff changeset
229 BOOL writePPMRow(void *cbdata, uint8_t *row, size_t len)
1063
75a7b2594d93 Add PNG output support to map2ppm.
Matti Hamalainen <ccr@tnsp.org>
parents: 1056
diff changeset
230 {
1762
a35b392a40ce Use uint8_t instead of unsigned char.
Matti Hamalainen <ccr@tnsp.org>
parents: 1692
diff changeset
231 return fwrite(row, sizeof(uint8_t), len, (FILE *) cbdata) == len;
1063
75a7b2594d93 Add PNG output support to map2ppm.
Matti Hamalainen <ccr@tnsp.org>
parents: 1056
diff changeset
232 }
75a7b2594d93 Add PNG output support to map2ppm.
Matti Hamalainen <ccr@tnsp.org>
parents: 1056
diff changeset
233
1317
bdecb935b4cb Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 1314
diff changeset
234
1309
5cd310e1dab9 Rename some structures/data types.
Matti Hamalainen <ccr@tnsp.org>
parents: 1308
diff changeset
235 int writePPMFile(FILE *outFile, MapBlock *map, int scale)
1063
75a7b2594d93 Add PNG output support to map2ppm.
Matti Hamalainen <ccr@tnsp.org>
parents: 1056
diff changeset
236 {
1770
cc59f80b0e78 Various cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1762
diff changeset
237 // Write header for 24-bit PPM
1063
75a7b2594d93 Add PNG output support to map2ppm.
Matti Hamalainen <ccr@tnsp.org>
parents: 1056
diff changeset
238 fprintf(outFile,
75a7b2594d93 Add PNG output support to map2ppm.
Matti Hamalainen <ccr@tnsp.org>
parents: 1056
diff changeset
239 "P6\n%d %d\n255\n",
1314
5d6ddd4d6781 Import more changes from Map Mask Designer, and do a cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 1310
diff changeset
240 map->width * scale, map->height * scale);
1063
75a7b2594d93 Add PNG output support to map2ppm.
Matti Hamalainen <ccr@tnsp.org>
parents: 1056
diff changeset
241
1770
cc59f80b0e78 Various cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1762
diff changeset
242 // Write image data
1063
75a7b2594d93 Add PNG output support to map2ppm.
Matti Hamalainen <ccr@tnsp.org>
parents: 1056
diff changeset
243 return writeImageData(map, (void *) outFile, writePPMRow, scale);
75a7b2594d93 Add PNG output support to map2ppm.
Matti Hamalainen <ccr@tnsp.org>
parents: 1056
diff changeset
244 }
75a7b2594d93 Add PNG output support to map2ppm.
Matti Hamalainen <ccr@tnsp.org>
parents: 1056
diff changeset
245
75a7b2594d93 Add PNG output support to map2ppm.
Matti Hamalainen <ccr@tnsp.org>
parents: 1056
diff changeset
246
75a7b2594d93 Add PNG output support to map2ppm.
Matti Hamalainen <ccr@tnsp.org>
parents: 1056
diff changeset
247 #ifdef HAVE_LIBPNG
1762
a35b392a40ce Use uint8_t instead of unsigned char.
Matti Hamalainen <ccr@tnsp.org>
parents: 1692
diff changeset
248 BOOL writePNGRow(void *cbdata, uint8_t *row, size_t len)
1063
75a7b2594d93 Add PNG output support to map2ppm.
Matti Hamalainen <ccr@tnsp.org>
parents: 1056
diff changeset
249 {
75a7b2594d93 Add PNG output support to map2ppm.
Matti Hamalainen <ccr@tnsp.org>
parents: 1056
diff changeset
250 png_structp png_ptr = cbdata;
75a7b2594d93 Add PNG output support to map2ppm.
Matti Hamalainen <ccr@tnsp.org>
parents: 1056
diff changeset
251 (void) len;
75a7b2594d93 Add PNG output support to map2ppm.
Matti Hamalainen <ccr@tnsp.org>
parents: 1056
diff changeset
252
1314
5d6ddd4d6781 Import more changes from Map Mask Designer, and do a cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 1310
diff changeset
253 if (setjmp(png_jmpbuf(png_ptr)))
1063
75a7b2594d93 Add PNG output support to map2ppm.
Matti Hamalainen <ccr@tnsp.org>
parents: 1056
diff changeset
254 return FALSE;
75a7b2594d93 Add PNG output support to map2ppm.
Matti Hamalainen <ccr@tnsp.org>
parents: 1056
diff changeset
255
75a7b2594d93 Add PNG output support to map2ppm.
Matti Hamalainen <ccr@tnsp.org>
parents: 1056
diff changeset
256 png_write_row(png_ptr, row);
75a7b2594d93 Add PNG output support to map2ppm.
Matti Hamalainen <ccr@tnsp.org>
parents: 1056
diff changeset
257
75a7b2594d93 Add PNG output support to map2ppm.
Matti Hamalainen <ccr@tnsp.org>
parents: 1056
diff changeset
258 return TRUE;
75a7b2594d93 Add PNG output support to map2ppm.
Matti Hamalainen <ccr@tnsp.org>
parents: 1056
diff changeset
259 }
75a7b2594d93 Add PNG output support to map2ppm.
Matti Hamalainen <ccr@tnsp.org>
parents: 1056
diff changeset
260
75a7b2594d93 Add PNG output support to map2ppm.
Matti Hamalainen <ccr@tnsp.org>
parents: 1056
diff changeset
261
1309
5cd310e1dab9 Rename some structures/data types.
Matti Hamalainen <ccr@tnsp.org>
parents: 1308
diff changeset
262 int writePNGFile(FILE *outFile, MapBlock *map, int scale)
1063
75a7b2594d93 Add PNG output support to map2ppm.
Matti Hamalainen <ccr@tnsp.org>
parents: 1056
diff changeset
263 {
75a7b2594d93 Add PNG output support to map2ppm.
Matti Hamalainen <ccr@tnsp.org>
parents: 1056
diff changeset
264 int width, height;
75a7b2594d93 Add PNG output support to map2ppm.
Matti Hamalainen <ccr@tnsp.org>
parents: 1056
diff changeset
265 png_structp png_ptr;
75a7b2594d93 Add PNG output support to map2ppm.
Matti Hamalainen <ccr@tnsp.org>
parents: 1056
diff changeset
266 png_infop info_ptr;
75a7b2594d93 Add PNG output support to map2ppm.
Matti Hamalainen <ccr@tnsp.org>
parents: 1056
diff changeset
267
1314
5d6ddd4d6781 Import more changes from Map Mask Designer, and do a cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 1310
diff changeset
268 width = map->width * scale;
5d6ddd4d6781 Import more changes from Map Mask Designer, and do a cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 1310
diff changeset
269 height = map->height * scale;
1543
0da2d8e74583 Cosmetics pass.
Matti Hamalainen <ccr@tnsp.org>
parents: 1487
diff changeset
270
1770
cc59f80b0e78 Various cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1762
diff changeset
271 // Create PNG structures
1063
75a7b2594d93 Add PNG output support to map2ppm.
Matti Hamalainen <ccr@tnsp.org>
parents: 1056
diff changeset
272 png_ptr = png_create_write_struct(
75a7b2594d93 Add PNG output support to map2ppm.
Matti Hamalainen <ccr@tnsp.org>
parents: 1056
diff changeset
273 PNG_LIBPNG_VER_STRING,
75a7b2594d93 Add PNG output support to map2ppm.
Matti Hamalainen <ccr@tnsp.org>
parents: 1056
diff changeset
274 NULL, NULL, NULL);
75a7b2594d93 Add PNG output support to map2ppm.
Matti Hamalainen <ccr@tnsp.org>
parents: 1056
diff changeset
275
1314
5d6ddd4d6781 Import more changes from Map Mask Designer, and do a cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 1310
diff changeset
276 if (png_ptr == NULL)
5d6ddd4d6781 Import more changes from Map Mask Designer, and do a cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 1310
diff changeset
277 {
1063
75a7b2594d93 Add PNG output support to map2ppm.
Matti Hamalainen <ccr@tnsp.org>
parents: 1056
diff changeset
278 THERR("PNG: png_create_write_struct() failed.\n");
75a7b2594d93 Add PNG output support to map2ppm.
Matti Hamalainen <ccr@tnsp.org>
parents: 1056
diff changeset
279 return -1;
75a7b2594d93 Add PNG output support to map2ppm.
Matti Hamalainen <ccr@tnsp.org>
parents: 1056
diff changeset
280 }
1543
0da2d8e74583 Cosmetics pass.
Matti Hamalainen <ccr@tnsp.org>
parents: 1487
diff changeset
281
1063
75a7b2594d93 Add PNG output support to map2ppm.
Matti Hamalainen <ccr@tnsp.org>
parents: 1056
diff changeset
282 info_ptr = png_create_info_struct(png_ptr);
1314
5d6ddd4d6781 Import more changes from Map Mask Designer, and do a cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 1310
diff changeset
283 if (info_ptr == NULL)
5d6ddd4d6781 Import more changes from Map Mask Designer, and do a cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 1310
diff changeset
284 {
1063
75a7b2594d93 Add PNG output support to map2ppm.
Matti Hamalainen <ccr@tnsp.org>
parents: 1056
diff changeset
285 THERR("PNG: png_create_info_struct(%p) failed.\n", png_ptr);
75a7b2594d93 Add PNG output support to map2ppm.
Matti Hamalainen <ccr@tnsp.org>
parents: 1056
diff changeset
286 return -2;
75a7b2594d93 Add PNG output support to map2ppm.
Matti Hamalainen <ccr@tnsp.org>
parents: 1056
diff changeset
287 }
1543
0da2d8e74583 Cosmetics pass.
Matti Hamalainen <ccr@tnsp.org>
parents: 1487
diff changeset
288
1314
5d6ddd4d6781 Import more changes from Map Mask Designer, and do a cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 1310
diff changeset
289 if (setjmp(png_jmpbuf(png_ptr)))
5d6ddd4d6781 Import more changes from Map Mask Designer, and do a cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 1310
diff changeset
290 {
1063
75a7b2594d93 Add PNG output support to map2ppm.
Matti Hamalainen <ccr@tnsp.org>
parents: 1056
diff changeset
291 THERR("PNG: Error during PNG init_io().\n");
75a7b2594d93 Add PNG output support to map2ppm.
Matti Hamalainen <ccr@tnsp.org>
parents: 1056
diff changeset
292 return -3;
75a7b2594d93 Add PNG output support to map2ppm.
Matti Hamalainen <ccr@tnsp.org>
parents: 1056
diff changeset
293 }
75a7b2594d93 Add PNG output support to map2ppm.
Matti Hamalainen <ccr@tnsp.org>
parents: 1056
diff changeset
294
75a7b2594d93 Add PNG output support to map2ppm.
Matti Hamalainen <ccr@tnsp.org>
parents: 1056
diff changeset
295 png_init_io(png_ptr, outFile);
75a7b2594d93 Add PNG output support to map2ppm.
Matti Hamalainen <ccr@tnsp.org>
parents: 1056
diff changeset
296
1770
cc59f80b0e78 Various cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1762
diff changeset
297 // Write PNG header info
1314
5d6ddd4d6781 Import more changes from Map Mask Designer, and do a cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 1310
diff changeset
298 if (setjmp(png_jmpbuf(png_ptr)))
5d6ddd4d6781 Import more changes from Map Mask Designer, and do a cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 1310
diff changeset
299 {
1063
75a7b2594d93 Add PNG output support to map2ppm.
Matti Hamalainen <ccr@tnsp.org>
parents: 1056
diff changeset
300 THERR("PNG: Error during writing header.\n");
75a7b2594d93 Add PNG output support to map2ppm.
Matti Hamalainen <ccr@tnsp.org>
parents: 1056
diff changeset
301 return -4;
75a7b2594d93 Add PNG output support to map2ppm.
Matti Hamalainen <ccr@tnsp.org>
parents: 1056
diff changeset
302 }
1543
0da2d8e74583 Cosmetics pass.
Matti Hamalainen <ccr@tnsp.org>
parents: 1487
diff changeset
303
1063
75a7b2594d93 Add PNG output support to map2ppm.
Matti Hamalainen <ccr@tnsp.org>
parents: 1056
diff changeset
304 png_set_IHDR(png_ptr, info_ptr,
75a7b2594d93 Add PNG output support to map2ppm.
Matti Hamalainen <ccr@tnsp.org>
parents: 1056
diff changeset
305 width,
75a7b2594d93 Add PNG output support to map2ppm.
Matti Hamalainen <ccr@tnsp.org>
parents: 1056
diff changeset
306 height,
1770
cc59f80b0e78 Various cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1762
diff changeset
307 8, // bits per component
cc59f80b0e78 Various cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1762
diff changeset
308 PNG_COLOR_TYPE_RGB, // 3 components, RGB
1063
75a7b2594d93 Add PNG output support to map2ppm.
Matti Hamalainen <ccr@tnsp.org>
parents: 1056
diff changeset
309 PNG_INTERLACE_NONE,
75a7b2594d93 Add PNG output support to map2ppm.
Matti Hamalainen <ccr@tnsp.org>
parents: 1056
diff changeset
310 PNG_COMPRESSION_TYPE_DEFAULT,
75a7b2594d93 Add PNG output support to map2ppm.
Matti Hamalainen <ccr@tnsp.org>
parents: 1056
diff changeset
311 PNG_FILTER_TYPE_DEFAULT);
75a7b2594d93 Add PNG output support to map2ppm.
Matti Hamalainen <ccr@tnsp.org>
parents: 1056
diff changeset
312
75a7b2594d93 Add PNG output support to map2ppm.
Matti Hamalainen <ccr@tnsp.org>
parents: 1056
diff changeset
313 // png_set_gAMA(png_ptr, info_ptr, 2.2);
75a7b2594d93 Add PNG output support to map2ppm.
Matti Hamalainen <ccr@tnsp.org>
parents: 1056
diff changeset
314
75a7b2594d93 Add PNG output support to map2ppm.
Matti Hamalainen <ccr@tnsp.org>
parents: 1056
diff changeset
315 png_write_info(png_ptr, info_ptr);
75a7b2594d93 Add PNG output support to map2ppm.
Matti Hamalainen <ccr@tnsp.org>
parents: 1056
diff changeset
316
75a7b2594d93 Add PNG output support to map2ppm.
Matti Hamalainen <ccr@tnsp.org>
parents: 1056
diff changeset
317
1770
cc59f80b0e78 Various cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1762
diff changeset
318 // Write compressed image data
1314
5d6ddd4d6781 Import more changes from Map Mask Designer, and do a cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 1310
diff changeset
319 if (setjmp(png_jmpbuf(png_ptr)))
5d6ddd4d6781 Import more changes from Map Mask Designer, and do a cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 1310
diff changeset
320 {
1063
75a7b2594d93 Add PNG output support to map2ppm.
Matti Hamalainen <ccr@tnsp.org>
parents: 1056
diff changeset
321 THERR("PNG: Error during writing image data.\n");
75a7b2594d93 Add PNG output support to map2ppm.
Matti Hamalainen <ccr@tnsp.org>
parents: 1056
diff changeset
322 return -5;
75a7b2594d93 Add PNG output support to map2ppm.
Matti Hamalainen <ccr@tnsp.org>
parents: 1056
diff changeset
323 }
75a7b2594d93 Add PNG output support to map2ppm.
Matti Hamalainen <ccr@tnsp.org>
parents: 1056
diff changeset
324
75a7b2594d93 Add PNG output support to map2ppm.
Matti Hamalainen <ccr@tnsp.org>
parents: 1056
diff changeset
325 writeImageData(map, (void *) png_ptr, writePNGRow, scale);
75a7b2594d93 Add PNG output support to map2ppm.
Matti Hamalainen <ccr@tnsp.org>
parents: 1056
diff changeset
326
75a7b2594d93 Add PNG output support to map2ppm.
Matti Hamalainen <ccr@tnsp.org>
parents: 1056
diff changeset
327
1770
cc59f80b0e78 Various cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1762
diff changeset
328 // Write footer
1314
5d6ddd4d6781 Import more changes from Map Mask Designer, and do a cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 1310
diff changeset
329 if (setjmp(png_jmpbuf(png_ptr)))
5d6ddd4d6781 Import more changes from Map Mask Designer, and do a cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 1310
diff changeset
330 {
1063
75a7b2594d93 Add PNG output support to map2ppm.
Matti Hamalainen <ccr@tnsp.org>
parents: 1056
diff changeset
331 THERR("PNG: Error during writing image footer.\n");
75a7b2594d93 Add PNG output support to map2ppm.
Matti Hamalainen <ccr@tnsp.org>
parents: 1056
diff changeset
332 return -6;
75a7b2594d93 Add PNG output support to map2ppm.
Matti Hamalainen <ccr@tnsp.org>
parents: 1056
diff changeset
333 }
75a7b2594d93 Add PNG output support to map2ppm.
Matti Hamalainen <ccr@tnsp.org>
parents: 1056
diff changeset
334
75a7b2594d93 Add PNG output support to map2ppm.
Matti Hamalainen <ccr@tnsp.org>
parents: 1056
diff changeset
335 png_write_end(png_ptr, NULL);
75a7b2594d93 Add PNG output support to map2ppm.
Matti Hamalainen <ccr@tnsp.org>
parents: 1056
diff changeset
336
1770
cc59f80b0e78 Various cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1762
diff changeset
337 // Dellocate shit
1063
75a7b2594d93 Add PNG output support to map2ppm.
Matti Hamalainen <ccr@tnsp.org>
parents: 1056
diff changeset
338 if (png_ptr && info_ptr)
75a7b2594d93 Add PNG output support to map2ppm.
Matti Hamalainen <ccr@tnsp.org>
parents: 1056
diff changeset
339 png_destroy_write_struct(&png_ptr, &info_ptr);
75a7b2594d93 Add PNG output support to map2ppm.
Matti Hamalainen <ccr@tnsp.org>
parents: 1056
diff changeset
340
75a7b2594d93 Add PNG output support to map2ppm.
Matti Hamalainen <ccr@tnsp.org>
parents: 1056
diff changeset
341 return 0;
75a7b2594d93 Add PNG output support to map2ppm.
Matti Hamalainen <ccr@tnsp.org>
parents: 1056
diff changeset
342 }
75a7b2594d93 Add PNG output support to map2ppm.
Matti Hamalainen <ccr@tnsp.org>
parents: 1056
diff changeset
343 #endif
75a7b2594d93 Add PNG output support to map2ppm.
Matti Hamalainen <ccr@tnsp.org>
parents: 1056
diff changeset
344
75a7b2594d93 Add PNG output support to map2ppm.
Matti Hamalainen <ccr@tnsp.org>
parents: 1056
diff changeset
345
96
3d10542703a9 New utility for converting map files to PPM format images
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
346 /* Main program
3d10542703a9 New utility for converting map files to PPM format images
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
347 */
3d10542703a9 New utility for converting map files to PPM format images
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
348 int main(int argc, char *argv[])
3d10542703a9 New utility for converting map files to PPM format images
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
349 {
714
304a50a9d51f Indentation fixes.
Matti Hamalainen <ccr@tnsp.org>
parents: 574
diff changeset
350 FILE *outFile;
1309
5cd310e1dab9 Rename some structures/data types.
Matti Hamalainen <ccr@tnsp.org>
parents: 1308
diff changeset
351 MapBlock *map;
1063
75a7b2594d93 Add PNG output support to map2ppm.
Matti Hamalainen <ccr@tnsp.org>
parents: 1056
diff changeset
352 int ret;
96
3d10542703a9 New utility for converting map files to PPM format images
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
353
1770
cc59f80b0e78 Various cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1762
diff changeset
354 // Initialize
1360
5d48d11fd280 Change program description.
Matti Hamalainen <ccr@tnsp.org>
parents: 1317
diff changeset
355 th_init("map2ppm", "ASCII map to PPM/PNG image converter", "0.5", NULL, NULL);
714
304a50a9d51f Indentation fixes.
Matti Hamalainen <ccr@tnsp.org>
parents: 574
diff changeset
356 th_verbosityLevel = 0;
1543
0da2d8e74583 Cosmetics pass.
Matti Hamalainen <ccr@tnsp.org>
parents: 1487
diff changeset
357
1770
cc59f80b0e78 Various cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1762
diff changeset
358 // Parse arguments
714
304a50a9d51f Indentation fixes.
Matti Hamalainen <ccr@tnsp.org>
parents: 574
diff changeset
359 if (!th_args_process(argc, argv, optList, optListN,
1484
2350cd3a6feb Update to match the changes in th_args API.
Matti Hamalainen <ccr@tnsp.org>
parents: 1470
diff changeset
360 argHandleOpt, argHandleFile, OPTH_BAILOUT))
714
304a50a9d51f Indentation fixes.
Matti Hamalainen <ccr@tnsp.org>
parents: 574
diff changeset
361 exit(1);
1543
0da2d8e74583 Cosmetics pass.
Matti Hamalainen <ccr@tnsp.org>
parents: 1487
diff changeset
362
1314
5d6ddd4d6781 Import more changes from Map Mask Designer, and do a cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 1310
diff changeset
363 if (srcFilename == NULL)
5d6ddd4d6781 Import more changes from Map Mask Designer, and do a cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 1310
diff changeset
364 {
714
304a50a9d51f Indentation fixes.
Matti Hamalainen <ccr@tnsp.org>
parents: 574
diff changeset
365 THERR("Nothing to do. (try --help)\n");
304a50a9d51f Indentation fixes.
Matti Hamalainen <ccr@tnsp.org>
parents: 574
diff changeset
366 exit(0);
304a50a9d51f Indentation fixes.
Matti Hamalainen <ccr@tnsp.org>
parents: 574
diff changeset
367 }
1543
0da2d8e74583 Cosmetics pass.
Matti Hamalainen <ccr@tnsp.org>
parents: 1487
diff changeset
368
1770
cc59f80b0e78 Various cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1762
diff changeset
369 // Read input file
714
304a50a9d51f Indentation fixes.
Matti Hamalainen <ccr@tnsp.org>
parents: 574
diff changeset
370 THMSG(1, "Reading map file '%s'\n", srcFilename);
1543
0da2d8e74583 Cosmetics pass.
Matti Hamalainen <ccr@tnsp.org>
parents: 1487
diff changeset
371
1314
5d6ddd4d6781 Import more changes from Map Mask Designer, and do a cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 1310
diff changeset
372 if ((map = mapBlockParseFile(srcFilename, optInputIsDiff)) == NULL)
5d6ddd4d6781 Import more changes from Map Mask Designer, and do a cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 1310
diff changeset
373 {
714
304a50a9d51f Indentation fixes.
Matti Hamalainen <ccr@tnsp.org>
parents: 574
diff changeset
374 THERR("Error reading map file '%s'!\n",
304a50a9d51f Indentation fixes.
Matti Hamalainen <ccr@tnsp.org>
parents: 574
diff changeset
375 srcFilename);
304a50a9d51f Indentation fixes.
Matti Hamalainen <ccr@tnsp.org>
parents: 574
diff changeset
376 exit(1);
304a50a9d51f Indentation fixes.
Matti Hamalainen <ccr@tnsp.org>
parents: 574
diff changeset
377 }
1543
0da2d8e74583 Cosmetics pass.
Matti Hamalainen <ccr@tnsp.org>
parents: 1487
diff changeset
378
1770
cc59f80b0e78 Various cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1762
diff changeset
379 // Open output file
714
304a50a9d51f Indentation fixes.
Matti Hamalainen <ccr@tnsp.org>
parents: 574
diff changeset
380 if (destFilename == NULL)
304a50a9d51f Indentation fixes.
Matti Hamalainen <ccr@tnsp.org>
parents: 574
diff changeset
381 outFile = stdout;
1692
f5d432ccc9d6 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 1615
diff changeset
382 else
f5d432ccc9d6 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 1615
diff changeset
383 if ((outFile = fopen(destFilename, "wb")) == NULL)
1314
5d6ddd4d6781 Import more changes from Map Mask Designer, and do a cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 1310
diff changeset
384 {
714
304a50a9d51f Indentation fixes.
Matti Hamalainen <ccr@tnsp.org>
parents: 574
diff changeset
385 THERR("Error opening output file '%s'!\n",
304a50a9d51f Indentation fixes.
Matti Hamalainen <ccr@tnsp.org>
parents: 574
diff changeset
386 destFilename);
1310
a6be5a68032f Rename map block handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 1309
diff changeset
387 mapBlockFree(map);
714
304a50a9d51f Indentation fixes.
Matti Hamalainen <ccr@tnsp.org>
parents: 574
diff changeset
388 exit(1);
304a50a9d51f Indentation fixes.
Matti Hamalainen <ccr@tnsp.org>
parents: 574
diff changeset
389 }
96
3d10542703a9 New utility for converting map files to PPM format images
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
390
1063
75a7b2594d93 Add PNG output support to map2ppm.
Matti Hamalainen <ccr@tnsp.org>
parents: 1056
diff changeset
391 THMSG(1, "Outputting image of %dx%d ...\n",
1314
5d6ddd4d6781 Import more changes from Map Mask Designer, and do a cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 1310
diff changeset
392 map->width * optScale, map->height * optScale);
1063
75a7b2594d93 Add PNG output support to map2ppm.
Matti Hamalainen <ccr@tnsp.org>
parents: 1056
diff changeset
393
75a7b2594d93 Add PNG output support to map2ppm.
Matti Hamalainen <ccr@tnsp.org>
parents: 1056
diff changeset
394 #ifdef HAVE_LIBPNG
75a7b2594d93 Add PNG output support to map2ppm.
Matti Hamalainen <ccr@tnsp.org>
parents: 1056
diff changeset
395 if (optPNGLevel >= 0)
75a7b2594d93 Add PNG output support to map2ppm.
Matti Hamalainen <ccr@tnsp.org>
parents: 1056
diff changeset
396 ret = writePNGFile(outFile, map, optScale);
75a7b2594d93 Add PNG output support to map2ppm.
Matti Hamalainen <ccr@tnsp.org>
parents: 1056
diff changeset
397 else
75a7b2594d93 Add PNG output support to map2ppm.
Matti Hamalainen <ccr@tnsp.org>
parents: 1056
diff changeset
398 #endif
75a7b2594d93 Add PNG output support to map2ppm.
Matti Hamalainen <ccr@tnsp.org>
parents: 1056
diff changeset
399 ret = writePPMFile(outFile, map, optScale);
75a7b2594d93 Add PNG output support to map2ppm.
Matti Hamalainen <ccr@tnsp.org>
parents: 1056
diff changeset
400
75a7b2594d93 Add PNG output support to map2ppm.
Matti Hamalainen <ccr@tnsp.org>
parents: 1056
diff changeset
401 if (ret != 0)
75a7b2594d93 Add PNG output support to map2ppm.
Matti Hamalainen <ccr@tnsp.org>
parents: 1056
diff changeset
402 THERR("Image write failed, code=%d\n", ret);
75a7b2594d93 Add PNG output support to map2ppm.
Matti Hamalainen <ccr@tnsp.org>
parents: 1056
diff changeset
403 else
75a7b2594d93 Add PNG output support to map2ppm.
Matti Hamalainen <ccr@tnsp.org>
parents: 1056
diff changeset
404 THMSG(1, "Done.\n");
1543
0da2d8e74583 Cosmetics pass.
Matti Hamalainen <ccr@tnsp.org>
parents: 1487
diff changeset
405
714
304a50a9d51f Indentation fixes.
Matti Hamalainen <ccr@tnsp.org>
parents: 574
diff changeset
406 fclose(outFile);
1063
75a7b2594d93 Add PNG output support to map2ppm.
Matti Hamalainen <ccr@tnsp.org>
parents: 1056
diff changeset
407
1310
a6be5a68032f Rename map block handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 1309
diff changeset
408 mapBlockFree(map);
1094
cdb9db1234be Fix two memory leaks, though they are not significant.
Matti Hamalainen <ccr@tnsp.org>
parents: 1066
diff changeset
409
1063
75a7b2594d93 Add PNG output support to map2ppm.
Matti Hamalainen <ccr@tnsp.org>
parents: 1056
diff changeset
410 return ret;
96
3d10542703a9 New utility for converting map files to PPM format images
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
411 }