GRASS Programmer's Manual  6.5.svn(2014)-r66266
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
pngdriver/read_ppm.c
Go to the documentation of this file.
1 
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <string.h>
5 
6 #include <grass/gis.h>
7 #include "pngdriver.h"
8 
9 void read_ppm(void)
10 {
11  FILE *input;
12  int x, y;
13  int i_width, i_height, maxval;
14  unsigned int rgb_mask = get_color(255, 255, 255, 0);
15  unsigned int *p;
16 
17  if (!true_color)
18  G_fatal_error("PNG: cannot use PPM/PGM with indexed color");
19 
20  input = fopen(file_name, "rb");
21  if (!input)
22  G_fatal_error("PNG: couldn't open input file %s", file_name);
23 
24  if (fscanf(input, "P6 %d %d %d", &i_width, &i_height, &maxval) != 3)
25  G_fatal_error("PNG: invalid input file %s", file_name);
26 
27  fgetc(input);
28 
29  if (i_width != width || i_height != height)
31  ("PNG: input file has incorrect dimensions: expected: %dx%d got: %dx%d",
32  width, height, i_width, i_height);
33 
34  for (y = 0, p = grid; y < height; y++) {
35  for (x = 0; x < width; x++, p++) {
36  unsigned int c = *p;
37 
38  int r = fgetc(input);
39  int g = fgetc(input);
40  int b = fgetc(input);
41 
42  r = r * 255 / maxval;
43  g = g * 255 / maxval;
44  b = b * 255 / maxval;
45 
46  c &= ~rgb_mask;
47  c |= get_color(r, g, b, 0);
48 
49  *p = c;
50  }
51  }
52 
53  fclose(input);
54 }
55 
56 void read_pgm(void)
57 {
58  char *mask_name = G_store(file_name);
59  FILE *input;
60  int x, y;
61  int i_width, i_height, maxval;
62  unsigned int rgb_mask = get_color(255, 255, 255, 0);
63  unsigned int *p;
64 
65  if (!true_color)
66  G_fatal_error("PNG: cannot use PPM/PGM with indexed color");
67 
68  mask_name[strlen(mask_name) - 2] = 'g';
69 
70  input = fopen(mask_name, "rb");
71  if (!input)
72  G_fatal_error("PNG: couldn't open input mask file %s", mask_name);
73 
74  if (fscanf(input, "P5 %d %d %d", &i_width, &i_height, &maxval) != 3)
75  G_fatal_error("PNG: invalid input mask file %s", mask_name);
76 
77  fgetc(input);
78 
79  if (i_width != width || i_height != height)
81  ("PNG: input mask file has incorrect dimensions: expected: %dx%d got: %dx%d",
82  width, height, i_width, i_height);
83 
84  G_free(mask_name);
85 
86  for (y = 0, p = grid; y < height; y++) {
87  for (x = 0; x < width; x++, p++) {
88  unsigned int c = *p;
89 
90  int k = fgetc(input);
91 
92  k = k * 255 / maxval;
93 
94  c &= rgb_mask;
95  c |= get_color(0, 0, 0, 255 - k);
96 
97  *p = c;
98  }
99  }
100 
101  fclose(input);
102 }
void G_free(void *buf)
Free allocated memory.
Definition: gis/alloc.c:142
float b
Definition: named_colr.c:8
char * G_store(const char *s)
Copy string to allocated memory.
Definition: store.c:32
float r
Definition: named_colr.c:8
tuple width
unsigned char * grid
int y
Definition: plot.c:34
int true_color
char * file_name
float g
Definition: named_colr.c:8
fclose(fd)
void read_ppm(void)
unsigned int get_color(int r, int g, int b, int a)
int G_fatal_error(const char *msg,...)
Print a fatal error message to stderr.
int height
void read_pgm(void)