Debian Codegraph @

raster/r.in.png/main.c

raw
1 /*****************************************************************************
2 *
3 * MODULE: r.in.png
4 * AUTHOR(S): Michael Shapiro - CERL
5 * Alex Shevlakov - [email protected]
6 * Glynn Clements
7 * PURPOSE: Import non-georeferenced Images in PNG format.
8 * COPYRIGHT: (C) 2000-2002, 2010-2011 by the GRASS Development Team
9 *
10 * This program is free software under the GNU General Public
11 * License (>=v2). Read the file COPYING that comes with GRASS
12 * for details.
13 *
14 *****************************************************************************/
15
16 #include <stdio.h>
17 #include <stdlib.h>
18 #include <string.h>
19 #include <math.h>
20
21 #include <png.h>
22
23 #include <grass/gis.h>
24 #include <grass/raster.h>
25 #include <grass/glocale.h>
26
27 typedef struct {
28 const char suffix[4];
29 int active;
30 int fd;
31 CELL *buf;
32 FCELL *fbuf;
33 CELL maxval;
34 char name[256];
35 } channel;
36
37 #define C_Y 0
38 #define C_P 1
39 #define C_R 2
40 #define C_G 3
41 #define C_B 4
42 #define C_A 5
43
44 static channel channels[6] = {{.suffix = ""}, {.suffix = ""},
45 {.suffix = ".r"}, {.suffix = ".g"},
46 {.suffix = ".b"}, {.suffix = ".a"}};
47
48 static int Header;
49 static int Float;
50
51 static char *input, *output, *title;
52 static double f_gamma, d_gamma, alpha, t_gamma;
53 static int ialpha;
54
55 static png_structp png_ptr;
56 static png_infop info_ptr;
57
58 static png_uint_32 width, height;
59 static int bit_depth, color_type, interlace_type, compression_type, filter_type;
60
61 static double gamma_correct(double k)
62 {
63 return pow(k, 1.0 / t_gamma);
64 }
65
66 static int intensity(double k)
67 {
68 return (int)(gamma_correct(k) * 255 + 0.5);
69 }
70
71 static int get_byte(png_bytep *pp)
72 {
73 return *(*pp)++;
74 }
75
76 static int get_png_val(png_bytep *pp, int bit_depth)
77 {
78 return (bit_depth == 16) ? (get_byte(pp) << 8) | get_byte(pp)
79 : get_byte(pp);
80 }
81
82 static void init_channel(channel *c)
83 {
84 snprintf(c->name, sizeof(c->name), "%s%s", output, c->suffix);
85
86 if (Float) {
87 c->fd = Rast_open_fp_new(c->name);
88 c->fbuf = Rast_allocate_f_buf();
89 }
90 else {
91 c->fd = Rast_open_c_new(c->name);
92 c->buf = Rast_allocate_c_buf();
93 }
94
95 c->active = 1;
96 }
97
98 static void write_row_int(png_bytep p)
99 {
100 unsigned int x, c;
101 channel *ch;
102
103 for (x = 0; x < width; x++)
104 for (c = 0; c < 6; c++) {
105 ch = &channels[c];
106 if (ch->active)
107 ch->buf[x] = (CELL)get_png_val(&p, bit_depth);
108 }
109
110 if (channels[C_A].active && ialpha > 0)
111 for (c = 0; c < 6; c++) {
112 ch = &channels[c];
113 if (c != C_A && ch->active)
114 for (x = 0; x < width; x++)
115 if (channels[C_A].buf[x] <= ialpha)
116 Rast_set_c_null_value(&ch->buf[x], 1);
117 }
118
119 for (c = 0; c < 6; c++) {
120 ch = &channels[c];
121 if (ch->active)
122 Rast_put_c_row(ch->fd, ch->buf);
123 }
124 }
125
126 static void write_row_float(png_bytep p)
127 {
128 unsigned int x, c;
129 channel *ch;
130
131 for (x = 0; x < width; x++)
132 for (c = 0; c < 6; c++) {
133 ch = &channels[c];
134 if (ch->active)
135 ch->fbuf[x] = (FCELL)get_png_val(&p, bit_depth) / ch->maxval;
136 }
137
138 if (t_gamma != 1.0)
139 for (c = 0; c < 6; c++) {
140 ch = &channels[c];
141 if (c != C_A && ch->active)
142 for (x = 0; x < width; x++)
143 ch->fbuf[x] = gamma_correct(ch->fbuf[x]);
144 }
145
146 if (channels[C_A].active && ialpha > 0)
147 for (c = 0; c < 6; c++) {
148 ch = &channels[c];
149 if (c != C_A && ch->active)
150 for (x = 0; x < width; x++)
151 if (channels[C_A].fbuf[x] <= alpha)
152 Rast_set_f_null_value(&ch->fbuf[x], 1);
153 }
154
155 for (c = 0; c < 6; c++) {
156 ch = &channels[c];
157 if (ch->active)
158 Rast_put_f_row(ch->fd, ch->fbuf);
159 }
160 }
161
162 static void write_colors_int(int c)
163 {
164 channel *ch = &channels[c];
165 CELL i0 = 0;
166 CELL i1 = ch->maxval;
167 struct Colors colors;
168 int i;
169
170 Rast_init_colors(&colors);
171
172 if (color_type == PNG_COLOR_TYPE_PALETTE) {
173 png_colorp palette;
174 int num_palette;
175
176 png_get_PLTE(png_ptr, info_ptr, &palette, &num_palette);
177
178 for (i = 0; i < num_palette; i++) {
179 png_colorp col = &palette[i];
180
181 Rast_set_c_color((CELL)i, col->red, col->green, col->blue, &colors);
182 }
183 }
184 else if (c == C_A || t_gamma == 1.0)
185 Rast_add_c_color_rule(&i0, 0, 0, 0, &i1, 255, 255, 255, &colors);
186 else
187 for (i = 0; i <= i1; i++) {
188 int v = intensity((double)i / i1);
189
190 Rast_set_c_color((CELL)i, v, v, v, &colors);
191 }
192
193 Rast_write_colors(ch->name, G_mapset(), &colors);
194 }
195
196 static void write_colors_float(int c)
197 {
198 channel *ch = &channels[c];
199 FCELL i0 = 0.0;
200 FCELL i1 = 1.0;
201 struct Colors colors;
202
203 Rast_init_colors(&colors);
204
205 Rast_add_f_color_rule(&i0, 0, 0, 0, &i1, 255, 255, 255, &colors);
206
207 Rast_write_colors(ch->name, G_mapset(), &colors);
208 }
209
210 static void print_header(void)
211 {
212 char gamma_string[80] = "";
213 const char *type_string = "";
214 const char *alpha_string = "";
215
216 switch (color_type) {
217 case PNG_COLOR_TYPE_GRAY:
218 type_string = "gray";
219 alpha_string = "";
220 break;
221
222 case PNG_COLOR_TYPE_GRAY_ALPHA:
223 type_string = "gray";
224 alpha_string = "+alpha";
225 break;
226
227 case PNG_COLOR_TYPE_PALETTE:
228 type_string = "palette";
229 alpha_string = "";
230 break;
231
232 case PNG_COLOR_TYPE_RGB:
233 type_string = "truecolor";
234 alpha_string = "";
235 break;
236
237 case PNG_COLOR_TYPE_RGB_ALPHA:
238 type_string = "truecolor";
239 alpha_string = "+alpha";
240 break;
241 }
242
243 if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS))
244 alpha_string = "+transparency";
245
246 if (png_get_valid(png_ptr, info_ptr, PNG_INFO_gAMA))
247 snprintf(gamma_string, sizeof(gamma_string), ", image gamma = %4.2f",
248 f_gamma);
249
250 fprintf(stderr, "%lu x %lu image, %d bit%s %s%s%s%s\n",
251 (unsigned long)width, (unsigned long)height, bit_depth,
252 bit_depth > 1 ? "s" : "", type_string, alpha_string, gamma_string,
253 interlace_type ? ", Adam7 interlaced" : "");
254 }
255
256 static void read_png(void)
257 {
258 unsigned char sig_buf[8];
259 png_bytep png_buffer;
260 png_bytep *png_rows;
261 int linesize;
262 struct Cell_head cellhd;
263 unsigned int y, c;
264 png_color_8p sig_bit;
265 int sbit, interlace;
266 FILE *ifp;
267
268 /* initialize input stream and PNG library */
269
270 ifp = fopen(input, "rb");
271 if (!ifp)
272 G_fatal_error(_("Unable to open PNG file '%s'"), input);
273
274 if (fread(sig_buf, sizeof(sig_buf), 1, ifp) != 1)
275 G_fatal_error(_("Input file empty or too short"));
276
277 if (png_sig_cmp(sig_buf, 0, sizeof(sig_buf)) != 0)
278 G_fatal_error(_("Input file not a PNG file"));
279
280 png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
281 if (!png_ptr)
282 G_fatal_error(_("Unable to allocate PNG structure"));
283
284 info_ptr = png_create_info_struct(png_ptr);
285 if (!info_ptr)
286 G_fatal_error(_("Unable to allocate PNG structure"));
287
288 if (setjmp(png_jmpbuf(png_ptr)))
289 G_fatal_error(_("PNG error"));
290
291 png_init_io(png_ptr, ifp);
292 png_set_sig_bytes(png_ptr, sizeof(sig_buf));
293
294 png_read_info(png_ptr, info_ptr);
295
296 png_get_IHDR(png_ptr, info_ptr, &width, &height, &bit_depth, &color_type,
297 &interlace_type, &compression_type, &filter_type);
298
299 if (Header || G_verbose() == G_verbose_max())
300 print_header();
301
302 if (Header) {
303 fclose(ifp);
304 exit(0);
305 }
306
307 /* read image parameters and set up data conversions */
308
309 if (png_get_bit_depth(png_ptr, info_ptr) < 8)
310 png_set_packing(png_ptr);
311
312 sbit = png_get_sBIT(png_ptr, info_ptr, &sig_bit);
313 if (sbit)
314 png_set_shift(png_ptr, sig_bit);
315
316 if (!png_get_gAMA(png_ptr, info_ptr, &f_gamma))
317 f_gamma = 0.0;
318
319 if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS))
320 png_set_tRNS_to_alpha(png_ptr);
321
322 if (Float && color_type == PNG_COLOR_TYPE_PALETTE)
323 png_set_palette_to_rgb(png_ptr);
324
325 png_read_update_info(png_ptr, info_ptr);
326
327 interlace = (interlace_type != PNG_INTERLACE_NONE);
328
329 ialpha = (int)(alpha * channels[C_A].maxval);
330
331 t_gamma = (f_gamma != 0.0 && d_gamma != 0.0) ? f_gamma * d_gamma : 1.0;
332
333 /* allocate input buffer */
334
335 linesize = png_get_rowbytes(png_ptr, info_ptr);
336
337 png_buffer = G_malloc((size_t)interlace ? (size_t)height * linesize
338 : (size_t)linesize);
339
340 if (interlace) {
341 png_rows = G_malloc(height * sizeof(png_bytep));
342 for (y = 0; y < height; y++)
343 png_rows[y] = png_buffer + y * linesize;
344 }
345
346 /* initialize cell header */
347
348 Rast_get_window(&cellhd);
349
350 cellhd.rows = height;
351 cellhd.cols = width;
352 cellhd.north = cellhd.rows;
353 cellhd.south = 0.0;
354 cellhd.east = cellhd.cols;
355 cellhd.west = 0.0;
356 cellhd.ns_res = 1;
357 cellhd.ew_res = 1;
358
359 Rast_set_window(&cellhd);
360
361 /* initialize channel information */
362
363 switch (color_type) {
364 case PNG_COLOR_TYPE_GRAY:
365 init_channel(&channels[C_Y]);
366 break;
367
368 case PNG_COLOR_TYPE_GRAY_ALPHA:
369 init_channel(&channels[C_Y]);
370 init_channel(&channels[C_A]);
371 break;
372
373 case PNG_COLOR_TYPE_PALETTE:
374 init_channel(&channels[C_P]);
375 break;
376
377 case PNG_COLOR_TYPE_RGB:
378 init_channel(&channels[C_R]);
379 init_channel(&channels[C_G]);
380 init_channel(&channels[C_B]);
381 break;
382
383 case PNG_COLOR_TYPE_RGB_ALPHA:
384 init_channel(&channels[C_R]);
385 init_channel(&channels[C_G]);
386 init_channel(&channels[C_B]);
387 init_channel(&channels[C_A]);
388 break;
389 }
390
391 if (sbit) {
392 channels[C_R].maxval = (1 << sig_bit->red) - 1;
393 channels[C_G].maxval = (1 << sig_bit->green) - 1;
394 channels[C_B].maxval = (1 << sig_bit->blue) - 1;
395 channels[C_Y].maxval = (1 << sig_bit->gray) - 1;
396 channels[C_A].maxval = (1 << sig_bit->alpha) - 1;
397 }
398 else {
399 channels[C_R].maxval = (1 << bit_depth) - 1;
400 channels[C_G].maxval = (1 << bit_depth) - 1;
401 channels[C_B].maxval = (1 << bit_depth) - 1;
402 channels[C_Y].maxval = (1 << bit_depth) - 1;
403 channels[C_A].maxval = (1 << bit_depth) - 1;
404 }
405
406 /* read image and write raster layers */
407
408 if (interlace)
409 png_read_image(png_ptr, png_rows);
410
411 for (y = 0; y < height; y++) {
412 png_bytep p;
413
414 if (interlace)
415 p = png_rows[y];
416 else {
417 png_read_row(png_ptr, png_buffer, NULL);
418 p = png_buffer;
419 }
420
421 if (Float)
422 write_row_float(p);
423 else
424 write_row_int(p);
425 }
426
427 png_read_end(png_ptr, NULL);
428
429 fclose(ifp);
430
431 /* close output files */
432
433 for (c = 0; c < 6; c++) {
434 channel *ch = &channels[c];
435
436 if (!ch->active)
437 continue;
438
439 Rast_close(ch->fd);
440
441 if (Float)
442 G_free(ch->fbuf);
443 else
444 G_free(ch->buf);
445 }
446
447 /* write title and color table */
448
449 G_verbose_message(_("Creating support files for <%s>..."), output);
450
451 for (c = 0; c < 6; c++) {
452 channel *ch = &channels[c];
453
454 if (!ch->active)
455 continue;
456
457 if (title && *title)
458 Rast_put_cell_title(ch->name, title);
459
460 if (Float)
461 write_colors_float(c);
462 else
463 write_colors_int(c);
464 }
465
466 G_free(png_buffer);
467 if (interlace)
468 G_free(png_rows);
469
470 png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
471 }
472
473 int main(int argc, char *argv[])
474 {
475 struct Option *inopt, *outopt, *titleopt, *gammaopt, *alphaopt;
476 struct Flag *fflag, *hflag;
477 struct GModule *module;
478
479 G_gisinit(argv[0]);
480
481 module = G_define_module();
482 G_add_keyword(_("raster"));
483 G_add_keyword(_("import"));
484 G_add_keyword("PNG");
485 module->description = _("Imports non-georeferenced PNG format image.");
486
487 inopt = G_define_standard_option(G_OPT_F_BIN_INPUT);
488
489 outopt = G_define_standard_option(G_OPT_R_OUTPUT);
490
491 titleopt = G_define_option();
492 titleopt->key = "title";
493 titleopt->type = TYPE_STRING;
494 titleopt->required = NO;
495 titleopt->description = _("Title for created raster map");
496
497 gammaopt = G_define_option();
498 gammaopt->key = "gamma";
499 gammaopt->type = TYPE_DOUBLE;
500 gammaopt->required = NO;
501 gammaopt->description = _("Display gamma");
502
503 alphaopt = G_define_option();
504 alphaopt->key = "alpha";
505 alphaopt->type = TYPE_DOUBLE;
506 alphaopt->required = NO;
507 alphaopt->description = _("Alpha threshold");
508
509 fflag = G_define_flag();
510 fflag->key = 'f';
511 fflag->description = _("Create floating-point map (0.0 - 1.0)");
512
513 hflag = G_define_flag();
514 hflag->key = 'h';
515 hflag->description = _("Output image file header only and exit");
516
517 if (G_parser(argc, argv))
518 exit(EXIT_FAILURE);
519
520 input = inopt->answer;
521 output = outopt->answer;
522 title = titleopt->answer;
523 d_gamma = gammaopt->answer ? atof(gammaopt->answer) : 0.0;
524 alpha = alphaopt->answer ? atof(alphaopt->answer) : -1.0;
525
526 Float = fflag->answer;
527 Header = hflag->answer;
528
529 read_png();
530
531 exit(EXIT_SUCCESS);
532 }