#include "DecompressorBc1.h"

#include "Utils/Alignment.h"

#include <cassert>
#include <limits>

namespace
{
    constexpr auto BC1_BLOCK_PIXELS = 4u;
    constexpr unsigned BC1_BLUE_MASK = 0x1F;
    constexpr unsigned BC1_BLUE_SHIFT = 0;
    constexpr unsigned BC1_BLUE_MAX = 0x1F;
    constexpr unsigned BC1_GREEN_MASK = 0x7E0;
    constexpr unsigned BC1_GREEN_SHIFT = 5;
    constexpr unsigned BC1_GREEN_MAX = 0x3F;
    constexpr unsigned BC1_RED_MASK = 0xF800;
    constexpr unsigned BC1_RED_SHIFT = 11;
    constexpr unsigned BC1_RED_MAX = 0x1F;
    constexpr auto BC1_COLOR_CHANNELS_SIZE = 8u;
    constexpr auto BC1_BLOCK_SIZE = BC1_COLOR_CHANNELS_SIZE;

    void SetupColorTables(
        const uint8_t* in, uint8_t (&colorTableRed)[4], uint8_t (&colorTableGreen)[4], uint8_t (&colorTableBlue)[4], uint8_t (&colorTableAlpha)[4])
    {
#define EXTRACT_COLOR_CHANNEL_BC1(color, mask, shift, colMax)                                                                                                  \
    static_cast<uint8_t>(static_cast<uint16_t>(((color) & (mask)) >> (shift)) * std::numeric_limits<uint8_t>::max() / (colMax))
#define INTERPOLATE_BC1(colorTable, val0, val1)                                                                                                                \
    static_cast<uint8_t>(                                                                                                                                      \
        (static_cast<uint16_t>(val0) * static_cast<uint16_t>((colorTable)[0]) + static_cast<uint16_t>(val1) * static_cast<uint16_t>((colorTable)[1]))          \
        / ((val0) + (val1)))

        const auto color0 = static_cast<uint16_t>(in[0] | (in[1] << 8));
        const auto color1 = static_cast<uint16_t>(in[2] | (in[3] << 8));

        colorTableRed[0] = EXTRACT_COLOR_CHANNEL_BC1(color0, BC1_RED_MASK, BC1_RED_SHIFT, BC1_RED_MAX);
        colorTableRed[1] = EXTRACT_COLOR_CHANNEL_BC1(color1, BC1_RED_MASK, BC1_RED_SHIFT, BC1_RED_MAX);

        colorTableGreen[0] = EXTRACT_COLOR_CHANNEL_BC1(color0, BC1_GREEN_MASK, BC1_GREEN_SHIFT, BC1_GREEN_MAX);
        colorTableGreen[1] = EXTRACT_COLOR_CHANNEL_BC1(color1, BC1_GREEN_MASK, BC1_GREEN_SHIFT, BC1_GREEN_MAX);

        colorTableBlue[0] = EXTRACT_COLOR_CHANNEL_BC1(color0, BC1_BLUE_MASK, BC1_BLUE_SHIFT, BC1_BLUE_MAX);
        colorTableBlue[1] = EXTRACT_COLOR_CHANNEL_BC1(color1, BC1_BLUE_MASK, BC1_BLUE_SHIFT, BC1_BLUE_MAX);

        colorTableAlpha[0] = std::numeric_limits<uint8_t>::max();
        colorTableAlpha[1] = std::numeric_limits<uint8_t>::max();
        colorTableAlpha[2] = std::numeric_limits<uint8_t>::max();

        if (color0 > color1)
        {
            colorTableRed[2] = INTERPOLATE_BC1(colorTableRed, 2, 1);
            colorTableRed[3] = INTERPOLATE_BC1(colorTableRed, 1, 2);

            colorTableGreen[2] = INTERPOLATE_BC1(colorTableGreen, 2, 1);
            colorTableGreen[3] = INTERPOLATE_BC1(colorTableGreen, 1, 2);

            colorTableBlue[2] = INTERPOLATE_BC1(colorTableBlue, 2, 1);
            colorTableBlue[3] = INTERPOLATE_BC1(colorTableBlue, 1, 2);

            colorTableAlpha[3] = std::numeric_limits<uint8_t>::max();
        }
        else
        {
            colorTableRed[2] = INTERPOLATE_BC1(colorTableRed, 1, 1);
            colorTableGreen[2] = INTERPOLATE_BC1(colorTableGreen, 1, 1);
            colorTableBlue[2] = INTERPOLATE_BC1(colorTableBlue, 1, 1);

            colorTableRed[3] = 0;
            colorTableGreen[3] = 0;
            colorTableBlue[3] = 0;
            colorTableAlpha[3] = 0;
        }

#undef INTERPOLATE_BC1
#undef EXTRACT_COLOR_CHANNEL_BC1
    }

