CompressedImageData.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /**
  2. * Copyright (c) 2006-2017 LOVE Development Team
  3. *
  4. * This software is provided 'as-is', without any express or implied
  5. * warranty. In no event will the authors be held liable for any damages
  6. * arising from the use of this software.
  7. *
  8. * Permission is granted to anyone to use this software for any purpose,
  9. * including commercial applications, and to alter it and redistribute it
  10. * freely, subject to the following restrictions:
  11. *
  12. * 1. The origin of this software must not be misrepresented; you must not
  13. * claim that you wrote the original software. If you use this software
  14. * in a product, an acknowledgment in the product documentation would be
  15. * appreciated but is not required.
  16. * 2. Altered source versions must be plainly marked as such, and must not be
  17. * misrepresented as being the original software.
  18. * 3. This notice may not be removed or altered from any source distribution.
  19. **/
  20. #include "CompressedImageData.h"
  21. namespace love
  22. {
  23. namespace image
  24. {
  25. love::Type CompressedImageData::type("CompressedImageData", &Data::type);
  26. CompressedImageData::CompressedImageData(const std::list<FormatHandler *> &formats, Data *filedata)
  27. : format(PIXELFORMAT_UNKNOWN)
  28. , sRGB(false)
  29. {
  30. FormatHandler *parser = nullptr;
  31. for (FormatHandler *handler : formats)
  32. {
  33. if (handler->canParseCompressed(filedata))
  34. {
  35. parser = handler;
  36. break;
  37. }
  38. }
  39. if (parser == nullptr)
  40. throw love::Exception("Could not parse compressed data: Unknown format.");
  41. memory = parser->parseCompressed(filedata, dataImages, format, sRGB);
  42. if (memory == nullptr)
  43. throw love::Exception("Could not parse compressed data.");
  44. if (format == PIXELFORMAT_UNKNOWN)
  45. throw love::Exception("Could not parse compressed data: Unknown format.");
  46. if (dataImages.size() == 0 || memory->size == 0)
  47. throw love::Exception("Could not parse compressed data: No valid data?");
  48. }
  49. CompressedImageData::CompressedImageData(const CompressedImageData &c)
  50. : format(c.format)
  51. , sRGB(c.sRGB)
  52. {
  53. memory.set(new CompressedMemory(c.memory->size), Acquire::NORETAIN);
  54. memcpy(memory->data, c.memory->data, memory->size);
  55. for (const auto &i : c.dataImages)
  56. {
  57. auto slice = new CompressedSlice(i->getFormat(), i->getWidth(), i->getHeight(), memory, i->getOffset(), i->getSize());
  58. dataImages.push_back(slice);
  59. slice->release();
  60. }
  61. }
  62. CompressedImageData *CompressedImageData::clone() const
  63. {
  64. return new CompressedImageData(*this);
  65. }
  66. CompressedImageData::~CompressedImageData()
  67. {
  68. }
  69. size_t CompressedImageData::getSize() const
  70. {
  71. return memory->size;
  72. }
  73. void *CompressedImageData::getData() const
  74. {
  75. return memory->data;
  76. }
  77. int CompressedImageData::getMipmapCount(int /*slice*/) const
  78. {
  79. return (int) dataImages.size();
  80. }
  81. int CompressedImageData::getSliceCount(int /*mip*/) const
  82. {
  83. return 1;
  84. }
  85. size_t CompressedImageData::getSize(int miplevel) const
  86. {
  87. checkSliceExists(0, miplevel);
  88. return dataImages[miplevel]->getSize();
  89. }
  90. void *CompressedImageData::getData(int miplevel) const
  91. {
  92. checkSliceExists(0, miplevel);
  93. return dataImages[miplevel]->getData();
  94. }
  95. int CompressedImageData::getWidth(int miplevel) const
  96. {
  97. checkSliceExists(0, miplevel);
  98. return dataImages[miplevel]->getWidth();
  99. }
  100. int CompressedImageData::getHeight(int miplevel) const
  101. {
  102. checkSliceExists(0, miplevel);
  103. return dataImages[miplevel]->getHeight();
  104. }
  105. PixelFormat CompressedImageData::getFormat() const
  106. {
  107. return format;
  108. }
  109. bool CompressedImageData::isSRGB() const
  110. {
  111. return sRGB;
  112. }
  113. CompressedSlice *CompressedImageData::getSlice(int slice, int miplevel) const
  114. {
  115. checkSliceExists(slice, miplevel);
  116. return dataImages[miplevel].get();
  117. }
  118. void CompressedImageData::checkSliceExists(int slice, int miplevel) const
  119. {
  120. if (slice != 0)
  121. throw love::Exception("Slice index %d does not exists", slice + 1);
  122. if (miplevel < 0 || miplevel >= (int) dataImages.size())
  123. throw love::Exception("Mipmap level %d does not exist", miplevel + 1);
  124. }
  125. } // image
  126. } // love