You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

132 line
2.7 KiB

  1. //
  2. // Lol Engine
  3. //
  4. // Copyright: (c) 2010-2011 Sam Hocevar <sam@hocevar.net>
  5. // This program is free software; you can redistribute it and/or
  6. // modify it under the terms of the Do What The Fuck You Want To
  7. // Public License, Version 2, as published by Sam Hocevar. See
  8. // http://www.wtfpl.net/ for more details.
  9. //
  10. //
  11. // The ImageData class
  12. // -------------------
  13. //
  14. #if !defined __LOL_IMAGE_PRIVATE_H__
  15. #define __LOL_IMAGE_PRIVATE_H__
  16. namespace lol
  17. {
  18. class ImageCodec
  19. {
  20. friend class ImageLoader;
  21. friend class ImageData;
  22. public:
  23. ImageData *(*fun)(char const *path);
  24. ImageCodec *next;
  25. int priority;
  26. static void RegisterLoader(ImageCodec *loader)
  27. {
  28. Helper(loader);
  29. }
  30. private:
  31. static ImageData *Load(char const *path)
  32. {
  33. ImageCodec *parser = Helper(nullptr);
  34. ImageData *ret = nullptr;
  35. while (parser && !ret)
  36. {
  37. ret = parser->fun(path);
  38. parser = parser->next;
  39. }
  40. return ret;
  41. }
  42. static ImageCodec *Helper(ImageCodec *set)
  43. {
  44. static ImageCodec *loaders = nullptr;
  45. if (!set)
  46. return loaders;
  47. ImageCodec **parser = &loaders;
  48. while (*parser && (*parser)->priority > set->priority)
  49. parser = &(*parser)->next;
  50. set->next = *parser;
  51. *parser = set;
  52. return nullptr;
  53. }
  54. };
  55. class ImageData
  56. {
  57. friend class Image;
  58. friend class ImageLoader;
  59. public:
  60. inline ImageData()
  61. : m_size(0, 0),
  62. m_format(PixelFormat::Unknown),
  63. m_refcount(0)
  64. { }
  65. virtual ~ImageData() {}
  66. virtual bool Open(char const *) = 0;
  67. virtual bool Close() = 0;
  68. virtual uint8_t *GetData() const = 0;
  69. protected:
  70. ivec2 m_size;
  71. PixelFormat m_format;
  72. int m_refcount;
  73. };
  74. #define REGISTER_IMAGE_LOADER(name) \
  75. extern void Register##name(); \
  76. Register##name();
  77. #define DECLARE_IMAGE_LOADER(name, prio) \
  78. template<typename T> class name##ImageCodec : public ImageCodec \
  79. { \
  80. public: \
  81. name##ImageCodec() \
  82. { \
  83. static ImageCodec loader; \
  84. loader.fun = Load; \
  85. loader.priority = prio; \
  86. RegisterLoader(&loader); \
  87. } \
  88. static ImageData *Load(char const *path) \
  89. { \
  90. T *ret = new T(); \
  91. if (!ret->Open(path)) \
  92. { \
  93. delete ret; \
  94. return nullptr; \
  95. } \
  96. return ret; \
  97. } \
  98. }; \
  99. class name; \
  100. name##ImageCodec<name> name##ImageCodecInstance; \
  101. void Register##name() \
  102. { \
  103. (void)&name##ImageCodecInstance; \
  104. } \
  105. class name : public ImageData
  106. } /* namespace lol */
  107. #endif // __LOL_IMAGE_PRIVATE_H__