GRASS Programmer's Manual  6.5.svn(2014)-r66266
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
gsd_img_ppm.c
Go to the documentation of this file.
1 
27 #include <stdlib.h>
28 
29 #include <grass/gis.h>
30 #include <grass/glocale.h>
31 #include <grass/ogsf_proto.h>
32 #include <grass/gstypes.h>
33 
42 int GS_write_ppm(const char *name)
43 {
44  unsigned int x;
45  int y;
46  unsigned int xsize, ysize;
47  FILE *fp;
48  unsigned char *pixbuf;
49 
50  gsd_getimage(&pixbuf, &xsize, &ysize);
51 
52  if (NULL == (fp = fopen(name, "w"))) {
53  G_warning(_("Unable to open file <%s> for writing"), name);
54  return (1);
55  }
56 
57  fprintf(fp, "P6 %d %d 255\n", xsize, ysize);
58 
59  for (y = ysize - 1; y >= 0; y--) {
60  for (x = 0; x < xsize; x++) {
61  unsigned char r = pixbuf[(y * xsize + x) * 4 + 0];
62  unsigned char g = pixbuf[(y * xsize + x) * 4 + 1];
63  unsigned char b = pixbuf[(y * xsize + x) * 4 + 2];
64 
65  fputc((int)r, fp);
66  fputc((int)g, fp);
67  fputc((int)b, fp);
68  }
69 
70  }
71  G_free(pixbuf);
72  fclose(fp);
73 
74  return (0);
75 }
76 
86 int GS_write_zoom(const char *name, unsigned int xsize, unsigned int ysize)
87 {
88  unsigned int x;
89  int y;
90  FILE *fp;
91  unsigned char *pixbuf;
92 
93  gsd_writeView(&pixbuf, xsize, ysize);
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 %d %d 255\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  }
114  free(pixbuf);
115  fclose(fp);
116 
117  return (0);
118 }
void G_free(void *buf)
Free allocated memory.
Definition: gis/alloc.c:142
float b
Definition: named_colr.c:8
string name
Definition: render.py:1314
float r
Definition: named_colr.c:8
int y
Definition: plot.c:34
int GS_write_zoom(const char *name, unsigned int xsize, unsigned int ysize)
Write zoom to file.
Definition: gsd_img_ppm.c:86
float g
Definition: named_colr.c:8
return NULL
Definition: dbfopen.c:1394
G_warning("category support for [%s] in mapset [%s] %s", name, mapset, type)
int GS_write_ppm(const char *name)
Save current GL screen to ppm file.
Definition: gsd_img_ppm.c:42
fclose(fd)
void free(void *)
int gsd_getimage(unsigned char **pixbuf, unsigned int *xsize, unsigned int *ysize)
Get image of current GL screen.
Definition: gsd_prim.c:893
int gsd_writeView(unsigned char **pixbuf, unsigned int xsize, unsigned int ysize)
Write view.
Definition: gsd_prim.c:951