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