GRASS GIS 8 Programmer's Manual  8.4.0dev(2024)-112dd97adf
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 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 
82  TIFFSetField(out, TIFFTAG_PHOTOMETRIC,
83  24 > 8 ? PHOTOMETRIC_RGB : PHOTOMETRIC_MINISBLACK);
84 
85  linebytes = ((xsize * ysize + 15) >> 3) & ~1;
86 
87  if (TIFFScanlineSize(out) > linebytes) {
88  buf = (unsigned char *)G_malloc(linebytes);
89  }
90  else {
91  buf = (unsigned char *)G_malloc(TIFFScanlineSize(out));
92  }
93 
94  if (rowsperstrip != (unsigned short)-1) {
95  rowsperstrip = (unsigned short)(8 * 1024 / linebytes);
96  }
97 
98  TIFFSetField(out, TIFFTAG_ROWSPERSTRIP,
99  rowsperstrip == 0 ? 1 : rowsperstrip);
100 
101  /* Done with Header Info */
102  for (y = 0; y < ysize; y++) {
103  unsigned int yy = ysize - y - 1;
104 
105  tmpptr = buf;
106 
107  for (x = 0; x < (xsize); x++) {
108  *tmpptr++ = pixbuf[(yy * xsize + x) * 4 + 0];
109  *tmpptr++ = pixbuf[(yy * xsize + x) * 4 + 1];
110  *tmpptr++ = pixbuf[(yy * xsize + x) * 4 + 2];
111  }
112 
113  if (TIFFWriteScanline(out, buf, y, 0) < 0) {
114  break;
115  }
116  }
117 
118  G_free((void *)pixbuf);
119  (void)TIFFClose(out);
120 
121  return (0);
122 }
123 
124 #endif /* HAVE_TIFF */
#define NULL
Definition: ccmath.h:32
void G_free(void *)
Free allocated memory.
Definition: gis/alloc.c:150
void G_warning(const char *,...) __attribute__((format(printf
#define G_malloc(n)
Definition: defs/gis.h:94
int gsd_getimage(unsigned char **, unsigned int *, unsigned int *)
Get image of current GL screen.
Definition: gsd_prim.c:902
#define _(str)
Definition: glocale.h:10
unsigned short rowsperstrip
Definition: gsd_img_tif.c:43
int GS_write_tif(const char *name)
Write data to tif file.
Definition: gsd_img_tif.c:53
unsigned short config
Definition: gsd_img_tif.c:41
unsigned short compression
Definition: gsd_img_tif.c:42
const char * name
Definition: named_colr.c:6
#define x