    void DecompressBlock(const uint8_t* in,
                         uint8_t* out,
                         const unsigned outPitch,
                         const unsigned outPixelSize,
                         const unsigned outOffsetR,
                         const unsigned outOffsetG,
                         const unsigned outOffsetB,
                         const unsigned outOffsetA,
                         const bool hasAlpha)
    {
        uint8_t colorTableRed[4];
        uint8_t colorTableGreen[4];
        uint8_t colorTableBlue[4];
        uint8_t colorTableAlpha[4];
        SetupColorTables(in, colorTableRed, colorTableGreen, colorTableBlue, colorTableAlpha);

        const uint32_t dataColor0 = in[4];
        const uint32_t dataColor1 = in[5];
        const uint32_t dataColor2 = in[6];
        const uint32_t dataColor3 = in[7];

        out[0 * outPitch + 0 * outPixelSize + outOffsetR] = colorTableRed[(dataColor0 & (0x3 << 0)) >> 0];
        out[0 * outPitch + 0 * outPixelSize + outOffsetG] = colorTableGreen[(dataColor0 & (0x3 << 0)) >> 0];
        out[0 * outPitch + 0 * outPixelSize + outOffsetB] = colorTableBlue[(dataColor0 & (0x3 << 0)) >> 0];

        out[0 * outPitch + 1 * outPixelSize + outOffsetR] = colorTableRed[(dataColor0 & (0x3 << 2)) >> 2];
        out[0 * outPitch + 1 * outPixelSize + outOffsetG] = colorTableGreen[(dataColor0 & (0x3 << 2)) >> 2];
        out[0 * outPitch + 1 * outPixelSize + outOffsetB] = colorTableBlue[(dataColor0 & (0x3 << 2)) >> 2];

        out[0 * outPitch + 2 * outPixelSize + outOffsetR] = colorTableRed[(dataColor0 & (0x3 << 4)) >> 4];
        out[0 * outPitch + 2 * outPixelSize + outOffsetG] = colorTableGreen[(dataColor0 & (0x3 << 4)) >> 4];
        out[0 * outPitch + 2 * outPixelSize + outOffsetB] = colorTableBlue[(dataColor0 & (0x3 << 4)) >> 4];

        out[0 * outPitch + 3 * outPixelSize + outOffsetR] = colorTableRed[(dataColor0 & (0x3 << 6)) >> 6];
        out[0 * outPitch + 3 * outPixelSize + outOffsetG] = colorTableGreen[(dataColor0 & (0x3 << 6)) >> 6];
        out[0 * outPitch + 3 * outPixelSize + outOffsetB] = colorTableBlue[(dataColor0 & (0x3 << 6)) >> 6];

        out[1 * outPitch + 0 * outPixelSize + outOffsetR] = colorTableRed[(dataColor1 & (0x3 << 0)) >> 0];
        out[1 * outPitch + 0 * outPixelSize + outOffsetG] = colorTableGreen[(dataColor1 & (0x3 << 0)) >> 0];
        out[1 * outPitch + 0 * outPixelSize + outOffsetB] = colorTableBlue[(dataColor1 & (0x3 << 0)) >> 0];

        out[1 * outPitch + 1 * outPixelSize + outOffsetR] = colorTableRed[(dataColor1 & (0x3 << 2)) >> 2];
        out[1 * outPitch + 1 * outPixelSize + outOffsetG] = colorTableGreen[(dataColor1 & (0x3 << 2)) >> 2];
        out[1 * outPitch + 1 * outPixelSize + outOffsetB] = colorTableBlue[(dataColor1 & (0x3 << 2)) >> 2];

        out[1 * outPitch + 2 * outPixelSize + outOffsetR] = colorTableRed[(dataColor1 & (0x3 << 4)) >> 4];
        out[1 * outPitch + 2 * outPixelSize + outOffsetG] = colorTableGreen[(dataColor1 & (0x3 << 4)) >> 4];
        out[1 * outPitch + 2 * outPixelSize + outOffsetB] = colorTableBlue[(dataColor1 & (0x3 << 4)) >> 4];

        out[1 * outPitch + 3 * outPixelSize + outOffsetR] = colorTableRed[(dataColor1 & (0x3 << 6)) >> 6];
        out[1 * outPitch + 3 * outPixelSize + outOffsetG] = colorTableGreen[(dataColor1 & (0x3 << 6)) >> 6];
        out[1 * outPitch + 3 * outPixelSize + outOffsetB] = colorTableBlue[(dataColor1 & (0x3 << 6)) >> 6];

        out[2 * outPitch + 0 * outPixelSize + outOffsetR] = colorTableRed[(dataColor2 & (0x3 << 0)) >> 0];
        out[2 * outPitch + 0 * outPixelSize + outOffsetG] = colorTableGreen[(dataColor2 & (0x3 << 0)) >> 0];
        out[2 * outPitch + 0 * outPixelSize + outOffsetB] = colorTableBlue[(dataColor2 & (0x3 << 0)) >> 0];

        out[2 * outPitch + 1 * outPixelSize + outOffsetR] = colorTableRed[(dataColor2 & (0x3 << 2)) >> 2];
        out[2 * outPitch + 1 * outPixelSize + outOffsetG] = colorTableGreen[(dataColor2 & (0x3 << 2)) >> 2];
        out[2 * outPitch + 1 * outPixelSize + outOffsetB] = colorTableBlue[(dataColor2 & (0x3 << 2)) >> 2];

        out[2 * outPitch + 2 * outPixelSize + outOffsetR] = colorTableRed[(dataColor2 & (0x3 << 4)) >> 4];
        out[2 * outPitch + 2 * outPixelSize + outOffsetG] = colorTableGreen[(dataColor2 & (0x3 << 4)) >> 4];
        out[2 * outPitch + 2 * outPixelSize + outOffsetB] = colorTableBlue[(dataColor2 & (0x3 << 4)) >> 4];

        out[2 * outPitch + 3 * outPixelSize + outOffsetR] = colorTableRed[(dataColor2 & (0x3 << 6)) >> 6];
        out[2 * outPitch + 3 * outPixelSize + outOffsetG] = colorTableGreen[(dataColor2 & (0x3 << 6)) >> 6];
        out[2 * outPitch + 3 * outPixelSize + outOffsetB] = colorTableBlue[(dataColor2 & (0x3 << 6)) >> 6];

        out[3 * outPitch + 0 * outPixelSize + outOffsetR] = colorTableRed[(dataColor3 & (0x3 << 0)) >> 0];
        out[3 * outPitch + 0 * outPixelSize + outOffsetG] = colorTableGreen[(dataColor3 & (0x3 << 0)) >> 0];
        out[3 * outPitch + 0 * outPixelSize + outOffsetB] = colorTableBlue[(dataColor3 & (0x3 << 0)) >> 0];

        out[3 * outPitch + 1 * outPixelSize + outOffsetR] = colorTableRed[(dataColor3 & (0x3 << 2)) >> 2];
        out[3 * outPitch + 1 * outPixelSize + outOffsetG] = colorTableGreen[(dataColor3 & (0x3 << 2)) >> 2];
        out[3 * outPitch + 1 * outPixelSize + outOffsetB] = colorTableBlue[(dataColor3 & (0x3 << 2)) >> 2];

        out[3 * outPitch + 2 * outPixelSize + outOffsetR] = colorTableRed[(dataColor3 & (0x3 << 4)) >> 4];
        out[3 * outPitch + 2 * outPixelSize + outOffsetG] = colorTableGreen[(dataColor3 & (0x3 << 4)) >> 4];
        out[3 * outPitch + 2 * outPixelSize + outOffsetB] = colorTableBlue[(dataColor3 & (0x3 << 4)) >> 4];

        out[3 * outPitch + 3 * outPixelSize + outOffsetR] = colorTableRed[(dataColor3 & (0x3 << 6)) >> 6];
        out[3 * outPitch + 3 * outPixelSize + outOffsetG] = colorTableGreen[(dataColor3 & (0x3 << 6)) >> 6];
        out[3 * outPitch + 3 * outPixelSize + outOffsetB] = colorTableBlue[(dataColor3 & (0x3 << 6)) >> 6];

        if (hasAlpha)
        {
            out[0 * outPitch + 0 * outPixelSize + outOffsetA] = colorTableAlpha[(dataColor0 & (0x3 << 0)) >> 0];
            out[0 * outPitch + 1 * outPixelSize + outOffsetA] = colorTableAlpha[(dataColor0 & (0x3 << 2)) >> 2];
            out[0 * outPitch + 2 * outPixelSize + outOffsetA] = colorTableAlpha[(dataColor0 & (0x3 << 4)) >> 4];
            out[0 * outPitch + 3 * outPixelSize + outOffsetA] = colorTableAlpha[(dataColor0 & (0x3 << 6)) >> 6];
            out[1 * outPitch + 0 * outPixelSize + outOffsetA] = colorTableAlpha[(dataColor1 & (0x3 << 0)) >> 0];
            out[1 * outPitch + 1 * outPixelSize + outOffsetA] = colorTableAlpha[(dataColor1 & (0x3 << 2)) >> 2];
            out[1 * outPitch + 2 * outPixelSize + outOffsetA] = colorTableAlpha[(dataColor1 & (0x3 << 4)) >> 4];
            out[1 * outPitch + 3 * outPixelSize + outOffsetA] = colorTableAlpha[(dataColor1 & (0x3 << 6)) >> 6];
            out[2 * outPitch + 0 * outPixelSize + outOffsetA] = colorTableAlpha[(dataColor2 & (0x3 << 0)) >> 0];
            out[2 * outPitch + 1 * outPixelSize + outOffsetA] = colorTableAlpha[(dataColor2 & (0x3 << 2)) >> 2];
            out[2 * outPitch + 2 * outPixelSize + outOffsetA] = colorTableAlpha[(dataColor2 & (0x3 << 4)) >> 4];
            out[2 * outPitch + 3 * outPixelSize + outOffsetA] = colorTableAlpha[(dataColor2 & (0x3 << 6)) >> 6];
            out[3 * outPitch + 0 * outPixelSize + outOffsetA] = colorTableAlpha[(dataColor3 & (0x3 << 0)) >> 0];
            out[3 * outPitch + 1 * outPixelSize + outOffsetA] = colorTableAlpha[(dataColor3 & (0x3 << 2)) >> 2];
            out[3 * outPitch + 2 * outPixelSize + outOffsetA] = colorTableAlpha[(dataColor3 & (0x3 << 4)) >> 4];
            out[3 * outPitch + 3 * outPixelSize + outOffsetA] = colorTableAlpha[(dataColor3 & (0x3 << 6)) >> 6];
        }
    }

