├── demo.png ├── LICENSE.md ├── s3tconv_internal.h ├── README.md ├── s3tconv.c ├── s3tconv.h └── s3tconv_atitc.c /demo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Triang3l/S3TConv/HEAD/demo.png -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | ===================== 3 | 4 | Copyright © 2017 [Triang3l](https://github.com/Triang3l). 5 | 6 | Contains portions of [Compressonator](https://github.com/GPUOpen-Tools/Compressonator): 7 | 8 | Copyright © 2016 Advanced Micro Devices, Inc. All rights reserved. 9 | 10 | Copyright © 2004-2006 ATI Technologies Inc. 11 | 12 | Permission is hereby granted, free of charge, to any person 13 | obtaining a copy of this software and associated documentation 14 | files (the “Software”), to deal in the Software without 15 | restriction, including without limitation the rights to use, 16 | copy, modify, merge, publish, distribute, sublicense, and/or sell 17 | copies of the Software, and to permit persons to whom the 18 | Software is furnished to do so, subject to the following 19 | conditions: 20 | 21 | The above copyright notice and this permission notice shall be 22 | included in all copies or substantial portions of the Software. 23 | 24 | THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, 25 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 26 | OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 27 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 28 | HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 29 | WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 30 | FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 31 | OTHER DEALINGS IN THE SOFTWARE. 32 | -------------------------------------------------------------------------------- /s3tconv_internal.h: -------------------------------------------------------------------------------- 1 | /* 2 | Part of S3TConv, a library for converting S3TC textures to other formats. 3 | https://github.com/Triang3l/S3TConv 4 | 5 | Copyright (c) 2017 Triang3l. 6 | Contains portions of Compressonator: 7 | Copyright (c) 2016 Advanced Micro Devices, Inc. All rights reserved. 8 | Copyright (c) 2004-2006 ATI Technologies Inc. 9 | 10 | Permission is hereby granted, free of charge, to any person obtaining a copy 11 | of this software and associated documentation files (the "Software"), to deal 12 | in the Software without restriction, including without limitation the rights 13 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 14 | copies of the Software, and to permit persons to whom the Software is 15 | furnished to do so, subject to the following conditions: 16 | 17 | The above copyright notice and this permission notice shall be included in 18 | all copies or substantial portions of the Software. 19 | 20 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 21 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 22 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 23 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 24 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 25 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 26 | THE SOFTWARE. 27 | */ 28 | 29 | #ifndef S3TCONV_INTERNAL_H 30 | #define S3TCONV_INTERNAL_H 31 | 32 | #include "s3tconv.h" 33 | 34 | #ifdef __cplusplus 35 | extern "C" { 36 | #endif 37 | 38 | void S3TConv_Utility_Color565To888(uint16_t color565, uint8_t color888[3]); 39 | 40 | static inline uint16_t S3TConv_Utility_Color565To555(uint16_t color565) { 41 | return (color565 & 0x001F) | ((color565 & 0xFFC0) >> 1); 42 | } 43 | 44 | #ifdef __cplusplus 45 | } 46 | #endif 47 | 48 | #endif 49 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # S3TConv 2 | **A library for load-time conversion of S3 Texture Compression textures to other formats.** 3 | 4 | Currently, the library only supports conversion to **ATI Texture Compression (ATITC or ATC)** used on Qualcomm Adreno. 5 | 6 | S3TConv is designed for load-time conversion, primarily for mobile ports of PC games. It doesn't re-compress images or blocks, instead, it performs a fixed set of checks to re-use the existing colors and color indices from S3TC blocks in the most accurate way. 7 | 8 | The ATITC conversion method is inspired by "[A Method for Load-Time Conversion of DXTC Assets to ATC](http://www.guildsoftware.com/papers/2012.Converting.DXTC.to.ATC.pdf)" by Ray Ratelis and John Bergman of [Guild Software](http://www.guildsoftware.com), but expanded to support both DXT1 modes and to give a higher bit depth to brighter colors, which matches the way ATITC encodes colors by design. 9 | 10 | As various S3TC compressors provide generally better results than the ATITC encoder in Compressonator, S3TConv can also be used offline to prepare compressed textures for Adreno, in some cases creating images more accurate than Compressonator. 11 | 12 | ![Conversion demo](/demo.png?raw=true) 13 | 14 | Usage 15 | ----- 16 | Add the C code and header files to your project and `#include "s3tconv.h"`. 17 | 18 | The library performs compression on block level, so you'll generally need to call the conversion functions in a loop through 8-byte DXT1 or 16-byte DXT3/DXT5 blocks. 19 | 20 | Some functions have `remainingWidth` and `remainingHeight` parameters. They are used to skip padding colors if the size of the image is not a multiple of 4 (or it's one of the smallest mipmaps). You need to pass the number of pixels left in the row/column starting from the leftmost/topmost pixel of the block. For mid-image blocks, they must be 4 or more, for right and bottom edges, they may be 4, 3, 2 or 1. 21 | 22 | The conversion functions may also take the `asDXT1` parameter, which should be: 23 | * 1 for DXT1. 24 | * 0 for DXT3 and DXT5, but in certain cases, may depend on how the textures were originally compressed: 25 | * NVIDIA GeForce 6xxx and 7xxx (2004–2006) graphics cards violate the S3TC specification by decoding DXT3 and DXT5 the same way as DXT1 — supporting both modes. This has behavior been corrected in newer models. According to the specification, DXT3 and DXT5 should use only one mode, regardless of whether the first or the second color is greater. 26 | * Compressors usually take this into account, using only one mode and make the first color greater than the second, so both 0 and 1 should be fine. 27 | * If that's not the case, 0 should be passed for specification-conforming textures, and 1 for textures targeting these GPUs. 28 | 29 | ### DXT1 to ATITC 30 | DXT1 has two ways of decoding — with and without punch-through alpha. ATITC doesn't support punch-through alpha, so it needs to be converted to explicit or to interpolated alpha if used. 31 | 32 | ATITC textures with alpha require twice as large storage as RGB-only textures. If you don't know in advance whether the texture has a punch-through alpha, you can check if a block has alpha using `S3TConv_DXT1_BlockHasPunchthroughPixels`. 33 | 34 | To convert to `ATC_RGB_AMD` (8 bytes per block), call `S3TConv_ATITC_RGBBlockFromDXT` for every 8-byte DXT1 block. 35 | 36 | To convert to `ATC_RGBA_EXPLICIT_ALPHA_AMD` or `ATC_RGBA_INTERPOLATED_ALPHA_AMD` (16 bytes per block), use `S3TConv_DXT1_PunchthroughToExplicitAlpha` or `S3TConv_DXT1_PunchthroughToInterpolatedAlpha` on the DXT1 block to write the first 8 bytes of the ATITC block, and `S3TConv_ATITC_RGBBlockFromDXT` to write the second 8 bytes. 37 | 38 | ### DXT3 to `ATC_RGBA_EXPLICIT_ALPHA_AMD` or DXT5 to `ATC_RGBA_INTERPOLATED_ALPHA_AMD` 39 | S3TC and ATITC use the same methods of encoding explicit and interpolated alpha, so for every 16-byte block, simply copy the first 8 bytes and run `S3TConv_ATITC_RGBBlockFromDXT` on the second 8 bytes. 40 | -------------------------------------------------------------------------------- /s3tconv.c: -------------------------------------------------------------------------------- 1 | /* 2 | Part of S3TConv, a library for converting S3TC textures to other formats. 3 | https://github.com/Triang3l/S3TConv 4 | 5 | Copyright (c) 2017 Triang3l. 6 | Contains portions of Compressonator: 7 | Copyright (c) 2016 Advanced Micro Devices, Inc. All rights reserved. 8 | Copyright (c) 2004-2006 ATI Technologies Inc. 9 | 10 | Permission is hereby granted, free of charge, to any person obtaining a copy 11 | of this software and associated documentation files (the "Software"), to deal 12 | in the Software without restriction, including without limitation the rights 13 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 14 | copies of the Software, and to permit persons to whom the Software is 15 | furnished to do so, subject to the following conditions: 16 | 17 | The above copyright notice and this permission notice shall be included in 18 | all copies or substantial portions of the Software. 19 | 20 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 21 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 22 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 23 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 24 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 25 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 26 | THE SOFTWARE. 27 | */ 28 | 29 | #include "s3tconv_internal.h" 30 | 31 | void S3TConv_Utility_Color565To888(uint16_t color565, uint8_t color888[3]) { 32 | // From Compressonator. 33 | color888[0] = (uint8_t) (((color565 & 0xF800) >> 8) | ((color565 & 0xE000) >> 13)); 34 | color888[1] = (uint8_t) (((color565 & 0x07E0) >> 3) | ((color565 & 0x0600) >> 9)); 35 | color888[2] = (uint8_t) (((color565 & 0x001F) << 3) | ((color565 & 0x001C) >> 2)); 36 | } 37 | 38 | int S3TConv_DXT1_BlockHasPunchthroughPixels(const uint8_t rgbBlock[8], 39 | unsigned int remainingWidth, unsigned int remainingHeight) { 40 | uint32_t indices; 41 | if (((uint16_t) rgbBlock[0] | ((uint16_t) rgbBlock[1] << 8)) > 42 | ((uint16_t) rgbBlock[2] | ((uint16_t) rgbBlock[3] << 8))) { 43 | return 0; 44 | } 45 | indices = (uint32_t) rgbBlock[4] | ((uint32_t) rgbBlock[5] << 8) | 46 | ((uint32_t) rgbBlock[6] << 16) | ((uint32_t) rgbBlock[7] << 24); 47 | if (remainingWidth < 4) { 48 | const uint32_t remainingWidthMask[] = { 0, 0x03030303, 0x0F0F0F0F, 0x3F3F3F3F }; 49 | indices &= remainingWidthMask[remainingWidth]; 50 | } 51 | if (remainingHeight < 4) { 52 | indices &= (1 << (remainingHeight << 3)) - 1; 53 | } 54 | return (indices & 0x55555555 & ((indices >> 1) & 0x55555555)) != 0; 55 | } 56 | 57 | void S3TConv_DXT1_PunchthroughToExplicitAlpha(const uint8_t rgbBlock[8], uint8_t alphaBlock[8]) { 58 | uint32_t colorIndexMask; 59 | unsigned int alphaByteIndex; 60 | 61 | if (((uint16_t) rgbBlock[0] | ((uint16_t) rgbBlock[1] << 8)) > 62 | ((uint16_t) rgbBlock[2] | ((uint16_t) rgbBlock[3] << 8))) { 63 | alphaBlock[0] = alphaBlock[1] = alphaBlock[2] = alphaBlock[3] = 64 | alphaBlock[4] = alphaBlock[5] = alphaBlock[6] = alphaBlock[7] = 0; 65 | return; 66 | } 67 | 68 | colorIndexMask = (uint32_t) rgbBlock[4] | ((uint32_t) rgbBlock[5] << 8) | 69 | ((uint32_t) rgbBlock[6] << 16) | ((uint32_t) rgbBlock[7] << 24); 70 | colorIndexMask = colorIndexMask & 0x55555555 & ((colorIndexMask & 0xAAAAAAAA) >> 1); 71 | colorIndexMask = ~(colorIndexMask | (colorIndexMask << 1)); 72 | 73 | for (alphaByteIndex = 0; alphaByteIndex < 8; ++alphaByteIndex) { 74 | alphaBlock[alphaByteIndex] = (uint8_t) ((colorIndexMask & 0x3) | ((colorIndexMask & 0x3) << 2) | 75 | ((colorIndexMask & 0xC) << 2) | ((colorIndexMask & 0xC) << 4)); 76 | colorIndexMask >>= 4; 77 | } 78 | } 79 | 80 | void S3TConv_DXT1_PunchthroughToInterpolatedAlpha(const uint8_t rgbBlock[8], uint8_t alphaBlock[8]) { 81 | uint32_t blackIndexBits; 82 | uint32_t alphaCodesLow, alphaCodesHigh; 83 | unsigned int pixelIndex; 84 | 85 | if (((uint16_t) rgbBlock[0] | ((uint16_t) rgbBlock[1] << 8)) > 86 | ((uint16_t) rgbBlock[2] | ((uint16_t) rgbBlock[3] << 8))) { 87 | alphaBlock[0] = alphaBlock[1] = 0xFF; 88 | alphaBlock[2] = alphaBlock[3] = alphaBlock[4] = 89 | alphaBlock[5] = alphaBlock[6] = alphaBlock[7] = 0; 90 | return; 91 | } 92 | 93 | blackIndexBits = (uint32_t) rgbBlock[4] | ((uint32_t) rgbBlock[5] << 8) | 94 | ((uint32_t) rgbBlock[6] << 16) | ((uint32_t) rgbBlock[7] << 24); 95 | blackIndexBits = blackIndexBits & 0x55555555 & ((blackIndexBits & 0xAAAAAAAA) >> 1); 96 | alphaCodesLow = alphaCodesHigh = 0; 97 | for (pixelIndex = 0; pixelIndex < 8; ++pixelIndex) { 98 | unsigned int alphaCodeShift = pixelIndex * 3; 99 | alphaCodesLow |= ((blackIndexBits >> (pixelIndex << 1)) & 1) << alphaCodeShift; 100 | alphaCodesHigh |= ((blackIndexBits >> (16 + (pixelIndex << 1))) & 1) << alphaCodeShift; 101 | } 102 | 103 | alphaBlock[0] = 0xFF; 104 | alphaBlock[1] = 0; 105 | alphaBlock[2] = (uint8_t) alphaCodesLow; 106 | alphaBlock[3] = (uint8_t) (alphaCodesLow >> 8); 107 | alphaBlock[4] = (uint8_t) (alphaCodesLow >> 16); 108 | alphaBlock[5] = (uint8_t) alphaCodesHigh; 109 | alphaBlock[6] = (uint8_t) (alphaCodesHigh >> 8); 110 | alphaBlock[7] = (uint8_t) (alphaCodesHigh >> 16); 111 | } 112 | -------------------------------------------------------------------------------- /s3tconv.h: -------------------------------------------------------------------------------- 1 | /* 2 | Part of S3TConv, a library for converting S3TC textures to other formats. 3 | https://github.com/Triang3l/S3TConv 4 | 5 | Copyright (c) 2017 Triang3l. 6 | Contains portions of Compressonator: 7 | Copyright (c) 2016 Advanced Micro Devices, Inc. All rights reserved. 8 | Copyright (c) 2004-2006 ATI Technologies Inc. 9 | 10 | Permission is hereby granted, free of charge, to any person obtaining a copy 11 | of this software and associated documentation files (the "Software"), to deal 12 | in the Software without restriction, including without limitation the rights 13 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 14 | copies of the Software, and to permit persons to whom the Software is 15 | furnished to do so, subject to the following conditions: 16 | 17 | The above copyright notice and this permission notice shall be included in 18 | all copies or substantial portions of the Software. 19 | 20 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 21 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 22 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 23 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 24 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 25 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 26 | THE SOFTWARE. 27 | */ 28 | 29 | #ifndef S3TCONV_H 30 | #define S3TCONV_H 31 | 32 | #include 33 | 34 | #ifdef __cplusplus 35 | extern "C" { 36 | #endif 37 | 38 | // 39 | // Target-independent functions. 40 | // 41 | 42 | /** 43 | * Returns whether a DXT1 block contains transparent pixels. 44 | * 45 | * This can be used to choose the right destination format if the 46 | * target doesn't support encoding punch-through transparency 47 | * directly in color blocks (such as ATITC). 48 | * 49 | * @param rgbBlock DXT1 block data. 50 | * @return 1 if the block contains punch-through transparency, 51 | * 0 if it's fully opaque. 52 | * @param remainingWidth How many pixels left in the row starting 53 | * from the leftmost pixel of this block, 54 | * to skip padding (4 or more for full block). 55 | * @param remainingHeight How many pixels left in the column starting 56 | * from the topmost pixel of this block, 57 | * to skip padding (4 or more for full block). 58 | */ 59 | int S3TConv_DXT1_BlockHasPunchthroughPixels(const uint8_t rgbBlock[8], 60 | unsigned int remainingWidth, unsigned int remainingHeight); 61 | 62 | /** 63 | * Extracts punch-through transparency from a DXT1 block to a DXT3 64 | * explicit alpha block, which can also be used for ATITC alpha. 65 | * 66 | * As an ATITC texture with alpha is twice as large as RGB-only one, 67 | * it's recommended to only use an RGBA destination format and 68 | * convert punch-through pixels only if there are any. This can be 69 | * checked using {@link S3TConv_DXT1_BlockHasPunchthroughPixels}. 70 | * 71 | * @param rgbBlock Source DXT1 block data. 72 | * @param alphaBlock Target DXT3 alpha block data. 73 | * @see S3TConv_DXT1_PunchthroughToInterpolatedAlpha 74 | */ 75 | void S3TConv_DXT1_PunchthroughToExplicitAlpha(const uint8_t rgbBlock[8], uint8_t alphaBlock[8]); 76 | 77 | /** 78 | * Extracts punch-through transparency from a DXT1 block to a DXT5 79 | * interpolated alpha block, which can also be used for ATITC alpha. 80 | * 81 | * As an ATITC texture with alpha is twice as large as RGB-only one, 82 | * it's recommended to only use an RGBA destination format and 83 | * convert punch-through pixels only if there are any. This can be 84 | * checked using {@link S3TConv_DXT1_BlockHasPunchthroughPixels}. 85 | * 86 | * @param rgbBlock Source DXT1 block data. 87 | * @param alphaBlock Target DXT5 alpha block data. 88 | * @see S3TConv_DXT1_PunchthroughToExplicitAlpha 89 | */ 90 | void S3TConv_DXT1_PunchthroughToInterpolatedAlpha(const uint8_t rgbBlock[8], uint8_t alphaBlock[8]); 91 | 92 | // 93 | // ATI Texture Compression (Qualcomm Adreno) conversion. 94 | // 95 | 96 | /** 97 | * Converts a DXT RGB block to ATITC. 98 | * 99 | * Alpha blocks can be copied directly for DXT3 (explicit alpha) 100 | * and DXT5 (interpolated alpha), and for RGBA DXT1, punch-through 101 | * transparency can be converted using either 102 | * {@link S3TConv_DXT1_PunchthroughToExplicitAlpha} or 103 | * {@link S3TConv_DXT1_PunchthroughToInterpolatedAlpha}. 104 | * 105 | * @param dxtBlock Source DXT RGB block data. 106 | * @param asDXT1 1 or other non-zero value if the texture is DXT1, 107 | * or if it's DXT3/DXT5 targeting GeForce 6xxx/7xxx, 108 | * 0 if it's specification-conforming DXT3/DXT5. 109 | * @param atitcBlock Target ATITC RGB block data. 110 | * @param remainingWidth How many pixels left in the row starting 111 | * from the leftmost pixel of this block, 112 | * to skip padding (4 or more for full block). 113 | * @param remainingHeight How many pixels left in the column starting 114 | * from the topmost pixel of this block, 115 | * to skip padding (4 or more for full block). 116 | */ 117 | void S3TConv_ATITC_RGBBlockFromDXT(const uint8_t dxtBlock[8], int asDXT1, uint8_t atitcBlock[8], 118 | unsigned int remainingWidth, unsigned int remainingHeight); 119 | 120 | #ifdef __cplusplus 121 | } 122 | #endif 123 | 124 | #endif 125 | -------------------------------------------------------------------------------- /s3tconv_atitc.c: -------------------------------------------------------------------------------- 1 | /* 2 | Part of S3TConv, a library for converting S3TC textures to other formats. 3 | https://github.com/Triang3l/S3TConv 4 | 5 | Copyright (c) 2017 Triang3l. 6 | Contains portions of Compressonator: 7 | Copyright (c) 2016 Advanced Micro Devices, Inc. All rights reserved. 8 | Copyright (c) 2004-2006 ATI Technologies Inc. 9 | 10 | Permission is hereby granted, free of charge, to any person obtaining a copy 11 | of this software and associated documentation files (the "Software"), to deal 12 | in the Software without restriction, including without limitation the rights 13 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 14 | copies of the Software, and to permit persons to whom the Software is 15 | furnished to do so, subject to the following conditions: 16 | 17 | The above copyright notice and this permission notice shall be included in 18 | all copies or substantial portions of the Software. 19 | 20 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 21 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 22 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 23 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 24 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 25 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 26 | THE SOFTWARE. 27 | */ 28 | 29 | #include "s3tconv_internal.h" 30 | 31 | static inline unsigned int S3TConv_ATITC_GetLuminance(const uint8_t color888[3]) { 32 | // From Compressonator, matches the hardware calculation. 33 | return (19 * (unsigned int) color888[0] + 34 | 38 * (unsigned int) color888[1] + 35 | 7 * (unsigned int) color888[2]) >> 6; 36 | } 37 | 38 | static void S3TConv_ATITC_ConvertBlackTrickDiscardingLowOrHigh( 39 | uint16_t colorLow565, uint16_t colorHigh565, unsigned int lumaLow, unsigned int lumaHigh, 40 | uint32_t dxtIndices, unsigned int indexCountLow, unsigned int indexCountHigh, 41 | uint16_t *atitcColorLow555, uint16_t *atitcColorHigh565, uint32_t *atitcIndices) { 42 | uint16_t colorMed565; 43 | uint8_t colorMed888[3]; 44 | unsigned int lumaMed; 45 | uint32_t colorIndexMask; 46 | 47 | colorMed565 = (((colorLow565 & 0x001F) + (colorHigh565 & 0x001F)) >> 1) | 48 | ((((colorLow565 & 0x07E0) + (colorHigh565 & 0x07E0)) >> 1) & 0x07E0) | 49 | ((((colorLow565 & 0xF800) + (colorHigh565 & 0xF800)) >> 1) & 0xF800); 50 | S3TConv_Utility_Color565To888(colorMed565, colorMed888); 51 | lumaMed = S3TConv_ATITC_GetLuminance(colorMed888); 52 | 53 | colorIndexMask = dxtIndices & 0x55555555 & ((dxtIndices & 0xAAAAAAAA) >> 1); 54 | colorIndexMask = ~(colorIndexMask | (colorIndexMask << 1)); 55 | 56 | if (indexCountLow > indexCountHigh) { // Not >= because low gets less green bits than high. 57 | if (lumaMed >= lumaLow) { 58 | *atitcColorLow555 = 0x8000 | S3TConv_Utility_Color565To555(colorLow565); 59 | *atitcColorHigh565 = colorMed565; 60 | // 0 1 2 3 -> 2 3 3 0. 61 | *atitcIndices = (dxtIndices | 0xAAAAAAAA | ((dxtIndices & 0xAAAAAAAA) >> 1)) & colorIndexMask; 62 | } else { 63 | // Generally shouldn't happen without hue variation. 64 | *atitcColorLow555 = 0x8000 | S3TConv_Utility_Color565To555(colorMed565); 65 | *atitcColorHigh565 = colorLow565; 66 | // 0 1 2 3 -> 3 2 2 0. 67 | *atitcIndices = ((dxtIndices | 0xAAAAAAAA) ^ ((~dxtIndices & 0xAAAAAAAA) >> 1)) & colorIndexMask; 68 | } 69 | } else { 70 | if (lumaMed <= lumaHigh) { 71 | *atitcColorLow555 = 0x8000 | S3TConv_Utility_Color565To555(colorMed565); 72 | *atitcColorHigh565 = colorHigh565; 73 | // 0 1 2 3 -> 2 3 2 0. 74 | *atitcIndices = (0xAAAAAAAA | dxtIndices) & colorIndexMask; 75 | } else { 76 | // Generally shouldn't happen without hue variation. 77 | *atitcColorLow555 = 0x8000 | S3TConv_Utility_Color565To555(colorHigh565); 78 | *atitcColorHigh565 = colorMed565; 79 | // 0 1 2 3 -> 3 2 3 0. 80 | *atitcIndices = ((dxtIndices | 0xAAAAAAAA) ^ 0x55555555) & colorIndexMask; 81 | } 82 | } 83 | } 84 | 85 | void S3TConv_ATITC_RGBBlockFromDXT(const uint8_t dxtBlock[8], int asDXT1, uint8_t atitcBlock[8], 86 | unsigned int remainingWidth, unsigned int remainingHeight) { 87 | // Source block data. 88 | uint16_t dxtColor0565, dxtColor1565; 89 | uint8_t dxtColor0888[3], dxtColor1888[3]; 90 | unsigned int dxtLuma0, dxtLuma1; 91 | uint32_t dxtSourceIndices; 92 | 93 | // Resulting block data. 94 | uint16_t atitcColorLow555, atitcColorHigh565; 95 | uint32_t atitcIndices; 96 | 97 | // Extracting data from the DXT block bytes. 98 | dxtColor0565 = (uint16_t) dxtBlock[0] | ((uint16_t) dxtBlock[1] << 8); 99 | dxtColor1565 = (uint16_t) dxtBlock[2] | ((uint16_t) dxtBlock[3] << 8); 100 | dxtSourceIndices = (uint32_t) dxtBlock[4] | ((uint32_t) dxtBlock[5] << 8) | 101 | ((uint32_t) dxtBlock[6] << 16) | ((uint32_t) dxtBlock[7] << 24); 102 | S3TConv_Utility_Color565To888(dxtColor0565, dxtColor0888); 103 | dxtLuma0 = S3TConv_ATITC_GetLuminance(dxtColor0888); 104 | S3TConv_Utility_Color565To888(dxtColor1565, dxtColor1888); 105 | dxtLuma1 = S3TConv_ATITC_GetLuminance(dxtColor1888); 106 | 107 | // Handling 2 main modes. 108 | if (!asDXT1 || dxtColor0565 > dxtColor1565) { 109 | // The RGB0, RGB1, (2*RGB0+RGB1)/3, (RGB0+2*RGB1)/3 mode. 110 | // Simply reordering the indices. DXT may be implemented as 1/3 and 2/3 or 3/8 and 5/8. ATITC is 3/8 and 5/8. 111 | // 0 1 2 3 -> 0 3 1 2, if color 0 is the lower one (which is unlikely though). 112 | atitcIndices = dxtSourceIndices ^ ((dxtSourceIndices & 0xAAAAAAAA) >> 1); 113 | atitcIndices ^= (atitcIndices & 0x55555555) << 1; 114 | if (dxtLuma0 >= dxtLuma1) { 115 | atitcColorLow555 = S3TConv_Utility_Color565To555(dxtColor1565); 116 | atitcColorHigh565 = dxtColor0565; 117 | atitcIndices = ~atitcIndices; 118 | } else { 119 | atitcColorLow555 = S3TConv_Utility_Color565To555(dxtColor0565); 120 | atitcColorHigh565 = dxtColor1565; 121 | } 122 | } else { 123 | // The RGB0, RGB1, (RGB0+RGB1)/2, BLACK mode. In general, can't be represented exactly by ATITC. 124 | 125 | uint16_t dxtColorLow565, dxtColorHigh565; 126 | const uint8_t *dxtColorLow888, *dxtColorHigh888; 127 | unsigned int dxtLumaLow, dxtLumaHigh; 128 | uint32_t dxtIndices = dxtSourceIndices; 129 | 130 | unsigned int dxtIndexCount[4] = { 0 }, dxtLowHighIndexCounts[4][2] = { 0 }; 131 | unsigned int pixelIndex; 132 | 133 | // Sort the DXT colors by luminance, so it's easier to interpolate and to match the way ATITC sorts shades. 134 | if (dxtLuma0 <= dxtLuma1) { 135 | dxtColorLow565 = dxtColor0565; 136 | dxtColorHigh565 = dxtColor1565; 137 | dxtColorLow888 = dxtColor0888; 138 | dxtColorHigh888 = dxtColor1888; 139 | dxtLumaLow = dxtLuma0; 140 | dxtLumaHigh = dxtLuma1; 141 | } else { 142 | dxtColorLow565 = dxtColor1565; 143 | dxtColorHigh565 = dxtColor0565; 144 | dxtColorLow888 = dxtColor1888; 145 | dxtColorHigh888 = dxtColor0888; 146 | dxtLumaLow = dxtLuma1; 147 | dxtLumaHigh = dxtLuma0; 148 | dxtIndices ^= (~dxtIndices & 0xAAAAAAAA) >> 1; // Swap RGB0 and RGB1. 149 | } 150 | 151 | // Count all indices in the whole block, and also low and high colors in 2x2 corners, so less common ones may be neglected. 152 | for (pixelIndex = 0; pixelIndex < 16; ++pixelIndex) { 153 | if ((pixelIndex & 3) >= remainingWidth || (pixelIndex >> 2) >= remainingHeight) { 154 | continue; 155 | } 156 | unsigned int dxtIndex = (dxtIndices >> (pixelIndex << 1)) & 3; 157 | ++dxtIndexCount[dxtIndex]; 158 | // Count low and high colors in 2x2 corners in case they will be used to replace the medium color with close interpolations. 159 | if ((dxtIndex & 2) == 0) { 160 | ++dxtLowHighIndexCounts[(pixelIndex >> 2) ^ (((pixelIndex >> 1) ^ (pixelIndex >> 2)) & 1)][dxtIndex]; 161 | } 162 | } 163 | 164 | // Try various conversions, from the most accurate ones in specific cases to the general ones. 165 | if (dxtIndexCount[2] == 0) { 166 | // If at least one shade is not used, the remaining two can be converted exactly. 167 | // The medium color isn't used. 168 | atitcColorLow555 = 0x8000 | S3TConv_Utility_Color565To555(dxtColorLow565); 169 | atitcColorHigh565 = dxtColorHigh565; 170 | // 0 1 3 -> 2 3 0. 171 | atitcIndices = (dxtIndices ^ 0xAAAAAAAA) & ~((dxtIndices & 0xAAAAAAAA) >> 1); 172 | } else if (dxtIndexCount[0] == 0 || dxtIndexCount[1] == 0) { 173 | // Similar case, but either low or high isn't used. 174 | S3TConv_ATITC_ConvertBlackTrickDiscardingLowOrHigh( 175 | dxtColorLow565, dxtColorHigh565, dxtLumaLow, dxtLumaHigh, 176 | dxtIndices, dxtIndexCount[0], dxtIndexCount[1], 177 | &atitcColorLow555, &atitcColorHigh565, &atitcIndices); 178 | } else if (dxtIndexCount[3] == 0) { 179 | // If black isn't used, approximate 1/2 with 3/8 or 5/8, depending on whether low or high is more common in each 2x2 corner. 180 | uint32_t medIndexMask; 181 | int lowIsMoreCommon = (dxtIndexCount[0] > dxtIndexCount[1]); // Not >= because high has more green bits. 182 | unsigned int subBlockIndex; 183 | atitcColorLow555 = S3TConv_Utility_Color565To555(dxtColorLow565); 184 | atitcColorHigh565 = dxtColorHigh565; 185 | // 0 1 2 -> 0 3 2, or 0 2 1 -> 0 3 1 if the lower approximation is the closer one. 186 | atitcIndices = dxtIndices | ((dxtIndices & 0x55555555) << 1); 187 | medIndexMask = dxtIndices & 0xAAAAAAAA; 188 | medIndexMask |= medIndexMask >> 1; 189 | for (subBlockIndex = 0; subBlockIndex < 4; ++subBlockIndex) { 190 | unsigned int countLow = dxtLowHighIndexCounts[subBlockIndex][0], countHigh = dxtLowHighIndexCounts[subBlockIndex][1]; 191 | if (countLow > countHigh || (lowIsMoreCommon && countLow == countHigh)) { 192 | atitcIndices ^= (0x00000F0F << (((subBlockIndex & 2) << 3) | ((subBlockIndex & 1) << 2))) & medIndexMask; 193 | } 194 | } 195 | } else { 196 | // 3 shades will be used if the resulting approximation is near-perfect (if medium is the most common), 197 | // or if it's at least somewhere between the original colors (if low or high is more common than medium). 198 | // In other cases, 2 shades will be picked, depending on which ones are the most common. 199 | 200 | unsigned int colorBlackTrickMedHigh888[3]; 201 | unsigned int blackTrickBoundScaleLow, blackTrickBoundScaleHigh; 202 | unsigned int blackTrickBoundLow[3], blackTrickBoundHigh[3]; 203 | 204 | colorBlackTrickMedHigh888[0] = dxtColorLow888[0] + (dxtColorHigh888[0] >> 2); 205 | colorBlackTrickMedHigh888[1] = dxtColorLow888[1] + (dxtColorHigh888[1] >> 2); 206 | colorBlackTrickMedHigh888[2] = dxtColorLow888[2] + (dxtColorHigh888[2] >> 2); 207 | 208 | blackTrickBoundScaleLow = ((dxtIndexCount[2] >= dxtIndexCount[0] && dxtIndexCount[2] >= dxtIndexCount[1]) ? 3 : 0); 209 | blackTrickBoundScaleHigh = 8 - blackTrickBoundScaleLow; 210 | 211 | blackTrickBoundLow[0] = (blackTrickBoundScaleHigh * dxtColorLow888[0] + blackTrickBoundScaleLow * dxtColorHigh888[0]) >> 3; 212 | blackTrickBoundLow[1] = (blackTrickBoundScaleHigh * dxtColorLow888[1] + blackTrickBoundScaleLow * dxtColorHigh888[1]) >> 3; 213 | blackTrickBoundLow[2] = (blackTrickBoundScaleHigh * dxtColorLow888[2] + blackTrickBoundScaleLow * dxtColorHigh888[2]) >> 3; 214 | blackTrickBoundHigh[0] = (blackTrickBoundScaleLow * dxtColorLow888[0] + blackTrickBoundScaleHigh * dxtColorHigh888[0]) >> 3; 215 | blackTrickBoundHigh[1] = (blackTrickBoundScaleLow * dxtColorLow888[1] + blackTrickBoundScaleHigh * dxtColorHigh888[1]) >> 3; 216 | blackTrickBoundHigh[2] = (blackTrickBoundScaleLow * dxtColorLow888[2] + blackTrickBoundScaleHigh * dxtColorHigh888[2]) >> 3; 217 | 218 | // Will also ensure there's no overflow in each component. 219 | if (colorBlackTrickMedHigh888[0] >= blackTrickBoundLow[0] && colorBlackTrickMedHigh888[0] <= blackTrickBoundHigh[0] && 220 | colorBlackTrickMedHigh888[1] >= blackTrickBoundLow[1] && colorBlackTrickMedHigh888[1] <= blackTrickBoundHigh[1] && 221 | colorBlackTrickMedHigh888[2] >= blackTrickBoundLow[2] && colorBlackTrickMedHigh888[2] <= blackTrickBoundHigh[2]) { 222 | atitcColorLow555 = (uint16_t) (0x8000 | ((colorBlackTrickMedHigh888[0] >> 3) << 10) | 223 | ((colorBlackTrickMedHigh888[1] >> 3) << 5) | (colorBlackTrickMedHigh888[2] >> 3)); 224 | atitcColorHigh565 = dxtColorHigh565; 225 | // 0 1 2 3 -> 1 3 2 0. 226 | atitcIndices = ~dxtIndices; 227 | atitcIndices ^= (atitcIndices & 0x55555555) << 1; 228 | atitcIndices ^= (atitcIndices & 0xAAAAAAAA) >> 1; 229 | } else { 230 | // Can't use the black trick to represent all 3 shades, so discard one of them. 231 | if (dxtIndexCount[2] <= dxtIndexCount[0] && dxtIndexCount[2] <= dxtIndexCount[1]) { 232 | // Discard the medium shade. 233 | uint32_t colorIndexMask; 234 | atitcColorLow555 = 0x8000 | S3TConv_Utility_Color565To555(dxtColorLow565); 235 | atitcColorHigh565 = dxtColorHigh565; 236 | colorIndexMask = dxtIndices & 0x55555555 & ((dxtIndices & 0xAAAAAAAA) >> 1); 237 | colorIndexMask = ~(colorIndexMask | (colorIndexMask << 1)); 238 | if (dxtIndexCount[0] > dxtIndexCount[1]) { 239 | // 0 1 2 3 -> 2 3 2 0. 240 | atitcIndices = (0xAAAAAAAA | dxtIndices) & colorIndexMask; 241 | } else { 242 | // 0 1 2 3 -> 2 3 3 0. 243 | atitcIndices = (dxtIndices | 0xAAAAAAAA | ((dxtIndices & 0xAAAAAAAA) >> 1)) & colorIndexMask; 244 | } 245 | } else { 246 | // Discard the low or the high shade. 247 | S3TConv_ATITC_ConvertBlackTrickDiscardingLowOrHigh( 248 | dxtColorLow565, dxtColorHigh565, dxtLumaLow, dxtLumaHigh, 249 | dxtIndices, dxtIndexCount[0], dxtIndexCount[1], 250 | &atitcColorLow555, &atitcColorHigh565, &atitcIndices); 251 | } 252 | } 253 | } 254 | } 255 | 256 | // Writing the ATITC block. 257 | atitcBlock[0] = (uint8_t) atitcColorLow555; 258 | atitcBlock[1] = (uint8_t) (atitcColorLow555 >> 8); 259 | atitcBlock[2] = (uint8_t) atitcColorHigh565; 260 | atitcBlock[3] = (uint8_t) (atitcColorHigh565 >> 8); 261 | atitcBlock[4] = (uint8_t) atitcIndices; 262 | atitcBlock[5] = (uint8_t) (atitcIndices >> 8); 263 | atitcBlock[6] = (uint8_t) (atitcIndices >> 16); 264 | atitcBlock[7] = (uint8_t) (atitcIndices >> 24); 265 | } 266 | --------------------------------------------------------------------------------