GRASS GIS 7 Programmer's Manual  7.5.svn(2017)-r71806
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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  gsd_getimage(&pixbuf, &xsize, &ysize);
50 
51  if (NULL == (fp = fopen(name, "w"))) {
52  G_warning(_("Unable to open file <%s> for writing"), name);
53  return (1);
54  }
55 
56  fprintf(fp, "P6\n%d %d\n255\n", xsize, ysize);
57 
58  for (y = ysize - 1; y >= 0; y--) {
59  for (x = 0; x < xsize; x++) {
60  unsigned char r = pixbuf[(y * xsize + x) * 4 + 0];
61  unsigned char g = pixbuf[(y * xsize + x) * 4 + 1];
62  unsigned char b = pixbuf[(y * xsize + x) * 4 + 2];
63 
64  fputc((int)r, fp);
65  fputc((int)g, fp);
66  fputc((int)b, fp);
67  }
68 
69  }
70  G_free(pixbuf);
71  fclose(fp);
72 
73  return (0);
74 }
75 
76 /*!
77  \brief Write zoom to file
78 
79  \param name file name
80  \param xsize,ysize
81 
82  \return 1 on failure
83  \return 0 on success
84  */
85 int GS_write_zoom(const char *name, unsigned int xsize, unsigned int ysize)
86 {
87  unsigned int x;
88  int y;
89  FILE *fp;
90  unsigned char *pixbuf;
91 
92  gsd_writeView(&pixbuf, xsize, ysize);
93 
94  if (NULL == (fp = fopen(name, "w"))) {
95  G_warning(_("Unable to open file <%s> for writing"), name);
96  return (1);
97  }
98 
99  fprintf(fp, "P6\n%d %d\n255\n", xsize, ysize);
100 
101  for (y = ysize - 1; y >= 0; y--) {
102  for (x = 0; x < xsize; x++) {
103  unsigned char r = pixbuf[(y * xsize + x) * 4 + 0];
104  unsigned char g = pixbuf[(y * xsize + x) * 4 + 1];
105  unsigned char b = pixbuf[(y * xsize + x) * 4 + 2];
106 
107  fputc((int)r, fp);
108  fputc((int)g, fp);
109  fputc((int)b, fp);
110  }
111 
112  }
113  free(pixbuf);
114  fclose(fp);
115 
116  return (0);
117 }
void G_free(void *buf)
Free allocated memory.
Definition: gis/alloc.c:149
void free(void *)
#define NULL
Definition: ccmath.h:32
#define x
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:85
float g
Definition: named_colr.c:8
fclose(fd)
int GS_write_ppm(const char *name)
Save current GL screen to ppm file.
Definition: gsd_img_ppm.c:41
int gsd_getimage(unsigned char **pixbuf, unsigned int *xsize, unsigned int *ysize)
Get image of current GL screen.
Definition: gsd_prim.c:893
#define _(str)
Definition: glocale.h:13
int gsd_writeView(unsigned char **pixbuf, unsigned int xsize, unsigned int ysize)
Write view.
Definition: gsd_prim.c:951
const char * name
Definition: named_colr.c:7
void G_warning(const char *msg,...)
Print a warning message to stderr.
Definition: gis/error.c:203
double r
Definition: r_raster.c:39