    void DecompressBlockEdge(const uint8_t* in,
                             uint8_t* out,
                             const unsigned width,
                             const unsigned height,
                             const unsigned outPitch,
                             const unsigned outPixelSize,
                             const unsigned outOffsetR,
                             const unsigned outOffsetG,
                             const unsigned outOffsetB,
                             const unsigned outOffsetA,
                             const bool hasAlpha)
    {
        assert(width <= BC1_BLOCK_PIXELS);
        assert(height <= BC1_BLOCK_PIXELS);

        uint8_t colorTableRed[4];
        uint8_t colorTableGreen[4];
        uint8_t colorTableBlue[4];
        uint8_t colorTableAlpha[4];
        SetupColorTables(in, colorTableRed, colorTableGreen, colorTableBlue, colorTableAlpha);

        const uint8_t dataColor0 = in[4];
        const uint8_t dataColor1 = in[5];
        const uint8_t dataColor2 = in[6];
        const uint8_t dataColor3 = in[7];

        const uint8_t pixelsRed[]{
            colorTableRed[(dataColor0 & (0x3 << 0)) >> 0],
            colorTableRed[(dataColor0 & (0x3 << 2)) >> 2],
            colorTableRed[(dataColor0 & (0x3 << 4)) >> 4],
            colorTableRed[(dataColor0 & (0x3 << 6)) >> 6],
            colorTableRed[(dataColor1 & (0x3 << 0)) >> 0],
            colorTableRed[(dataColor1 & (0x3 << 2)) >> 2],
            colorTableRed[(dataColor1 & (0x3 << 4)) >> 4],
            colorTableRed[(dataColor1 & (0x3 << 6)) >> 6],
            colorTableRed[(dataColor2 & (0x3 << 0)) >> 0],
            colorTableRed[(dataColor2 & (0x3 << 2)) >> 2],
            colorTableRed[(dataColor2 & (0x3 << 4)) >> 4],
            colorTableRed[(dataColor2 & (0x3 << 6)) >> 6],
            colorTableRed[(dataColor3 & (0x3 << 0)) >> 0],
            colorTableRed[(dataColor3 & (0x3 << 2)) >> 2],
            colorTableRed[(dataColor3 & (0x3 << 4)) >> 4],
            colorTableRed[(dataColor3 & (0x3 << 6)) >> 6],
        };
        static_assert(std::extent_v<decltype(pixelsRed)> == BC1_BLOCK_PIXELS * BC1_BLOCK_PIXELS);

        const uint8_t pixelsGreen[]{
            colorTableGreen[(dataColor0 & (0x3 << 0)) >> 0],
            colorTableGreen[(dataColor0 & (0x3 << 2)) >> 2],
            colorTableGreen[(dataColor0 & (0x3 << 4)) >> 4],
            colorTableGreen[(dataColor0 & (0x3 << 6)) >> 6],
            colorTableGreen[(dataColor1 & (0x3 << 0)) >> 0],
            colorTableGreen[(dataColor1 & (0x3 << 2)) >> 2],
            colorTableGreen[(dataColor1 & (0x3 << 4)) >> 4],
            colorTableGreen[(dataColor1 & (0x3 << 6)) >> 6],
            colorTableGreen[(dataColor2 & (0x3 << 0)) >> 0],
            colorTableGreen[(dataColor2 & (0x3 << 2)) >> 2],
            colorTableGreen[(dataColor2 & (0x3 << 4)) >> 4],
            colorTableGreen[(dataColor2 & (0x3 << 6)) >> 6],
            colorTableGreen[(dataColor3 & (0x3 << 0)) >> 0],
            colorTableGreen[(dataColor3 & (0x3 << 2)) >> 2],
            colorTableGreen[(dataColor3 & (0x3 << 4)) >> 4],
            colorTableGreen[(dataColor3 & (0x3 << 6)) >> 6],
        };
        static_assert(std::extent_v<decltype(pixelsGreen)> == BC1_BLOCK_PIXELS * BC1_BLOCK_PIXELS);

        const uint8_t pixelsBlue[]{
            colorTableBlue[(dataColor0 & (0x3 << 0)) >> 0],
            colorTableBlue[(dataColor0 & (0x3 << 2)) >> 2],
            colorTableBlue[(dataColor0 & (0x3 << 4)) >> 4],
            colorTableBlue[(dataColor0 & (0x3 << 6)) >> 6],
            colorTableBlue[(dataColor1 & (0x3 << 0)) >> 0],
            colorTableBlue[(dataColor1 & (0x3 << 2)) >> 2],
            colorTableBlue[(dataColor1 & (0x3 << 4)) >> 4],
            colorTableBlue[(dataColor1 & (0x3 << 6)) >> 6],
            colorTableBlue[(dataColor2 & (0x3 << 0)) >> 0],
            colorTableBlue[(dataColor2 & (0x3 << 2)) >> 2],
            colorTableBlue[(dataColor2 & (0x3 << 4)) >> 4],
            colorTableBlue[(dataColor2 & (0x3 << 6)) >> 6],
            colorTableBlue[(dataColor3 & (0x3 << 0)) >> 0],
            colorTableBlue[(dataColor3 & (0x3 << 2)) >> 2],
            colorTableBlue[(dataColor3 & (0x3 << 4)) >> 4],
            colorTableBlue[(dataColor3 & (0x3 << 6)) >> 6],
        };
        static_assert(std::extent_v<decltype(pixelsBlue)> == BC1_BLOCK_PIXELS * BC1_BLOCK_PIXELS);

        for (auto curHeight = 0u; curHeight < height; curHeight++)
        {
            for (auto curWidth = 0u; curWidth < width; curWidth++)
            {
                out[curHeight * outPitch + curWidth * outPixelSize + outOffsetR] = pixelsRed[curHeight * BC1_BLOCK_PIXELS + curWidth];
                out[curHeight * outPitch + curWidth * outPixelSize + outOffsetG] = pixelsGreen[curHeight * BC1_BLOCK_PIXELS + curWidth];
                out[curHeight * outPitch + curWidth * outPixelSize + outOffsetB] = pixelsBlue[curHeight * BC1_BLOCK_PIXELS + curWidth];
            }
        }

        if (hasAlpha)
        {
            const uint8_t pixelsAlpha[]{
                colorTableAlpha[(dataColor0 & (0x3 << 0)) >> 0],
                colorTableAlpha[(dataColor0 & (0x3 << 2)) >> 2],
                colorTableAlpha[(dataColor0 & (0x3 << 4)) >> 4],
                colorTableAlpha[(dataColor0 & (0x3 << 6)) >> 6],
                colorTableAlpha[(dataColor1 & (0x3 << 0)) >> 0],
                colorTableAlpha[(dataColor1 & (0x3 << 2)) >> 2],
                colorTableAlpha[(dataColor1 & (0x3 << 4)) >> 4],
                colorTableAlpha[(dataColor1 & (0x3 << 6)) >> 6],
                colorTableAlpha[(dataColor2 & (0x3 << 0)) >> 0],
                colorTableAlpha[(dataColor2 & (0x3 << 2)) >> 2],
                colorTableAlpha[(dataColor2 & (0x3 << 4)) >> 4],
                colorTableAlpha[(dataColor2 & (0x3 << 6)) >> 6],
                colorTableAlpha[(dataColor3 & (0x3 << 0)) >> 0],
                colorTableAlpha[(dataColor3 & (0x3 << 2)) >> 2],
                colorTableAlpha[(dataColor3 & (0x3 << 4)) >> 4],
                colorTableAlpha[(dataColor3 & (0x3 << 6)) >> 6],
            };
            static_assert(std::extent_v<decltype(pixelsAlpha)> == BC1_BLOCK_PIXELS * BC1_BLOCK_PIXELS);

            for (auto curHeight = 0u; curHeight < height; curHeight++)
            {
                for (auto curWidth = 0u; curWidth < width; curWidth++)
                {
                    out[curHeight * outPitch + curWidth * outPixelSize + outOffsetA] = pixelsAlpha[curHeight * BC1_BLOCK_PIXELS + curWidth];
                }
            }
        }
    }
} // namespace

