gsd_img_tif.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. if (0 == gsd_getimage(&pixbuf, &xsize, &ysize)) {
  48. G_warning(_("Unable to get image of current GL screen"));
  49. return (1);
  50. }
  51. out = TIFFOpen(name, "w");
  52. if (out == NULL) {
  53. G_warning(_("Unable to open file <%s> for writing"), name);
  54. return (1);
  55. }
  56. /* Write out TIFF Tags */
  57. /* Assuming 24 bit RGB Tif */
  58. TIFFSetField(out, TIFFTAG_IMAGEWIDTH, xsize);
  59. TIFFSetField(out, TIFFTAG_IMAGELENGTH, ysize);
  60. TIFFSetField(out, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT);
  61. TIFFSetField(out, TIFFTAG_SAMPLESPERPIXEL, 24 > 8 ? 3 : 1);
  62. TIFFSetField(out, TIFFTAG_BITSPERSAMPLE, 24 > 1 ? 8 : 1);
  63. TIFFSetField(out, TIFFTAG_PLANARCONFIG, config);
  64. mapsize = 1 << 24;
  65. TIFFSetField(out, TIFFTAG_PHOTOMETRIC, 24 > 8 ?
  66. PHOTOMETRIC_RGB : PHOTOMETRIC_MINISBLACK);
  67. linebytes = ((xsize * ysize + 15) >> 3) & ~1;
  68. if (TIFFScanlineSize(out) > linebytes) {
  69. buf = (unsigned char *)G_malloc(linebytes);
  70. }
  71. else {
  72. buf = (unsigned char *)G_malloc(TIFFScanlineSize(out));
  73. }
  74. if (rowsperstrip != (unsigned short)-1) {
  75. rowsperstrip = (unsigned short)(8 * 1024 / linebytes);
  76. }
  77. TIFFSetField(out, TIFFTAG_ROWSPERSTRIP,
  78. rowsperstrip == 0 ? 1 : rowsperstrip);
  79. /* Done with Header Info */
  80. for (y = 0; y < ysize; y++) {
  81. unsigned int yy = ysize - y - 1;
  82. tmpptr = buf;
  83. for (x = 0; x < (xsize); x++) {
  84. *tmpptr++ = pixbuf[(yy * xsize + x) * 4 + 0];
  85. *tmpptr++ = pixbuf[(yy * xsize + x) * 4 + 1];
  86. *tmpptr++ = pixbuf[(yy * xsize + x) * 4 + 2];
  87. }
  88. if (TIFFWriteScanline(out, buf, y, 0) < 0) {
  89. break;
  90. }
  91. }
  92. G_free((void *)pixbuf);
  93. (void)TIFFClose(out);
  94. return (0);
  95. }
  96. #endif /* HAVE_TIFF */