3
* AUTHOR(S): Glynn Clements
4
* PURPOSE: g.ppmtopng isn't meant for end users. It's an internal tool for use by
5
* the script to generate thumbnails for the r.colors manual page.
6
* COPYRIGHT: (C) 2009 by Glynn Clements and the GRASS Development Team
8
* This program is free software under the GNU General Public
9
* License (>=v2). Read the file COPYING that comes with GRASS
19
#include <grass/gis.h>
20
#include <grass/glocale.h>
22
static int width, height;
23
static unsigned char *buf;
25
static void read_ppm(const char *filename)
32
input = fopen(filename, "rb");
34
G_fatal_error(_("Unable to open input file %s"), filename);
36
if (fscanf(input, "P6 %d %d %d", &width, &height, &maxval) != 3)
37
G_fatal_error(_("Invalid input file %s"), filename);
41
buf = G_malloc(width * height * 3);
44
for (y = 0; y < height; y++) {
45
for (x = 0; x < width; x++) {
50
*p++ = (unsigned char) (r * 255 / maxval);
51
*p++ = (unsigned char) (g * 255 / maxval);
52
*p++ = (unsigned char) (b * 255 / maxval);
59
static void write_png(const char *filename)
62
static png_struct *png_ptr;
63
static png_info *info_ptr;
68
png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, &jbuf, NULL, NULL);
70
G_fatal_error(_("Unable to allocate PNG structure"));
72
info_ptr = png_create_info_struct(png_ptr);
74
G_fatal_error(_("Unable to allocate PNG structure"));
76
if (setjmp(png_jmpbuf(png_ptr)))
77
G_fatal_error(_("Error writing PNG file"));
79
output = fopen(filename, "wb");
81
G_fatal_error(_("Unable to open output file %s"), filename);
83
png_init_io(png_ptr, output);
85
png_set_IHDR(png_ptr, info_ptr,
87
8, PNG_COLOR_TYPE_RGB,
89
PNG_COMPRESSION_TYPE_DEFAULT,
90
PNG_FILTER_TYPE_DEFAULT);
92
png_set_invert_alpha(png_ptr);
94
png_write_info(png_ptr, info_ptr);
96
for (y = 0, p = buf; y < height; y++, p += 3 * width)
97
png_write_row(png_ptr, p);
99
png_write_end(png_ptr, info_ptr);
101
png_destroy_write_struct(&png_ptr, &info_ptr);
106
int main(int argc, char *argv[])
108
struct GModule *module;
111
struct Option *in, *out;
116
module = G_define_module();
117
G_add_keyword(_("general"));
118
G_add_keyword(_("display"));
119
module->description = _("Converts between PPM/PGM and PNG image formats.");
121
opt.in = G_define_standard_option(G_OPT_F_INPUT);
123
opt.out = G_define_standard_option(G_OPT_F_OUTPUT);
125
if (G_parser(argc, argv))
128
read_ppm(opt.in->answer);
129
write_png(opt.out->answer);