tb_image_loader_stb.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. // ================================================================================
  2. // == This file is a part of Turbo Badger. (C) 2011-2014, Emil Segerås ==
  3. // == See tb_core.h for more information. ==
  4. // ================================================================================
  5. #include "tb_bitmap_fragment.h"
  6. #include "tb_system.h"
  7. #ifdef TB_IMAGE_LOADER_STB
  8. namespace tb {
  9. // Configure stb image and remove some features we don't use to reduce binary size.
  10. #define STB_IMAGE_STATIC
  11. #define STB_IMAGE_IMPLEMENTATION
  12. //#define STBI_SIMD
  13. #define STBI_NO_STDIO
  14. #define STBI_NO_FAILURE_STRINGS
  15. #define STBI_NO_HDR
  16. // Disable unused function warnings for stb_image.h. Since STB_IMAGE_STATIC is
  17. // defined, it will contain a couple of unused static functions.
  18. #pragma GCC diagnostic push
  19. #pragma GCC diagnostic ignored "-Wunused-function"
  20. // Include stb image - Tiny portable and reasonable fast image loader from http://nothings.org/
  21. // Should not be used for content not distributed with your app (May not be secure and doesn't
  22. // support all formats fully)
  23. #include "thirdparty/stb_image.h"
  24. #pragma GCC diagnostic pop
  25. class STBI_Loader : public TBImageLoader
  26. {
  27. public:
  28. int width, height;
  29. unsigned char *data;
  30. STBI_Loader() : width(0), height(0), data(nullptr) {}
  31. ~STBI_Loader() { stbi_image_free(data); }
  32. virtual int Width() { return width; }
  33. virtual int Height() { return height; }
  34. virtual uint32 *Data() { return (uint32*)data; }
  35. };
  36. TBImageLoader *TBImageLoader::CreateFromFile(const char *filename)
  37. {
  38. // Load directly from file
  39. /*int w, h, comp;
  40. if (unsigned char *data = stbi_load(filename, &w, &h, &comp, 4))
  41. {
  42. if (STBI_Loader *img = new STBI_Loader())
  43. {
  44. img->width = w;
  45. img->height = h;
  46. img->data = data;
  47. return img;
  48. }
  49. else
  50. stbi_image_free(data);
  51. }
  52. return nullptr;*/
  53. if (TBFile *file = TBFile::Open(filename, TBFile::MODE_READ))
  54. {
  55. long size = file->Size();
  56. if (unsigned char *data = new unsigned char[size])
  57. {
  58. size = file->Read(data, 1, size);
  59. int w, h, comp;
  60. if (unsigned char *img_data = stbi_load_from_memory(data, size, &w, &h, &comp, 4))
  61. {
  62. if (STBI_Loader *img = new STBI_Loader())
  63. {
  64. img->width = w;
  65. img->height = h;
  66. img->data = img_data;
  67. delete [] data;
  68. delete file;
  69. return img;
  70. }
  71. else
  72. stbi_image_free(img_data);
  73. }
  74. delete data;
  75. }
  76. delete file;
  77. }
  78. return nullptr;
  79. }
  80. }; // namespace tb
  81. #endif // TB_IMAGE_LOADER_STB