CompressedData.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /**
  2. * Copyright (c) 2006-2014 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 "CompressedData.h"
  21. #include "ddsHandler.h"
  22. namespace love
  23. {
  24. namespace image
  25. {
  26. namespace magpie
  27. {
  28. CompressedData::CompressedData(std::list<CompressedFormatHandler *> formats, love::filesystem::FileData *filedata)
  29. : formatHandlers(formats)
  30. {
  31. for (CompressedFormatHandler *handler : formatHandlers)
  32. handler->retain();
  33. load(filedata);
  34. }
  35. CompressedData::~CompressedData()
  36. {
  37. delete[] data;
  38. for (CompressedFormatHandler *handler : formatHandlers)
  39. handler->release();
  40. }
  41. void CompressedData::load(love::filesystem::FileData *filedata)
  42. {
  43. CompressedFormatHandler *parser = nullptr;
  44. for (CompressedFormatHandler *handler : formatHandlers)
  45. {
  46. if (handler->canParse(filedata))
  47. {
  48. parser = handler;
  49. break;
  50. }
  51. }
  52. if (parser == nullptr)
  53. throw love::Exception("Could not parse compressed data: Unknown format.");
  54. // SubImage vector will be populated by a parser.
  55. std::vector<SubImage> parsedimages;
  56. Format texformat = FORMAT_UNKNOWN;
  57. size_t newdata_size = 0;
  58. uint8 *newdata = parser->parse(filedata, parsedimages, newdata_size, texformat);
  59. if (newdata == nullptr)
  60. throw love::Exception("Could not parse compressed data.");
  61. if (texformat == FORMAT_UNKNOWN)
  62. {
  63. delete[] newdata;
  64. throw love::Exception("Could not parse compressed data: Unknown format.");
  65. }
  66. if (parsedimages.size() == 0 || newdata_size == 0)
  67. {
  68. delete[] newdata;
  69. throw love::Exception("Could not parse compressed data: No valid data?");
  70. }
  71. // Make sure to clean up any previously loaded data.
  72. delete[] data;
  73. data = newdata;
  74. dataSize = newdata_size;
  75. dataImages = parsedimages;
  76. format = texformat;
  77. }
  78. } // magpie
  79. } // image
  80. } // love