GRASS Programmer's Manual  6.5.svn(2014)-r66266
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
gsd_img_tif.c
Go to the documentation of this file.
1 
26 #include <grass/config.h>
27 
28 #ifdef HAVE_TIFFIO_H
29 
30 #include <stdlib.h>
31 #include <sys/types.h>
32 
33 #include <grass/gis.h>
34 #include <grass/gstypes.h>
35 #include <grass/glocale.h>
36 
37 #include <tiffio.h>
38 
39 unsigned short config = PLANARCONFIG_CONTIG;
40 unsigned short compression = -1;
41 unsigned short rowsperstrip = 0;
42 
51 int GS_write_tif(const char *name)
52 {
53  TIFF *out;
54  int y, x;
55  unsigned int xsize, ysize;
56  int mapsize, linebytes;
57  unsigned char *buf, *tmpptr;
58  unsigned char *pixbuf;
59 
60  gsd_getimage(&pixbuf, &xsize, &ysize);
61 
62  out = TIFFOpen(name, "w");
63  if (out == NULL) {
64  G_warning(_("Unable to open file <%s> for writing"), name);
65  return (1);
66  }
67 
68  /* Write out TIFF Tags */
69  /* Assuming 24 bit RGB Tif */
70  TIFFSetField(out, TIFFTAG_IMAGEWIDTH, xsize);
71  TIFFSetField(out, TIFFTAG_IMAGELENGTH, ysize);
72  TIFFSetField(out, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT);
73  TIFFSetField(out, TIFFTAG_SAMPLESPERPIXEL, 24 > 8 ? 3 : 1);
74  TIFFSetField(out, TIFFTAG_BITSPERSAMPLE, 24 > 1 ? 8 : 1);
75  TIFFSetField(out, TIFFTAG_PLANARCONFIG, config);
76  mapsize = 1 << 24;
77 
78  TIFFSetField(out, TIFFTAG_PHOTOMETRIC, 24 > 8 ?
79  PHOTOMETRIC_RGB : PHOTOMETRIC_MINISBLACK);
80 
81  linebytes = ((xsize * ysize + 15) >> 3) & ~1;
82 
83  if (TIFFScanlineSize(out) > linebytes) {
84  buf = (unsigned char *)G_malloc(linebytes);
85  }
86  else {
87  buf = (unsigned char *)G_malloc(TIFFScanlineSize(out));
88  }
89 
90  if (rowsperstrip != (unsigned short)-1) {
91  rowsperstrip = (unsigned short)(8 * 1024 / linebytes);
92  }
93 
94  TIFFSetField(out, TIFFTAG_ROWSPERSTRIP,
95  rowsperstrip == 0 ? 1 : rowsperstrip);
96 
97  /* Done with Header Info */
98  for (y = 0; y < ysize; y++) {
99  int yy = ysize - y - 1;
100 
101  tmpptr = buf;
102 
103  for (x = 0; x < (xsize); x++) {
104  *tmpptr++ = pixbuf[(yy * xsize + x) * 4 + 0];
105  *tmpptr++ = pixbuf[(yy * xsize + x) * 4 + 1];
106  *tmpptr++ = pixbuf[(yy * xsize + x) * 4 + 2];
107  }
108 
109  if (TIFFWriteScanline(out, buf, y, 0) < 0) {
110  break;
111  }
112  }
113 
114  G_free((void *)pixbuf);
115  (void)TIFFClose(out);
116 
117  return (0);
118 }
119 
120 #endif /* HAVE_TIFF */
void G_free(void *buf)
Free allocated memory.
Definition: gis/alloc.c:142
unsigned short compression
Definition: gsd_img_tif.c:40
string name
Definition: render.py:1314
int y
Definition: plot.c:34
int GS_write_tif(const char *name)
Write data to tif file.
Definition: gsd_img_tif.c:51
unsigned short rowsperstrip
Definition: gsd_img_tif.c:41
char buf[GNAME_MAX+sizeof(G3D_DIRECTORY)+2]
Definition: g3drange.c:62
return NULL
Definition: dbfopen.c:1394
G_warning("category support for [%s] in mapset [%s] %s", name, mapset, type)
int gsd_getimage(unsigned char **pixbuf, unsigned int *xsize, unsigned int *ysize)
Get image of current GL screen.
Definition: gsd_prim.c:893
unsigned short config
Definition: gsd_img_tif.c:39