2016-08-22 18:26:36 +00:00
|
|
|
//-------------------------------------------------------------------------------------
|
|
|
|
// DirectXTexResize.cpp
|
|
|
|
//
|
|
|
|
// DirectX Texture Library - Image resizing 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
|
|
|
|
//-------------------------------------------------------------------------------------
|
|
|
|
|
2019-05-25 23:00:32 +00:00
|
|
|
#include "DirectXTexP.h"
|
2016-08-22 18:26:36 +00:00
|
|
|
|
|
|
|
#include "filters.h"
|
|
|
|
|
2016-09-09 02:09:46 +00:00
|
|
|
using namespace DirectX;
|
2020-09-12 00:21:56 +00:00
|
|
|
#if !defined(_DXTX_NOWIN)
|
2016-08-22 18:26:36 +00:00
|
|
|
using Microsoft::WRL::ComPtr;
|
2020-09-12 00:21:56 +00:00
|
|
|
#endif
|
2016-08-22 18:26:36 +00:00
|
|
|
|
|
|
|
namespace DirectX
|
|
|
|
{
|
2020-09-12 00:21:56 +00:00
|
|
|
#if !defined(_DXTX_NOWIN)
|
2016-09-09 02:09:46 +00:00
|
|
|
extern HRESULT _ResizeSeparateColorAndAlpha(_In_ IWICImagingFactory* pWIC, _In_ bool iswic2, _In_ IWICBitmap* original,
|
2020-06-01 07:42:02 +00:00
|
|
|
_In_ size_t newWidth, _In_ size_t newHeight, _In_ TEX_FILTER_FLAGS filter, _Inout_ const Image* img) noexcept;
|
2020-09-12 00:21:56 +00:00
|
|
|
#endif
|
2016-09-09 02:09:46 +00:00
|
|
|
}
|
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
|
|
|
{
|
2020-09-12 00:21:56 +00:00
|
|
|
#if !defined(_DXTX_NOWIN)
|
2016-09-09 02:09:46 +00:00
|
|
|
//--- Do image resize using WIC ---
|
|
|
|
HRESULT PerformResizeUsingWIC(
|
|
|
|
const Image& srcImage,
|
2020-06-01 07:42:02 +00:00
|
|
|
TEX_FILTER_FLAGS filter,
|
2016-09-09 02:09:46 +00:00
|
|
|
const WICPixelFormatGUID& pfGUID,
|
2019-12-13 08:01:17 +00:00
|
|
|
const Image& destImage) noexcept
|
2016-09-09 02:09:46 +00:00
|
|
|
{
|
|
|
|
if (!srcImage.pixels || !destImage.pixels)
|
|
|
|
return E_POINTER;
|
2016-08-22 18:26:36 +00:00
|
|
|
|
2016-09-09 02:09:46 +00:00
|
|
|
assert(srcImage.format == destImage.format);
|
2016-08-22 18:26:36 +00:00
|
|
|
|
2016-09-09 02:09:46 +00:00
|
|
|
bool iswic2 = false;
|
2020-01-24 16:55:54 +00:00
|
|
|
auto pWIC = GetWICFactory(iswic2);
|
2016-09-09 02:09:46 +00:00
|
|
|
if (!pWIC)
|
|
|
|
return E_NOINTERFACE;
|
2016-08-22 18:26:36 +00:00
|
|
|
|
2016-09-09 02:09:46 +00:00
|
|
|
ComPtr<IWICComponentInfo> componentInfo;
|
|
|
|
HRESULT hr = pWIC->CreateComponentInfo(pfGUID, componentInfo.GetAddressOf());
|
|
|
|
if (FAILED(hr))
|
2016-08-22 18:26:36 +00:00
|
|
|
return hr;
|
2016-09-09 02:09:46 +00:00
|
|
|
|
|
|
|
ComPtr<IWICPixelFormatInfo2> pixelFormatInfo;
|
|
|
|
hr = componentInfo.As(&pixelFormatInfo);
|
|
|
|
if (FAILED(hr))
|
2016-08-22 18:26:36 +00:00
|
|
|
return hr;
|
|
|
|
|
2016-09-09 02:09:46 +00:00
|
|
|
BOOL supportsTransparency = FALSE;
|
|
|
|
hr = pixelFormatInfo->SupportsTransparency(&supportsTransparency);
|
|
|
|
if (FAILED(hr))
|
2016-08-22 18:26:36 +00:00
|
|
|
return hr;
|
|
|
|
|
2018-07-27 08:02:56 +00:00
|
|
|
if (srcImage.rowPitch > UINT32_MAX || srcImage.slicePitch > UINT32_MAX
|
|
|
|
|| destImage.rowPitch > UINT32_MAX || destImage.slicePitch > UINT32_MAX)
|
|
|
|
return HRESULT_FROM_WIN32(ERROR_ARITHMETIC_OVERFLOW);
|
|
|
|
|
2016-09-09 02:09:46 +00:00
|
|
|
ComPtr<IWICBitmap> source;
|
|
|
|
hr = pWIC->CreateBitmapFromMemory(static_cast<UINT>(srcImage.width), static_cast<UINT>(srcImage.height), pfGUID,
|
|
|
|
static_cast<UINT>(srcImage.rowPitch), static_cast<UINT>(srcImage.slicePitch),
|
|
|
|
srcImage.pixels, source.GetAddressOf());
|
|
|
|
if (FAILED(hr))
|
2016-08-22 18:26:36 +00:00
|
|
|
return hr;
|
|
|
|
|
2016-09-09 02:09:46 +00:00
|
|
|
if ((filter & TEX_FILTER_SEPARATE_ALPHA) && supportsTransparency)
|
2016-08-22 18:26:36 +00:00
|
|
|
{
|
2016-09-09 02:09:46 +00:00
|
|
|
hr = _ResizeSeparateColorAndAlpha(pWIC, iswic2, source.Get(), destImage.width, destImage.height, filter, &destImage);
|
|
|
|
if (FAILED(hr))
|
2016-08-22 18:26:36 +00:00
|
|
|
return hr;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-09-09 02:09:46 +00:00
|
|
|
ComPtr<IWICBitmapScaler> scaler;
|
|
|
|
hr = pWIC->CreateBitmapScaler(scaler.GetAddressOf());
|
|
|
|
if (FAILED(hr))
|
2016-08-22 18:26:36 +00:00
|
|
|
return hr;
|
|
|
|
|
2016-09-09 02:09:46 +00:00
|
|
|
hr = scaler->Initialize(source.Get(), static_cast<UINT>(destImage.width), static_cast<UINT>(destImage.height), _GetWICInterp(filter));
|
|
|
|
if (FAILED(hr))
|
2016-08-22 18:26:36 +00:00
|
|
|
return hr;
|
|
|
|
|
2016-09-09 02:09:46 +00:00
|
|
|
WICPixelFormatGUID pfScaler;
|
|
|
|
hr = scaler->GetPixelFormat(&pfScaler);
|
|
|
|
if (FAILED(hr))
|
2016-08-22 18:26:36 +00:00
|
|
|
return hr;
|
|
|
|
|
2016-09-09 02:09:46 +00:00
|
|
|
if (memcmp(&pfScaler, &pfGUID, sizeof(WICPixelFormatGUID)) == 0)
|
|
|
|
{
|
2018-06-11 19:22:52 +00:00
|
|
|
hr = scaler->CopyPixels(nullptr, static_cast<UINT>(destImage.rowPitch), static_cast<UINT>(destImage.slicePitch), destImage.pixels);
|
2016-09-09 02:09:46 +00:00
|
|
|
if (FAILED(hr))
|
|
|
|
return hr;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// The WIC bitmap scaler is free to return a different pixel format than the source image, so here we
|
|
|
|
// convert it back
|
|
|
|
ComPtr<IWICFormatConverter> FC;
|
|
|
|
hr = pWIC->CreateFormatConverter(FC.GetAddressOf());
|
|
|
|
if (FAILED(hr))
|
|
|
|
return hr;
|
|
|
|
|
|
|
|
BOOL canConvert = FALSE;
|
|
|
|
hr = FC->CanConvert(pfScaler, pfGUID, &canConvert);
|
|
|
|
if (FAILED(hr) || !canConvert)
|
|
|
|
{
|
|
|
|
return E_UNEXPECTED;
|
|
|
|
}
|
2016-08-22 18:26:36 +00:00
|
|
|
|
2017-07-03 23:04:01 +00:00
|
|
|
hr = FC->Initialize(scaler.Get(), pfGUID, _GetWICDither(filter), nullptr, 0, WICBitmapPaletteTypeMedianCut);
|
2016-09-09 02:09:46 +00:00
|
|
|
if (FAILED(hr))
|
|
|
|
return hr;
|
2016-08-22 18:26:36 +00:00
|
|
|
|
2018-06-11 19:22:52 +00:00
|
|
|
hr = FC->CopyPixels(nullptr, static_cast<UINT>(destImage.rowPitch), static_cast<UINT>(destImage.slicePitch), destImage.pixels);
|
2016-09-09 02:09:46 +00:00
|
|
|
if (FAILED(hr))
|
|
|
|
return hr;
|
|
|
|
}
|
|
|
|
}
|
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
|
|
|
//--- Do conversion, resize using WIC, conversion cycle ---
|
|
|
|
HRESULT PerformResizeViaF32(
|
|
|
|
const Image& srcImage,
|
2020-06-01 07:42:02 +00:00
|
|
|
TEX_FILTER_FLAGS filter,
|
2019-12-13 08:01:17 +00:00
|
|
|
const Image& destImage) noexcept
|
2016-09-09 02:09:46 +00:00
|
|
|
{
|
|
|
|
if (!srcImage.pixels || !destImage.pixels)
|
|
|
|
return E_POINTER;
|
2016-08-22 18:26:36 +00:00
|
|
|
|
2016-09-09 02:09:46 +00:00
|
|
|
assert(srcImage.format != DXGI_FORMAT_R32G32B32A32_FLOAT);
|
|
|
|
assert(srcImage.format == destImage.format);
|
2016-08-22 18:26:36 +00:00
|
|
|
|
2016-09-09 02:09:46 +00:00
|
|
|
ScratchImage temp;
|
|
|
|
HRESULT hr = _ConvertToR32G32B32A32(srcImage, temp);
|
|
|
|
if (FAILED(hr))
|
|
|
|
return hr;
|
2016-08-22 18:26:36 +00:00
|
|
|
|
2016-09-09 02:09:46 +00:00
|
|
|
const Image *tsrc = temp.GetImage(0, 0, 0);
|
|
|
|
if (!tsrc)
|
|
|
|
return E_POINTER;
|
2016-08-22 18:26:36 +00:00
|
|
|
|
2016-09-09 02:09:46 +00:00
|
|
|
ScratchImage rtemp;
|
|
|
|
hr = rtemp.Initialize2D(DXGI_FORMAT_R32G32B32A32_FLOAT, destImage.width, destImage.height, 1, 1);
|
|
|
|
if (FAILED(hr))
|
|
|
|
return hr;
|
2016-08-22 18:26:36 +00:00
|
|
|
|
2016-09-09 02:09:46 +00:00
|
|
|
const Image *tdest = rtemp.GetImage(0, 0, 0);
|
|
|
|
if (!tdest)
|
|
|
|
return E_POINTER;
|
2016-08-22 18:26:36 +00:00
|
|
|
|
2016-09-09 02:09:46 +00:00
|
|
|
hr = PerformResizeUsingWIC(*tsrc, filter, GUID_WICPixelFormat128bppRGBAFloat, *tdest);
|
|
|
|
if (FAILED(hr))
|
|
|
|
return hr;
|
2016-08-22 18:26:36 +00:00
|
|
|
|
2016-09-09 02:09:46 +00:00
|
|
|
temp.Release();
|
2016-08-22 18:26:36 +00:00
|
|
|
|
2016-09-09 02:09:46 +00:00
|
|
|
hr = _ConvertFromR32G32B32A32(*tdest, destImage);
|
|
|
|
if (FAILED(hr))
|
|
|
|
return hr;
|
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
|
|
|
//--- determine when to use WIC vs. non-WIC paths ---
|
2020-06-01 07:42:02 +00:00
|
|
|
bool UseWICFiltering(_In_ DXGI_FORMAT format, _In_ TEX_FILTER_FLAGS filter) noexcept
|
2016-08-22 18:26:36 +00:00
|
|
|
{
|
2016-09-09 02:09:46 +00:00
|
|
|
if (filter & TEX_FILTER_FORCE_NON_WIC)
|
2016-08-22 18:26:36 +00:00
|
|
|
{
|
2016-09-09 02:09:46 +00:00
|
|
|
// Explicit flag indicates use of non-WIC code paths
|
2016-08-22 18:26:36 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-09-09 02:09:46 +00:00
|
|
|
if (filter & TEX_FILTER_FORCE_WIC)
|
|
|
|
{
|
|
|
|
// Explicit flag to use WIC code paths, skips all the case checks below
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (IsSRGB(format) || (filter & TEX_FILTER_SRGB))
|
2016-08-22 18:26:36 +00:00
|
|
|
{
|
2016-09-09 02:09:46 +00:00
|
|
|
// Use non-WIC code paths for sRGB correct filtering
|
2016-08-22 18:26:36 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-08-12 21:35:39 +00:00
|
|
|
#if (defined(_XBOX_ONE) && defined(_TITLE)) || defined(_GAMING_XBOX)
|
2016-09-09 02:09:46 +00:00
|
|
|
if (format == DXGI_FORMAT_R16G16B16A16_FLOAT
|
|
|
|
|| format == DXGI_FORMAT_R16_FLOAT)
|
2016-08-22 18:26:36 +00:00
|
|
|
{
|
2020-08-12 21:35:39 +00:00
|
|
|
// Use non-WIC code paths as these conversions are not supported by Xbox version of WIC
|
2016-08-22 18:26:36 +00:00
|
|
|
return false;
|
|
|
|
}
|
2016-09-09 02:09:46 +00:00
|
|
|
#endif
|
2016-08-22 18:26:36 +00:00
|
|
|
|
2016-09-09 02:09:46 +00:00
|
|
|
static_assert(TEX_FILTER_POINT == 0x100000, "TEX_FILTER_ flag values don't match TEX_FILTER_MASK");
|
|
|
|
|
2020-06-01 07:42:02 +00:00
|
|
|
switch (filter & TEX_FILTER_MODE_MASK)
|
2016-08-22 18:26:36 +00:00
|
|
|
{
|
2016-09-09 02:09:46 +00:00
|
|
|
case TEX_FILTER_LINEAR:
|
|
|
|
if (filter & TEX_FILTER_WRAP)
|
|
|
|
{
|
|
|
|
// WIC only supports 'clamp' semantics (MIRROR is equivalent to clamp for linear)
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (BitsPerColor(format) > 8)
|
|
|
|
{
|
|
|
|
// Avoid the WIC bitmap scaler when doing Linear filtering of XR/HDR formats
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TEX_FILTER_CUBIC:
|
|
|
|
if (filter & (TEX_FILTER_WRAP | TEX_FILTER_MIRROR))
|
|
|
|
{
|
|
|
|
// WIC only supports 'clamp' semantics
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (BitsPerColor(format) > 8)
|
|
|
|
{
|
|
|
|
// Avoid the WIC bitmap scaler when doing Cubic filtering of XR/HDR formats
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TEX_FILTER_TRIANGLE:
|
|
|
|
// WIC does not implement this filter
|
2016-08-22 18:26:36 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-09-09 02:09:46 +00:00
|
|
|
return true;
|
2016-08-22 18:26:36 +00:00
|
|
|
}
|
2020-09-12 00:21:56 +00:00
|
|
|
#endif
|
2016-08-22 18:26:36 +00:00
|
|
|
|
2016-09-09 02:09:46 +00:00
|
|
|
//-------------------------------------------------------------------------------------
|
|
|
|
// Resize custom filters
|
|
|
|
//-------------------------------------------------------------------------------------
|
2016-08-22 18:26:36 +00:00
|
|
|
|
2016-09-09 02:09:46 +00:00
|
|
|
//--- Point Filter ---
|
2019-12-13 08:01:17 +00:00
|
|
|
HRESULT ResizePointFilter(const Image& srcImage, const Image& destImage) noexcept
|
2016-09-09 02:09:46 +00:00
|
|
|
{
|
|
|
|
assert(srcImage.pixels && destImage.pixels);
|
|
|
|
assert(srcImage.format == destImage.format);
|
2016-08-22 18:26:36 +00:00
|
|
|
|
2016-09-09 02:09:46 +00:00
|
|
|
// Allocate temporary space (2 scanlines)
|
2020-09-12 00:21:56 +00:00
|
|
|
ScopedAlignedArrayXMVECTOR scanline(static_cast<XMVECTOR*>(AllocateVectorAligned(
|
|
|
|
(sizeof(XMVECTOR) * (srcImage.width + destImage.width)))));
|
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
|
|
|
XMVECTOR* target = scanline.get();
|
2016-08-22 18:26:36 +00:00
|
|
|
|
2016-09-09 02:09:46 +00:00
|
|
|
XMVECTOR* row = target + destImage.width;
|
2016-08-22 18:26:36 +00:00
|
|
|
|
|
|
|
#ifdef _DEBUG
|
2016-09-09 02:09:46 +00:00
|
|
|
memset(row, 0xCD, sizeof(XMVECTOR)*srcImage.width);
|
2016-08-22 18:26:36 +00:00
|
|
|
#endif
|
|
|
|
|
2016-09-09 02:09:46 +00:00
|
|
|
const uint8_t* pSrc = srcImage.pixels;
|
|
|
|
uint8_t* pDest = destImage.pixels;
|
2016-08-22 18:26:36 +00:00
|
|
|
|
2016-09-09 02:09:46 +00:00
|
|
|
size_t rowPitch = srcImage.rowPitch;
|
2016-08-22 18:26:36 +00:00
|
|
|
|
2016-09-09 02:09:46 +00:00
|
|
|
size_t xinc = (srcImage.width << 16) / destImage.width;
|
|
|
|
size_t yinc = (srcImage.height << 16) / destImage.height;
|
2016-08-22 18:26:36 +00:00
|
|
|
|
2016-09-09 02:09:46 +00:00
|
|
|
size_t lasty = size_t(-1);
|
2016-08-22 18:26:36 +00:00
|
|
|
|
2016-09-09 02:09:46 +00:00
|
|
|
size_t sy = 0;
|
|
|
|
for (size_t y = 0; y < destImage.height; ++y)
|
2016-08-22 18:26:36 +00:00
|
|
|
{
|
2016-09-09 02:09:46 +00:00
|
|
|
if ((lasty ^ sy) >> 16)
|
|
|
|
{
|
|
|
|
if (!_LoadScanline(row, srcImage.width, pSrc + (rowPitch * (sy >> 16)), rowPitch, srcImage.format))
|
|
|
|
return E_FAIL;
|
|
|
|
lasty = sy;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t sx = 0;
|
|
|
|
for (size_t x = 0; x < destImage.width; ++x)
|
|
|
|
{
|
|
|
|
target[x] = row[sx >> 16];
|
|
|
|
sx += xinc;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!_StoreScanline(pDest, destImage.rowPitch, destImage.format, target, destImage.width))
|
2016-08-22 18:26:36 +00:00
|
|
|
return E_FAIL;
|
2016-09-09 02:09:46 +00:00
|
|
|
pDest += destImage.rowPitch;
|
2016-08-22 18:26:36 +00:00
|
|
|
|
2016-09-09 02:09:46 +00:00
|
|
|
sy += yinc;
|
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
|
|
|
//--- Box Filter ---
|
2020-06-01 07:42:02 +00:00
|
|
|
HRESULT ResizeBoxFilter(const Image& srcImage, TEX_FILTER_FLAGS filter, const Image& destImage) noexcept
|
2016-09-09 02:09:46 +00:00
|
|
|
{
|
|
|
|
assert(srcImage.pixels && destImage.pixels);
|
|
|
|
assert(srcImage.format == destImage.format);
|
2016-08-22 18:26:36 +00:00
|
|
|
|
2016-09-09 02:09:46 +00:00
|
|
|
if (((destImage.width << 1) != srcImage.width) || ((destImage.height << 1) != srcImage.height))
|
|
|
|
return E_FAIL;
|
2016-08-22 18:26:36 +00:00
|
|
|
|
2016-09-09 02:09:46 +00:00
|
|
|
// Allocate temporary space (3 scanlines)
|
2020-09-12 00:21:56 +00:00
|
|
|
ScopedAlignedArrayXMVECTOR scanline(static_cast<XMVECTOR*>(AllocateVectorAligned(
|
|
|
|
(sizeof(XMVECTOR) * (srcImage.width * 2 + destImage.width)))));
|
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
|
|
|
XMVECTOR* target = scanline.get();
|
2016-08-22 18:26:36 +00:00
|
|
|
|
2016-09-09 02:09:46 +00:00
|
|
|
XMVECTOR* urow0 = target + destImage.width;
|
|
|
|
XMVECTOR* urow1 = urow0 + srcImage.width;
|
2016-08-22 18:26:36 +00:00
|
|
|
|
|
|
|
#ifdef _DEBUG
|
2016-09-09 02:09:46 +00:00
|
|
|
memset(urow0, 0xCD, sizeof(XMVECTOR)*srcImage.width);
|
|
|
|
memset(urow1, 0xDD, sizeof(XMVECTOR)*srcImage.width);
|
2016-08-22 18:26:36 +00:00
|
|
|
#endif
|
|
|
|
|
2016-09-09 02:09:46 +00:00
|
|
|
const XMVECTOR* urow2 = urow0 + 1;
|
|
|
|
const XMVECTOR* urow3 = urow1 + 1;
|
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;
|
2016-08-22 18:26:36 +00:00
|
|
|
|
2016-09-09 02:09:46 +00:00
|
|
|
size_t rowPitch = srcImage.rowPitch;
|
2016-08-22 18:26:36 +00:00
|
|
|
|
2016-09-09 02:09:46 +00:00
|
|
|
for (size_t y = 0; y < destImage.height; ++y)
|
2016-08-22 18:26:36 +00:00
|
|
|
{
|
2016-09-09 02:09:46 +00:00
|
|
|
if (!_LoadScanlineLinear(urow0, srcImage.width, pSrc, rowPitch, srcImage.format, filter))
|
2016-08-22 18:26:36 +00:00
|
|
|
return E_FAIL;
|
|
|
|
pSrc += rowPitch;
|
|
|
|
|
2016-09-09 02:09:46 +00:00
|
|
|
if (urow0 != urow1)
|
|
|
|
{
|
|
|
|
if (!_LoadScanlineLinear(urow1, srcImage.width, pSrc, rowPitch, srcImage.format, filter))
|
|
|
|
return E_FAIL;
|
|
|
|
pSrc += rowPitch;
|
|
|
|
}
|
2016-08-22 18:26:36 +00:00
|
|
|
|
2016-09-09 02:09:46 +00:00
|
|
|
for (size_t x = 0; x < destImage.width; ++x)
|
|
|
|
{
|
|
|
|
size_t x2 = x << 1;
|
|
|
|
|
2019-06-26 19:47:34 +00:00
|
|
|
AVERAGE4(target[x], urow0[x2], urow1[x2], urow2[x2], urow3[x2])
|
2016-09-09 02:09:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!_StoreScanlineLinear(pDest, destImage.rowPitch, destImage.format, target, destImage.width, filter))
|
|
|
|
return E_FAIL;
|
|
|
|
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
|
|
|
//--- Linear Filter ---
|
2020-06-01 07:42:02 +00:00
|
|
|
HRESULT ResizeLinearFilter(const Image& srcImage, TEX_FILTER_FLAGS filter, const Image& destImage) noexcept
|
2016-09-09 02:09:46 +00:00
|
|
|
{
|
|
|
|
assert(srcImage.pixels && destImage.pixels);
|
|
|
|
assert(srcImage.format == destImage.format);
|
2016-08-22 18:26:36 +00:00
|
|
|
|
2016-09-09 02:09:46 +00:00
|
|
|
// Allocate temporary space (3 scanlines, plus X and Y filters)
|
2020-09-12 00:21:56 +00:00
|
|
|
ScopedAlignedArrayXMVECTOR scanline(static_cast<XMVECTOR*>(AllocateVectorAligned(
|
|
|
|
(sizeof(XMVECTOR) * (srcImage.width * 2 + destImage.width)))));
|
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
|
|
|
std::unique_ptr<LinearFilter[]> lf(new (std::nothrow) LinearFilter[destImage.width + destImage.height]);
|
|
|
|
if (!lf)
|
|
|
|
return E_OUTOFMEMORY;
|
2016-08-22 18:26:36 +00:00
|
|
|
|
2016-09-09 02:09:46 +00:00
|
|
|
LinearFilter* lfX = lf.get();
|
|
|
|
LinearFilter* lfY = lf.get() + destImage.width;
|
2016-08-22 18:26:36 +00:00
|
|
|
|
2016-09-09 02:09:46 +00:00
|
|
|
_CreateLinearFilter(srcImage.width, destImage.width, (filter & TEX_FILTER_WRAP_U) != 0, lfX);
|
|
|
|
_CreateLinearFilter(srcImage.height, destImage.height, (filter & TEX_FILTER_WRAP_V) != 0, lfY);
|
2016-08-22 18:26:36 +00:00
|
|
|
|
2016-09-09 02:09:46 +00:00
|
|
|
XMVECTOR* target = scanline.get();
|
2016-08-22 18:26:36 +00:00
|
|
|
|
2016-09-09 02:09:46 +00:00
|
|
|
XMVECTOR* row0 = target + destImage.width;
|
|
|
|
XMVECTOR* row1 = row0 + srcImage.width;
|
2016-08-22 18:26:36 +00:00
|
|
|
|
|
|
|
#ifdef _DEBUG
|
2016-09-09 02:09:46 +00:00
|
|
|
memset(row0, 0xCD, sizeof(XMVECTOR)*srcImage.width);
|
|
|
|
memset(row1, 0xDD, sizeof(XMVECTOR)*srcImage.width);
|
2016-08-22 18:26:36 +00:00
|
|
|
#endif
|
|
|
|
|
2016-09-09 02:09:46 +00:00
|
|
|
const uint8_t* pSrc = srcImage.pixels;
|
|
|
|
uint8_t* pDest = destImage.pixels;
|
2016-08-22 18:26:36 +00:00
|
|
|
|
2016-09-09 02:09:46 +00:00
|
|
|
size_t rowPitch = srcImage.rowPitch;
|
2016-08-22 18:26:36 +00:00
|
|
|
|
2016-09-09 02:09:46 +00:00
|
|
|
size_t u0 = size_t(-1);
|
|
|
|
size_t u1 = size_t(-1);
|
2016-08-22 18:26:36 +00:00
|
|
|
|
2016-09-09 02:09:46 +00:00
|
|
|
for (size_t y = 0; y < destImage.height; ++y)
|
2016-08-22 18:26:36 +00:00
|
|
|
{
|
2016-09-09 02:09:46 +00:00
|
|
|
auto& toY = lfY[y];
|
|
|
|
|
|
|
|
if (toY.u0 != u0)
|
2016-08-22 18:26:36 +00:00
|
|
|
{
|
2016-09-09 02:09:46 +00:00
|
|
|
if (toY.u0 != u1)
|
|
|
|
{
|
|
|
|
u0 = toY.u0;
|
2016-08-22 18:26:36 +00:00
|
|
|
|
2016-09-09 02:09:46 +00:00
|
|
|
if (!_LoadScanlineLinear(row0, srcImage.width, pSrc + (rowPitch * u0), rowPitch, srcImage.format, filter))
|
|
|
|
return E_FAIL;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
u0 = u1;
|
|
|
|
u1 = size_t(-1);
|
|
|
|
|
|
|
|
std::swap(row0, row1);
|
|
|
|
}
|
2016-08-22 18:26:36 +00:00
|
|
|
}
|
2016-09-09 02:09:46 +00:00
|
|
|
|
|
|
|
if (toY.u1 != u1)
|
2016-08-22 18:26:36 +00:00
|
|
|
{
|
2016-09-09 02:09:46 +00:00
|
|
|
u1 = toY.u1;
|
2016-08-22 18:26:36 +00:00
|
|
|
|
2016-09-09 02:09:46 +00:00
|
|
|
if (!_LoadScanlineLinear(row1, srcImage.width, pSrc + (rowPitch * u1), rowPitch, srcImage.format, filter))
|
|
|
|
return E_FAIL;
|
2016-08-22 18:26:36 +00:00
|
|
|
}
|
|
|
|
|
2016-09-09 02:09:46 +00:00
|
|
|
for (size_t x = 0; x < destImage.width; ++x)
|
|
|
|
{
|
|
|
|
auto& toX = lfX[x];
|
2016-08-22 18:26:36 +00:00
|
|
|
|
2019-06-26 19:47:34 +00:00
|
|
|
BILINEAR_INTERPOLATE(target[x], toX, toY, row0, row1)
|
2016-09-09 02:09:46 +00:00
|
|
|
}
|
2016-08-22 18:26:36 +00:00
|
|
|
|
2016-09-09 02:09:46 +00:00
|
|
|
if (!_StoreScanlineLinear(pDest, destImage.rowPitch, destImage.format, target, destImage.width, filter))
|
|
|
|
return E_FAIL;
|
|
|
|
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
|
|
|
//--- Cubic Filter ---
|
2020-06-01 07:42:02 +00:00
|
|
|
HRESULT ResizeCubicFilter(const Image& srcImage, TEX_FILTER_FLAGS filter, const Image& destImage) noexcept
|
2016-09-09 02:09:46 +00:00
|
|
|
{
|
|
|
|
assert(srcImage.pixels && destImage.pixels);
|
|
|
|
assert(srcImage.format == destImage.format);
|
2016-08-22 18:26:36 +00:00
|
|
|
|
2016-09-09 02:09:46 +00:00
|
|
|
// Allocate temporary space (5 scanlines, plus X and Y filters)
|
2020-09-12 00:21:56 +00:00
|
|
|
ScopedAlignedArrayXMVECTOR scanline(static_cast<XMVECTOR*>(AllocateVectorAligned(
|
|
|
|
(sizeof(XMVECTOR) * (srcImage.width * 4 + destImage.width)))));
|
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
|
|
|
std::unique_ptr<CubicFilter[]> cf(new (std::nothrow) CubicFilter[destImage.width + destImage.height]);
|
|
|
|
if (!cf)
|
|
|
|
return E_OUTOFMEMORY;
|
2016-08-22 18:26:36 +00:00
|
|
|
|
2016-09-09 02:09:46 +00:00
|
|
|
CubicFilter* cfX = cf.get();
|
|
|
|
CubicFilter* cfY = cf.get() + destImage.width;
|
2016-08-22 18:26:36 +00:00
|
|
|
|
2016-09-09 02:09:46 +00:00
|
|
|
_CreateCubicFilter(srcImage.width, destImage.width, (filter & TEX_FILTER_WRAP_U) != 0, (filter & TEX_FILTER_MIRROR_U) != 0, cfX);
|
|
|
|
_CreateCubicFilter(srcImage.height, destImage.height, (filter & TEX_FILTER_WRAP_V) != 0, (filter & TEX_FILTER_MIRROR_V) != 0, cfY);
|
2016-08-22 18:26:36 +00:00
|
|
|
|
2016-09-09 02:09:46 +00:00
|
|
|
XMVECTOR* target = scanline.get();
|
2016-08-22 18:26:36 +00:00
|
|
|
|
2016-09-09 02:09:46 +00:00
|
|
|
XMVECTOR* row0 = target + destImage.width;
|
|
|
|
XMVECTOR* row1 = row0 + srcImage.width;
|
|
|
|
XMVECTOR* row2 = row0 + srcImage.width * 2;
|
|
|
|
XMVECTOR* row3 = row0 + srcImage.width * 3;
|
2016-08-22 18:26:36 +00:00
|
|
|
|
|
|
|
#ifdef _DEBUG
|
2016-09-09 02:09:46 +00:00
|
|
|
memset(row0, 0xCD, sizeof(XMVECTOR)*srcImage.width);
|
|
|
|
memset(row1, 0xDD, sizeof(XMVECTOR)*srcImage.width);
|
|
|
|
memset(row2, 0xED, sizeof(XMVECTOR)*srcImage.width);
|
|
|
|
memset(row3, 0xFD, sizeof(XMVECTOR)*srcImage.width);
|
2016-08-22 18:26:36 +00:00
|
|
|
#endif
|
|
|
|
|
2016-09-09 02:09:46 +00:00
|
|
|
const uint8_t* pSrc = srcImage.pixels;
|
|
|
|
uint8_t* pDest = destImage.pixels;
|
2016-08-22 18:26:36 +00:00
|
|
|
|
2016-09-09 02:09:46 +00:00
|
|
|
size_t rowPitch = srcImage.rowPitch;
|
2016-08-22 18:26:36 +00:00
|
|
|
|
2016-09-09 02:09:46 +00:00
|
|
|
size_t u0 = size_t(-1);
|
|
|
|
size_t u1 = size_t(-1);
|
|
|
|
size_t u2 = size_t(-1);
|
|
|
|
size_t u3 = size_t(-1);
|
2016-08-22 18:26:36 +00:00
|
|
|
|
2016-09-09 02:09:46 +00:00
|
|
|
for (size_t y = 0; y < destImage.height; ++y)
|
2016-08-22 18:26:36 +00:00
|
|
|
{
|
2016-09-09 02:09:46 +00:00
|
|
|
auto& toY = cfY[y];
|
2016-08-22 18:26:36 +00:00
|
|
|
|
2016-09-09 02:09:46 +00:00
|
|
|
// Scanline 1
|
|
|
|
if (toY.u0 != u0)
|
2016-08-22 18:26:36 +00:00
|
|
|
{
|
2016-09-09 02:09:46 +00:00
|
|
|
if (toY.u0 != u1 && toY.u0 != u2 && toY.u0 != u3)
|
|
|
|
{
|
|
|
|
u0 = toY.u0;
|
2016-08-22 18:26:36 +00:00
|
|
|
|
2016-09-09 02:09:46 +00:00
|
|
|
if (!_LoadScanlineLinear(row0, srcImage.width, pSrc + (rowPitch * u0), rowPitch, srcImage.format, filter))
|
|
|
|
return E_FAIL;
|
|
|
|
}
|
|
|
|
else if (toY.u0 == u1)
|
|
|
|
{
|
|
|
|
u0 = u1;
|
|
|
|
u1 = size_t(-1);
|
2016-08-22 18:26:36 +00:00
|
|
|
|
2016-09-09 02:09:46 +00:00
|
|
|
std::swap(row0, row1);
|
|
|
|
}
|
|
|
|
else if (toY.u0 == u2)
|
|
|
|
{
|
|
|
|
u0 = u2;
|
|
|
|
u2 = size_t(-1);
|
|
|
|
|
|
|
|
std::swap(row0, row2);
|
|
|
|
}
|
|
|
|
else if (toY.u0 == u3)
|
|
|
|
{
|
|
|
|
u0 = u3;
|
|
|
|
u3 = size_t(-1);
|
2016-08-22 18:26:36 +00:00
|
|
|
|
2016-09-09 02:09:46 +00:00
|
|
|
std::swap(row0, row3);
|
|
|
|
}
|
2016-08-22 18:26:36 +00:00
|
|
|
}
|
|
|
|
|
2016-09-09 02:09:46 +00:00
|
|
|
// Scanline 2
|
|
|
|
if (toY.u1 != u1)
|
2016-08-22 18:26:36 +00:00
|
|
|
{
|
2016-09-09 02:09:46 +00:00
|
|
|
if (toY.u1 != u2 && toY.u1 != u3)
|
|
|
|
{
|
|
|
|
u1 = toY.u1;
|
2016-08-22 18:26:36 +00:00
|
|
|
|
2016-09-09 02:09:46 +00:00
|
|
|
if (!_LoadScanlineLinear(row1, srcImage.width, pSrc + (rowPitch * u1), rowPitch, srcImage.format, filter))
|
|
|
|
return E_FAIL;
|
|
|
|
}
|
|
|
|
else if (toY.u1 == u2)
|
|
|
|
{
|
|
|
|
u1 = u2;
|
|
|
|
u2 = size_t(-1);
|
2016-08-22 18:26:36 +00:00
|
|
|
|
2016-09-09 02:09:46 +00:00
|
|
|
std::swap(row1, row2);
|
|
|
|
}
|
|
|
|
else if (toY.u1 == u3)
|
|
|
|
{
|
|
|
|
u1 = u3;
|
|
|
|
u3 = size_t(-1);
|
2016-08-22 18:26:36 +00:00
|
|
|
|
2016-09-09 02:09:46 +00:00
|
|
|
std::swap(row1, row3);
|
|
|
|
}
|
2016-08-22 18:26:36 +00:00
|
|
|
}
|
|
|
|
|
2016-09-09 02:09:46 +00:00
|
|
|
// Scanline 3
|
|
|
|
if (toY.u2 != u2)
|
2016-08-22 18:26:36 +00:00
|
|
|
{
|
2016-09-09 02:09:46 +00:00
|
|
|
if (toY.u2 != u3)
|
|
|
|
{
|
|
|
|
u2 = toY.u2;
|
2016-08-22 18:26:36 +00:00
|
|
|
|
2016-09-09 02:09:46 +00:00
|
|
|
if (!_LoadScanlineLinear(row2, srcImage.width, pSrc + (rowPitch * u2), rowPitch, srcImage.format, filter))
|
|
|
|
return E_FAIL;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
u2 = u3;
|
|
|
|
u3 = size_t(-1);
|
|
|
|
|
|
|
|
std::swap(row2, row3);
|
|
|
|
}
|
2016-08-22 18:26:36 +00:00
|
|
|
}
|
2016-09-09 02:09:46 +00:00
|
|
|
|
|
|
|
// Scanline 4
|
|
|
|
if (toY.u3 != u3)
|
2016-08-22 18:26:36 +00:00
|
|
|
{
|
2016-09-09 02:09:46 +00:00
|
|
|
u3 = toY.u3;
|
2016-08-22 18:26:36 +00:00
|
|
|
|
2016-09-09 02:09:46 +00:00
|
|
|
if (!_LoadScanlineLinear(row3, srcImage.width, pSrc + (rowPitch * u3), rowPitch, srcImage.format, filter))
|
|
|
|
return E_FAIL;
|
2016-08-22 18:26:36 +00:00
|
|
|
}
|
|
|
|
|
2016-09-09 02:09:46 +00:00
|
|
|
for (size_t x = 0; x < destImage.width; ++x)
|
|
|
|
{
|
|
|
|
auto& toX = cfX[x];
|
2016-08-22 18:26:36 +00:00
|
|
|
|
2016-09-09 02:09:46 +00:00
|
|
|
XMVECTOR C0, C1, C2, C3;
|
2016-08-22 18:26:36 +00:00
|
|
|
|
2019-06-26 19:47:34 +00:00
|
|
|
CUBIC_INTERPOLATE(C0, toX.x, row0[toX.u0], row0[toX.u1], row0[toX.u2], row0[toX.u3])
|
|
|
|
CUBIC_INTERPOLATE(C1, toX.x, row1[toX.u0], row1[toX.u1], row1[toX.u2], row1[toX.u3])
|
|
|
|
CUBIC_INTERPOLATE(C2, toX.x, row2[toX.u0], row2[toX.u1], row2[toX.u2], row2[toX.u3])
|
|
|
|
CUBIC_INTERPOLATE(C3, toX.x, row3[toX.u0], row3[toX.u1], row3[toX.u2], row3[toX.u3])
|
2016-08-22 18:26:36 +00:00
|
|
|
|
2019-06-26 19:47:34 +00:00
|
|
|
CUBIC_INTERPOLATE(target[x], toY.x, C0, C1, C2, C3)
|
2016-09-09 02:09:46 +00:00
|
|
|
}
|
2016-08-22 18:26:36 +00:00
|
|
|
|
2016-09-09 02:09:46 +00:00
|
|
|
if (!_StoreScanlineLinear(pDest, destImage.rowPitch, destImage.format, target, destImage.width, filter))
|
|
|
|
return E_FAIL;
|
|
|
|
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
|
|
|
//--- Triangle Filter ---
|
2020-06-01 07:42:02 +00:00
|
|
|
HRESULT ResizeTriangleFilter(const Image& srcImage, TEX_FILTER_FLAGS filter, const Image& destImage) noexcept
|
2016-09-09 02:09:46 +00:00
|
|
|
{
|
|
|
|
assert(srcImage.pixels && destImage.pixels);
|
|
|
|
assert(srcImage.format == destImage.format);
|
2016-08-22 18:26:36 +00:00
|
|
|
|
2016-09-09 02:09:46 +00:00
|
|
|
using namespace TriangleFilter;
|
2016-08-22 18:26:36 +00:00
|
|
|
|
2016-09-09 02:09:46 +00:00
|
|
|
// Allocate initial temporary space (1 scanline, accumulation rows, plus X and Y filters)
|
2020-09-12 00:21:56 +00:00
|
|
|
ScopedAlignedArrayXMVECTOR scanline(static_cast<XMVECTOR*>(AllocateVectorAligned(sizeof(XMVECTOR) * srcImage.width)));
|
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
|
|
|
std::unique_ptr<TriangleRow[]> rowActive(new (std::nothrow) TriangleRow[destImage.height]);
|
|
|
|
if (!rowActive)
|
|
|
|
return E_OUTOFMEMORY;
|
2016-08-22 18:26:36 +00:00
|
|
|
|
2016-09-09 02:09:46 +00:00
|
|
|
TriangleRow * rowFree = nullptr;
|
2016-08-22 18:26:36 +00:00
|
|
|
|
2016-09-09 02:09:46 +00:00
|
|
|
std::unique_ptr<Filter> tfX;
|
|
|
|
HRESULT hr = _Create(srcImage.width, destImage.width, (filter & TEX_FILTER_WRAP_U) != 0, tfX);
|
|
|
|
if (FAILED(hr))
|
|
|
|
return hr;
|
2016-08-22 18:26:36 +00:00
|
|
|
|
2016-09-09 02:09:46 +00:00
|
|
|
std::unique_ptr<Filter> tfY;
|
|
|
|
hr = _Create(srcImage.height, destImage.height, (filter & TEX_FILTER_WRAP_V) != 0, tfY);
|
|
|
|
if (FAILED(hr))
|
|
|
|
return hr;
|
2016-08-22 18:26:36 +00:00
|
|
|
|
2016-09-09 02:09:46 +00:00
|
|
|
XMVECTOR* row = scanline.get();
|
2016-08-22 18:26:36 +00:00
|
|
|
|
|
|
|
#ifdef _DEBUG
|
2016-09-09 02:09:46 +00:00
|
|
|
memset(row, 0xCD, sizeof(XMVECTOR)*srcImage.width);
|
2016-08-22 18:26:36 +00:00
|
|
|
#endif
|
|
|
|
|
2016-09-09 02:09:46 +00:00
|
|
|
auto xFromEnd = reinterpret_cast<const FilterFrom*>(reinterpret_cast<const uint8_t*>(tfX.get()) + tfX->sizeInBytes);
|
|
|
|
auto yFromEnd = reinterpret_cast<const FilterFrom*>(reinterpret_cast<const uint8_t*>(tfY.get()) + tfY->sizeInBytes);
|
2016-08-22 18:26:36 +00:00
|
|
|
|
2016-09-09 02:09:46 +00:00
|
|
|
// Count times rows get written
|
|
|
|
for (FilterFrom* yFrom = tfY->from; yFrom < yFromEnd; )
|
2016-08-22 18:26:36 +00:00
|
|
|
{
|
2016-09-09 02:09:46 +00:00
|
|
|
for (size_t j = 0; j < yFrom->count; ++j)
|
|
|
|
{
|
|
|
|
size_t v = yFrom->to[j].u;
|
|
|
|
assert(v < destImage.height);
|
|
|
|
++rowActive[v].remaining;
|
|
|
|
}
|
2016-08-22 18:26:36 +00:00
|
|
|
|
2016-09-09 02:09:46 +00:00
|
|
|
yFrom = reinterpret_cast<FilterFrom*>(reinterpret_cast<uint8_t*>(yFrom) + yFrom->sizeInBytes);
|
|
|
|
}
|
2016-08-22 18:26:36 +00:00
|
|
|
|
2016-09-09 02:09:46 +00:00
|
|
|
// Filter image
|
|
|
|
const uint8_t* pSrc = srcImage.pixels;
|
|
|
|
size_t rowPitch = srcImage.rowPitch;
|
|
|
|
const uint8_t* pEndSrc = pSrc + rowPitch * srcImage.height;
|
2016-08-22 18:26:36 +00:00
|
|
|
|
2016-09-09 02:09:46 +00:00
|
|
|
uint8_t* pDest = destImage.pixels;
|
2016-08-22 18:26:36 +00:00
|
|
|
|
2016-09-09 02:09:46 +00:00
|
|
|
for (FilterFrom* yFrom = tfY->from; yFrom < yFromEnd; )
|
2016-08-22 18:26:36 +00:00
|
|
|
{
|
2016-09-09 02:09:46 +00:00
|
|
|
// Create accumulation rows as needed
|
|
|
|
for (size_t j = 0; j < yFrom->count; ++j)
|
2016-08-22 18:26:36 +00:00
|
|
|
{
|
2016-09-09 02:09:46 +00:00
|
|
|
size_t v = yFrom->to[j].u;
|
|
|
|
assert(v < destImage.height);
|
|
|
|
TriangleRow* rowAcc = &rowActive[v];
|
|
|
|
|
|
|
|
if (!rowAcc->scanline)
|
2016-08-22 18:26:36 +00:00
|
|
|
{
|
2016-09-09 02:09:46 +00:00
|
|
|
if (rowFree)
|
|
|
|
{
|
|
|
|
// Steal and reuse scanline from 'free row' list
|
2018-06-13 01:07:34 +00:00
|
|
|
assert(rowFree->scanline != nullptr);
|
2016-09-09 02:09:46 +00:00
|
|
|
rowAcc->scanline.reset(rowFree->scanline.release());
|
|
|
|
rowFree = rowFree->next;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-09-12 00:21:56 +00:00
|
|
|
rowAcc->scanline.reset(static_cast<XMVECTOR*>(AllocateVectorAligned(sizeof(XMVECTOR) * destImage.width)));
|
2016-09-09 02:09:46 +00:00
|
|
|
if (!rowAcc->scanline)
|
|
|
|
return E_OUTOFMEMORY;
|
|
|
|
}
|
2016-08-22 18:26:36 +00:00
|
|
|
|
2016-09-09 02:09:46 +00:00
|
|
|
memset(rowAcc->scanline.get(), 0, sizeof(XMVECTOR) * destImage.width);
|
|
|
|
}
|
2016-08-22 18:26:36 +00:00
|
|
|
}
|
|
|
|
|
2016-09-09 02:09:46 +00:00
|
|
|
// Load source scanline
|
|
|
|
if ((pSrc + rowPitch) > pEndSrc)
|
|
|
|
return E_FAIL;
|
2016-08-22 18:26:36 +00:00
|
|
|
|
2016-09-09 02:09:46 +00:00
|
|
|
if (!_LoadScanlineLinear(row, srcImage.width, pSrc, rowPitch, srcImage.format, filter))
|
|
|
|
return E_FAIL;
|
2016-08-22 18:26:36 +00:00
|
|
|
|
2016-09-09 02:09:46 +00:00
|
|
|
pSrc += rowPitch;
|
2016-08-22 18:26:36 +00:00
|
|
|
|
2016-09-09 02:09:46 +00:00
|
|
|
// Process row
|
|
|
|
size_t x = 0;
|
|
|
|
for (FilterFrom* xFrom = tfX->from; xFrom < xFromEnd; ++x)
|
2016-08-22 18:26:36 +00:00
|
|
|
{
|
2016-09-09 02:09:46 +00:00
|
|
|
for (size_t j = 0; j < yFrom->count; ++j)
|
2016-08-22 18:26:36 +00:00
|
|
|
{
|
2016-09-09 02:09:46 +00:00
|
|
|
size_t v = yFrom->to[j].u;
|
|
|
|
assert(v < destImage.height);
|
|
|
|
float yweight = yFrom->to[j].weight;
|
2016-08-22 18:26:36 +00:00
|
|
|
|
2016-09-09 02:09:46 +00:00
|
|
|
XMVECTOR* accPtr = rowActive[v].scanline.get();
|
|
|
|
if (!accPtr)
|
|
|
|
return E_POINTER;
|
2016-08-22 18:26:36 +00:00
|
|
|
|
2016-09-09 02:09:46 +00:00
|
|
|
for (size_t k = 0; k < xFrom->count; ++k)
|
|
|
|
{
|
|
|
|
size_t u = xFrom->to[k].u;
|
|
|
|
assert(u < destImage.width);
|
2016-08-22 18:26:36 +00:00
|
|
|
|
2016-09-09 02:09:46 +00:00
|
|
|
XMVECTOR weight = XMVectorReplicate(yweight * xFrom->to[k].weight);
|
2016-08-22 18:26:36 +00:00
|
|
|
|
2016-09-09 02:09:46 +00:00
|
|
|
assert(x < srcImage.width);
|
|
|
|
accPtr[u] = XMVectorMultiplyAdd(row[x], weight, accPtr[u]);
|
|
|
|
}
|
|
|
|
}
|
2016-08-22 18:26:36 +00:00
|
|
|
|
2016-09-09 02:09:46 +00:00
|
|
|
xFrom = reinterpret_cast<FilterFrom*>(reinterpret_cast<uint8_t*>(xFrom) + xFrom->sizeInBytes);
|
|
|
|
}
|
2016-08-22 18:26:36 +00:00
|
|
|
|
2016-09-09 02:09:46 +00:00
|
|
|
// Write completed accumulation rows
|
|
|
|
for (size_t j = 0; j < yFrom->count; ++j)
|
2016-08-22 18:26:36 +00:00
|
|
|
{
|
2016-09-09 02:09:46 +00:00
|
|
|
size_t v = yFrom->to[j].u;
|
|
|
|
assert(v < destImage.height);
|
|
|
|
TriangleRow* rowAcc = &rowActive[v];
|
|
|
|
|
|
|
|
assert(rowAcc->remaining > 0);
|
|
|
|
--rowAcc->remaining;
|
2016-08-22 18:26:36 +00:00
|
|
|
|
2016-09-09 02:09:46 +00:00
|
|
|
if (!rowAcc->remaining)
|
2016-08-22 18:26:36 +00:00
|
|
|
{
|
2016-09-09 02:09:46 +00:00
|
|
|
XMVECTOR* pAccSrc = rowAcc->scanline.get();
|
|
|
|
if (!pAccSrc)
|
|
|
|
return E_POINTER;
|
|
|
|
|
|
|
|
switch (destImage.format)
|
|
|
|
{
|
|
|
|
case DXGI_FORMAT_R10G10B10A2_UNORM:
|
|
|
|
case DXGI_FORMAT_R10G10B10A2_UINT:
|
2016-08-22 18:26:36 +00:00
|
|
|
{
|
|
|
|
// Need to slightly bias results for floating-point error accumulation which can
|
|
|
|
// be visible with harshly quantized values
|
2017-07-12 07:56:51 +00:00
|
|
|
static const XMVECTORF32 Bias = { { { 0.f, 0.f, 0.f, 0.1f } } };
|
2016-09-09 02:09:46 +00:00
|
|
|
|
2016-08-22 18:26:36 +00:00
|
|
|
XMVECTOR* ptr = pAccSrc;
|
2016-09-09 02:09:46 +00:00
|
|
|
for (size_t i = 0; i < destImage.width; ++i, ++ptr)
|
2016-08-22 18:26:36 +00:00
|
|
|
{
|
2016-09-09 02:09:46 +00:00
|
|
|
*ptr = XMVectorAdd(*ptr, Bias);
|
2016-08-22 18:26:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2017-07-14 02:24:22 +00:00
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
2016-09-09 02:09:46 +00:00
|
|
|
}
|
2016-08-22 18:26:36 +00:00
|
|
|
|
2016-09-09 02:09:46 +00:00
|
|
|
// This performs any required clamping
|
|
|
|
if (!_StoreScanlineLinear(pDest + (destImage.rowPitch * v), destImage.rowPitch, destImage.format, pAccSrc, destImage.width, filter))
|
|
|
|
return E_FAIL;
|
2016-08-22 18:26:36 +00:00
|
|
|
|
2016-09-09 02:09:46 +00:00
|
|
|
// Put row on freelist to reuse it's allocated scanline
|
|
|
|
rowAcc->next = rowFree;
|
|
|
|
rowFree = rowAcc;
|
|
|
|
}
|
2016-08-22 18:26:36 +00:00
|
|
|
}
|
2016-09-09 02:09:46 +00:00
|
|
|
|
|
|
|
yFrom = reinterpret_cast<FilterFrom*>(reinterpret_cast<uint8_t*>(yFrom) + yFrom->sizeInBytes);
|
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
|
|
|
//--- Custom filter resize ---
|
2020-06-01 07:42:02 +00:00
|
|
|
HRESULT PerformResizeUsingCustomFilters(const Image& srcImage, TEX_FILTER_FLAGS filter, const Image& destImage) noexcept
|
2016-09-09 02:09:46 +00:00
|
|
|
{
|
|
|
|
if (!srcImage.pixels || !destImage.pixels)
|
|
|
|
return E_POINTER;
|
2016-08-22 18:26:36 +00:00
|
|
|
|
2016-09-09 02:09:46 +00:00
|
|
|
static_assert(TEX_FILTER_POINT == 0x100000, "TEX_FILTER_ flag values don't match TEX_FILTER_MASK");
|
2016-08-22 18:26:36 +00:00
|
|
|
|
2020-06-01 07:42:02 +00:00
|
|
|
unsigned long filter_select = filter & TEX_FILTER_MODE_MASK;
|
2016-09-09 02:09:46 +00:00
|
|
|
if (!filter_select)
|
|
|
|
{
|
|
|
|
// Default filter choice
|
|
|
|
filter_select = (((destImage.width << 1) == srcImage.width) && ((destImage.height << 1) == srcImage.height))
|
|
|
|
? TEX_FILTER_BOX : TEX_FILTER_LINEAR;
|
|
|
|
}
|
2016-08-22 18:26:36 +00:00
|
|
|
|
2016-09-09 02:09:46 +00:00
|
|
|
switch (filter_select)
|
|
|
|
{
|
|
|
|
case TEX_FILTER_POINT:
|
|
|
|
return ResizePointFilter(srcImage, destImage);
|
2016-08-22 18:26:36 +00:00
|
|
|
|
2016-09-09 02:09:46 +00:00
|
|
|
case TEX_FILTER_BOX:
|
|
|
|
return ResizeBoxFilter(srcImage, filter, destImage);
|
2016-08-22 18:26:36 +00:00
|
|
|
|
2016-09-09 02:09:46 +00:00
|
|
|
case TEX_FILTER_LINEAR:
|
|
|
|
return ResizeLinearFilter(srcImage, filter, destImage);
|
2016-08-22 18:26:36 +00:00
|
|
|
|
2016-09-09 02:09:46 +00:00
|
|
|
case TEX_FILTER_CUBIC:
|
|
|
|
return ResizeCubicFilter(srcImage, filter, destImage);
|
2016-08-22 18:26:36 +00:00
|
|
|
|
2016-09-09 02:09:46 +00:00
|
|
|
case TEX_FILTER_TRIANGLE:
|
|
|
|
return ResizeTriangleFilter(srcImage, filter, destImage);
|
2016-08-22 18:26:36 +00:00
|
|
|
|
2016-09-09 02:09:46 +00:00
|
|
|
default:
|
|
|
|
return HRESULT_FROM_WIN32(ERROR_NOT_SUPPORTED);
|
|
|
|
}
|
2016-08-22 18:26:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//=====================================================================================
|
|
|
|
// Entry-points
|
|
|
|
//=====================================================================================
|
|
|
|
|
|
|
|
//-------------------------------------------------------------------------------------
|
|
|
|
// Resize image
|
|
|
|
//-------------------------------------------------------------------------------------
|
|
|
|
_Use_decl_annotations_
|
2016-09-09 02:09:46 +00:00
|
|
|
HRESULT DirectX::Resize(
|
|
|
|
const Image& srcImage,
|
|
|
|
size_t width,
|
|
|
|
size_t height,
|
2020-06-01 07:42:02 +00:00
|
|
|
TEX_FILTER_FLAGS filter,
|
2019-12-13 08:01:17 +00:00
|
|
|
ScratchImage& image) noexcept
|
2016-08-22 18:26:36 +00:00
|
|
|
{
|
2016-09-09 02:09:46 +00:00
|
|
|
if (width == 0 || height == 0)
|
2016-08-22 18:26:36 +00:00
|
|
|
return E_INVALIDARG;
|
|
|
|
|
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
|
|
|
if ((width > UINT32_MAX) || (height > UINT32_MAX))
|
2016-08-22 18:26:36 +00:00
|
|
|
return E_INVALIDARG;
|
|
|
|
|
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))
|
2016-08-22 18:26:36 +00:00
|
|
|
{
|
|
|
|
// We don't support resizing compressed images
|
2016-09-09 02:09:46 +00:00
|
|
|
return HRESULT_FROM_WIN32(ERROR_NOT_SUPPORTED);
|
2016-08-22 18:26:36 +00:00
|
|
|
}
|
|
|
|
|
2020-09-12 00:21:56 +00:00
|
|
|
#if !defined(_DXTX_NOWIN)
|
2018-08-02 19:31:20 +00:00
|
|
|
bool usewic = UseWICFiltering(srcImage.format, filter);
|
|
|
|
|
|
|
|
WICPixelFormatGUID pfGUID = {};
|
|
|
|
bool wicpf = (usewic) ? _DXGIToWIC(srcImage.format, pfGUID, true) : false;
|
|
|
|
|
|
|
|
if (usewic && !wicpf)
|
|
|
|
{
|
|
|
|
// Check to see if the source and/or result size is too big for WIC
|
|
|
|
uint64_t expandedSize = uint64_t(width) * uint64_t(height) * sizeof(float) * 4;
|
|
|
|
uint64_t expandedSize2 = uint64_t(srcImage.width) * uint64_t(srcImage.height) * sizeof(float) * 4;
|
|
|
|
if (expandedSize > UINT32_MAX || expandedSize2 > UINT32_MAX)
|
|
|
|
{
|
|
|
|
if (filter & TEX_FILTER_FORCE_WIC)
|
|
|
|
return HRESULT_FROM_WIN32(ERROR_ARITHMETIC_OVERFLOW);
|
|
|
|
|
|
|
|
usewic = false;
|
|
|
|
}
|
|
|
|
}
|
2020-09-12 00:21:56 +00:00
|
|
|
#endif
|
2018-08-02 19:31:20 +00:00
|
|
|
|
2016-09-09 02:09:46 +00:00
|
|
|
HRESULT hr = image.Initialize2D(srcImage.format, width, 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
|
|
|
return E_POINTER;
|
|
|
|
|
2020-09-12 00:21:56 +00:00
|
|
|
#if !defined(_DXTX_NOWIN)
|
2018-08-02 19:31:20 +00:00
|
|
|
if (usewic)
|
2016-08-22 18:26:36 +00:00
|
|
|
{
|
2018-08-02 19:31:20 +00:00
|
|
|
if (wicpf)
|
2016-08-22 18:26:36 +00:00
|
|
|
{
|
|
|
|
// Case 1: Source format is supported by Windows Imaging Component
|
2016-09-09 02:09:46 +00:00
|
|
|
hr = PerformResizeUsingWIC(srcImage, filter, pfGUID, *rimage);
|
2016-08-22 18:26:36 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Case 2: Source format is not supported by WIC, so we have to convert, resize, and convert back
|
2016-09-09 02:09:46 +00:00
|
|
|
hr = PerformResizeViaF32(srcImage, filter, *rimage);
|
2016-08-22 18:26:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2020-09-12 00:21:56 +00:00
|
|
|
#endif
|
2016-08-22 18:26:36 +00:00
|
|
|
{
|
2018-08-02 19:31:20 +00:00
|
|
|
// Case 3: not using WIC resizing
|
2016-09-09 02:09:46 +00:00
|
|
|
hr = PerformResizeUsingCustomFilters(srcImage, filter, *rimage);
|
2016-08-22 18:26:36 +00:00
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//-------------------------------------------------------------------------------------
|
|
|
|
// Resize image (complex)
|
|
|
|
//-------------------------------------------------------------------------------------
|
|
|
|
_Use_decl_annotations_
|
2016-09-09 02:09:46 +00:00
|
|
|
HRESULT DirectX::Resize(
|
|
|
|
const Image* srcImages,
|
|
|
|
size_t nimages,
|
|
|
|
const TexMetadata& metadata,
|
|
|
|
size_t width,
|
|
|
|
size_t height,
|
2020-06-01 07:42:02 +00:00
|
|
|
TEX_FILTER_FLAGS filter,
|
2019-12-13 08:01:17 +00:00
|
|
|
ScratchImage& result) noexcept
|
2016-08-22 18:26:36 +00:00
|
|
|
{
|
2016-09-09 02:09:46 +00:00
|
|
|
if (!srcImages || !nimages || width == 0 || height == 0)
|
2016-08-22 18:26:36 +00:00
|
|
|
return E_INVALIDARG;
|
|
|
|
|
2016-09-09 02:09:46 +00:00
|
|
|
if ((width > UINT32_MAX) || (height > UINT32_MAX))
|
2016-08-22 18:26:36 +00:00
|
|
|
return E_INVALIDARG;
|
|
|
|
|
|
|
|
TexMetadata mdata2 = metadata;
|
|
|
|
mdata2.width = width;
|
|
|
|
mdata2.height = height;
|
|
|
|
mdata2.mipLevels = 1;
|
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;
|
|
|
|
|
2020-09-12 00:21:56 +00:00
|
|
|
#if !defined(_DXTX_NOWIN)
|
2016-09-14 07:03:22 +00:00
|
|
|
bool usewic = !metadata.IsPMAlpha() && UseWICFiltering(metadata.format, filter);
|
2016-08-22 18:26:36 +00:00
|
|
|
|
2018-01-18 23:02:05 +00:00
|
|
|
WICPixelFormatGUID pfGUID = {};
|
2016-09-09 02:09:46 +00:00
|
|
|
bool wicpf = (usewic) ? _DXGIToWIC(metadata.format, pfGUID, true) : false;
|
2016-08-22 18:26:36 +00:00
|
|
|
|
2018-08-02 19:31:20 +00:00
|
|
|
if (usewic && !wicpf)
|
|
|
|
{
|
|
|
|
// Check to see if the source and/or result size is too big for WIC
|
|
|
|
uint64_t expandedSize = uint64_t(width) * uint64_t(height) * sizeof(float) * 4;
|
|
|
|
uint64_t expandedSize2 = uint64_t(metadata.width) * uint64_t(metadata.height) * sizeof(float) * 4;
|
|
|
|
if (expandedSize > UINT32_MAX || expandedSize2 > UINT32_MAX)
|
|
|
|
{
|
|
|
|
if (filter & TEX_FILTER_FORCE_WIC)
|
|
|
|
return HRESULT_FROM_WIN32(ERROR_ARITHMETIC_OVERFLOW);
|
|
|
|
|
|
|
|
usewic = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-12 00:21:56 +00:00
|
|
|
#endif
|
|
|
|
|
2016-09-09 02:09:46 +00:00
|
|
|
switch (metadata.dimension)
|
2016-08-22 18:26:36 +00:00
|
|
|
{
|
|
|
|
case TEX_DIMENSION_TEXTURE1D:
|
|
|
|
case TEX_DIMENSION_TEXTURE2D:
|
2016-09-09 02:09:46 +00:00
|
|
|
assert(metadata.depth == 1);
|
2016-08-22 18:26:36 +00:00
|
|
|
|
2016-09-09 02:09:46 +00:00
|
|
|
for (size_t item = 0; item < metadata.arraySize; ++item)
|
2016-08-22 18:26:36 +00:00
|
|
|
{
|
2016-09-09 02:09:46 +00:00
|
|
|
size_t srcIndex = metadata.ComputeIndex(0, item, 0);
|
|
|
|
if (srcIndex >= nimages)
|
2016-08-22 18:26:36 +00:00
|
|
|
{
|
|
|
|
result.Release();
|
|
|
|
return E_FAIL;
|
|
|
|
}
|
|
|
|
|
2016-09-09 02:09:46 +00:00
|
|
|
const Image* srcimg = &srcImages[srcIndex];
|
|
|
|
const Image* destimg = result.GetImage(0, item, 0);
|
|
|
|
if (!srcimg || !destimg)
|
2016-08-22 18:26:36 +00:00
|
|
|
{
|
|
|
|
result.Release();
|
|
|
|
return E_POINTER;
|
|
|
|
}
|
|
|
|
|
2016-09-09 02:09:46 +00:00
|
|
|
if (srcimg->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 ((srcimg->width > UINT32_MAX) || (srcimg->height > UINT32_MAX))
|
2016-08-22 18:26:36 +00:00
|
|
|
{
|
|
|
|
result.Release();
|
|
|
|
return E_FAIL;
|
|
|
|
}
|
|
|
|
|
2020-09-12 00:21:56 +00:00
|
|
|
#if !defined(_DXTX_NOWIN)
|
2016-09-09 02:09:46 +00:00
|
|
|
if (usewic)
|
2016-08-22 18:26:36 +00:00
|
|
|
{
|
2016-09-09 02:09:46 +00:00
|
|
|
if (wicpf)
|
2016-08-22 18:26:36 +00:00
|
|
|
{
|
|
|
|
// Case 1: Source format is supported by Windows Imaging Component
|
2016-09-09 02:09:46 +00:00
|
|
|
hr = PerformResizeUsingWIC(*srcimg, filter, pfGUID, *destimg);
|
2016-08-22 18:26:36 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Case 2: Source format is not supported by WIC, so we have to convert, resize, and convert back
|
2016-09-09 02:09:46 +00:00
|
|
|
hr = PerformResizeViaF32(*srcimg, filter, *destimg);
|
2016-08-22 18:26:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2020-09-12 00:21:56 +00:00
|
|
|
#endif
|
2016-08-22 18:26:36 +00:00
|
|
|
{
|
|
|
|
// Case 3: not using WIC resizing
|
2016-09-09 02:09:46 +00:00
|
|
|
hr = PerformResizeUsingCustomFilters(*srcimg, filter, *destimg);
|
2016-08-22 18:26:36 +00:00
|
|
|
}
|
|
|
|
|
2016-09-09 02:09:46 +00:00
|
|
|
if (FAILED(hr))
|
2016-08-22 18:26:36 +00:00
|
|
|
{
|
|
|
|
result.Release();
|
|
|
|
return hr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TEX_DIMENSION_TEXTURE3D:
|
2016-09-09 02:09:46 +00:00
|
|
|
assert(metadata.arraySize == 1);
|
2016-08-22 18:26:36 +00:00
|
|
|
|
2016-09-09 02:09:46 +00:00
|
|
|
for (size_t slice = 0; slice < metadata.depth; ++slice)
|
2016-08-22 18:26:36 +00:00
|
|
|
{
|
2016-09-09 02:09:46 +00:00
|
|
|
size_t srcIndex = metadata.ComputeIndex(0, 0, slice);
|
|
|
|
if (srcIndex >= nimages)
|
2016-08-22 18:26:36 +00:00
|
|
|
{
|
|
|
|
result.Release();
|
|
|
|
return E_FAIL;
|
|
|
|
}
|
|
|
|
|
2016-09-09 02:09:46 +00:00
|
|
|
const Image* srcimg = &srcImages[srcIndex];
|
|
|
|
const Image* destimg = result.GetImage(0, 0, slice);
|
|
|
|
if (!srcimg || !destimg)
|
2016-08-22 18:26:36 +00:00
|
|
|
{
|
|
|
|
result.Release();
|
|
|
|
return E_POINTER;
|
|
|
|
}
|
|
|
|
|
2016-09-09 02:09:46 +00:00
|
|
|
if (srcimg->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 ((srcimg->width > UINT32_MAX) || (srcimg->height > UINT32_MAX))
|
2016-08-22 18:26:36 +00:00
|
|
|
{
|
|
|
|
result.Release();
|
|
|
|
return E_FAIL;
|
|
|
|
}
|
|
|
|
|
2020-09-12 00:21:56 +00:00
|
|
|
#if !defined(_DXTX_NOWIN)
|
2016-09-09 02:09:46 +00:00
|
|
|
if (usewic)
|
2016-08-22 18:26:36 +00:00
|
|
|
{
|
2016-09-09 02:09:46 +00:00
|
|
|
if (wicpf)
|
2016-08-22 18:26:36 +00:00
|
|
|
{
|
|
|
|
// Case 1: Source format is supported by Windows Imaging Component
|
2016-09-09 02:09:46 +00:00
|
|
|
hr = PerformResizeUsingWIC(*srcimg, filter, pfGUID, *destimg);
|
2016-08-22 18:26:36 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Case 2: Source format is not supported by WIC, so we have to convert, resize, and convert back
|
2016-09-09 02:09:46 +00:00
|
|
|
hr = PerformResizeViaF32(*srcimg, filter, *destimg);
|
2016-08-22 18:26:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2020-09-12 00:21:56 +00:00
|
|
|
#endif
|
2016-08-22 18:26:36 +00:00
|
|
|
{
|
|
|
|
// Case 3: not using WIC resizing
|
2016-09-09 02:09:46 +00:00
|
|
|
hr = PerformResizeUsingCustomFilters(*srcimg, filter, *destimg);
|
2016-08-22 18:26:36 +00:00
|
|
|
}
|
|
|
|
|
2016-09-09 02:09:46 +00:00
|
|
|
if (FAILED(hr))
|
2016-08-22 18:26:36 +00:00
|
|
|
{
|
|
|
|
result.Release();
|
|
|
|
return hr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
result.Release();
|
|
|
|
return E_FAIL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return S_OK;
|
|
|
|
}
|