gsd_img_tif.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*!
  2. \file lib/ogsf/gsd_img_tif.c
  3. \brief OGSF library - TIFF stuff
  4. GRASS OpenGL gsurf OGSF Library
  5. (C) 1999-2008 by the GRASS Development Team
  6. - added little/big endian test Markus Neteler
  7. - modified to PPM by Bob Covill <bcovill@tekmap.ns.ca>
  8. - changed 10/99 Jaro
  9. - Created new function GS_write_tif based on RGB dump
  10. This program is free software under the
  11. GNU General Public License (>=v2).
  12. Read the file COPYING that comes with GRASS
  13. for details.
  14. \author Bill Brown USACERL, GMSL/University of Illinois
  15. \author Markus Neteler
  16. \author Jaro Hofierka
  17. \author Doxygenized by Martin Landa <landa.martin gmail.com> (May 2008)
  18. */
  19. #include <grass/config.h>
  20. #ifdef HAVE_TIFFIO_H
  21. #include <stdlib.h>
  22. #include <sys/types.h>
  23. #ifdef __CYGWIN__
  24. #include <w32api/wtypes.h>
  25. #endif
  26. #include <grass/gis.h>
  27. #include <grass/ogsf.h>
  28. #include <grass/glocale.h>
  29. #include <tiffio.h>
  30. unsigned short config = PLANARCONFIG_CONTIG;
  31. unsigned short compression = -1;
  32. unsigned short rowsperstrip = 0;
  33. /*!
  34. \brief Write data to tif file
  35. \param name filename
  36. \return 1 on error
  37. \return 0 on success
  38. */
  39. int GS_write_tif(const char *name)
  40. {
  41. TIFF *out;
  42. unsigned int y, x;
  43. unsigned int xsize, ysize;
  44. int mapsize, linebytes;
  45. unsigned char *buf, *tmpptr;
  46. unsigned char *pixbuf;
  47. gsd_getimage(&pixbuf, &xsize, &ysize);
  48. out = TIFFOpen(name, "w");
  49. if (out == NULL) {
  50. G_warning(_("Unable to open file <%s> for writing"), name);
  51. return (1);
  52. }
  53. /* Write out TIFF Tags */
  54. /* Assuming 24 bit RGB Tif */
  55. TIFFSetField(out, TIFFTAG_IMAGEWIDTH, xsize);
  56. TIFFSetField(out, TIFFTAG_IMAGELENGTH, ysize);
  57. TIFFSetField(out, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT);
  58. TIFFSetField(out, TIFFTAG_SAMPLESPERPIXEL, 24 > 8 ? 3 : 1);
  59. TIFFSetField(out, TIFFTAG_BITSPERSAMPLE, 24 > 1 ? 8 : 1);
  60. TIFFSetField(out, TIFFTAG_PLANARCONFIG, config);
  61. mapsize = 1 << 24;
  62. TIFFSetField(out, TIFFTAG_PHOTOMETRIC, 24 > 8 ?
  63. PHOTOMETRIC_RGB : PHOTOMETRIC_MINISBLACK);
  64. linebytes = ((xsize * ysize + 15) >> 3) & ~1;
  65. if (TIFFScanlineSize(out) > linebytes) {
  66. buf = (unsigned char *)G_malloc(linebytes);
  67. }
  68. else {
  69. buf = (unsigned char *)G_malloc(TIFFScanlineSize(out));
  70. }
  71. if (rowsperstrip != (unsigned short)-1) {
  72. rowsperstrip = (unsigned short)(8 * 1024 / linebytes);
  73. }
  74. TIFFSetField(out, TIFFTAG_ROWSPERSTRIP,
  75. rowsperstrip == 0 ? 1 : rowsperstrip);
  76. /* Done with Header Info */
  77. for (y = 0; y < ysize; y++) {
  78. unsigned int yy = ysize - y - 1;
  79. tmpptr = buf;
  80. for (x = 0; x < (xsize); x++) {
  81. *tmpptr++ = pixbuf[(yy * xsize + x) * 4 + 0];
  82. *tmpptr++ = pixbuf[(yy * xsize + x) * 4 + 1];
  83. *tmpptr++ = pixbuf[(yy * xsize + x) * 4 + 2];
  84. }
  85. if (TIFFWriteScanline(out, buf, y, 0) < 0) {
  86. break;
  87. }
  88. }
  89. G_free((void *)pixbuf);
  90. (void)TIFFClose(out);
  91. return (0);
  92. }
  93. #endif /* HAVE_TIFF */