GRASS GIS 7 Programmer's Manual  7.9.dev(2021)-e5379bbd7
gsd_img_tif.c
Go to the documentation of this file.
1 /*!
2  \file lib/ogsf/gsd_img_tif.c
3 
4  \brief OGSF library - TIFF 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_tif 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 Jaro Hofierka
23  \author Doxygenized by Martin Landa <landa.martin gmail.com> (May 2008)
24  */
25 
26 #include <grass/config.h>
27 
28 #ifdef HAVE_TIFFIO_H
29 
30 #include <stdlib.h>
31 #include <sys/types.h>
32 #ifdef __CYGWIN__
33  #include <w32api/wtypes.h>
34 #endif
35 #include <grass/gis.h>
36 #include <grass/ogsf.h>
37 #include <grass/glocale.h>
38 
39 #include <tiffio.h>
40 
41 unsigned short config = PLANARCONFIG_CONTIG;
42 unsigned short compression = -1;
43 unsigned short rowsperstrip = 0;
44 
45 /*!
46  \brief Write data to tif file
47 
48  \param name filename
49 
50  \return 1 on error
51  \return 0 on success
52  */
53 int GS_write_tif(const char *name)
54 {
55  TIFF *out;
56  unsigned int y, x;
57  unsigned int xsize, ysize;
58  int mapsize, linebytes;
59  unsigned char *buf, *tmpptr;
60  unsigned char *pixbuf;
61 
62  if (0 == gsd_getimage(&pixbuf, &xsize, &ysize)) {
63  G_warning(_("Unable to get image of current GL screen"));
64  return (1);
65  }
66 
67  out = TIFFOpen(name, "w");
68  if (out == NULL) {
69  G_warning(_("Unable to open file <%s> for writing"), name);
70  return (1);
71  }
72 
73  /* Write out TIFF Tags */
74  /* Assuming 24 bit RGB Tif */
75  TIFFSetField(out, TIFFTAG_IMAGEWIDTH, xsize);
76  TIFFSetField(out, TIFFTAG_IMAGELENGTH, ysize);
77  TIFFSetField(out, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT);
78  TIFFSetField(out, TIFFTAG_SAMPLESPERPIXEL, 24 > 8 ? 3 : 1);
79  TIFFSetField(out, TIFFTAG_BITSPERSAMPLE, 24 > 1 ? 8 : 1);
80  TIFFSetField(out, TIFFTAG_PLANARCONFIG, config);
81  mapsize = 1 << 24;
82 
83  TIFFSetField(out, TIFFTAG_PHOTOMETRIC, 24 > 8 ?
84  PHOTOMETRIC_RGB : PHOTOMETRIC_MINISBLACK);
85 
86  linebytes = ((xsize * ysize + 15) >> 3) & ~1;
87 
88  if (TIFFScanlineSize(out) > linebytes) {
89  buf = (unsigned char *)G_malloc(linebytes);
90  }
91  else {
92  buf = (unsigned char *)G_malloc(TIFFScanlineSize(out));
93  }
94 
95  if (rowsperstrip != (unsigned short)-1) {
96  rowsperstrip = (unsigned short)(8 * 1024 / linebytes);
97  }
98 
99  TIFFSetField(out, TIFFTAG_ROWSPERSTRIP,
100  rowsperstrip == 0 ? 1 : rowsperstrip);
101 
102  /* Done with Header Info */
103  for (y = 0; y < ysize; y++) {
104  unsigned int yy = ysize - y - 1;
105 
106  tmpptr = buf;
107 
108  for (x = 0; x < (xsize); x++) {
109  *tmpptr++ = pixbuf[(yy * xsize + x) * 4 + 0];
110  *tmpptr++ = pixbuf[(yy * xsize + x) * 4 + 1];
111  *tmpptr++ = pixbuf[(yy * xsize + x) * 4 + 2];
112  }
113 
114  if (TIFFWriteScanline(out, buf, y, 0) < 0) {
115  break;
116  }
117  }
118 
119  G_free((void *)pixbuf);
120  (void)TIFFClose(out);
121 
122  return (0);
123 }
124 
125 #endif /* HAVE_TIFF */
#define G_malloc(n)
Definition: defs/gis.h:112
unsigned short compression
Definition: gsd_img_tif.c:42
void G_free(void *)
Free allocated memory.
Definition: gis/alloc.c:149
#define NULL
Definition: ccmath.h:32
#define x
int GS_write_tif(const char *name)
Write data to tif file.
Definition: gsd_img_tif.c:53
int gsd_getimage(unsigned char **, unsigned int *, unsigned int *)
Get image of current GL screen.
Definition: gsd_prim.c:905
unsigned short rowsperstrip
Definition: gsd_img_tif.c:43
void G_warning(const char *,...) __attribute__((format(printf
#define _(str)
Definition: glocale.h:10
const char * name
Definition: named_colr.c:7
unsigned short config
Definition: gsd_img_tif.c:41