namespace image
{
    std::unique_ptr<Texture> DecompressorBc1::Decompress(const Texture& input, const ImageFormat* targetFormat)
    {
        assert(targetFormat->GetType() == ImageFormatType::UNSIGNED);
        if (targetFormat->GetType() != ImageFormatType::UNSIGNED)
            return nullptr;

        const auto* unsignedTargetFormat = dynamic_cast<const ImageFormatUnsigned*>(targetFormat);

        const auto outPixelSize = unsignedTargetFormat->m_bits_per_pixel / 8u;

        // Only support formats with byte aligned channels
        const auto redByteAligned = unsignedTargetFormat->m_r_size == 8 && (unsignedTargetFormat->m_r_offset % 8) == 0;
        const auto greenByteAligned = unsignedTargetFormat->m_g_size == 8 && (unsignedTargetFormat->m_g_offset % 8) == 0;
        const auto blueByteAligned = unsignedTargetFormat->m_b_size == 8 && (unsignedTargetFormat->m_b_offset % 8) == 0;
        const auto hasAlpha = unsignedTargetFormat->m_a_size > 0;
        const auto alphaByteAligned = unsignedTargetFormat->m_a_size == 8 && (unsignedTargetFormat->m_a_offset % 8) == 0;
        assert(redByteAligned);
        assert(greenByteAligned);
        assert(blueByteAligned);
        assert(!hasAlpha || alphaByteAligned);
        if (!redByteAligned || !greenByteAligned || !blueByteAligned || (hasAlpha && !alphaByteAligned))
            return nullptr;

        const auto outOffsetR = unsignedTargetFormat->m_r_offset / 8;
        const auto outOffsetG = unsignedTargetFormat->m_g_offset / 8;
        const auto outOffsetB = unsignedTargetFormat->m_b_offset / 8;
        const auto outOffsetA = unsignedTargetFormat->m_a_offset / 8;

        const auto width = input.GetWidth();
        const auto height = input.GetHeight();
        const auto depth = input.GetDepth();

        auto result = Texture::CreateForType(input.GetTextureType(), targetFormat, width, height, depth, input.HasMipMaps());
        result->Allocate();

        const auto faceCount = result->GetFaceCount();
        const auto mipCount = result->HasMipMaps() ? result->GetMipMapCount() : 1;
        assert(mipCount == (input.HasMipMaps() ? input.GetMipMapCount() : 1));

        for (auto mipLevel = 0; mipLevel < mipCount; mipLevel++)
        {
            const auto mipWidth = std::max<unsigned>(width >> mipLevel, 1u);
            const auto mipHeight = std::max<unsigned>(height >> mipLevel, 1u);
            const auto mipDepth = std::max<unsigned>(depth >> mipLevel, 1u);
            const unsigned mipPitch = outPixelSize * mipWidth;
            assert(mipPitch == result->GetFormat()->GetPitch(mipLevel, width));

            for (auto face = 0; face < faceCount; face++)
            {
                const auto* bufferIn = input.GetBufferForMipLevel(mipLevel, face);
                auto* bufferOut = result->GetBufferForMipLevel(mipLevel, face);

                for (auto curDepth = 0u; curDepth < mipDepth; curDepth++)
                {
                    const auto fullBlocksWidth = utils::AlignToPrevious(mipWidth, BC1_BLOCK_PIXELS);
                    const auto fullBlocksHeight = utils::AlignToPrevious(mipHeight, BC1_BLOCK_PIXELS);

                    for (auto curHeight = 0u; curHeight < fullBlocksHeight; curHeight += BC1_BLOCK_PIXELS)
                    {
                        for (auto curWidth = 0u; curWidth < fullBlocksWidth; curWidth += BC1_BLOCK_PIXELS)
                        {
                            DecompressBlock(bufferIn, bufferOut, mipPitch, outPixelSize, outOffsetR, outOffsetG, outOffsetB, outOffsetA, hasAlpha);
                            bufferIn += BC1_BLOCK_SIZE;
                            bufferOut += outPixelSize * BC1_BLOCK_PIXELS;
                        }

                        if (fullBlocksWidth < mipWidth)
                        {
                            const auto edgeBlockWidth = mipWidth - fullBlocksWidth;
                            DecompressBlockEdge(bufferIn,
                                                bufferOut,
                                                edgeBlockWidth,
                                                BC1_BLOCK_PIXELS,
                                                mipPitch,
                                                outPixelSize,
                                                outOffsetR,
                                                outOffsetG,
                                                outOffsetB,
                                                outOffsetA,
                                                hasAlpha);
                            bufferIn += BC1_BLOCK_SIZE;
                            bufferOut += outPixelSize * edgeBlockWidth;
                        }

                        bufferOut += mipPitch * (BC1_BLOCK_PIXELS - 1);
                    }

                    if (fullBlocksHeight < mipHeight)
                    {
                        const auto edgeBlockHeight = mipHeight - fullBlocksHeight;
                        for (auto curWidth = 0u; curWidth < fullBlocksWidth; curWidth += BC1_BLOCK_PIXELS)
                        {
                            DecompressBlockEdge(bufferIn,
                                                bufferOut,
                                                BC1_BLOCK_PIXELS,
                                                edgeBlockHeight,
                                                mipPitch,
                                                outPixelSize,
                                                outOffsetR,
                                                outOffsetG,
                                                outOffsetB,
                                                outOffsetA,
                                                hasAlpha);
                            bufferIn += BC1_BLOCK_SIZE;
                            bufferOut += outPixelSize * BC1_BLOCK_PIXELS;
                        }

                        if (fullBlocksWidth < mipWidth)
                        {
                            const auto edgeBlockWidth = mipWidth - fullBlocksWidth;
                            DecompressBlockEdge(bufferIn,
                                                bufferOut,
                                                edgeBlockWidth,
                                                edgeBlockHeight,
                                                mipPitch,
                                                outPixelSize,
                                                outOffsetR,
                                                outOffsetG,
                                                outOffsetB,
                                                outOffsetA,
                                                hasAlpha);
                            bufferIn += BC1_BLOCK_SIZE;
                            bufferOut += outPixelSize * edgeBlockWidth;
                        }

                        bufferOut += mipPitch * (BC1_BLOCK_PIXELS - 1);
                    }
                }
            }
        }

        return result;
    }
} // namespace image
