GRASS 8 Programmer's Manual 8.6.0dev(2026)-1d1e47ad9d
Loading...
Searching...
No Matches
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#include <stdio.h>
15#include <stdlib.h>
16#include <string.h>
17
18#include <grass/gis.h>
19#include <grass/glocale.h>
20#include "pngdriver.h"
21
22void read_ppm(void)
23{
24 FILE *input;
25 int x, y;
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 if (fgetc(input) == EOF)
41 G_fatal_error(_("PNG: invalid input file %s"), png.file_name);
42
43 if (i_width != png.width || i_height != png.height)
44 G_fatal_error("PNG: input file has incorrect dimensions: expected: "
45 "%dx%d got: %dx%d",
46 png.width, png.height, i_width, i_height);
47
48 for (y = 0, p = png.grid; y < png.height; y++) {
49 for (x = 0; x < png.width; x++, p++) {
50 unsigned int c = *p;
51
52 int r = fgetc(input);
53 int g = fgetc(input);
54 int b = fgetc(input);
55
56 r = r * 255 / maxval;
57 g = g * 255 / maxval;
58 b = b * 255 / maxval;
59
60 c &= ~rgb_mask;
61 c |= png_get_color(r, g, b, 0);
62
63 *p = c;
64 }
65 }
66
67 fclose(input);
68}
69
70void read_pgm(void)
71{
72 char *mask_name = G_store(png.file_name);
73 FILE *input;
74 int x, y;
76 unsigned int rgb_mask = png_get_color(255, 255, 255, 0);
77 unsigned int *p;
78
79 if (!png.true_color)
80 G_fatal_error("PNG: cannot use PPM/PGM with indexed color");
81
82 mask_name[strlen(mask_name) - 2] = 'g';
83
84 input = fopen(mask_name, "rb");
85 if (!input)
86 G_fatal_error("PNG: couldn't open input mask file %s", mask_name);
87
88 if (fscanf(input, "P5 %d %d %d", &i_width, &i_height, &maxval) != 3)
89 G_fatal_error("PNG: invalid input mask file %s", mask_name);
90
91 if (fgetc(input) == EOF)
92 G_fatal_error(_("PNG: invalid input mask file %s"), mask_name);
93
94 if (i_width != png.width || i_height != png.height)
95 G_fatal_error("PNG: input mask file has incorrect dimensions: "
96 "expected: %dx%d got: %dx%d",
97 png.width, png.height, i_width, i_height);
98
100
101 for (y = 0, p = png.grid; y < png.height; y++) {
102 for (x = 0; x < png.width; x++, p++) {
103 unsigned int c = *p;
104
105 int k = fgetc(input);
106
107 k = k * 255 / maxval;
108
109 c &= rgb_mask;
110 c |= png_get_color(0, 0, 0, 255 - k);
111
112 *p = c;
113 }
114 }
115
116 fclose(input);
117}
unsigned int png_get_color(int r, int g, int b, int a)
void G_free(void *)
Free allocated memory.
Definition gis/alloc.c:147
void void void void G_fatal_error(const char *,...) __attribute__((format(printf
char * G_store(const char *)
Copy string to allocated memory.
Definition strings.c:87
#define _(str)
Definition glocale.h:10
float g
Definition named_colr.c:7
struct png_state png
void read_ppm(void)
void read_pgm(void)
GRASS png display driver - header file.
double b
Definition r_raster.c:39
double r
Definition r_raster.c:39
#define x