IMG_tif.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /*
  2. SDL_image: An example image loading library for use with SDL
  3. Copyright (C) 1999, 2000, 2001 Sam Lantinga
  4. This library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Library General Public
  6. License as published by the Free Software Foundation; either
  7. version 2 of the License, or (at your option) any later version.
  8. This library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Library General Public License for more details.
  12. You should have received a copy of the GNU Library General Public
  13. License along with this library; if not, write to the Free
  14. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  15. Sam Lantinga
  16. slouken@libsdl.org
  17. */
  18. /* $Id: IMG_tif.c,v 1.1 2004/07/21 16:24:11 paigoddess Exp $ */
  19. /* This is a TIFF image file loading framework */
  20. #include <stdio.h>
  21. #include "SDL_image.h"
  22. #include <tiffio.h>
  23. /*
  24. * These are the thunking routine to use the SDL_RWops* routines from
  25. * libtiff's internals.
  26. */
  27. static tsize_t tiff_read(thandle_t fd, tdata_t buf, tsize_t size)
  28. {
  29. return SDL_RWread((SDL_RWops*)fd, buf, 1, size);
  30. }
  31. static toff_t tiff_seek(thandle_t fd, toff_t offset, int origin)
  32. {
  33. return SDL_RWseek((SDL_RWops*)fd, offset, origin);
  34. }
  35. static tsize_t tiff_write(thandle_t fd, tdata_t buf, tsize_t size)
  36. {
  37. return SDL_RWwrite((SDL_RWops*)fd, buf, 1, size);
  38. }
  39. static int tiff_close(thandle_t fd)
  40. {
  41. /*
  42. * We don't want libtiff closing our SDL_RWops*, but if it's not given
  43. * a routine to try, and if the image isn't a TIFF, it'll segfault.
  44. */
  45. return 0;
  46. }
  47. static toff_t tiff_size(thandle_t fd)
  48. {
  49. Uint32 save_pos;
  50. toff_t size;
  51. save_pos = SDL_RWtell((SDL_RWops*)fd);
  52. SDL_RWseek((SDL_RWops*)fd, 0, SEEK_END);
  53. size = SDL_RWtell((SDL_RWops*)fd);
  54. SDL_RWseek((SDL_RWops*)fd, save_pos, SEEK_SET);
  55. return size;
  56. }
  57. int IMG_isTIF(SDL_RWops* src)
  58. {
  59. TIFF* tiff;
  60. TIFFErrorHandler prev_handler;
  61. /* Suppress output from libtiff */
  62. prev_handler = TIFFSetErrorHandler(NULL);
  63. /* Attempt to process the given file data */
  64. /* turn off memory mapped access with the m flag */
  65. tiff = TIFFClientOpen("SDL_image", "rm", (thandle_t)src,
  66. tiff_read, tiff_write, tiff_seek, tiff_close, tiff_size, NULL, NULL);
  67. /* Reset the default error handler, since it can be useful for info */
  68. TIFFSetErrorHandler(prev_handler);
  69. /* If it's not a TIFF, then tiff will be NULL. */
  70. if(!tiff)
  71. return 0;
  72. /* Free up any dynamically allocated memory libtiff uses */
  73. TIFFClose(tiff);
  74. return 1;
  75. }
  76. SDL_Surface* IMG_LoadTIF_RW(SDL_RWops* src)
  77. {
  78. TIFF* tiff;
  79. SDL_Surface* surface = NULL;
  80. Uint32 img_width, img_height;
  81. Uint32 Rmask, Gmask, Bmask, Amask, mask;
  82. Uint32 x, y;
  83. Uint32 half;
  84. /* turn off memory mapped access with the m flag */
  85. tiff = TIFFClientOpen("SDL_image", "rm", (thandle_t)src,
  86. tiff_read, tiff_write, tiff_seek, tiff_close, tiff_size, NULL, NULL);
  87. if(!tiff)
  88. return NULL;
  89. /* Retrieve the dimensions of the image from the TIFF tags */
  90. TIFFGetField(tiff, TIFFTAG_IMAGEWIDTH, &img_width);
  91. TIFFGetField(tiff, TIFFTAG_IMAGELENGTH, &img_height);
  92. Rmask = 0x000000FF;
  93. Gmask = 0x0000FF00;
  94. Bmask = 0x00FF0000;
  95. Amask = 0xFF000000;
  96. surface = SDL_AllocSurface(SDL_SWSURFACE, img_width, img_height, 32,
  97. Rmask, Gmask, Bmask, Amask);
  98. if(!surface)
  99. return NULL;
  100. if(!TIFFReadRGBAImage(tiff, img_width, img_height, surface->pixels, 0))
  101. return NULL;
  102. /* libtiff loads the image upside-down, flip it back */
  103. half = img_height / 2;
  104. for(y = 0; y < half; y++)
  105. {
  106. Uint32 *top = (Uint32 *)surface->pixels + y * surface->pitch/4;
  107. Uint32 *bot = (Uint32 *)surface->pixels
  108. + (img_height - y - 1) * surface->pitch/4;
  109. for(x = 0; x < img_width; x++)
  110. {
  111. Uint32 tmp = top[x];
  112. top[x] = bot[x];
  113. bot[x] = tmp;
  114. }
  115. }
  116. TIFFClose(tiff);
  117. return surface;
  118. }