Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

104 rader
2.4 KiB

  1. //
  2. // Lol Engine
  3. //
  4. // Copyright: (c) 2010-2014 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. #pragma once
  11. //
  12. // The ImageCodecData class
  13. // ------------------------
  14. //
  15. namespace lol
  16. {
  17. class PixelDataBase
  18. {
  19. public:
  20. virtual void *data() = 0;
  21. virtual void const *data() const = 0;
  22. virtual void *data2d() = 0;
  23. virtual void const *data2d() const = 0;
  24. inline virtual ~PixelDataBase() {}
  25. };
  26. template<PixelFormat T>
  27. class PixelData : public PixelDataBase
  28. {
  29. public:
  30. inline PixelData(ivec2 size) { m_array2d.resize(size); }
  31. virtual void *data() { return m_array2d.data(); }
  32. virtual void const *data() const { return m_array2d.data(); }
  33. virtual void *data2d() { return &m_array2d; }
  34. virtual void const *data2d() const { return &m_array2d; }
  35. array2d<typename PixelType<T>::type> m_array2d;
  36. };
  37. class ImageData
  38. {
  39. friend class Image;
  40. public:
  41. ImageData()
  42. : m_size(0, 0),
  43. m_wrap_x(WrapMode::Clamp),
  44. m_wrap_y(WrapMode::Clamp),
  45. m_format(PixelFormat::Unknown)
  46. {}
  47. ivec2 m_size;
  48. /* The wrap modes for pixel access */
  49. WrapMode m_wrap_x, m_wrap_y;
  50. /* A map of the various available bitplanes */
  51. map<int, PixelDataBase *> m_pixels;
  52. /* The last bitplane being accessed for writing */
  53. PixelFormat m_format;
  54. };
  55. class ImageCodec
  56. {
  57. public:
  58. virtual char const *GetName() { return "<ImageCodec>"; }
  59. virtual bool Load(Image *image, char const *path) = 0;
  60. virtual bool Save(Image *image, char const *path) = 0;
  61. /* TODO: this should become more fine-grained */
  62. int m_priority;
  63. };
  64. #define REGISTER_IMAGE_CODEC(name) \
  65. extern ImageCodec *Register##name(); \
  66. { \
  67. /* Insert image codecs in a sorted list */ \
  68. ImageCodec *codec = Register##name(); \
  69. int i = 0, prio = codec->m_priority; \
  70. for ( ; i < codeclist.count(); ++i) \
  71. { \
  72. if (codeclist[i]->m_priority <= prio) \
  73. break; \
  74. } \
  75. codeclist.insert(codec, i); \
  76. }
  77. #define DECLARE_IMAGE_CODEC(name, priority) \
  78. ImageCodec *Register##name() \
  79. { \
  80. ImageCodec *ret = new name(); \
  81. ret->m_priority = priority; \
  82. return ret; \
  83. }
  84. } /* namespace lol */