rg_etc1.h 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. // File: rg_etc1.h - Fast, high quality ETC1 block packer/unpacker - Rich Geldreich <richgel99@gmail.com>
  2. // Please see ZLIB license at the end of this file.
  3. #pragma once
  4. namespace rg_etc1
  5. {
  6. // Unpacks an 8-byte ETC1 compressed block to a block of 4x4 32bpp RGBA pixels.
  7. // Returns false if the block is invalid. Invalid blocks will still be unpacked with clamping.
  8. // This function is thread safe, and does not dynamically allocate any memory.
  9. // If preserve_alpha is true, the alpha channel of the destination pixels will not be overwritten. Otherwise, alpha will be set to 255.
  10. bool unpack_etc1_block(const void *pETC1_block, unsigned int* pDst_pixels_rgba, bool preserve_alpha = false);
  11. // Quality setting = the higher the quality, the slower.
  12. // To pack large textures, it is highly recommended to call pack_etc1_block() in parallel, on different blocks, from multiple threads (particularly when using cHighQuality).
  13. enum etc1_quality
  14. {
  15. cLowQuality,
  16. cMediumQuality,
  17. cHighQuality,
  18. };
  19. struct etc1_pack_params
  20. {
  21. etc1_quality m_quality;
  22. bool m_dithering;
  23. inline etc1_pack_params()
  24. {
  25. clear();
  26. }
  27. void clear()
  28. {
  29. m_quality = cHighQuality;
  30. m_dithering = false;
  31. }
  32. };
  33. // Important: pack_etc1_block_init() must be called before calling pack_etc1_block().
  34. void pack_etc1_block_init();
  35. // Packs a 4x4 block of 32bpp RGBA pixels to an 8-byte ETC1 block.
  36. // 32-bit RGBA pixels must always be arranged as (R,G,B,A) (R first, A last) in memory, independent of platform endianness. A should always be 255.
  37. // Returns squared error of result.
  38. // This function is thread safe, and does not dynamically allocate any memory.
  39. // pack_etc1_block() does not currently support "perceptual" colorspace metrics - it primarily optimizes for RGB RMSE.
  40. unsigned int pack_etc1_block(void* pETC1_block, const unsigned int* pSrc_pixels_rgba, etc1_pack_params& pack_params);
  41. } // namespace rg_etc1
  42. //------------------------------------------------------------------------------
  43. //
  44. // rg_etc1 uses the ZLIB license:
  45. // http://opensource.org/licenses/Zlib
  46. //
  47. // Copyright (c) 2012 Rich Geldreich
  48. //
  49. // This software is provided 'as-is', without any express or implied
  50. // warranty. In no event will the authors be held liable for any damages
  51. // arising from the use of this software.
  52. //
  53. // Permission is granted to anyone to use this software for any purpose,
  54. // including commercial applications, and to alter it and redistribute it
  55. // freely, subject to the following restrictions:
  56. //
  57. // 1. The origin of this software must not be misrepresented; you must not
  58. // claim that you wrote the original software. If you use this software
  59. // in a product, an acknowledgment in the product documentation would be
  60. // appreciated but is not required.
  61. //
  62. // 2. Altered source versions must be plainly marked as such, and must not be
  63. // misrepresented as being the original software.
  64. //
  65. // 3. This notice may not be removed or altered from any source distribution.
  66. //
  67. //------------------------------------------------------------------------------