GRASS 8 Programmer's Manual 8.6.0dev(2026)-745b61fbf6
Loading...
Searching...
No Matches
pngdriver/write_ppm.c
Go to the documentation of this file.
1/*!
2 \file lib/pngdriver/write_ppm.c
3
4 \brief GRASS png display driver - write PPM 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 "pngdriver.h"
19
20void write_ppm(void)
21{
22 FILE *output;
23 int x, y;
24 unsigned int *p;
25
26 output = fopen(png.file_name, "wb");
27 if (!output)
28 G_fatal_error("PNG: couldn't open output file %s", png.file_name);
29
30 fprintf(output, "P6\n%d %d\n255\n", png.width, png.height);
31
32 for (y = 0, p = png.grid; y < png.height; y++) {
33 for (x = 0; x < png.width; x++, p++) {
34 unsigned int c = *p;
35 int r, g, b, a;
36
37 png_get_pixel(c, &r, &g, &b, &a);
38
39 fputc((unsigned char)r, output);
40 fputc((unsigned char)g, output);
41 fputc((unsigned char)b, output);
42 }
43 }
44
46}
47
48void write_pgm(void)
49{
50 char *mask_name = G_store(png.file_name);
51 FILE *output;
52 int x, y;
53 unsigned int *p;
54
55 mask_name[strlen(mask_name) - 2] = 'g';
56
57 output = fopen(mask_name, "wb");
58 if (!output)
59 G_fatal_error("PNG: couldn't open mask file %s", mask_name);
60
62
63 fprintf(output, "P5\n%d %d\n255\n", png.width, png.height);
64
65 for (y = 0, p = png.grid; y < png.height; y++) {
66 for (x = 0; x < png.width; x++, p++) {
67 unsigned int c = *p;
68 int r, g, b, a;
69
70 png_get_pixel(c, &r, &g, &b, &a);
71
72 fputc((unsigned char)(255 - a), output);
73 }
74 }
75
77}
void png_get_pixel(unsigned int pixel, 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
float g
Definition named_colr.c:7
struct png_state png
void write_pgm(void)
void write_ppm(void)
GRASS png display driver - header file.
void output(const char *fmt,...)
double b
Definition r_raster.c:37
double r
Definition r_raster.c:37
#define x