crossxtex/DirectXTex/DirectXTexPMAlpha.cpp

325 lines
11 KiB
C++
Raw Normal View History

2016-08-22 18:26:36 +00:00
//-------------------------------------------------------------------------------------
// DirectXTexPMAlpha.cpp
//
// DirectX Texture Library - Premultiplied alpha operations
//
// Copyright (c) Microsoft Corporation. All rights reserved.
2018-02-24 06:24:46 +00:00
// Licensed under the MIT License.
2016-08-22 18:26:36 +00:00
//
// http://go.microsoft.com/fwlink/?LinkId=248926
//-------------------------------------------------------------------------------------
2018-06-13 01:07:34 +00:00
#include "DirectXTexp.h"
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
using namespace DirectX;
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
namespace
2016-08-22 18:26:36 +00:00
{
//---------------------------------------------------------------------------------
// NonPremultiplied alpha -> Premultiplied alpha
2016-09-09 02:09:46 +00:00
HRESULT PremultiplyAlpha_(const Image& srcImage, const Image& destImage)
{
assert(srcImage.width == destImage.width);
assert(srcImage.height == destImage.height);
2016-08-22 18:26:36 +00:00
2018-03-29 19:00:16 +00:00
ScopedAlignedArrayXMVECTOR scanline(static_cast<XMVECTOR*>(_aligned_malloc((sizeof(XMVECTOR)*srcImage.width), 16)));
2016-09-09 02:09:46 +00:00
if (!scanline)
return E_OUTOFMEMORY;
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
const uint8_t *pSrc = srcImage.pixels;
uint8_t *pDest = destImage.pixels;
if (!pSrc || !pDest)
return E_POINTER;
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
for (size_t h = 0; h < srcImage.height; ++h)
2016-08-22 18:26:36 +00:00
{
2016-09-09 02:09:46 +00:00
if (!_LoadScanline(scanline.get(), srcImage.width, pSrc, srcImage.rowPitch, srcImage.format))
return E_FAIL;
XMVECTOR* ptr = scanline.get();
for (size_t w = 0; w < srcImage.width; ++w)
{
XMVECTOR v = *ptr;
XMVECTOR alpha = XMVectorSplatW(*ptr);
alpha = XMVectorMultiply(v, alpha);
*(ptr++) = XMVectorSelect(v, alpha, g_XMSelect1110);
}
if (!_StoreScanline(pDest, destImage.rowPitch, destImage.format, scanline.get(), srcImage.width))
return E_FAIL;
pSrc += srcImage.rowPitch;
pDest += destImage.rowPitch;
2016-08-22 18:26:36 +00:00
}
2016-09-09 02:09:46 +00:00
return S_OK;
2016-08-22 18:26:36 +00:00
}
2016-09-09 02:09:46 +00:00
HRESULT PremultiplyAlphaLinear(const Image& srcImage, DWORD flags, const Image& destImage)
{
assert(srcImage.width == destImage.width);
assert(srcImage.height == destImage.height);
2016-08-22 18:26:36 +00:00
2017-07-12 07:56:51 +00:00
static_assert(static_cast<int>(TEX_PMALPHA_SRGB_IN) == static_cast<int>(TEX_FILTER_SRGB_IN), "TEX_PMALHPA_SRGB* should match TEX_FILTER_SRGB*");
static_assert(static_cast<int>(TEX_PMALPHA_SRGB_OUT) == static_cast<int>(TEX_FILTER_SRGB_OUT), "TEX_PMALHPA_SRGB* should match TEX_FILTER_SRGB*");
static_assert(static_cast<int>(TEX_PMALPHA_SRGB) == static_cast<int>(TEX_FILTER_SRGB), "TEX_PMALHPA_SRGB* should match TEX_FILTER_SRGB*");
2016-09-09 02:09:46 +00:00
flags &= TEX_PMALPHA_SRGB;
2016-08-22 18:26:36 +00:00
2018-03-29 19:00:16 +00:00
ScopedAlignedArrayXMVECTOR scanline(static_cast<XMVECTOR*>(_aligned_malloc((sizeof(XMVECTOR)*srcImage.width), 16)));
2016-09-09 02:09:46 +00:00
if (!scanline)
return E_OUTOFMEMORY;
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
const uint8_t *pSrc = srcImage.pixels;
uint8_t *pDest = destImage.pixels;
if (!pSrc || !pDest)
return E_POINTER;
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
for (size_t h = 0; h < srcImage.height; ++h)
2016-08-22 18:26:36 +00:00
{
2016-09-09 02:09:46 +00:00
if (!_LoadScanlineLinear(scanline.get(), srcImage.width, pSrc, srcImage.rowPitch, srcImage.format, flags))
return E_FAIL;
XMVECTOR* ptr = scanline.get();
for (size_t w = 0; w < srcImage.width; ++w)
{
XMVECTOR v = *ptr;
XMVECTOR alpha = XMVectorSplatW(*ptr);
alpha = XMVectorMultiply(v, alpha);
*(ptr++) = XMVectorSelect(v, alpha, g_XMSelect1110);
}
if (!_StoreScanlineLinear(pDest, destImage.rowPitch, destImage.format, scanline.get(), srcImage.width, flags))
return E_FAIL;
pSrc += srcImage.rowPitch;
pDest += destImage.rowPitch;
2016-08-22 18:26:36 +00:00
}
2016-09-09 02:09:46 +00:00
return S_OK;
2016-08-22 18:26:36 +00:00
}
//---------------------------------------------------------------------------------
// Premultiplied alpha -> NonPremultiplied alpha (a.k.a. Straight alpha)
HRESULT DemultiplyAlpha(const Image& srcImage, const Image& destImage)
{
assert(srcImage.width == destImage.width);
assert(srcImage.height == destImage.height);
2018-03-29 19:00:16 +00:00
ScopedAlignedArrayXMVECTOR scanline(static_cast<XMVECTOR*>(_aligned_malloc((sizeof(XMVECTOR)*srcImage.width), 16)));
if (!scanline)
return E_OUTOFMEMORY;
const uint8_t *pSrc = srcImage.pixels;
uint8_t *pDest = destImage.pixels;
if (!pSrc || !pDest)
return E_POINTER;
for (size_t h = 0; h < srcImage.height; ++h)
{
if (!_LoadScanline(scanline.get(), srcImage.width, pSrc, srcImage.rowPitch, srcImage.format))
return E_FAIL;
XMVECTOR* ptr = scanline.get();
for (size_t w = 0; w < srcImage.width; ++w)
{
XMVECTOR v = *ptr;
XMVECTOR alpha = XMVectorSplatW(*ptr);
alpha = XMVectorDivide(v, alpha);
*(ptr++) = XMVectorSelect(v, alpha, g_XMSelect1110);
}
if (!_StoreScanline(pDest, destImage.rowPitch, destImage.format, scanline.get(), srcImage.width))
return E_FAIL;
pSrc += srcImage.rowPitch;
pDest += destImage.rowPitch;
}
return S_OK;
}
HRESULT DemultiplyAlphaLinear(const Image& srcImage, DWORD flags, const Image& destImage)
{
assert(srcImage.width == destImage.width);
assert(srcImage.height == destImage.height);
2017-07-12 07:56:51 +00:00
static_assert(static_cast<int>(TEX_PMALPHA_SRGB_IN) == static_cast<int>(TEX_FILTER_SRGB_IN), "TEX_PMALHPA_SRGB* should match TEX_FILTER_SRGB*");
static_assert(static_cast<int>(TEX_PMALPHA_SRGB_OUT) == static_cast<int>(TEX_FILTER_SRGB_OUT), "TEX_PMALHPA_SRGB* should match TEX_FILTER_SRGB*");
static_assert(static_cast<int>(TEX_PMALPHA_SRGB) == static_cast<int>(TEX_FILTER_SRGB), "TEX_PMALHPA_SRGB* should match TEX_FILTER_SRGB*");
flags &= TEX_PMALPHA_SRGB;
2018-03-29 19:00:16 +00:00
ScopedAlignedArrayXMVECTOR scanline(static_cast<XMVECTOR*>(_aligned_malloc((sizeof(XMVECTOR)*srcImage.width), 16)));
if (!scanline)
return E_OUTOFMEMORY;
const uint8_t *pSrc = srcImage.pixels;
uint8_t *pDest = destImage.pixels;
if (!pSrc || !pDest)
return E_POINTER;
for (size_t h = 0; h < srcImage.height; ++h)
{
if (!_LoadScanlineLinear(scanline.get(), srcImage.width, pSrc, srcImage.rowPitch, srcImage.format, flags))
return E_FAIL;
XMVECTOR* ptr = scanline.get();
for (size_t w = 0; w < srcImage.width; ++w)
{
XMVECTOR v = *ptr;
XMVECTOR alpha = XMVectorSplatW(*ptr);
alpha = XMVectorDivide(v, alpha);
*(ptr++) = XMVectorSelect(v, alpha, g_XMSelect1110);
}
if (!_StoreScanlineLinear(pDest, destImage.rowPitch, destImage.format, scanline.get(), srcImage.width, flags))
return E_FAIL;
pSrc += srcImage.rowPitch;
pDest += destImage.rowPitch;
}
return S_OK;
}
2016-08-22 18:26:36 +00:00
}
//=====================================================================================
// Entry-points
//=====================================================================================
//-------------------------------------------------------------------------------------
// Converts to/from a premultiplied alpha version of the texture
2016-08-22 18:26:36 +00:00
//-------------------------------------------------------------------------------------
_Use_decl_annotations_
2016-09-09 02:09:46 +00:00
HRESULT DirectX::PremultiplyAlpha(
const Image& srcImage,
DWORD flags,
2016-09-14 07:21:44 +00:00
ScratchImage& image)
2016-08-22 18:26:36 +00:00
{
2016-09-09 02:09:46 +00:00
if (!srcImage.pixels)
2016-08-22 18:26:36 +00:00
return E_POINTER;
2016-09-09 02:09:46 +00:00
if (IsCompressed(srcImage.format)
|| IsPlanar(srcImage.format)
|| IsPalettized(srcImage.format)
|| IsTypeless(srcImage.format)
|| !HasAlpha(srcImage.format))
return HRESULT_FROM_WIN32(ERROR_NOT_SUPPORTED);
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
if ((srcImage.width > UINT32_MAX) || (srcImage.height > UINT32_MAX))
2016-08-22 18:26:36 +00:00
return E_INVALIDARG;
2016-09-09 02:09:46 +00:00
HRESULT hr = image.Initialize2D(srcImage.format, srcImage.width, srcImage.height, 1, 1);
if (FAILED(hr))
2016-08-22 18:26:36 +00:00
return hr;
2016-09-09 02:09:46 +00:00
const Image *rimage = image.GetImage(0, 0, 0);
if (!rimage)
2016-08-22 18:26:36 +00:00
{
image.Release();
return E_POINTER;
}
2016-09-14 07:21:44 +00:00
if (flags & TEX_PMALPHA_REVERSE)
{
hr = (flags & TEX_PMALPHA_IGNORE_SRGB) ? DemultiplyAlpha(srcImage, *rimage) : DemultiplyAlphaLinear(srcImage, flags, *rimage);
}
else
{
hr = (flags & TEX_PMALPHA_IGNORE_SRGB) ? PremultiplyAlpha_(srcImage, *rimage) : PremultiplyAlphaLinear(srcImage, flags, *rimage);
}
2016-09-09 02:09:46 +00:00
if (FAILED(hr))
2016-08-22 18:26:36 +00:00
{
image.Release();
return hr;
}
return S_OK;
}
//-------------------------------------------------------------------------------------
// Converts to/from a premultiplied alpha version of the texture (complex)
2016-08-22 18:26:36 +00:00
//-------------------------------------------------------------------------------------
_Use_decl_annotations_
2016-09-09 02:09:46 +00:00
HRESULT DirectX::PremultiplyAlpha(
const Image* srcImages,
size_t nimages,
const TexMetadata& metadata,
DWORD flags,
2016-09-14 07:21:44 +00:00
ScratchImage& result)
2016-08-22 18:26:36 +00:00
{
2016-09-09 02:09:46 +00:00
if (!srcImages || !nimages)
2016-08-22 18:26:36 +00:00
return E_INVALIDARG;
2016-09-09 02:09:46 +00:00
if (IsCompressed(metadata.format)
|| IsPlanar(metadata.format)
|| IsPalettized(metadata.format)
|| IsTypeless(metadata.format)
|| !HasAlpha(metadata.format))
return HRESULT_FROM_WIN32(ERROR_NOT_SUPPORTED);
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
if ((metadata.width > UINT32_MAX) || (metadata.height > UINT32_MAX))
2016-08-22 18:26:36 +00:00
return E_INVALIDARG;
2016-09-14 07:21:44 +00:00
if (metadata.IsPMAlpha() != ((flags & TEX_PMALPHA_REVERSE) != 0))
2016-08-22 18:26:36 +00:00
return E_FAIL;
TexMetadata mdata2 = metadata;
2016-09-14 07:21:44 +00:00
mdata2.SetAlphaMode((flags & TEX_PMALPHA_REVERSE) ? TEX_ALPHA_MODE_STRAIGHT : TEX_ALPHA_MODE_PREMULTIPLIED);
2016-09-09 02:09:46 +00:00
HRESULT hr = result.Initialize(mdata2);
if (FAILED(hr))
2016-08-22 18:26:36 +00:00
return hr;
2016-09-09 02:09:46 +00:00
if (nimages != result.GetImageCount())
2016-08-22 18:26:36 +00:00
{
result.Release();
return E_FAIL;
}
const Image* dest = result.GetImages();
2016-09-09 02:09:46 +00:00
if (!dest)
2016-08-22 18:26:36 +00:00
{
result.Release();
return E_POINTER;
}
2016-09-09 02:09:46 +00:00
for (size_t index = 0; index < nimages; ++index)
2016-08-22 18:26:36 +00:00
{
2016-09-09 02:09:46 +00:00
const Image& src = srcImages[index];
if (src.format != metadata.format)
2016-08-22 18:26:36 +00:00
{
result.Release();
return E_FAIL;
}
2016-09-09 02:09:46 +00:00
if ((src.width > UINT32_MAX) || (src.height > UINT32_MAX))
2016-08-22 18:26:36 +00:00
return E_FAIL;
2016-09-01 08:33:23 +00:00
2016-09-09 02:09:46 +00:00
const Image& dst = dest[index];
assert(dst.format == metadata.format);
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
if (src.width != dst.width || src.height != dst.height)
2016-08-22 18:26:36 +00:00
{
result.Release();
return E_FAIL;
}
2016-09-14 07:21:44 +00:00
if (flags & TEX_PMALPHA_REVERSE)
{
hr = (flags & TEX_PMALPHA_IGNORE_SRGB) ? DemultiplyAlpha(src, dst) : DemultiplyAlphaLinear(src, flags, dst);
}
else
{
hr = (flags & TEX_PMALPHA_IGNORE_SRGB) ? PremultiplyAlpha_(src, dst) : PremultiplyAlphaLinear(src, flags, dst);
}
2016-09-09 02:09:46 +00:00
if (FAILED(hr))
2016-08-22 18:26:36 +00:00
{
result.Release();
return hr;
}
}
return S_OK;
}