GRASS 8 Programmer's Manual 8.6.0dev(2026)-c83afef6d3
Loading...
Searching...
No Matches
gsd_img_ppm.c
Go to the documentation of this file.
1/*!
2 \file lib/ogsf/gsd_img_ppm.c
3
4 \brief OGSF library - PPM stuff
5
6 GRASS OpenGL gsurf OGSF Library
7
8 SPDX-FileCopyrightText: 1999-2008 GRASS Development Team
9 SPDX-License-Identifier: GPL-2.0-or-later
10
11 - added little/big endian test Markus Neteler
12 - modified to PPM by Bob Covill <bcovill@tekmap.ns.ca>
13 - changed 10/99 Jaro
14 - Created new function GS_write_ppm based on RGB dump
15
16 \author Bill Brown USACERL, GMSL/University of Illinois
17 \author Markus Neteler
18 \author Bob Covill
19 \author Jaro Hofierka
20 \author Doxygenized by Martin Landa <landa.martin gmail.com> (May 2008)
21 */
22
23#include <stdlib.h>
24
25#include <grass/gis.h>
26#include <grass/glocale.h>
27#include <grass/ogsf.h>
28
29/*!
30 \brief Save current GL screen to ppm file
31
32 \param name file name
33
34 \return 1 on failure
35 \return 0 on success
36 */
37int GS_write_ppm(const char *name)
38{
39 unsigned int x;
40 int y;
41 unsigned int xsize, ysize;
42 FILE *fp;
43 unsigned char *pixbuf;
44
45 if (0 == gsd_getimage(&pixbuf, &xsize, &ysize)) {
46 G_warning(_("Unable to get image of current GL screen"));
47 return (1);
48 }
49
50 if (NULL == (fp = fopen(name, "w"))) {
51 G_warning(_("Unable to open file <%s> for writing"), name);
52 return (1);
53 }
54
55 fprintf(fp, "P6\n%d %d\n255\n", xsize, ysize);
56
57 for (y = ysize - 1; y >= 0; y--) {
58 for (x = 0; x < xsize; x++) {
59 unsigned char r = pixbuf[(y * xsize + x) * 4 + 0];
60 unsigned char g = pixbuf[(y * xsize + x) * 4 + 1];
61 unsigned char b = pixbuf[(y * xsize + x) * 4 + 2];
62
63 fputc((int)r, fp);
64 fputc((int)g, fp);
65 fputc((int)b, fp);
66 }
67 }
69 fclose(fp);
70
71 return (0);
72}
73
74/*!
75 \brief Write zoom to file
76
77 \param name file name
78 \param xsize,ysize
79
80 \return 1 on failure
81 \return 0 on success
82 */
83int GS_write_zoom(const char *name, unsigned int xsize, unsigned int ysize)
84{
85 unsigned int x;
86 int y;
87 FILE *fp;
88 unsigned char *pixbuf;
89
90 if (0 == gsd_writeView(&pixbuf, xsize, ysize)) {
91 G_warning(_("Unable to write view"));
92 return (1);
93 }
94
95 if (NULL == (fp = fopen(name, "w"))) {
96 G_warning(_("Unable to open file <%s> for writing"), name);
97 return (1);
98 }
99
100 fprintf(fp, "P6\n%d %d\n255\n", xsize, ysize);
101
102 for (y = ysize - 1; y >= 0; y--) {
103 for (x = 0; x < xsize; x++) {
104 unsigned char r = pixbuf[(y * xsize + x) * 4 + 0];
105 unsigned char g = pixbuf[(y * xsize + x) * 4 + 1];
106 unsigned char b = pixbuf[(y * xsize + x) * 4 + 2];
107
108 fputc((int)r, fp);
109 fputc((int)g, fp);
110 fputc((int)b, fp);
111 }
112 }
113 free(pixbuf);
114 fclose(fp);
115
116 return (0);
117}
#define NULL
Definition ccmath.h:32
void G_free(void *)
Free allocated memory.
Definition gis/alloc.c:145
void G_warning(const char *,...) __attribute__((format(printf
int gsd_writeView(unsigned char **, unsigned int, unsigned int)
Write view.
Definition gsd_prim.c:965
int gsd_getimage(unsigned char **, unsigned int *, unsigned int *)
Get image of current GL screen.
Definition gsd_prim.c:898
#define _(str)
Definition glocale.h:10
int GS_write_zoom(const char *name, unsigned int xsize, unsigned int ysize)
Write zoom to file.
Definition gsd_img_ppm.c:83
int GS_write_ppm(const char *name)
Save current GL screen to ppm file.
Definition gsd_img_ppm.c:37
float g
Definition named_colr.c:7
const char * name
Definition named_colr.c:6
OGSF header file (structures)
double b
Definition r_raster.c:37
double r
Definition r_raster.c:37
void free(void *)
#define x