GRASS GIS 7 Programmer's Manual  7.5.svn(2018)-r72651
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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  gsd_getimage(&pixbuf, &xsize, &ysize);
63 
64  out = TIFFOpen(name, "w");
65  if (out == NULL) {
66  G_warning(_("Unable to open file <%s> for writing"), name);
67  return (1);
68  }
69 
70  /* Write out TIFF Tags */
71  /* Assuming 24 bit RGB Tif */
72  TIFFSetField(out, TIFFTAG_IMAGEWIDTH, xsize);
73  TIFFSetField(out, TIFFTAG_IMAGELENGTH, ysize);
74  TIFFSetField(out, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT);
75  TIFFSetField(out, TIFFTAG_SAMPLESPERPIXEL, 24 > 8 ? 3 : 1);
76  TIFFSetField(out, TIFFTAG_BITSPERSAMPLE, 24 > 1 ? 8 : 1);
77  TIFFSetField(out, TIFFTAG_PLANARCONFIG, config);
78  mapsize = 1 << 24;
79 
80  TIFFSetField(out, TIFFTAG_PHOTOMETRIC, 24 > 8 ?
81  PHOTOMETRIC_RGB : PHOTOMETRIC_MINISBLACK);
82 
83  linebytes = ((xsize * ysize + 15) >> 3) & ~1;
84 
85  if (TIFFScanlineSize(out) > linebytes) {
86  buf = (unsigned char *)G_malloc(linebytes);
87  }
88  else {
89  buf = (unsigned char *)G_malloc(TIFFScanlineSize(out));
90  }
91 
92  if (rowsperstrip != (unsigned short)-1) {
93  rowsperstrip = (unsigned short)(8 * 1024 / linebytes);
94  }
95 
96  TIFFSetField(out, TIFFTAG_ROWSPERSTRIP,
97  rowsperstrip == 0 ? 1 : rowsperstrip);
98 
99  /* Done with Header Info */
100  for (y = 0; y < ysize; y++) {
101  unsigned int yy = ysize - y - 1;
102 
103  tmpptr = buf;
104 
105  for (x = 0; x < (xsize); x++) {
106  *tmpptr++ = pixbuf[(yy * xsize + x) * 4 + 0];
107  *tmpptr++ = pixbuf[(yy * xsize + x) * 4 + 1];
108  *tmpptr++ = pixbuf[(yy * xsize + x) * 4 + 2];
109  }
110 
111  if (TIFFWriteScanline(out, buf, y, 0) < 0) {
112  break;
113  }
114  }
115 
116  G_free((void *)pixbuf);
117  (void)TIFFClose(out);
118 
119  return (0);
120 }
121 
122 #endif /* HAVE_TIFF */
void G_free(void *buf)
Free allocated memory.
Definition: gis/alloc.c:149
unsigned short compression
Definition: gsd_img_tif.c:42
#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
unsigned short rowsperstrip
Definition: gsd_img_tif.c:43
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
const char * name
Definition: named_colr.c:7
void G_warning(const char *msg,...)
Print a warning message to stderr.
Definition: gis/error.c:204
unsigned short config
Definition: gsd_img_tif.c:41