crossxtex/DirectXTex/DirectXTexFlipRotate.cpp

344 lines
10 KiB
C++
Raw Normal View History

2016-08-22 18:26:36 +00:00
//-------------------------------------------------------------------------------------
// DirectXTexFlipRotate.cpp
//
// DirectX Texture Library - Image flip/rotate operations
//
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
// ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
// THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
// PARTICULAR PURPOSE.
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// http://go.microsoft.com/fwlink/?LinkId=248926
//-------------------------------------------------------------------------------------
#include "directxtexp.h"
2016-09-09 02:09:46 +00:00
using namespace DirectX;
2016-08-22 18:26:36 +00:00
using Microsoft::WRL::ComPtr;
2016-09-09 02:09:46 +00:00
namespace
2016-08-22 18:26:36 +00:00
{
2016-09-09 02:09:46 +00:00
//-------------------------------------------------------------------------------------
// Do flip/rotate operation using WIC
//-------------------------------------------------------------------------------------
HRESULT PerformFlipRotateUsingWIC(
const Image& srcImage,
DWORD flags,
const WICPixelFormatGUID& pfGUID,
const Image& destImage)
{
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;
IWICImagingFactory* pWIC = GetWICFactory(iswic2);
if (!pWIC)
return E_NOINTERFACE;
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
ComPtr<IWICBitmap> source;
HRESULT 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))
return hr;
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
ComPtr<IWICBitmapFlipRotator> FR;
hr = pWIC->CreateBitmapFlipRotator(FR.GetAddressOf());
if (FAILED(hr))
return hr;
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
hr = FR->Initialize(source.Get(), static_cast<WICBitmapTransformOptions>(flags));
if (FAILED(hr))
return hr;
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
WICPixelFormatGUID pfFR;
hr = FR->GetPixelFormat(&pfFR);
if (FAILED(hr))
return hr;
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
if (memcmp(&pfFR, &pfGUID, sizeof(GUID)) != 0)
{
// Flip/rotate should return the same format as the source...
return HRESULT_FROM_WIN32(ERROR_NOT_SUPPORTED);
}
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
UINT nwidth, nheight;
hr = FR->GetSize(&nwidth, &nheight);
if (FAILED(hr))
return hr;
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
if (destImage.width != nwidth || destImage.height != nheight)
return E_FAIL;
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
hr = FR->CopyPixels(0, static_cast<UINT>(destImage.rowPitch), static_cast<UINT>(destImage.slicePitch), destImage.pixels);
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, flip/rotate using WIC, conversion cycle
//-------------------------------------------------------------------------------------
HRESULT PerformFlipRotateViaF32(
const Image& srcImage,
DWORD flags,
const Image& destImage)
{
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 = PerformFlipRotateUsingWIC(*tsrc, flags, 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
}
//=====================================================================================
// Entry-points
//=====================================================================================
//-------------------------------------------------------------------------------------
// Flip/rotate image
//-------------------------------------------------------------------------------------
_Use_decl_annotations_
2016-09-09 02:09:46 +00:00
HRESULT DirectX::FlipRotate(
const Image& srcImage,
DWORD flags,
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 (!flags)
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 (IsCompressed(srcImage.format))
2016-08-22 18:26:36 +00:00
{
// We don't support flip/rotate operations on 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
}
2016-09-09 02:09:46 +00:00
static_assert(TEX_FR_ROTATE0 == WICBitmapTransformRotate0, "TEX_FR_ROTATE0 no longer matches WIC");
static_assert(TEX_FR_ROTATE90 == WICBitmapTransformRotate90, "TEX_FR_ROTATE90 no longer matches WIC");
static_assert(TEX_FR_ROTATE180 == WICBitmapTransformRotate180, "TEX_FR_ROTATE180 no longer matches WIC");
static_assert(TEX_FR_ROTATE270 == WICBitmapTransformRotate270, "TEX_FR_ROTATE270 no longer matches WIC");
static_assert(TEX_FR_FLIP_HORIZONTAL == WICBitmapTransformFlipHorizontal, "TEX_FR_FLIP_HORIZONTAL no longer matches WIC");
static_assert(TEX_FR_FLIP_VERTICAL == WICBitmapTransformFlipVertical, "TEX_FR_FLIP_VERTICAL no longer matches WIC");
2016-08-22 18:26:36 +00:00
// Only supports 90, 180, 270, or no rotation flags... not a combination of rotation flags
2016-09-09 02:09:46 +00:00
switch (flags & (TEX_FR_ROTATE90 | TEX_FR_ROTATE180 | TEX_FR_ROTATE270))
2016-08-22 18:26:36 +00:00
{
case 0:
case TEX_FR_ROTATE90:
case TEX_FR_ROTATE180:
case TEX_FR_ROTATE270:
break;
default:
return E_INVALIDARG;
}
size_t nwidth = srcImage.width;
size_t nheight = srcImage.height;
2016-09-09 02:09:46 +00:00
if (flags & (TEX_FR_ROTATE90 | TEX_FR_ROTATE270))
2016-08-22 18:26:36 +00:00
{
nwidth = srcImage.height;
nheight = srcImage.width;
}
2016-09-09 02:09:46 +00:00
HRESULT hr = image.Initialize2D(srcImage.format, nwidth, nheight, 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-09-12 19:01:48 +00:00
{
image.Release();
2016-08-22 18:26:36 +00:00
return E_POINTER;
2016-09-12 19:01:48 +00:00
}
2016-08-22 18:26:36 +00:00
WICPixelFormatGUID pfGUID;
2016-09-09 02:09:46 +00:00
if (_DXGIToWIC(srcImage.format, pfGUID))
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 = PerformFlipRotateUsingWIC(srcImage, flags, 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, flip/rotate, and convert back
2016-09-09 02:09:46 +00:00
hr = PerformFlipRotateViaF32(srcImage, flags, *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;
}
//-------------------------------------------------------------------------------------
// Flip/rotate image (complex)
//-------------------------------------------------------------------------------------
_Use_decl_annotations_
2016-09-09 02:09:46 +00:00
HRESULT DirectX::FlipRotate(
const Image* srcImages,
size_t nimages,
const TexMetadata& metadata,
DWORD flags,
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))
2016-08-22 18:26:36 +00:00
{
// We don't support flip/rotate operations on 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
}
2016-09-09 02:09:46 +00:00
static_assert(TEX_FR_ROTATE0 == WICBitmapTransformRotate0, "TEX_FR_ROTATE0 no longer matches WIC");
static_assert(TEX_FR_ROTATE90 == WICBitmapTransformRotate90, "TEX_FR_ROTATE90 no longer matches WIC");
static_assert(TEX_FR_ROTATE180 == WICBitmapTransformRotate180, "TEX_FR_ROTATE180 no longer matches WIC");
static_assert(TEX_FR_ROTATE270 == WICBitmapTransformRotate270, "TEX_FR_ROTATE270 no longer matches WIC");
static_assert(TEX_FR_FLIP_HORIZONTAL == WICBitmapTransformFlipHorizontal, "TEX_FR_FLIP_HORIZONTAL no longer matches WIC");
static_assert(TEX_FR_FLIP_VERTICAL == WICBitmapTransformFlipVertical, "TEX_FR_FLIP_VERTICAL no longer matches WIC");
2016-08-22 18:26:36 +00:00
// Only supports 90, 180, 270, or no rotation flags... not a combination of rotation flags
2016-09-09 02:09:46 +00:00
switch (flags & (TEX_FR_ROTATE90 | TEX_FR_ROTATE180 | TEX_FR_ROTATE270))
2016-08-22 18:26:36 +00:00
{
case 0:
case TEX_FR_ROTATE90:
case TEX_FR_ROTATE180:
case TEX_FR_ROTATE270:
break;
default:
return E_INVALIDARG;
}
TexMetadata mdata2 = metadata;
bool flipwh = false;
2016-09-09 02:09:46 +00:00
if (flags & (TEX_FR_ROTATE90 | TEX_FR_ROTATE270))
2016-08-22 18:26:36 +00:00
{
flipwh = true;
mdata2.width = metadata.height;
mdata2.height = metadata.width;
}
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;
}
WICPixelFormatGUID pfGUID;
2016-09-09 02:09:46 +00:00
bool wicpf = _DXGIToWIC(metadata.format, pfGUID);
2016-08-22 18:26:36 +00:00
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-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 (flipwh)
2016-08-22 18:26:36 +00:00
{
2016-09-09 02:09:46 +00:00
if (src.width != dst.height || src.height != dst.width)
2016-08-22 18:26:36 +00:00
{
result.Release();
return E_FAIL;
}
}
else
{
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;
}
}
if (wicpf)
{
// Case 1: Source format is supported by Windows Imaging Component
2016-09-09 02:09:46 +00:00
hr = PerformFlipRotateUsingWIC(src, flags, pfGUID, dst);
2016-08-22 18:26:36 +00:00
}
else
{
// Case 2: Source format is not supported by WIC, so we have to convert, flip/rotate, and convert back
2016-09-09 02:09:46 +00:00
hr = PerformFlipRotateViaF32(src, flags, dst);
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;
}
}
return S_OK;
}