Compressor.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /**
  2. * Copyright (c) 2006-2015 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. #ifndef LOVE_MATH_COMPRESSOR_H
  21. #define LOVE_MATH_COMPRESSOR_H
  22. // LOVE
  23. #include "common/StringMap.h"
  24. namespace love
  25. {
  26. namespace math
  27. {
  28. /**
  29. * Base class for backends for different compression formats.
  30. **/
  31. class Compressor
  32. {
  33. public:
  34. enum Format
  35. {
  36. FORMAT_LZ4,
  37. FORMAT_ZLIB,
  38. FORMAT_MAX_ENUM
  39. };
  40. /**
  41. * Creates a new Compressor that can compress and decompress a specific
  42. * format.
  43. **/
  44. static Compressor *Create(Format format);
  45. virtual ~Compressor() {}
  46. /**
  47. * Compresses input data, and returns the compressed result.
  48. *
  49. * @param[in] data The input (uncompressed) data.
  50. * @param[in] dataSize The size in bytes of the input data.
  51. * @param[in] level The amount of compression to apply (between 0 and 9.)
  52. * A value of -1 indicates the default amount of compression.
  53. * Specific formats may not use every level.
  54. * @param[out] compressedSize The size in bytes of the compressed result.
  55. *
  56. * @return The newly compressed data (allocated with new[]).
  57. **/
  58. virtual char *compress(const char *data, size_t dataSize, int level, size_t &compressedSize) = 0;
  59. /**
  60. * Decompresses compressed data, and returns the decompressed result.
  61. *
  62. * @param[in] data The input (compressed) data.
  63. * @param[in] dataSize The size in bytes of the compressed data.
  64. * @param[inout] decompressedSize On input, the size in bytes of the
  65. * original uncompressed data, or 0 if unknown. On return, the
  66. * size in bytes of the decompressed data.
  67. *
  68. * @return The decompressed data (allocated with new[]).
  69. **/
  70. virtual char *decompress(const char *data, size_t dataSize, size_t &decompressedSize) = 0;
  71. /**
  72. * Gets the compression format implemented by this backend.
  73. *
  74. * @return The supported format.
  75. **/
  76. virtual Format getFormat() const = 0;
  77. static bool getConstant(const char *in, Format &out);
  78. static bool getConstant(Format in, const char *&out);
  79. private:
  80. static StringMap<Format, FORMAT_MAX_ENUM>::Entry formatEntries[];
  81. static StringMap<Format, FORMAT_MAX_ENUM> formatNames;
  82. }; // Compressor
  83. } // math
  84. } // love
  85. #endif // LOVE_MATH_COMPRESSOR_H