GRASS Programmer's Manual
6.4.2(2012)
|
00001 00026 #include <grass/config.h> 00027 00028 #ifdef HAVE_TIFFIO_H 00029 00030 #include <stdlib.h> 00031 #include <sys/types.h> 00032 00033 #include <grass/gis.h> 00034 #include <grass/gstypes.h> 00035 #include <grass/glocale.h> 00036 00037 #include <tiffio.h> 00038 00039 unsigned short config = PLANARCONFIG_CONTIG; 00040 unsigned short compression = -1; 00041 unsigned short rowsperstrip = 0; 00042 00051 int GS_write_tif(const char *name) 00052 { 00053 TIFF *out; 00054 int y, x; 00055 unsigned int xsize, ysize; 00056 int mapsize, linebytes; 00057 unsigned char *buf, *tmpptr; 00058 unsigned char *pixbuf; 00059 00060 gsd_getimage(&pixbuf, &xsize, &ysize); 00061 00062 out = TIFFOpen(name, "w"); 00063 if (out == NULL) { 00064 G_warning(_("Unable to open file <%s> for writing"), name); 00065 return (1); 00066 } 00067 00068 /* Write out TIFF Tags */ 00069 /* Assuming 24 bit RGB Tif */ 00070 TIFFSetField(out, TIFFTAG_IMAGEWIDTH, xsize); 00071 TIFFSetField(out, TIFFTAG_IMAGELENGTH, ysize); 00072 TIFFSetField(out, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT); 00073 TIFFSetField(out, TIFFTAG_SAMPLESPERPIXEL, 24 > 8 ? 3 : 1); 00074 TIFFSetField(out, TIFFTAG_BITSPERSAMPLE, 24 > 1 ? 8 : 1); 00075 TIFFSetField(out, TIFFTAG_PLANARCONFIG, config); 00076 mapsize = 1 << 24; 00077 00078 TIFFSetField(out, TIFFTAG_PHOTOMETRIC, 24 > 8 ? 00079 PHOTOMETRIC_RGB : PHOTOMETRIC_MINISBLACK); 00080 00081 linebytes = ((xsize * ysize + 15) >> 3) & ~1; 00082 00083 if (TIFFScanlineSize(out) > linebytes) { 00084 buf = (unsigned char *)G_malloc(linebytes); 00085 } 00086 else { 00087 buf = (unsigned char *)G_malloc(TIFFScanlineSize(out)); 00088 } 00089 00090 if (rowsperstrip != (unsigned short)-1) { 00091 rowsperstrip = (unsigned short)(8 * 1024 / linebytes); 00092 } 00093 00094 TIFFSetField(out, TIFFTAG_ROWSPERSTRIP, 00095 rowsperstrip == 0 ? 1 : rowsperstrip); 00096 00097 /* Done with Header Info */ 00098 for (y = 0; y < ysize; y++) { 00099 int yy = ysize - y - 1; 00100 00101 tmpptr = buf; 00102 00103 for (x = 0; x < (xsize); x++) { 00104 *tmpptr++ = pixbuf[(yy * xsize + x) * 4 + 0]; 00105 *tmpptr++ = pixbuf[(yy * xsize + x) * 4 + 1]; 00106 *tmpptr++ = pixbuf[(yy * xsize + x) * 4 + 2]; 00107 } 00108 00109 if (TIFFWriteScanline(out, buf, y, 0) < 0) { 00110 break; 00111 } 00112 } 00113 00114 G_free((void *)pixbuf); 00115 (void)TIFFClose(out); 00116 00117 return (0); 00118 } 00119 00120 #endif /* HAVE_TIFF */