GRASS GIS 8 Programmer's Manual 8.2.0(2022)-exported
pngdriver/read_ppm.c
Go to the documentation of this file.
1/*!
2 \file lib/pngdriver/read_ppm.c
3
4 \brief GRASS png display driver - read image (lower level functions)
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*/
13
14
15#include <stdio.h>
16#include <stdlib.h>
17#include <string.h>
18
19#include <grass/gis.h>
20#include "pngdriver.h"
21
22void read_ppm(void)
23{
24 FILE *input;
25 int x, y;
26 int i_width, i_height, maxval;
27 unsigned int rgb_mask = png_get_color(255, 255, 255, 0);
28 unsigned int *p;
29
30 if (!png.true_color)
31 G_fatal_error("PNG: cannot use PPM/PGM with indexed color");
32
33 input = fopen(png.file_name, "rb");
34 if (!input)
35 G_fatal_error("PNG: couldn't open input file %s", png.file_name);
36
37 if (fscanf(input, "P6 %d %d %d", &i_width, &i_height, &maxval) != 3)
38 G_fatal_error("PNG: invalid input file %s", png.file_name);
39
40 fgetc(input);
41
42 if (i_width != png.width || i_height != png.height)
44 ("PNG: input file has incorrect dimensions: expected: %dx%d got: %dx%d",
45 png.width, png.height, i_width, i_height);
46
47 for (y = 0, p = png.grid; y < png.height; y++) {
48 for (x = 0; x < png.width; x++, p++) {
49 unsigned int c = *p;
50
51 int r = fgetc(input);
52 int g = fgetc(input);
53 int b = fgetc(input);
54
55 r = r * 255 / maxval;
56 g = g * 255 / maxval;
57 b = b * 255 / maxval;
58
59 c &= ~rgb_mask;
60 c |= png_get_color(r, g, b, 0);
61
62 *p = c;
63 }
64 }
65
66 fclose(input);
67}
68
69void read_pgm(void)
70{
71 char *mask_name = G_store(png.file_name);
72 FILE *input;
73 int x, y;
74 int i_width, i_height, maxval;
75 unsigned int rgb_mask = png_get_color(255, 255, 255, 0);
76 unsigned int *p;
77
78 if (!png.true_color)
79 G_fatal_error("PNG: cannot use PPM/PGM with indexed color");
80
81 mask_name[strlen(mask_name) - 2] = 'g';
82
83 input = fopen(mask_name, "rb");
84 if (!input)
85 G_fatal_error("PNG: couldn't open input mask file %s", mask_name);
86
87 if (fscanf(input, "P5 %d %d %d", &i_width, &i_height, &maxval) != 3)
88 G_fatal_error("PNG: invalid input mask file %s", mask_name);
89
90 fgetc(input);
91
92 if (i_width != png.width || i_height != png.height)
94 ("PNG: input mask file has incorrect dimensions: expected: %dx%d got: %dx%d",
95 png.width, png.height, i_width, i_height);
96
97 G_free(mask_name);
98
99 for (y = 0, p = png.grid; y < png.height; y++) {
100 for (x = 0; x < png.width; x++, p++) {
101 unsigned int c = *p;
102
103 int k = fgetc(input);
104
105 k = k * 255 / maxval;
106
107 c &= rgb_mask;
108 c |= png_get_color(0, 0, 0, 255 - k);
109
110 *p = c;
111 }
112 }
113
114 fclose(input);
115}
void G_free(void *buf)
Free allocated memory.
Definition: alloc.c:149
unsigned int png_get_color(int r, int g, int b, int a)
Definition: color_table.c:120
double b
double r
void G_fatal_error(const char *msg,...)
Print a fatal error message to stderr.
Definition: gis/error.c:160
float g
Definition: named_colr.c:8
struct png_state png
void read_ppm(void)
void read_pgm(void)
GRASS png display driver - header file.
char * G_store(const char *s)
Copy string to allocated memory.
Definition: strings.c:87
char * file_name
Definition: pngdriver.h:33
int true_color
Definition: pngdriver.h:35
int height
Definition: pngdriver.h:43
unsigned int * grid
Definition: pngdriver.h:44
int width
Definition: pngdriver.h:43
#define x