Change how DPI works when loading bitmaps

DPI metadata in the source image file is now ignored (because it turns out this
is wrong too often to be relied on in image files found out in the wild, and to
match the behavior of most other APIs and rendering engines).

Instead, we add CanvasBitmap.LoadAsync overloads that explicitly specify what
DPI to load at. If the DPI is not explicitly specified, it defaults to 96.

Also removed a couple of unnecessary method overloads (CanvasRenderTarget
constructor that took a Size struct, and bitmap + rendertarget
CreateFromDirect3D11Surface methods that took DPI but not alpha behavior).
This commit is contained in:
Shawn Hargreaves 2014-11-12 15:17:30 -08:00
Родитель 9121ab6f0e
Коммит 55c600c18d
19 изменённых файлов: 256 добавлений и 260 удалений

Просмотреть файл

@ -87,7 +87,7 @@ namespace CsConsumer
UpdateCanvasControlSize();
m_imageBrush = new CanvasImageBrush(sender);
m_offscreenTarget = new CanvasRenderTarget(sender, new Size(100, 100));
m_offscreenTarget = new CanvasRenderTarget(sender, 100, 100);
using (CanvasDrawingSession ds = m_offscreenTarget.CreateDrawingSession())
{

Просмотреть файл

@ -60,8 +60,7 @@ namespace ExampleGallery
{
oldCanvasSize = newCanvasSize;
// CanvasRenderTarget expects an integer size.
SetupText(new Size((int)newCanvasSize.Width, (int)newCanvasSize.Height));
SetupText(newCanvasSize);
};
UpdateAnimation((float)timer.Elapsed.TotalSeconds);
@ -87,7 +86,7 @@ namespace ExampleGallery
/// </summary>
private void SetupText(Size targetSize)
{
textRenderTarget = new CanvasRenderTarget(this.canvas, targetSize);
textRenderTarget = new CanvasRenderTarget(this.canvas, (int)targetSize.Width, (int)targetSize.Height);
using (var ds = textRenderTarget.CreateDrawingSession())
{
@ -188,7 +187,7 @@ namespace ExampleGallery
private void textInput_TextChanged(object sender, TextChangedEventArgs e)
{
SetupText(new Size((int)oldCanvasSize.Width, (int)oldCanvasSize.Height));
SetupText(oldCanvasSize);
}
}
}

Просмотреть файл

@ -755,7 +755,7 @@ namespace ExampleGallery
private ICanvasImage CreateShadow()
{
var renderTarget = new CanvasRenderTarget(canvas, new Size(360, 150));
var renderTarget = new CanvasRenderTarget(canvas, 360, 150);
using (var ds = renderTarget.CreateDrawingSession())
{
@ -847,7 +847,7 @@ namespace ExampleGallery
private ICanvasImage AddTextOverlay(ICanvasImage effect, float x, float y)
{
var textOverlay = new CanvasRenderTarget(canvas, new Size(200, 30));
var textOverlay = new CanvasRenderTarget(canvas, 200, 30);
using (var ds = textOverlay.CreateDrawingSession())
{

Просмотреть файл

@ -60,20 +60,9 @@ under the License.
<member name="M:Microsoft.Graphics.Canvas.CanvasBitmap.Dispose">
<summary>Releases all resources used by the CanvasBitmap.</summary>
</member>
<member name="M:Microsoft.Graphics.Canvas.CanvasBitmap.LoadAsync(Microsoft.Graphics.Canvas.ICanvasResourceCreator,System.String)">
<summary>Loads a bitmap from an image file (jpeg, png, etc.)</summary>
</member>
<member name="M:Microsoft.Graphics.Canvas.CanvasBitmap.LoadAsync(Microsoft.Graphics.Canvas.ICanvasResourceCreator,Windows.Storage.Streams.IRandomAccessStream)">
<summary>Loads a bitmap from a stream, with a default alpha behavior of CanvasAlphaBehavior.Premultiplied.</summary>
<remarks>This method requires that the stream be readable.</remarks>
</member>
<member name="M:Microsoft.Graphics.Canvas.CanvasBitmap.LoadAsync(Microsoft.Graphics.Canvas.ICanvasResourceCreator,Windows.Storage.Streams.IRandomAccessStream,Microsoft.Graphics.Canvas.CanvasAlphaBehavior)">
<summary>Loads a bitmap from a stream, with the specified alpha behavior.</summary>
<remarks>This method requires that the stream be readable.</remarks>
</member>
<member name="M:Microsoft.Graphics.Canvas.CanvasBitmap.CreateFromDirect3D11Surface(Microsoft.Graphics.Canvas.ICanvasResourceCreator,Microsoft.Graphics.Canvas.DirectX.Direct3D11.IDirect3DSurface)">
<summary>Creates a CanvasBitmap from an existing Direct3D graphics surface.</summary>
<summary>Creates a CanvasBitmap from an existing Direct3D graphics surface, using premultiplied alpha and default (96) DPI.</summary>
<remarks>
<p>The bitmap's properties are determined by the properties of surface.</p>
<p>If the surface is able to be used as a render target then a
@ -100,9 +89,6 @@ under the License.
<member name="M:Microsoft.Graphics.Canvas.CanvasBitmap.CreateFromDirect3D11Surface(Microsoft.Graphics.Canvas.ICanvasResourceCreator,Microsoft.Graphics.Canvas.DirectX.Direct3D11.IDirect3DSurface,Microsoft.Graphics.Canvas.CanvasAlphaBehavior)">
<summary>Creates a CanvasBitmap from an existing Direct3D graphics surface, using the specified alpha behavior and default (96) DPI.</summary>
</member>
<member name="M:Microsoft.Graphics.Canvas.CanvasBitmap.CreateFromDirect3D11Surface(Microsoft.Graphics.Canvas.ICanvasResourceCreator,Microsoft.Graphics.Canvas.DirectX.Direct3D11.IDirect3DSurface,System.Single)">
<summary>Creates a CanvasBitmap from an existing Direct3D graphics surface, using the specified DPI and premultiplied alpha behavior.</summary>
</member>
<member name="M:Microsoft.Graphics.Canvas.CanvasBitmap.CreateFromDirect3D11Surface(Microsoft.Graphics.Canvas.ICanvasResourceCreator,Microsoft.Graphics.Canvas.DirectX.Direct3D11.IDirect3DSurface,Microsoft.Graphics.Canvas.CanvasAlphaBehavior,System.Single)">
<summary>Creates a CanvasBitmap from an existing Direct3D graphics surface, using the specified alpha behavior and DPI.</summary>
</member>
@ -118,14 +104,42 @@ under the License.
<member name="M:Microsoft.Graphics.Canvas.CanvasBitmap.CreateFromColors(Microsoft.Graphics.Canvas.ICanvasResourceCreator,Windows.UI.Color[],System.Int32,System.Int32,Microsoft.Graphics.Canvas.CanvasAlphaBehavior,System.Single)">
<summary>Creates a CanvasBitmap from an array of colors, using the specified pixel width/height, alpha behavior and DPI.</summary>
</member>
<member name="M:Microsoft.Graphics.Canvas.CanvasBitmap.LoadAsync(Microsoft.Graphics.Canvas.ICanvasResourceCreator,System.String)">
<summary>Loads a bitmap from an image file (jpeg, png, etc.)</summary>
<remarks>The bitmap is created at default (96) DPI and set to premultiplied alpha.</remarks>
</member>
<member name="M:Microsoft.Graphics.Canvas.CanvasBitmap.LoadAsync(Microsoft.Graphics.Canvas.ICanvasResourceCreator,System.String,Microsoft.Graphics.Canvas.CanvasAlphaBehavior)">
<summary>Loads a bitmap from an image file (jpeg, png, etc.), and assigns it the specified alpha behavior.</summary>
<remarks>The bitmap is created at default (96) DPI.</remarks>
</member>
<member name="M:Microsoft.Graphics.Canvas.CanvasBitmap.LoadAsync(Microsoft.Graphics.Canvas.ICanvasResourceCreator,System.String,Microsoft.Graphics.Canvas.CanvasAlphaBehavior,System.Single)">
<summary>Loads a bitmap from an image file (jpeg, png, etc.), and assigns it the specified alpha behavior and DPI.</summary>
</member>
<member name="M:Microsoft.Graphics.Canvas.CanvasBitmap.LoadAsync(Microsoft.Graphics.Canvas.ICanvasResourceCreator,System.Uri)">
<summary>Loads a bitmap from an image file (jpeg, png, etc.) located at a URI.</summary>
<remarks>The bitmap is created at default (96) DPI and set to premultiplied alpha.</remarks>
</member>
<member name="M:Microsoft.Graphics.Canvas.CanvasBitmap.LoadAsync(Microsoft.Graphics.Canvas.ICanvasResourceCreator,System.Uri,Microsoft.Graphics.Canvas.CanvasAlphaBehavior)">
<summary>Loads a bitmap from an image file (jpeg, png, etc.) located at a URI, and assigns it the specified alpha behavior.</summary>
<remarks>The bitmap is created at default (96) DPI.</remarks>
</member>
<member name="M:Microsoft.Graphics.Canvas.CanvasBitmap.LoadAsync(Microsoft.Graphics.Canvas.ICanvasResourceCreator,System.Uri,Microsoft.Graphics.Canvas.CanvasAlphaBehavior,System.Single)">
<summary>Loads a bitmap from an image file (jpeg, png, etc.) located at a URI, and assigns it the specified alpha behavior and DPI.</summary>
</member>
<member name="M:Microsoft.Graphics.Canvas.CanvasBitmap.LoadAsync(Microsoft.Graphics.Canvas.ICanvasResourceCreator,Windows.Storage.Streams.IRandomAccessStream)">
<summary>Loads a bitmap from a stream.</summary>
<remarks>This method requires that the stream be readable.
The bitmap is created at default (96) DPI and set to premultiplied alpha.</remarks>
</member>
<member name="M:Microsoft.Graphics.Canvas.CanvasBitmap.LoadAsync(Microsoft.Graphics.Canvas.ICanvasResourceCreator,Windows.Storage.Streams.IRandomAccessStream,Microsoft.Graphics.Canvas.CanvasAlphaBehavior)">
<summary>Loads a bitmap from a stream, and assigns it the specified alpha behavior.</summary>
<remarks>This method requires that the stream be readable.
The bitmap is created at default (96) DPI.</remarks>
</member>
<member name="M:Microsoft.Graphics.Canvas.CanvasBitmap.LoadAsync(Microsoft.Graphics.Canvas.ICanvasResourceCreator,Windows.Storage.Streams.IRandomAccessStream,Microsoft.Graphics.Canvas.CanvasAlphaBehavior,System.Single)">
<summary>Loads a bitmap from a stream, and assigns it the specified alpha behavior and DPI.</summary>
<remarks>This method requires that the stream be readable.</remarks>
</member>
<member name="M:Microsoft.Graphics.Canvas.CanvasBitmap.SaveToFileAsync(System.String)">

Просмотреть файл

@ -25,16 +25,6 @@ under the License.
<p>When using native interop with CanvasRenderTarget, the version of GetOrCreate that takes a device must be used.</p>
</remarks>
</member>
<member name="M:Microsoft.Graphics.Canvas.CanvasRenderTarget.#ctor(Microsoft.Graphics.Canvas.ICanvasResourceCreator,Windows.Foundation.Size)">
<summary>Initializes a new instance of the CanvasRenderTarget class.</summary>
</member>
<member name="M:Microsoft.Graphics.Canvas.CanvasRenderTarget.CreateFromDirect3D11Surface(Microsoft.Graphics.Canvas.ICanvasResourceCreator,Microsoft.Graphics.Canvas.DirectX.Direct3D11.IDirect3DSurface)">
<summary>Creates a CanvasBitmap from an existing graphics surface.</summary>
<remarks>
<p>The bitmap's properties are determined by the properties of surface.</p>
<p>This method will fail if the surface cannot be used as a render target.</p>
</remarks>
</member>
<member name="M:Microsoft.Graphics.Canvas.CanvasRenderTarget.CreateDrawingSession">
<summary>Returns a new drawing session. The drawing session draws onto the CanvasRenderTarget.</summary>
<remarks>
@ -68,14 +58,14 @@ under the License.
<member name="M:Microsoft.Graphics.Canvas.CanvasRenderTarget.#ctor(Microsoft.Graphics.Canvas.ICanvasResourceCreator,System.Single,System.Single,Microsoft.Graphics.Canvas.DirectX.DirectXPixelFormat,Microsoft.Graphics.Canvas.CanvasAlphaBehavior,System.Single)">
<summary>Initializes a new instance of the CanvasRenderTarget class.</summary>
</member>
<member name="M:Microsoft.Graphics.Canvas.CanvasRenderTarget.CreateFromDirect3D11Surface(Microsoft.Graphics.Canvas.ICanvasResourceCreator,Microsoft.Graphics.Canvas.DirectX.Direct3D11.IDirect3DSurface,Microsoft.Graphics.Canvas.CanvasAlphaBehavior)">
<summary>Creates a CanvasBitmap from an existing Direct3D graphics surface, using the specified alpha behavior and default (96) DPI.</summary>
<member name="M:Microsoft.Graphics.Canvas.CanvasRenderTarget.CreateFromDirect3D11Surface(Microsoft.Graphics.Canvas.ICanvasResourceCreator,Microsoft.Graphics.Canvas.DirectX.Direct3D11.IDirect3DSurface)">
<summary>Creates a CanvasRenderTarget from an existing Direct3D graphics surface, using premultiplied alpha and default (96) DPI.</summary>
</member>
<member name="M:Microsoft.Graphics.Canvas.CanvasRenderTarget.CreateFromDirect3D11Surface(Microsoft.Graphics.Canvas.ICanvasResourceCreator,Microsoft.Graphics.Canvas.DirectX.Direct3D11.IDirect3DSurface,System.Single)">
<summary>Creates a CanvasBitmap from an existing Direct3D graphics surface, using the specified DPI and premultiplied alpha behavior.</summary>
<member name="M:Microsoft.Graphics.Canvas.CanvasRenderTarget.CreateFromDirect3D11Surface(Microsoft.Graphics.Canvas.ICanvasResourceCreator,Microsoft.Graphics.Canvas.DirectX.Direct3D11.IDirect3DSurface,Microsoft.Graphics.Canvas.CanvasAlphaBehavior)">
<summary>Creates a CanvasRenderTarget from an existing Direct3D graphics surface, using the specified alpha behavior and default (96) DPI.</summary>
</member>
<member name="M:Microsoft.Graphics.Canvas.CanvasRenderTarget.CreateFromDirect3D11Surface(Microsoft.Graphics.Canvas.ICanvasResourceCreator,Microsoft.Graphics.Canvas.DirectX.Direct3D11.IDirect3DSurface,Microsoft.Graphics.Canvas.CanvasAlphaBehavior,System.Single)">
<summary>Creates a CanvasBitmap from an existing Direct3D graphics surface, using the specified alpha behavior and DPI.</summary>
<summary>Creates a CanvasRenderTarget from an existing Direct3D graphics surface, using the specified alpha behavior and DPI.</summary>
</member>
</members>

Просмотреть файл

@ -191,13 +191,6 @@ namespace Microsoft.Graphics.Canvas
[in] CanvasAlphaBehavior alpha,
[out, retval] CanvasBitmap** bitmap);
[overload("CreateFromDirect3D11Surface")]
HRESULT CreateFromDirect3D11SurfaceWithDpi(
[in] ICanvasResourceCreator* resourceCreator,
[in] Microsoft.Graphics.Canvas.DirectX.Direct3D11.IDirect3DSurface* surface,
[in] float dpi,
[out, retval] CanvasBitmap** bitmap);
[overload("CreateFromDirect3D11Surface")]
HRESULT CreateFromDirect3D11SurfaceWithAlphaAndDpi(
[in] ICanvasResourceCreator* resourceCreator,
@ -263,6 +256,14 @@ namespace Microsoft.Graphics.Canvas
[in] CanvasAlphaBehavior alpha,
[out, retval] Windows.Foundation.IAsyncOperation<CanvasBitmap*>** canvasBitmap);
[overload("LoadAsync")]
HRESULT LoadAsyncFromHstringWithAlphaAndDpi(
[in] ICanvasResourceCreator* resourceCreator,
[in] HSTRING fileName,
[in] CanvasAlphaBehavior alpha,
[in] float dpi,
[out, retval] Windows.Foundation.IAsyncOperation<CanvasBitmap*>** canvasBitmap);
[overload("LoadAsync"), default_overload]
HRESULT LoadAsyncFromUri(
[in] ICanvasResourceCreator* resourceCreator,
@ -276,6 +277,14 @@ namespace Microsoft.Graphics.Canvas
[in] CanvasAlphaBehavior alpha,
[out, retval] Windows.Foundation.IAsyncOperation<CanvasBitmap*>** canvasBitmap);
[overload("LoadAsync"), default_overload]
HRESULT LoadAsyncFromUriWithAlphaAndDpi(
[in] ICanvasResourceCreator* resourceCreator,
[in] Windows.Foundation.Uri* uri,
[in] CanvasAlphaBehavior alpha,
[in] float dpi,
[out, retval] Windows.Foundation.IAsyncOperation<CanvasBitmap*>** canvasBitmap);
[overload("LoadAsync")]
HRESULT LoadAsyncFromStream(
[in] ICanvasResourceCreator* resourceCreator,
@ -288,6 +297,14 @@ namespace Microsoft.Graphics.Canvas
[in] Windows.Storage.Streams.IRandomAccessStream* stream,
[in] CanvasAlphaBehavior alpha,
[out, retval] Windows.Foundation.IAsyncOperation<CanvasBitmap*>** canvasBitmap);
[overload("LoadAsync")]
HRESULT LoadAsyncFromStreamWithAlphaAndDpi(
[in] ICanvasResourceCreator* resourceCreator,
[in] Windows.Storage.Streams.IRandomAccessStream* stream,
[in] CanvasAlphaBehavior alpha,
[in] float dpi,
[out, retval] Windows.Foundation.IAsyncOperation<CanvasBitmap*>** canvasBitmap);
};
[version(VERSION), composable(ICanvasBitmapFactory, public, VERSION), threading(both), marshaling_behavior(agile), static(ICanvasBitmapStatics, VERSION)]
@ -306,18 +323,13 @@ namespace Microsoft.Graphics.Canvas
interface ICanvasRenderTargetFactory : IInspectable
{
//
// Creates a renderable bitmap with some defaults- a format of B8G8R8A8
// UNORM, premultiplied alpha, 96x96 DPI, and the render target view
// For constructor overloads that don't specify them, defaults are a format of
// B8G8R8A8_UNORM, premultiplied alpha, 96x96 DPI, and the render target view
// bind flag as well as shader resource view. The D2D resource is
// flagged as TARGET, meaning this bitmap can be used both as a source
// and as a target.
//
HRESULT Create(
[in] ICanvasResourceCreator* resourceCreator,
[in] Windows.Foundation.Size size,
[out, retval] CanvasRenderTarget** renderTarget);
HRESULT CreateWithWidthAndHeight(
[in] ICanvasResourceCreator* resourceCreator,
[in] float width,
@ -365,13 +377,6 @@ namespace Microsoft.Graphics.Canvas
[in] CanvasAlphaBehavior alpha,
[out, retval] CanvasRenderTarget** bitmap);
[overload("CreateFromDirect3D11Surface")]
HRESULT CreateFromDirect3D11SurfaceWithDpi(
[in] ICanvasResourceCreator* resourceCreator,
[in] Microsoft.Graphics.Canvas.DirectX.Direct3D11.IDirect3DSurface* surface,
[in] float dpi,
[out, retval] CanvasRenderTarget** bitmap);
[overload("CreateFromDirect3D11Surface")]
HRESULT CreateFromDirect3D11SurfaceWithAlphaAndDpi(
[in] ICanvasResourceCreator* resourceCreator,

Просмотреть файл

@ -36,12 +36,13 @@ namespace ABI { namespace Microsoft { namespace Graphics { namespace Canvas
ComPtr<CanvasBitmap> CanvasBitmapManager::CreateNew(
ICanvasDevice* canvasDevice,
HSTRING fileName,
CanvasAlphaBehavior alpha)
CanvasAlphaBehavior alpha,
float dpi)
{
ComPtr<ICanvasDeviceInternal> canvasDeviceInternal;
ThrowIfFailed(canvasDevice->QueryInterface(canvasDeviceInternal.GetAddressOf()));
auto d2dBitmap = canvasDeviceInternal->CreateBitmapFromWicResource(m_adapter->CreateWICFormatConverter(fileName).Get(), alpha);
auto d2dBitmap = canvasDeviceInternal->CreateBitmapFromWicResource(m_adapter->CreateWICFormatConverter(fileName).Get(), alpha, dpi);
auto bitmap = Make<CanvasBitmap>(
shared_from_this(),
@ -55,12 +56,13 @@ namespace ABI { namespace Microsoft { namespace Graphics { namespace Canvas
ComPtr<CanvasBitmap> CanvasBitmapManager::CreateNew(
ICanvasDevice* canvasDevice,
IStream* fileStream,
CanvasAlphaBehavior alpha)
CanvasAlphaBehavior alpha,
float dpi)
{
ComPtr<ICanvasDeviceInternal> canvasDeviceInternal;
ThrowIfFailed(canvasDevice->QueryInterface(canvasDeviceInternal.GetAddressOf()));
auto d2dBitmap = canvasDeviceInternal->CreateBitmapFromWicResource(m_adapter->CreateWICFormatConverter(fileStream).Get(), alpha);
auto d2dBitmap = canvasDeviceInternal->CreateBitmapFromWicResource(m_adapter->CreateWICFormatConverter(fileStream).Get(), alpha, dpi);
auto bitmap = Make<CanvasBitmap>(
shared_from_this(),
@ -238,20 +240,6 @@ namespace ABI { namespace Microsoft { namespace Graphics { namespace Canvas
canvasBitmap);
}
IFACEMETHODIMP CanvasBitmapFactory::CreateFromDirect3D11SurfaceWithDpi(
ICanvasResourceCreator* resourceCreator,
IDirect3DSurface* surface,
float dpi,
ICanvasBitmap** canvasBitmap)
{
return CreateFromDirect3D11SurfaceWithAlphaAndDpi(
resourceCreator,
surface,
CanvasAlphaBehavior::Premultiplied,
dpi,
canvasBitmap);
}
IFACEMETHODIMP CanvasBitmapFactory::CreateFromDirect3D11SurfaceWithAlphaAndDpi(
ICanvasResourceCreator* resourceCreator,
IDirect3DSurface* surface,
@ -391,10 +379,11 @@ namespace ABI { namespace Microsoft { namespace Graphics { namespace Canvas
HSTRING fileName,
ABI::Windows::Foundation::IAsyncOperation<CanvasBitmap*>** canvasBitmapAsyncOperation)
{
return LoadAsyncFromHstringWithAlpha(
return LoadAsyncFromHstringWithAlphaAndDpi(
resourceCreator,
fileName,
CanvasAlphaBehavior::Premultiplied,
DEFAULT_DPI,
canvasBitmapAsyncOperation);
}
@ -403,6 +392,21 @@ namespace ABI { namespace Microsoft { namespace Graphics { namespace Canvas
HSTRING fileName,
CanvasAlphaBehavior alpha,
ABI::Windows::Foundation::IAsyncOperation<CanvasBitmap*>** canvasBitmapAsyncOperation)
{
return LoadAsyncFromHstringWithAlphaAndDpi(
resourceCreator,
fileName,
alpha,
DEFAULT_DPI,
canvasBitmapAsyncOperation);
}
IFACEMETHODIMP CanvasBitmapFactory::LoadAsyncFromHstringWithAlphaAndDpi(
ICanvasResourceCreator* resourceCreator,
HSTRING fileName,
CanvasAlphaBehavior alpha,
float dpi,
ABI::Windows::Foundation::IAsyncOperation<CanvasBitmap*>** canvasBitmapAsyncOperation)
{
return ExceptionBoundary(
[&]
@ -419,7 +423,7 @@ namespace ABI { namespace Microsoft { namespace Graphics { namespace Canvas
auto asyncOperation = Make<AsyncOperation<CanvasBitmap>>(
[=]
{
return GetManager()->CreateBitmap(canvasDevice.Get(), fileName, alpha);
return GetManager()->CreateBitmap(canvasDevice.Get(), fileName, alpha, dpi);
});
CheckMakeResult(asyncOperation);
@ -432,10 +436,11 @@ namespace ABI { namespace Microsoft { namespace Graphics { namespace Canvas
ABI::Windows::Foundation::IUriRuntimeClass* uri,
ABI::Windows::Foundation::IAsyncOperation<CanvasBitmap*>** canvasBitmapAsyncOperation)
{
return LoadAsyncFromUriWithAlpha(
return LoadAsyncFromUriWithAlphaAndDpi(
resourceCreator,
uri,
CanvasAlphaBehavior::Premultiplied,
DEFAULT_DPI,
canvasBitmapAsyncOperation);
}
@ -445,7 +450,21 @@ namespace ABI { namespace Microsoft { namespace Graphics { namespace Canvas
CanvasAlphaBehavior alpha,
ABI::Windows::Foundation::IAsyncOperation<CanvasBitmap*>** canvasBitmapAsyncOperation)
{
return LoadAsyncFromUriWithAlphaAndDpi(
resourceCreator,
uri,
alpha,
DEFAULT_DPI,
canvasBitmapAsyncOperation);
}
IFACEMETHODIMP CanvasBitmapFactory::LoadAsyncFromUriWithAlphaAndDpi(
ICanvasResourceCreator* resourceCreator,
ABI::Windows::Foundation::IUriRuntimeClass* uri,
CanvasAlphaBehavior alpha,
float dpi,
ABI::Windows::Foundation::IAsyncOperation<CanvasBitmap*>** canvasBitmapAsyncOperation)
{
return ExceptionBoundary(
[&]
{
@ -472,7 +491,7 @@ namespace ABI { namespace Microsoft { namespace Graphics { namespace Canvas
ComPtr<IStream> stream;
ThrowIfFailed(CreateStreamOverRandomAccessStream(randomAccessStream.Get(), IID_PPV_ARGS(&stream)));
return GetManager()->CreateBitmap(canvasDevice.Get(), stream.Get(), alpha);
return GetManager()->CreateBitmap(canvasDevice.Get(), stream.Get(), alpha, dpi);
});
CheckMakeResult(asyncOperation);
@ -485,17 +504,33 @@ namespace ABI { namespace Microsoft { namespace Graphics { namespace Canvas
IRandomAccessStream* stream,
ABI::Windows::Foundation::IAsyncOperation<CanvasBitmap*>** canvasBitmapAsyncOperation)
{
return LoadAsyncFromStreamWithAlpha(
return LoadAsyncFromStreamWithAlphaAndDpi(
resourceCreator,
stream,
CanvasAlphaBehavior::Premultiplied,
DEFAULT_DPI,
canvasBitmapAsyncOperation);
}
IFACEMETHODIMP CanvasBitmapFactory::LoadAsyncFromStreamWithAlpha(
ICanvasResourceCreator* resourceCreator,
IRandomAccessStream* stream,
CanvasAlphaBehavior alpha,
ABI::Windows::Foundation::IAsyncOperation<CanvasBitmap*>** canvasBitmapAsyncOperation)
{
return LoadAsyncFromStreamWithAlphaAndDpi(
resourceCreator,
stream,
alpha,
DEFAULT_DPI,
canvasBitmapAsyncOperation);
}
IFACEMETHODIMP CanvasBitmapFactory::LoadAsyncFromStreamWithAlphaAndDpi(
ICanvasResourceCreator* resourceCreator,
IRandomAccessStream* rawStream,
CanvasAlphaBehavior alpha,
float dpi,
ABI::Windows::Foundation::IAsyncOperation<CanvasBitmap*>** canvasBitmapAsyncOperation)
{
return ExceptionBoundary(
@ -516,7 +551,7 @@ namespace ABI { namespace Microsoft { namespace Graphics { namespace Canvas
ComPtr<IStream> nativeStream;
ThrowIfFailed(CreateStreamOverRandomAccessStream(stream.Get(), IID_PPV_ARGS(&nativeStream)));
return GetManager()->CreateBitmap(canvasDevice.Get(), nativeStream.Get(), alpha);
return GetManager()->CreateBitmap(canvasDevice.Get(), nativeStream.Get(), alpha, dpi);
});
CheckMakeResult(asyncOperation);

Просмотреть файл

@ -99,12 +99,6 @@ namespace ABI { namespace Microsoft { namespace Graphics { namespace Canvas
CanvasAlphaBehavior alpha,
ICanvasBitmap** canvasBitmap) override;
IFACEMETHOD(CreateFromDirect3D11SurfaceWithDpi)(
ICanvasResourceCreator* resourceCreator,
IDirect3DSurface* surface,
float dpi,
ICanvasBitmap** canvasBitmap) override;
IFACEMETHOD(CreateFromDirect3D11SurfaceWithAlphaAndDpi)(
ICanvasResourceCreator* resourceCreator,
IDirect3DSurface* surface,
@ -163,6 +157,13 @@ namespace ABI { namespace Microsoft { namespace Graphics { namespace Canvas
CanvasAlphaBehavior alpha,
ABI::Windows::Foundation::IAsyncOperation<CanvasBitmap*>** canvasBitmapAsyncOperation) override;
IFACEMETHOD(LoadAsyncFromHstringWithAlphaAndDpi)(
ICanvasResourceCreator* resourceCreator,
HSTRING fileName,
CanvasAlphaBehavior alpha,
float dpi,
ABI::Windows::Foundation::IAsyncOperation<CanvasBitmap*>** canvasBitmapAsyncOperation) override;
IFACEMETHOD(LoadAsyncFromUri)(
ICanvasResourceCreator* resourceCreator,
ABI::Windows::Foundation::IUriRuntimeClass* uri,
@ -174,6 +175,13 @@ namespace ABI { namespace Microsoft { namespace Graphics { namespace Canvas
CanvasAlphaBehavior alpha,
ABI::Windows::Foundation::IAsyncOperation<CanvasBitmap*>** canvasBitmapAsyncOperation) override;
IFACEMETHOD(LoadAsyncFromUriWithAlphaAndDpi)(
ICanvasResourceCreator* resourceCreator,
ABI::Windows::Foundation::IUriRuntimeClass* uri,
CanvasAlphaBehavior alpha,
float dpi,
ABI::Windows::Foundation::IAsyncOperation<CanvasBitmap*>** canvasBitmapAsyncOperation) override;
IFACEMETHOD(LoadAsyncFromStream)(
ICanvasResourceCreator* resourceCreator,
IRandomAccessStream* stream,
@ -185,6 +193,13 @@ namespace ABI { namespace Microsoft { namespace Graphics { namespace Canvas
CanvasAlphaBehavior alpha,
ABI::Windows::Foundation::IAsyncOperation<CanvasBitmap*>** canvasBitmapAsyncOperation) override;
IFACEMETHOD(LoadAsyncFromStreamWithAlphaAndDpi)(
ICanvasResourceCreator* resourceCreator,
IRandomAccessStream* stream,
CanvasAlphaBehavior alpha,
float dpi,
ABI::Windows::Foundation::IAsyncOperation<CanvasBitmap*>** canvasBitmapAsyncOperation) override;
//
// ICanvasDeviceResourceFactoryNative
//
@ -705,12 +720,14 @@ namespace ABI { namespace Microsoft { namespace Graphics { namespace Canvas
ComPtr<CanvasBitmap> CreateNew(
ICanvasDevice* canvasDevice,
HSTRING fileName,
CanvasAlphaBehavior alpha);
CanvasAlphaBehavior alpha,
float dpi);
ComPtr<CanvasBitmap> CreateNew(
ICanvasDevice* canvasDevice,
IStream* fileStream,
CanvasAlphaBehavior alpha);
CanvasAlphaBehavior alpha,
float dpi);
ComPtr<CanvasBitmap> CreateNew(
ICanvasDevice* device,

Просмотреть файл

@ -456,17 +456,14 @@ namespace ABI { namespace Microsoft { namespace Graphics { namespace Canvas
ComPtr<ID2D1Bitmap1> CanvasDevice::CreateBitmapFromWicResource(
IWICFormatConverter* wicConverter,
CanvasAlphaBehavior alpha)
CanvasAlphaBehavior alpha,
float dpi)
{
auto deviceContext = m_d2dResourceCreationDeviceContext.EnsureNotClosed();
D2D1_BITMAP_PROPERTIES1 bitmapProperties = D2D1::BitmapProperties1();
bitmapProperties.pixelFormat.alphaMode = ToD2DAlphaMode(alpha);
double dpiX, dpiY;
ThrowIfFailed(wicConverter->GetResolution(&dpiX, &dpiY));
bitmapProperties.dpiX = static_cast<float>(dpiX);
bitmapProperties.dpiY = static_cast<float>(dpiY);
bitmapProperties.dpiX = bitmapProperties.dpiY = dpi;
ComPtr<ID2D1Bitmap1> bitmap;
ThrowIfFailed(deviceContext->CreateBitmapFromWicBitmap(wicConverter, &bitmapProperties, &bitmap));

Просмотреть файл

@ -71,7 +71,8 @@ namespace ABI { namespace Microsoft { namespace Graphics { namespace Canvas
virtual ComPtr<ID2D1SolidColorBrush> CreateSolidColorBrush(D2D1_COLOR_F const& color) = 0;
virtual ComPtr<ID2D1Bitmap1> CreateBitmapFromWicResource(
IWICFormatConverter* wicConverter,
CanvasAlphaBehavior alpha) = 0;
CanvasAlphaBehavior alpha,
float dpi) = 0;
virtual ComPtr<ID2D1Bitmap1> CreateRenderTargetBitmap(
float width,
float height,
@ -162,7 +163,8 @@ namespace ABI { namespace Microsoft { namespace Graphics { namespace Canvas
virtual ComPtr<ID2D1SolidColorBrush> CreateSolidColorBrush(D2D1_COLOR_F const& color) override;
virtual ComPtr<ID2D1Bitmap1> CreateBitmapFromWicResource(
IWICFormatConverter* wicConverter,
CanvasAlphaBehavior alpha) override;
CanvasAlphaBehavior alpha,
float dpi) override;
virtual ComPtr<ID2D1Bitmap1> CreateRenderTargetBitmap(
float width,
float height,

Просмотреть файл

@ -75,21 +75,6 @@ namespace ABI { namespace Microsoft { namespace Graphics { namespace Canvas
{
}
IFACEMETHODIMP CanvasRenderTargetFactory::Create(
ICanvasResourceCreator* resourceCreator,
ABI::Windows::Foundation::Size size,
ICanvasRenderTarget** renderTarget)
{
return CreateWithWidthAndHeightAndFormatAndAlphaAndDpi(
resourceCreator,
size.Width,
size.Height,
DirectXPixelFormat::B8G8R8A8UIntNormalized,
CanvasAlphaBehavior::Premultiplied,
DEFAULT_DPI,
renderTarget);
}
IFACEMETHODIMP CanvasRenderTargetFactory::CreateWithWidthAndHeight(
ICanvasResourceCreator* resourceCreator,
float width,
@ -218,20 +203,6 @@ namespace ABI { namespace Microsoft { namespace Graphics { namespace Canvas
canvasRenderTarget);
}
IFACEMETHODIMP CanvasRenderTargetFactory::CreateFromDirect3D11SurfaceWithDpi(
ICanvasResourceCreator* resourceCreator,
IDirect3DSurface* surface,
float dpi,
ICanvasRenderTarget** canvasRenderTarget)
{
return CreateFromDirect3D11SurfaceWithAlphaAndDpi(
resourceCreator,
surface,
CanvasAlphaBehavior::Premultiplied,
dpi,
canvasRenderTarget);
}
IFACEMETHODIMP CanvasRenderTargetFactory::CreateFromDirect3D11SurfaceWithAlphaAndDpi(
ICanvasResourceCreator* resourceCreator,
IDirect3DSurface* surface,

Просмотреть файл

@ -28,11 +28,6 @@ namespace ABI { namespace Microsoft { namespace Graphics { namespace Canvas
public:
CanvasRenderTargetFactory();
IFACEMETHOD(Create)(
ICanvasResourceCreator* resourceCreator,
ABI::Windows::Foundation::Size size,
ICanvasRenderTarget** renderTarget);
IFACEMETHOD(CreateWithWidthAndHeight)(
ICanvasResourceCreator* resourceCreator,
float width,
@ -83,12 +78,6 @@ namespace ABI { namespace Microsoft { namespace Graphics { namespace Canvas
CanvasAlphaBehavior alpha,
ICanvasRenderTarget** canvasRenderTarget) override;
IFACEMETHOD(CreateFromDirect3D11SurfaceWithDpi)(
ICanvasResourceCreator* resourceCreator,
IDirect3DSurface* surface,
float dpi,
ICanvasRenderTarget** canvasRenderTarget) override;
IFACEMETHOD(CreateFromDirect3D11SurfaceWithAlphaAndDpi)(
ICanvasResourceCreator* resourceCreator,
IDirect3DSurface* surface,

Просмотреть файл

@ -22,7 +22,6 @@ using namespace Windows::UI;
using namespace Windows::Storage::Streams;
using Platform::String;
const float c_dpiTolerance = 0.1f;
const int c_subresourceSliceCount = 3;
struct SignedRect
@ -37,51 +36,35 @@ struct SignedRect
TEST_CLASS(CanvasBitmapTests)
{
struct TestImage
{
Platform::String^ fileName;
int widthInPixels;
int heightInPixels;
float m_dpi;
float widthInDip;
float heightInDip;
};
TestImage m_testImage;
CanvasDevice^ m_sharedDevice;
static const int testImageWidth = 196;
static const int testImageHeight = 147;
Platform::String^ testImageFileName;
public:
CanvasBitmapTests()
{
m_testImage.fileName = L"Assets/imageTiger.jpg";
m_testImage.widthInPixels = 196;
m_testImage.heightInPixels = 147;
testImageFileName = L"Assets/imageTiger.jpg";
m_testImage.m_dpi = 72.0f;
const float dpiScaling = DEFAULT_DPI / m_testImage.m_dpi;
m_testImage.widthInDip = 196.0f * dpiScaling;
m_testImage.heightInDip = 147.0f * dpiScaling;
m_sharedDevice = ref new CanvasDevice();
}
TEST_METHOD(CanvasBitmap_PropertiesAndClose)
{
WIN32_FILE_ATTRIBUTE_DATA fileInfo;
Assert::AreNotEqual(GetFileAttributesEx(m_testImage.fileName->Data(), GetFileExInfoStandard, &fileInfo), 0);
Assert::AreNotEqual(GetFileAttributesEx(testImageFileName->Data(), GetFileExInfoStandard, &fileInfo), 0);
CanvasDevice^ canvasDevice = ref new CanvasDevice();
auto bitmapJpeg = WaitExecution(CanvasBitmap::LoadAsync(canvasDevice, m_testImage.fileName));
Assert::AreEqual(m_testImage.widthInPixels, (int)bitmapJpeg->SizeInPixels.Width);
Assert::AreEqual(m_testImage.heightInPixels, (int)bitmapJpeg->SizeInPixels.Height);
Assert::AreEqual(m_testImage.widthInDip, bitmapJpeg->Size.Width);
Assert::AreEqual(m_testImage.heightInDip, bitmapJpeg->Size.Height);
auto bitmapJpeg = WaitExecution(CanvasBitmap::LoadAsync(canvasDevice, testImageFileName));
Assert::AreEqual((float)testImageWidth, bitmapJpeg->SizeInPixels.Width);
Assert::AreEqual((float)testImageHeight, bitmapJpeg->SizeInPixels.Height);
Assert::AreEqual((float)testImageWidth, bitmapJpeg->Size.Width);
Assert::AreEqual((float)testImageHeight, bitmapJpeg->Size.Height);
}
TEST_METHOD(CanvasBitmap_LoadFromInvalidParameters)
{
CanvasDevice^ canvasDevice = ref new CanvasDevice();
@ -112,11 +95,15 @@ public:
for (int i = 0; i < 2; ++i)
{
IAsyncOperation<CanvasBitmap^>^ bitmapAsync;
IAsyncOperation<CanvasBitmap^>^ highDpiAsync;
const float highDpi = 150;
Uri^ uri = ref new Uri("ms-appx:///Assets/HighDpiGrid.png");
if (i == 0)
{
bitmapAsync = CanvasBitmap::LoadAsync(canvasDevice, uri, CanvasAlphaBehavior::Ignore);
highDpiAsync = CanvasBitmap::LoadAsync(canvasDevice, uri, CanvasAlphaBehavior::Ignore, highDpi);
}
else
{
@ -124,18 +111,36 @@ public:
auto stream = WaitExecution(storageFile->OpenReadAsync());
bitmapAsync = CanvasBitmap::LoadAsync(canvasDevice, stream, CanvasAlphaBehavior::Ignore);
storageFile = WaitExecution(Windows::Storage::StorageFile::GetFileFromApplicationUriAsync(uri));
stream = WaitExecution(storageFile->OpenReadAsync());
highDpiAsync = CanvasBitmap::LoadAsync(canvasDevice, stream, CanvasAlphaBehavior::Ignore, highDpi);
}
// Verify loading at default DPI.
auto bitmap = WaitExecution(bitmapAsync);
Assert::AreEqual(2.00001454f, bitmap->Size.Width);
Assert::AreEqual(2.00001454f, bitmap->Size.Height);
Assert::AreEqual(4.0f, bitmap->Size.Width);
Assert::AreEqual(4.0f, bitmap->Size.Height);
Assert::AreEqual(4.0f, bitmap->SizeInPixels.Width);
Assert::AreEqual(4.0f, bitmap->SizeInPixels.Height);
auto d2dBitmap = GetWrappedResource<ID2D1Bitmap1>(bitmap);
VerifyDpiAndAlpha(d2dBitmap, 192, D2D1_ALPHA_MODE_IGNORE, c_dpiTolerance);
VerifyDpiAndAlpha(d2dBitmap, 96, D2D1_ALPHA_MODE_IGNORE);
// Verify loading at high DPI.
bitmap = WaitExecution(highDpiAsync);
Assert::AreEqual(4.0f * 96 / highDpi, bitmap->Size.Width);
Assert::AreEqual(4.0f * 96 / highDpi, bitmap->Size.Height);
Assert::AreEqual(4.0f, bitmap->SizeInPixels.Width);
Assert::AreEqual(4.0f, bitmap->SizeInPixels.Height);
d2dBitmap = GetWrappedResource<ID2D1Bitmap1>(bitmap);
VerifyDpiAndAlpha(d2dBitmap, highDpi, D2D1_ALPHA_MODE_IGNORE);
}
}
@ -194,12 +199,11 @@ public:
}
}
TEST_METHOD(CanvasBitmap_NativeInterop)
{
auto canvasDevice = ref new CanvasDevice();
auto originalBitmap = WaitExecution(CanvasBitmap::LoadAsync(canvasDevice, m_testImage.fileName));
auto originalBitmap = WaitExecution(CanvasBitmap::LoadAsync(canvasDevice, testImageFileName));
auto originalD2DBitmap = GetWrappedResource<ID2D1Bitmap1>(originalBitmap);
auto newBitmap = GetOrCreate<CanvasBitmap>(canvasDevice, originalD2DBitmap.Get());
auto newD2DBitmap = GetWrappedResource<ID2D1Bitmap1>(newBitmap);
@ -353,37 +357,15 @@ public:
auto canvasBitmap = WaitExecution(CanvasBitmap::LoadAsync(canvasDevice, L"Assets/HighDpiGrid.png"));
auto d2dBitmap = GetWrappedResource<ID2D1Bitmap1>(canvasBitmap);
VerifyDpiAndAlpha(d2dBitmap, 192, D2D1_ALPHA_MODE_PREMULTIPLIED, c_dpiTolerance);
// We should ignore the DPI metadata saved in the file, and return a bitmap with default 96 DPI.
VerifyDpiAndAlpha(d2dBitmap, DEFAULT_DPI, D2D1_ALPHA_MODE_PREMULTIPLIED);
}
enum DpiVerifyType
void VerifyBitmapDecoderDimensionsMatchTestImage(BitmapDecoder^ bitmapDecoder)
{
Exact,
Fuzzy,
NotSupported
};
void VerifyBitmapDecoderDimensionsMatchTestImage(BitmapDecoder^ bitmapDecoder, DpiVerifyType dpiVerifyType)
{
Assert::AreEqual(m_testImage.widthInPixels, (int)bitmapDecoder->PixelWidth);
Assert::AreEqual(m_testImage.heightInPixels, (int)bitmapDecoder->PixelHeight);
if (dpiVerifyType == Exact)
{
Assert::AreEqual(m_testImage.m_dpi, static_cast<float>(bitmapDecoder->DpiX));
Assert::AreEqual(m_testImage.m_dpi, static_cast<float>(bitmapDecoder->DpiY));
}
else if (dpiVerifyType == Fuzzy)
{
Assert::AreEqual(m_testImage.m_dpi, static_cast<float>(bitmapDecoder->DpiX), c_dpiTolerance);
Assert::AreEqual(m_testImage.m_dpi, static_cast<float>(bitmapDecoder->DpiY), c_dpiTolerance);
}
else
{
assert(dpiVerifyType == NotSupported);
Assert::AreEqual(DEFAULT_DPI, static_cast<float>(bitmapDecoder->DpiX));
Assert::AreEqual(DEFAULT_DPI, static_cast<float>(bitmapDecoder->DpiY));
}
Assert::AreEqual(testImageWidth, (int)bitmapDecoder->PixelWidth);
Assert::AreEqual(testImageHeight, (int)bitmapDecoder->PixelHeight);
}
BitmapDecoder^ LoadBitmapDecoderFromPath(String^ path, ULONGLONG* optionalSizeOut = nullptr)
@ -444,7 +426,7 @@ public:
TEST_METHOD(CanvasBitmap_SaveToFileAsync_DetermineEncoderFromFileExtension)
{
auto canvasBitmap = WaitExecution(CanvasBitmap::LoadAsync(m_sharedDevice, m_testImage.fileName));
auto canvasBitmap = WaitExecution(CanvasBitmap::LoadAsync(m_sharedDevice, testImageFileName));
String^ tempFolder = Windows::Storage::ApplicationData::Current->TemporaryFolder->Path;
String^ pathPrefix = String::Concat(tempFolder, L"\\test.");
@ -453,22 +435,21 @@ public:
{
std::wstring Extension;
Platform::Guid ImagingFormat;
DpiVerifyType DpiVerify;
} testCases[] = {
{ L"bmp", BitmapDecoder::BmpDecoderId, DpiVerifyType::Fuzzy },
{ L"dib", BitmapDecoder::BmpDecoderId, DpiVerifyType::Fuzzy },
{ L"rle", BitmapDecoder::BmpDecoderId, DpiVerifyType::Fuzzy },
{ L"png", BitmapDecoder::PngDecoderId, DpiVerifyType::Fuzzy },
{ L"jpg", BitmapDecoder::JpegDecoderId, DpiVerifyType::Exact },
{ L"JPE", BitmapDecoder::JpegDecoderId, DpiVerifyType::Exact },
{ L"jpeg", BitmapDecoder::JpegDecoderId, DpiVerifyType::Exact },
{ L"jfif", BitmapDecoder::JpegDecoderId, DpiVerifyType::Exact },
{ L"EXIF", BitmapDecoder::JpegDecoderId, DpiVerifyType::Exact },
{ L"tif", BitmapDecoder::TiffDecoderId, DpiVerifyType::Exact },
{ L"tiff", BitmapDecoder::TiffDecoderId, DpiVerifyType::Exact },
{ L"gif", BitmapDecoder::GifDecoderId, DpiVerifyType::NotSupported },
{ L"wdp", BitmapDecoder::JpegXRDecoderId, DpiVerifyType::Exact },
{ L"jxr", BitmapDecoder::JpegXRDecoderId, DpiVerifyType::Exact },
{ L"bmp", BitmapDecoder::BmpDecoderId },
{ L"dib", BitmapDecoder::BmpDecoderId },
{ L"rle", BitmapDecoder::BmpDecoderId },
{ L"png", BitmapDecoder::PngDecoderId },
{ L"jpg", BitmapDecoder::JpegDecoderId },
{ L"JPE", BitmapDecoder::JpegDecoderId },
{ L"jpeg", BitmapDecoder::JpegDecoderId },
{ L"jfif", BitmapDecoder::JpegDecoderId },
{ L"EXIF", BitmapDecoder::JpegDecoderId },
{ L"tif", BitmapDecoder::TiffDecoderId },
{ L"tiff", BitmapDecoder::TiffDecoderId },
{ L"gif", BitmapDecoder::GifDecoderId },
{ L"wdp", BitmapDecoder::JpegXRDecoderId },
{ L"jxr", BitmapDecoder::JpegXRDecoderId },
};
for (auto testCase : testCases)
@ -481,13 +462,13 @@ public:
Assert::AreEqual(testCase.ImagingFormat, bitmapDecoder->DecoderInformation->CodecId);
VerifyBitmapDecoderDimensionsMatchTestImage(bitmapDecoder, testCase.DpiVerify);
VerifyBitmapDecoderDimensionsMatchTestImage(bitmapDecoder);
}
}
TEST_METHOD(CanvasBitmap_SaveToFileAndStreamAsync_UseSpecifiedEncoder)
{
auto canvasBitmap = WaitExecution(CanvasBitmap::LoadAsync(m_sharedDevice, m_testImage.fileName));
auto canvasBitmap = WaitExecution(CanvasBitmap::LoadAsync(m_sharedDevice, testImageFileName));
String^ tempFolder = Windows::Storage::ApplicationData::Current->TemporaryFolder->Path;
String^ targetPath = String::Concat(tempFolder, L"\\test.bin");
@ -501,14 +482,13 @@ public:
{
CanvasBitmapFileFormat CanvasFormat;
Platform::Guid ImagingFormat;
DpiVerifyType DpiVerify;
} testCases[] = {
{ CanvasBitmapFileFormat::Jpeg, BitmapDecoder::JpegDecoderId, DpiVerifyType::Exact },
{ CanvasBitmapFileFormat::Bmp, BitmapDecoder::BmpDecoderId, DpiVerifyType::Fuzzy },
{ CanvasBitmapFileFormat::Png, BitmapDecoder::PngDecoderId, DpiVerifyType::Fuzzy },
{ CanvasBitmapFileFormat::Tiff, BitmapDecoder::TiffDecoderId, DpiVerifyType::Exact },
{ CanvasBitmapFileFormat::Gif, BitmapDecoder::GifDecoderId, DpiVerifyType::NotSupported },
{ CanvasBitmapFileFormat::JpegXR, BitmapDecoder::JpegXRDecoderId, DpiVerifyType::Exact },
{ CanvasBitmapFileFormat::Jpeg, BitmapDecoder::JpegDecoderId },
{ CanvasBitmapFileFormat::Bmp, BitmapDecoder::BmpDecoderId },
{ CanvasBitmapFileFormat::Png, BitmapDecoder::PngDecoderId },
{ CanvasBitmapFileFormat::Tiff, BitmapDecoder::TiffDecoderId },
{ CanvasBitmapFileFormat::Gif, BitmapDecoder::GifDecoderId },
{ CanvasBitmapFileFormat::JpegXR, BitmapDecoder::JpegXRDecoderId },
};
for (auto testCase : testCases)
@ -519,7 +499,7 @@ public:
Assert::AreEqual(testCase.ImagingFormat, bitmapDecoder->DecoderInformation->CodecId);
VerifyBitmapDecoderDimensionsMatchTestImage(bitmapDecoder, testCase.DpiVerify);
VerifyBitmapDecoderDimensionsMatchTestImage(bitmapDecoder);
}
for (auto testCase : testCases)
@ -532,13 +512,13 @@ public:
Assert::AreEqual(testCase.ImagingFormat, bitmapDecoder->DecoderInformation->CodecId);
VerifyBitmapDecoderDimensionsMatchTestImage(bitmapDecoder, testCase.DpiVerify);
VerifyBitmapDecoderDimensionsMatchTestImage(bitmapDecoder);
}
}
TEST_METHOD(CanvasBitmap_SaveToBitmapAsync_ImageQuality)
{
auto canvasBitmap = WaitExecution(CanvasBitmap::LoadAsync(m_sharedDevice, m_testImage.fileName));
auto canvasBitmap = WaitExecution(CanvasBitmap::LoadAsync(m_sharedDevice, testImageFileName));
String^ tempFolder = Windows::Storage::ApplicationData::Current->TemporaryFolder->Path;
@ -546,14 +526,13 @@ public:
{
CanvasBitmapFileFormat FileFormat;
bool SupportsQuality;
DpiVerifyType DpiVerify;
} testCases[] {
{ CanvasBitmapFileFormat::Bmp, false, DpiVerifyType::Fuzzy },
{ CanvasBitmapFileFormat::Gif, false, DpiVerifyType::NotSupported },
{ CanvasBitmapFileFormat::Jpeg, true, DpiVerifyType::Exact },
{ CanvasBitmapFileFormat::JpegXR, true, DpiVerifyType::Exact },
{ CanvasBitmapFileFormat::Png, false, DpiVerifyType::Fuzzy },
{ CanvasBitmapFileFormat::Tiff, false, DpiVerifyType::Exact },
{ CanvasBitmapFileFormat::Bmp, false },
{ CanvasBitmapFileFormat::Gif, false },
{ CanvasBitmapFileFormat::Jpeg, true },
{ CanvasBitmapFileFormat::JpegXR, true },
{ CanvasBitmapFileFormat::Png, false },
{ CanvasBitmapFileFormat::Tiff, false },
};
for (auto testCase : testCases)
@ -566,11 +545,11 @@ public:
ULONGLONG lowQSize;
auto lowQDecoder = LoadBitmapDecoderFromPath(lowQPath, &lowQSize);
VerifyBitmapDecoderDimensionsMatchTestImage(lowQDecoder, testCase.DpiVerify);
VerifyBitmapDecoderDimensionsMatchTestImage(lowQDecoder);
ULONGLONG highQSize;
auto highQDecoder = LoadBitmapDecoderFromPath(highQPath, &highQSize);
VerifyBitmapDecoderDimensionsMatchTestImage(highQDecoder, testCase.DpiVerify);
VerifyBitmapDecoderDimensionsMatchTestImage(highQDecoder);
if (testCase.SupportsQuality) Assert::IsTrue(lowQSize < highQSize);
else Assert::AreEqual(lowQSize, highQSize);

Просмотреть файл

@ -69,7 +69,7 @@ public:
TEST_METHOD(CanvasImageBrush_ImageProperty_WithRenderTarget)
{
auto anyRenderTarget = ref new CanvasRenderTarget(m_device, Size{1,1});
auto anyRenderTarget = ref new CanvasRenderTarget(m_device, 1, 1);
auto brush = ref new CanvasImageBrush(m_device);
brush->Image = anyRenderTarget;

Просмотреть файл

@ -27,7 +27,7 @@ TEST_CLASS(CanvasRenderTargetTests)
// While this scenario is impractical and uncommon, verify it does not
// throw any errors.
//
CanvasRenderTarget^ renderTarget = ref new CanvasRenderTarget(canvasDevice, Size(0, 0));
CanvasRenderTarget^ renderTarget = ref new CanvasRenderTarget(canvasDevice, 0, 0);
Assert::AreEqual(0.0f, renderTarget->Size.Width);
Assert::AreEqual(0.0f, renderTarget->Size.Height);
Assert::AreEqual(0.0f, renderTarget->SizeInPixels.Width);
@ -42,7 +42,7 @@ TEST_CLASS(CanvasRenderTargetTests)
TEST_METHOD(CanvasRenderTarget_NativeInterop)
{
auto canvasDevice = ref new CanvasDevice();
auto originalRenderTarget = ref new CanvasRenderTarget(canvasDevice, Size(1, 1));
auto originalRenderTarget = ref new CanvasRenderTarget(canvasDevice, 1, 1);
auto originalD2DBitmap = GetWrappedResource<ID2D1Bitmap1>(originalRenderTarget);
//

Просмотреть файл

@ -48,13 +48,13 @@ ComPtr<ID2D1Bitmap1> CreateTestD2DBitmap(D2D1_BITMAP_OPTIONS options, ComPtr<ID2
return d2dBitmap;
}
void VerifyDpiAndAlpha(ComPtr<ID2D1Bitmap1> const& d2dBitmap, float expectedDpi, D2D1_ALPHA_MODE expectedAlphaMode, float dpiTolerance)
void VerifyDpiAndAlpha(ComPtr<ID2D1Bitmap1> const& d2dBitmap, float expectedDpi, D2D1_ALPHA_MODE expectedAlphaMode)
{
float dpiX, dpiY;
d2dBitmap->GetDpi(&dpiX, &dpiY);
Assert::AreEqual(expectedDpi, dpiX, dpiTolerance);
Assert::AreEqual(expectedDpi, dpiY, dpiTolerance);
Assert::AreEqual(expectedDpi, dpiX);
Assert::AreEqual(expectedDpi, dpiY);
Assert::AreEqual(expectedAlphaMode, d2dBitmap->GetPixelFormat().alphaMode);
}

Просмотреть файл

@ -425,7 +425,7 @@ ComPtr<ID2D1DeviceContext1> CreateTestD2DDeviceContext(CanvasDevice^ device = nu
ComPtr<ID2D1Bitmap1> CreateTestD2DBitmap(D2D1_BITMAP_OPTIONS options, ComPtr<ID2D1DeviceContext1> deviceContext = nullptr);
void VerifyDpiAndAlpha(ComPtr<ID2D1Bitmap1> const& d2dBitmap, float expectedDpi, D2D1_ALPHA_MODE expectedAlphaMode, float dpiTolerance = 0.0f);
void VerifyDpiAndAlpha(ComPtr<ID2D1Bitmap1> const& d2dBitmap, float expectedDpi, D2D1_ALPHA_MODE expectedAlphaMode);
struct WicBitmapTestFixture
{

Просмотреть файл

@ -67,7 +67,7 @@ TEST_CLASS(CanvasBitmapUnitTest)
{
Fixture f;
auto canvasBitmap = f.m_bitmapManager->Create(f.m_canvasDevice.Get(), f.m_testFileName, CanvasAlphaBehavior::Premultiplied);
auto canvasBitmap = f.m_bitmapManager->Create(f.m_canvasDevice.Get(), f.m_testFileName, CanvasAlphaBehavior::Premultiplied, DEFAULT_DPI);
ASSERT_IMPLEMENTS_INTERFACE(canvasBitmap, ICanvasBitmap);
ASSERT_IMPLEMENTS_INTERFACE(canvasBitmap, ICanvasImage);
@ -89,7 +89,7 @@ TEST_CLASS(CanvasBitmapUnitTest)
Direct3DSurfaceDescription surfaceDescription;
ComPtr<IDXGISurface> dxgiSurface;
auto canvasBitmap = f.m_bitmapManager->Create(f.m_canvasDevice.Get(), f.m_testFileName, CanvasAlphaBehavior::Premultiplied);
auto canvasBitmap = f.m_bitmapManager->Create(f.m_canvasDevice.Get(), f.m_testFileName, CanvasAlphaBehavior::Premultiplied, DEFAULT_DPI);
Assert::IsNotNull(canvasBitmap.Get());
@ -105,7 +105,7 @@ TEST_CLASS(CanvasBitmapUnitTest)
Assert::AreEqual(RO_E_CLOSED, canvasBitmap->GetBounds(drawingSession.Get(), &bounds));
Assert::AreEqual(RO_E_CLOSED, canvasBitmap->GetBoundsWithTransform(drawingSession.Get(), matrix, &bounds));
auto otherBitmap = f.m_bitmapManager->Create(f.m_canvasDevice.Get(), f.m_testFileName, CanvasAlphaBehavior::Premultiplied);
auto otherBitmap = f.m_bitmapManager->Create(f.m_canvasDevice.Get(), f.m_testFileName, CanvasAlphaBehavior::Premultiplied, DEFAULT_DPI);
Assert::AreEqual(RO_E_CLOSED, canvasBitmap->CopyPixelsFromBitmap(otherBitmap.Get()));
Assert::AreEqual(RO_E_CLOSED, canvasBitmap->CopyPixelsFromBitmapWithDestPoint(otherBitmap.Get(), 0, 0));
Assert::AreEqual(RO_E_CLOSED, canvasBitmap->CopyPixelsFromBitmapWithDestPointAndSourceRect(otherBitmap.Get(), 0, 0, 0, 0, 0, 0));
@ -125,7 +125,7 @@ TEST_CLASS(CanvasBitmapUnitTest)
isConverterCreated = true;
};
auto canvasBitmap = f.m_bitmapManager->Create(f.m_canvasDevice.Get(), f.m_testFileName, CanvasAlphaBehavior::Premultiplied);
auto canvasBitmap = f.m_bitmapManager->Create(f.m_canvasDevice.Get(), f.m_testFileName, CanvasAlphaBehavior::Premultiplied, DEFAULT_DPI);
Assert::AreEqual(true, isConverterCreated);
@ -154,7 +154,7 @@ TEST_CLASS(CanvasBitmapUnitTest)
isConverterCreated = true;
};
auto canvasBitmap = f.m_bitmapManager->Create(f.m_canvasDevice.Get(), f.m_testFileName, CanvasAlphaBehavior::Premultiplied);
auto canvasBitmap = f.m_bitmapManager->Create(f.m_canvasDevice.Get(), f.m_testFileName, CanvasAlphaBehavior::Premultiplied, DEFAULT_DPI);
Assert::AreEqual(true, isConverterCreated);
@ -173,7 +173,7 @@ TEST_CLASS(CanvasBitmapUnitTest)
ABI::Windows::Foundation::Rect bounds;
Numerics::Matrix3x2 matrix = { 0 };
auto canvasBitmap = f.m_bitmapManager->Create(f.m_canvasDevice.Get(), f.m_testFileName, CanvasAlphaBehavior::Premultiplied);
auto canvasBitmap = f.m_bitmapManager->Create(f.m_canvasDevice.Get(), f.m_testFileName, CanvasAlphaBehavior::Premultiplied, DEFAULT_DPI);
auto drawingSession = CreateStubDrawingSession();
Assert::AreEqual(E_INVALIDARG, canvasBitmap->GetBounds(nullptr, &bounds));
@ -186,7 +186,7 @@ TEST_CLASS(CanvasBitmapUnitTest)
{
Fixture f;
auto canvasBitmap = f.m_bitmapManager->Create(f.m_canvasDevice.Get(), f.m_testFileName, CanvasAlphaBehavior::Premultiplied);
auto canvasBitmap = f.m_bitmapManager->Create(f.m_canvasDevice.Get(), f.m_testFileName, CanvasAlphaBehavior::Premultiplied, DEFAULT_DPI);
Assert::AreEqual(E_INVALIDARG, canvasBitmap->CopyPixelsFromBitmap(nullptr));
Assert::AreEqual(E_INVALIDARG, canvasBitmap->CopyPixelsFromBitmapWithDestPoint(nullptr, 0, 0));
@ -214,7 +214,7 @@ TEST_CLASS(CanvasBitmapUnitTest)
{
return sourceD2DBitmap;
};
SourceBitmap = m_bitmapManager->Create(canvasDevice.Get(), m_testFileName, CanvasAlphaBehavior::Premultiplied);
SourceBitmap = m_bitmapManager->Create(canvasDevice.Get(), m_testFileName, CanvasAlphaBehavior::Premultiplied, DEFAULT_DPI);
bool expectDestPoint = false;
D2D1_POINT_2U expectedDestPoint;
@ -275,13 +275,10 @@ TEST_CLASS(CanvasBitmapUnitTest)
{
return destD2DBitmap;
};
DestBitmap = m_bitmapManager->Create(canvasDevice.Get(), m_testFileName, CanvasAlphaBehavior::Premultiplied);
DestBitmap = m_bitmapManager->Create(canvasDevice.Get(), m_testFileName, CanvasAlphaBehavior::Premultiplied, DEFAULT_DPI);
}
};
TEST_METHOD_EX(CanvasBitmap_CopyPixelsFromBitmap)
{
CopyFromBitmapFixture f;
@ -327,11 +324,11 @@ TEST_CLASS(CanvasBitmapUnitTest)
auto sourceD2DBitmap = Make<StubD2DBitmap>();
canvasDevice->MockCreateBitmapFromWicResource = [&]() -> ComPtr<ID2D1Bitmap1> { return sourceD2DBitmap; };
auto sourceBitmap = f.m_bitmapManager->Create(canvasDevice.Get(), f.m_testFileName, CanvasAlphaBehavior::Premultiplied);
auto sourceBitmap = f.m_bitmapManager->Create(canvasDevice.Get(), f.m_testFileName, CanvasAlphaBehavior::Premultiplied, DEFAULT_DPI);
auto destD2DBitmap = Make<StubD2DBitmap>();
canvasDevice->MockCreateBitmapFromWicResource = [&]() -> ComPtr<ID2D1Bitmap1> { return destD2DBitmap; };
auto destBitmap = f.m_bitmapManager->Create(canvasDevice.Get(), f.m_testFileName, CanvasAlphaBehavior::Premultiplied);
auto destBitmap = f.m_bitmapManager->Create(canvasDevice.Get(), f.m_testFileName, CanvasAlphaBehavior::Premultiplied, DEFAULT_DPI);
// Negative coordinates
Assert::AreEqual(E_INVALIDARG, destBitmap->CopyPixelsFromBitmapWithDestPoint(sourceBitmap.Get(), -1, 100));

Просмотреть файл

@ -134,7 +134,8 @@ namespace canvas
virtual ComPtr<ID2D1Bitmap1> CreateBitmapFromWicResource(
IWICFormatConverter* converter,
CanvasAlphaBehavior alpha) override
CanvasAlphaBehavior alpha,
float dpi) override
{
if (!MockCreateBitmapFromWicResource)
{