IMG.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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.c,v 1.3 2006/07/10 16:34:18 paigoddess Exp $ */
  19. /* A simple library to load images of various formats as SDL surfaces */
  20. #include <stdio.h>
  21. #include <string.h>
  22. #include <ctype.h>
  23. #include "SDL_image.h"
  24. #define ARRAYSIZE(a) (sizeof(a) / sizeof((a)[0]))
  25. /* Table of image detection and loading functions */
  26. static struct {
  27. char *type;
  28. int (*is)(SDL_RWops *src);
  29. SDL_Surface *(*load)(SDL_RWops *src);
  30. } supported[] = {
  31. /* keep magicless formats first */
  32. { "TGA", 0, IMG_LoadTGA_RW },
  33. { "BMP", IMG_isBMP, IMG_LoadBMP_RW },
  34. // { "PNM", IMG_isPNM, IMG_LoadPNM_RW }, /* P[BGP]M share code */ /* disabled less common filetypes */
  35. // { "XPM", IMG_isXPM, IMG_LoadXPM_RW }, /* disabled less common filetypes */
  36. // { "XCF", IMG_isXCF, IMG_LoadXCF_RW }, /* disabled less common filetypes */
  37. { "PCX", IMG_isPCX, IMG_LoadPCX_RW },
  38. { "GIF", IMG_isGIF, IMG_LoadGIF_RW },
  39. // { "JPG", IMG_isJPG, IMG_LoadJPG_RW }, /* external support required */
  40. // { "TIF", IMG_isTIF, IMG_LoadTIF_RW }, /* external support required */
  41. // { "LBM", IMG_isLBM, IMG_LoadLBM_RW }, /* disabled less common filetypes */
  42. { "PNG", IMG_isPNG, IMG_LoadPNG_RW }
  43. };
  44. /* Load an image from a file */
  45. SDL_Surface *IMG_Load(const char *file)
  46. {
  47. SDL_RWops *src = SDL_RWFromFile(file, "rb");
  48. char *ext = strrchr(file, '.');
  49. if(ext)
  50. ext++;
  51. return IMG_LoadTyped_RW(src, 1, ext);
  52. }
  53. /* Load an image from an SDL datasource (for compatibility) */
  54. SDL_Surface *IMG_Load_RW(SDL_RWops *src, int freesrc)
  55. {
  56. return IMG_LoadTyped_RW(src, freesrc, NULL);
  57. }
  58. /* Portable case-insensitive string compare function */
  59. static int IMG_string_equals(const char *str1, const char *str2)
  60. {
  61. while ( *str1 && *str2 ) {
  62. if ( toupper((unsigned char)*str1) !=
  63. toupper((unsigned char)*str2) )
  64. break;
  65. ++str1;
  66. ++str2;
  67. }
  68. return (!*str1 && !*str2);
  69. }
  70. /* Load an image from an SDL datasource, optionally specifying the type */
  71. SDL_Surface *IMG_LoadTyped_RW(SDL_RWops *src, int freesrc, char *type)
  72. {
  73. unsigned int i;
  74. int start;
  75. SDL_Surface *image;
  76. /* Make sure there is something to do.. */
  77. if ( src == NULL ) {
  78. return(NULL);
  79. }
  80. /* See whether or not this data source can handle seeking */
  81. if ( SDL_RWseek(src, 0, SEEK_CUR) < 0 ) {
  82. IMG_SetError("Can't seek in this data source");
  83. if(freesrc)
  84. SDL_RWclose(src);
  85. return(NULL);
  86. }
  87. /* Detect the type of image being loaded */
  88. start = SDL_RWtell(src);
  89. image = NULL;
  90. for ( i=0; i < ARRAYSIZE(supported); ++i ) {
  91. if(supported[i].is) {
  92. SDL_RWseek(src, start, SEEK_SET);
  93. if(!supported[i].is(src))
  94. continue;
  95. } else {
  96. /* magicless format */
  97. if(!type
  98. || !IMG_string_equals(type, supported[i].type))
  99. continue;
  100. }
  101. #ifdef DEBUG_IMGLIB
  102. fprintf(stderr, "IMGLIB: Loading image as %s\n",
  103. supported[i].type);
  104. #endif
  105. SDL_RWseek(src, start, SEEK_SET);
  106. image = supported[i].load(src);
  107. if(freesrc)
  108. SDL_RWclose(src);
  109. return image;
  110. }
  111. if ( freesrc ) {
  112. SDL_RWclose(src);
  113. }
  114. IMG_SetError("Unsupported image format");
  115. return NULL;
  116. }
  117. /* Invert the alpha of a surface for use with OpenGL
  118. This function is a no-op and only kept for backwards compatibility.
  119. */
  120. int IMG_InvertAlpha(int on)
  121. {
  122. return 1;
  123. }