~ubuntu-branches/ubuntu/wily/grass/wily

« back to all changes in this revision

Viewing changes to general/g.ppmtopng/main.c

Tags: 7.0.0~rc1+ds1-1~exp1
* New upstream release candidate.
* Repack upstream tarball, remove precompiled Python objects.
* Add upstream metadata.
* Update gbp.conf and Vcs-Git URL to use the experimental branch.
* Update watch file for GRASS 7.0.
* Drop build dependencies for Tcl/Tk, add build dependencies:
  python-numpy, libnetcdf-dev, netcdf-bin, libblas-dev, liblapack-dev
* Update Vcs-Browser URL to use cgit instead of gitweb.
* Update paths to use grass70.
* Add configure options: --with-netcdf, --with-blas, --with-lapack,
  remove --with-tcltk-includes.
* Update patches for GRASS 7.
* Update copyright file, changes:
  - Update copyright years
  - Group files by license
  - Remove unused license sections
* Add patches for various typos.
* Fix desktop file with patch instead of d/rules.
* Use minimal dh rules.
* Bump Standards-Version to 3.9.6, no changes.
* Use dpkg-maintscript-helper to replace directories with symlinks.
  (closes: #776349)
* Update my email to use @debian.org address.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * MODULE:       g.ppmtopng
 
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
 
7
 *
 
8
 *               This program is free software under the GNU General Public
 
9
 *               License (>=v2). Read the file COPYING that comes with GRASS
 
10
 *               for details.
 
11
 *
 
12
 */
 
13
 
 
14
#include <stdio.h>
 
15
#include <stdlib.h>
 
16
#include <stdarg.h>
 
17
#include <string.h>
 
18
#include <png.h>
 
19
#include <grass/gis.h>
 
20
#include <grass/glocale.h>
 
21
 
 
22
static int width, height;
 
23
static unsigned char *buf;
 
24
 
 
25
static void read_ppm(const char *filename)
 
26
{
 
27
    FILE *input;
 
28
    int x, y;
 
29
    int maxval;
 
30
    unsigned char *p;
 
31
 
 
32
    input = fopen(filename, "rb");
 
33
    if (!input)
 
34
        G_fatal_error(_("Unable to open input file %s"), filename);
 
35
 
 
36
    if (fscanf(input, "P6 %d %d %d", &width, &height, &maxval) != 3)
 
37
        G_fatal_error(_("Invalid input file %s"), filename);
 
38
 
 
39
    fgetc(input);
 
40
 
 
41
    buf = G_malloc(width * height * 3);
 
42
 
 
43
    p = buf;
 
44
    for (y = 0; y < height; y++) {
 
45
        for (x = 0; x < width; x++) {
 
46
            int r = fgetc(input);
 
47
            int g = fgetc(input);
 
48
            int b = fgetc(input);
 
49
 
 
50
            *p++ = (unsigned char) (r * 255 / maxval);
 
51
            *p++ = (unsigned char) (g * 255 / maxval);
 
52
            *p++ = (unsigned char) (b * 255 / maxval);
 
53
        }
 
54
    }
 
55
 
 
56
    fclose(input);
 
57
}
 
58
 
 
59
static void write_png(const char *filename)
 
60
{
 
61
    static jmp_buf jbuf;
 
62
    static png_struct *png_ptr;
 
63
    static png_info *info_ptr;
 
64
    FILE *output;
 
65
    int y;
 
66
    unsigned char *p;
 
67
 
 
68
    png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, &jbuf, NULL, NULL);
 
69
    if (!png_ptr)
 
70
        G_fatal_error(_("Unable to allocate PNG structure"));
 
71
 
 
72
    info_ptr = png_create_info_struct(png_ptr);
 
73
    if (!info_ptr)
 
74
        G_fatal_error(_("Unable to allocate PNG structure"));
 
75
 
 
76
    if (setjmp(png_jmpbuf(png_ptr)))
 
77
        G_fatal_error(_("Error writing PNG file"));
 
78
 
 
79
    output = fopen(filename, "wb");
 
80
    if (!output)
 
81
        G_fatal_error(_("Unable to open output file %s"), filename);
 
82
 
 
83
    png_init_io(png_ptr, output);
 
84
 
 
85
    png_set_IHDR(png_ptr, info_ptr,
 
86
                 width, height,
 
87
                 8, PNG_COLOR_TYPE_RGB,
 
88
                 PNG_INTERLACE_NONE,
 
89
                 PNG_COMPRESSION_TYPE_DEFAULT,
 
90
                 PNG_FILTER_TYPE_DEFAULT);
 
91
 
 
92
    png_set_invert_alpha(png_ptr);
 
93
 
 
94
    png_write_info(png_ptr, info_ptr);
 
95
 
 
96
    for (y = 0, p = buf; y < height; y++, p += 3 * width)
 
97
        png_write_row(png_ptr, p);
 
98
 
 
99
    png_write_end(png_ptr, info_ptr);
 
100
 
 
101
    png_destroy_write_struct(&png_ptr, &info_ptr);
 
102
 
 
103
    fclose(output);
 
104
}
 
105
 
 
106
int main(int argc, char *argv[])
 
107
{
 
108
    struct GModule *module;
 
109
    struct
 
110
    {
 
111
        struct Option *in, *out;
 
112
    } opt;
 
113
 
 
114
    G_gisinit(argv[0]);
 
115
 
 
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.");
 
120
 
 
121
    opt.in = G_define_standard_option(G_OPT_F_INPUT);
 
122
 
 
123
    opt.out = G_define_standard_option(G_OPT_F_OUTPUT);
 
124
 
 
125
    if (G_parser(argc, argv))
 
126
        exit(EXIT_FAILURE);
 
127
 
 
128
    read_ppm(opt.in->answer);
 
129
    write_png(opt.out->answer);
 
130
 
 
131
    return 0;
 
132
}