GRASS GIS 8 Programmer's Manual  8.5.0dev(2024)-a16a02f7eb
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 "pngdriver.h"
20 
21 void read_ppm(void)
22 {
23  FILE *input;
24  int x, y;
25  int i_width, i_height, maxval;
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  fgetc(input);
40 
41  if (i_width != png.width || i_height != png.height)
42  G_fatal_error("PNG: input file has incorrect dimensions: expected: "
43  "%dx%d got: %dx%d",
44  png.width, png.height, i_width, i_height);
45 
46  for (y = 0, p = png.grid; y < png.height; y++) {
47  for (x = 0; x < png.width; x++, p++) {
48  unsigned int c = *p;
49 
50  int r = fgetc(input);
51  int g = fgetc(input);
52  int b = fgetc(input);
53 
54  r = r * 255 / maxval;
55  g = g * 255 / maxval;
56  b = b * 255 / maxval;
57 
58  c &= ~rgb_mask;
59  c |= png_get_color(r, g, b, 0);
60 
61  *p = c;
62  }
63  }
64 
65  fclose(input);
66 }
67 
68 void read_pgm(void)
69 {
70  char *mask_name = G_store(png.file_name);
71  FILE *input;
72  int x, y;
73  int i_width, i_height, maxval;
74  unsigned int rgb_mask = png_get_color(255, 255, 255, 0);
75  unsigned int *p;
76 
77  if (!png.true_color)
78  G_fatal_error("PNG: cannot use PPM/PGM with indexed color");
79 
80  mask_name[strlen(mask_name) - 2] = 'g';
81 
82  input = fopen(mask_name, "rb");
83  if (!input)
84  G_fatal_error("PNG: couldn't open input mask file %s", mask_name);
85 
86  if (fscanf(input, "P5 %d %d %d", &i_width, &i_height, &maxval) != 3)
87  G_fatal_error("PNG: invalid input mask file %s", mask_name);
88 
89  fgetc(input);
90 
91  if (i_width != png.width || i_height != png.height)
92  G_fatal_error("PNG: input mask file has incorrect dimensions: "
93  "expected: %dx%d got: %dx%d",
94  png.width, png.height, i_width, i_height);
95 
96  G_free(mask_name);
97 
98  for (y = 0, p = png.grid; y < png.height; y++) {
99  for (x = 0; x < png.width; x++, p++) {
100  unsigned int c = *p;
101 
102  int k = fgetc(input);
103 
104  k = k * 255 / maxval;
105 
106  c &= rgb_mask;
107  c |= png_get_color(0, 0, 0, 255 - k);
108 
109  *p = c;
110  }
111  }
112 
113  fclose(input);
114 }
unsigned int png_get_color(int r, int g, int b, int a)
Definition: color_table.c:118
void G_free(void *)
Free allocated memory.
Definition: gis/alloc.c:150
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
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
char * file_name
Definition: pngdriver.h:32
int true_color
Definition: pngdriver.h:34
int height
Definition: pngdriver.h:42
unsigned int * grid
Definition: pngdriver.h:43
int width
Definition: pngdriver.h:42
#define x