GRASS GIS 7 Programmer's Manual  7.9.dev(2021)-e5379bbd7
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  }
73  G_free(pixbuf);
74  fclose(fp);
75 
76  return (0);
77 }
78 
79 /*!
80  \brief Write zoom to file
81 
82  \param name file name
83  \param xsize,ysize
84 
85  \return 1 on failure
86  \return 0 on success
87  */
88 int GS_write_zoom(const char *name, unsigned int xsize, unsigned int ysize)
89 {
90  unsigned int x;
91  int y;
92  FILE *fp;
93  unsigned char *pixbuf;
94 
95  if (0 == gsd_writeView(&pixbuf, xsize, ysize)) {
96  G_warning(_("Unable to write view"));
97  return (1);
98  }
99 
100  if (NULL == (fp = fopen(name, "w"))) {
101  G_warning(_("Unable to open file <%s> for writing"), name);
102  return (1);
103  }
104 
105  fprintf(fp, "P6\n%d %d\n255\n", xsize, ysize);
106 
107  for (y = ysize - 1; y >= 0; y--) {
108  for (x = 0; x < xsize; x++) {
109  unsigned char r = pixbuf[(y * xsize + x) * 4 + 0];
110  unsigned char g = pixbuf[(y * xsize + x) * 4 + 1];
111  unsigned char b = pixbuf[(y * xsize + x) * 4 + 2];
112 
113  fputc((int)r, fp);
114  fputc((int)g, fp);
115  fputc((int)b, fp);
116  }
117 
118  }
119  free(pixbuf);
120  fclose(fp);
121 
122  return (0);
123 }
void G_free(void *)
Free allocated memory.
Definition: gis/alloc.c:149
void free(void *)
#define NULL
Definition: ccmath.h:32
#define x
int gsd_getimage(unsigned char **, unsigned int *, unsigned int *)
Get image of current GL screen.
Definition: gsd_prim.c:905
double b
Definition: r_raster.c:39
int GS_write_zoom(const char *name, unsigned int xsize, unsigned int ysize)
Write zoom to file.
Definition: gsd_img_ppm.c:88
float g
Definition: named_colr.c:8
void G_warning(const char *,...) __attribute__((format(printf
int GS_write_ppm(const char *name)
Save current GL screen to ppm file.
Definition: gsd_img_ppm.c:41
#define _(str)
Definition: glocale.h:10
const char * name
Definition: named_colr.c:7
int gsd_writeView(unsigned char **, unsigned int, unsigned int)
Write view.
Definition: gsd_prim.c:971
double r
Definition: r_raster.c:39