gsd_img_tif.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*!
  2. \file 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. #include <grass/gis.h>
  24. #include <grass/ogsf.h>
  25. #include <grass/glocale.h>
  26. #include <tiffio.h>
  27. unsigned short config = PLANARCONFIG_CONTIG;
  28. unsigned short compression = -1;
  29. unsigned short rowsperstrip = 0;
  30. /*!
  31. \brief Write data to tif file
  32. \param name filename
  33. \return 1 on error
  34. \return 0 on success
  35. */
  36. int GS_write_tif(const char *name)
  37. {
  38. TIFF *out;
  39. unsigned int y, x;
  40. unsigned int xsize, ysize;
  41. int mapsize, linebytes;
  42. unsigned char *buf, *tmpptr;
  43. unsigned char *pixbuf;
  44. gsd_getimage(&pixbuf, &xsize, &ysize);
  45. out = TIFFOpen(name, "w");
  46. if (out == NULL) {
  47. G_warning(_("Unable to open file <%s> for writing"), name);
  48. return (1);
  49. }
  50. /* Write out TIFF Tags */
  51. /* Assuming 24 bit RGB Tif */
  52. TIFFSetField(out, TIFFTAG_IMAGEWIDTH, xsize);
  53. TIFFSetField(out, TIFFTAG_IMAGELENGTH, ysize);
  54. TIFFSetField(out, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT);
  55. TIFFSetField(out, TIFFTAG_SAMPLESPERPIXEL, 24 > 8 ? 3 : 1);
  56. TIFFSetField(out, TIFFTAG_BITSPERSAMPLE, 24 > 1 ? 8 : 1);
  57. TIFFSetField(out, TIFFTAG_PLANARCONFIG, config);
  58. mapsize = 1 << 24;
  59. TIFFSetField(out, TIFFTAG_PHOTOMETRIC, 24 > 8 ?
  60. PHOTOMETRIC_RGB : PHOTOMETRIC_MINISBLACK);
  61. linebytes = ((xsize * ysize + 15) >> 3) & ~1;
  62. if (TIFFScanlineSize(out) > linebytes) {
  63. buf = (unsigned char *)G_malloc(linebytes);
  64. }
  65. else {
  66. buf = (unsigned char *)G_malloc(TIFFScanlineSize(out));
  67. }
  68. if (rowsperstrip != (unsigned short)-1) {
  69. rowsperstrip = (unsigned short)(8 * 1024 / linebytes);
  70. }
  71. TIFFSetField(out, TIFFTAG_ROWSPERSTRIP,
  72. rowsperstrip == 0 ? 1 : rowsperstrip);
  73. /* Done with Header Info */
  74. for (y = 0; y < ysize; y++) {
  75. unsigned int yy = ysize - y - 1;
  76. tmpptr = buf;
  77. for (x = 0; x < (xsize); x++) {
  78. *tmpptr++ = pixbuf[(yy * xsize + x) * 4 + 0];
  79. *tmpptr++ = pixbuf[(yy * xsize + x) * 4 + 1];
  80. *tmpptr++ = pixbuf[(yy * xsize + x) * 4 + 2];
  81. }
  82. if (TIFFWriteScanline(out, buf, y, 0) < 0) {
  83. break;
  84. }
  85. }
  86. G_free((void *)pixbuf);
  87. (void)TIFFClose(out);
  88. return (0);
  89. }
  90. #endif /* HAVE_TIFF */