~ubuntu-branches/ubuntu/vivid/grass/vivid-proposed

« back to all changes in this revision

Viewing changes to lib/pngdriver/read_png.c

  • Committer: Package Import Robot
  • Author(s): Bas Couwenberg
  • Date: 2015-02-20 23:12:08 UTC
  • mfrom: (8.2.6 experimental)
  • Revision ID: package-import@ubuntu.com-20150220231208-1u6qvqm84v430b10
Tags: 7.0.0-1~exp1
* New upstream release.
* Update python-ctypes-ternary.patch to use if/else instead of and/or.
* Drop check4dev patch, rely on upstream check.
* Add build dependency on libpq-dev to grass-dev for libpq-fe.h.
* Drop patches applied upstream, refresh remaining patches.
* Update symlinks for images switched from jpg to png.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*!
 
2
  \file lib/pngdriver/read_png.c
 
3
 
 
4
  \brief GRASS png display driver - read png
 
5
 
 
6
  (C) 2007-2014 by Glynn Clements and the GRASS Development Team
 
7
  
 
8
  This program is free software under the GNU General Public License
 
9
  (>=v2). Read the file COPYING that comes with GRASS for details.
 
10
  
 
11
  \author Glynn Clements
 
12
*/
1
13
 
2
14
#include <stdio.h>
3
15
#include <stdlib.h>
6
18
#include <grass/gis.h>
7
19
#include "pngdriver.h"
8
20
 
 
21
static void read_data(png_structp png_ptr, png_bytep data, png_size_t length)
 
22
{
 
23
  png_size_t check;
 
24
  FILE *fp;
 
25
 
 
26
  if (png_ptr == NULL )
 
27
    return;
 
28
 
 
29
  fp = (FILE *) png_get_io_ptr(png_ptr);
 
30
 
 
31
  if ( fp == NULL )
 
32
    return;
 
33
 
 
34
  /* fread() returns 0 on error, so it is OK to store this in a png_size_t
 
35
   * instead of an int, which is what fread() actually returns.
 
36
   */
 
37
  check = fread(data, 1, length, fp);
 
38
 
 
39
  if (check != length)
 
40
    G_fatal_error("PNG: Read Error");
 
41
}
 
42
 
9
43
void read_png(void)
10
44
{
11
45
    static jmp_buf jbuf;
30
64
    if (setjmp(png_jmpbuf(png_ptr)))
31
65
        G_fatal_error("error reading PNG file");
32
66
 
33
 
    input = fopen(file_name, "rb");
 
67
    input = fopen(png.file_name, "rb");
34
68
    if (!input)
35
 
        G_fatal_error("PNG: couldn't open output file %s", file_name);
 
69
        G_fatal_error("PNG: couldn't open output file %s", png.file_name);
36
70
 
37
 
    png_init_io(png_ptr, input);
 
71
    png_set_read_fn(png_ptr, input, read_data);
38
72
 
39
73
    png_read_info(png_ptr, info_ptr);
40
74
 
44
78
    if (depth != 8)
45
79
        G_fatal_error("PNG: input file is not 8-bit");
46
80
 
47
 
    if (i_width != width || i_height != height)
 
81
    if (i_width != png.width || i_height != png.height)
48
82
        G_fatal_error
49
83
            ("PNG: input file has incorrect dimensions: expected: %dx%d got: %lux%lu",
50
 
             width, height, (unsigned long) i_width, (unsigned long) i_height);
 
84
             png.width, png.height, (unsigned long) i_width, (unsigned long) i_height);
51
85
 
52
 
    if (true_color) {
 
86
    if (png.true_color) {
53
87
        if (color_type != PNG_COLOR_TYPE_RGB_ALPHA)
54
88
            G_fatal_error("PNG: input file is not RGBA");
55
89
    }
58
92
            G_fatal_error("PNG: input file is not indexed color");
59
93
    }
60
94
 
61
 
    if (!true_color && has_alpha) {
 
95
    if (!png.true_color && png.has_alpha) {
62
96
        png_bytep trans;
63
97
        int num_trans;
64
98
 
68
102
            G_fatal_error("PNG: input file has invalid palette");
69
103
    }
70
104
 
71
 
    if (true_color)
 
105
    if (png.true_color)
72
106
        png_set_invert_alpha(png_ptr);
73
107
    else {
74
108
        png_colorp png_pal;
81
115
            num_palette = 256;
82
116
 
83
117
        for (i = 0; i < num_palette; i++) {
84
 
            png_palette[i][0] = png_pal[i].red;
85
 
            png_palette[i][1] = png_pal[i].green;
86
 
            png_palette[i][2] = png_pal[i].blue;
 
118
            png.palette[i][0] = png_pal[i].red;
 
119
            png.palette[i][1] = png_pal[i].green;
 
120
            png.palette[i][2] = png_pal[i].blue;
87
121
        }
88
122
    }
89
123
 
90
 
    line = G_malloc(width * 4);
 
124
    line = G_malloc(png.width * 4);
91
125
 
92
 
    for (y = 0, p = grid; y < height; y++) {
 
126
    for (y = 0, p = png.grid; y < png.height; y++) {
93
127
        png_bytep q = line;
94
128
 
95
129
        png_read_row(png_ptr, q, NULL);
96
130
 
97
 
        if (true_color)
98
 
            for (x = 0; x < width; x++, p++) {
 
131
        if (png.true_color)
 
132
            for (x = 0; x < png.width; x++, p++) {
99
133
                int r = *q++;
100
134
                int g = *q++;
101
135
                int b = *q++;
102
136
                int a = *q++;
103
 
                unsigned int c = get_color(r, g, b, a);
 
137
                unsigned int c = png_get_color(r, g, b, a);
104
138
 
105
139
                *p = c;
106
140
            }
107
141
        else
108
 
            for (x = 0; x < width; x++, p++, q++)
 
142
            for (x = 0; x < png.width; x++, p++, q++)
109
143
                *p = (png_byte) * q;
110
144
    }
111
145