GRASS 8 Programmer's Manual 8.6.0dev(2026)-c83afef6d3
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 SPDX-FileCopyrightText: 2007-2014 Glynn Clements
7 SPDX-FileCopyrightText: GRASS Development Team
8 SPDX-License-Identifier: GPL-2.0-or-later
9
10 \author Glynn Clements
11 */
12
13#include <stdio.h>
14#include <stdlib.h>
15#include <string.h>
16
17#include <grass/gis.h>
18#include <grass/glocale.h>
19#include "pngdriver.h"
20
21void read_ppm(void)
22{
23 FILE *input;
24 int x, y;
26 unsigned int rgb_mask = png_get_color(255, 255, 255, 0);
27 unsigned int *p;
28
29 if (!png.true_color)
30 G_fatal_error("PNG: cannot use PPM/PGM with indexed color");
31
32 input = fopen(png.file_name, "rb");
33 if (!input)
34 G_fatal_error("PNG: couldn't open input file %s", png.file_name);
35
36 if (fscanf(input, "P6 %d %d %d", &i_width, &i_height, &maxval) != 3)
37 G_fatal_error("PNG: invalid input file %s", png.file_name);
38
39 if (fgetc(input) == EOF)
40 G_fatal_error(_("PNG: invalid input file %s"), png.file_name);
41
42 if (i_width != png.width || i_height != png.height)
43 G_fatal_error("PNG: input file has incorrect dimensions: expected: "
44 "%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;
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 if (fgetc(input) == EOF)
91 G_fatal_error(_("PNG: invalid input mask file %s"), mask_name);
92
93 if (i_width != png.width || i_height != png.height)
94 G_fatal_error("PNG: input mask file has incorrect dimensions: "
95 "expected: %dx%d got: %dx%d",
96 png.width, png.height, i_width, i_height);
97
99
100 for (y = 0, p = png.grid; y < png.height; y++) {
101 for (x = 0; x < png.width; x++, p++) {
102 unsigned int c = *p;
103
104 int k = fgetc(input);
105
106 k = k * 255 / maxval;
107
108 c &= rgb_mask;
109 c |= png_get_color(0, 0, 0, 255 - k);
110
111 *p = c;
112 }
113 }
114
115 fclose(input);
116}
unsigned int png_get_color(int r, int g, int b, int a)
void G_free(void *)
Free allocated memory.
Definition gis/alloc.c:145
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:85
#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:37
double r
Definition r_raster.c:37
#define x