Skip to content

Commit

Permalink
Add read/write support for the mask region types
Browse files Browse the repository at this point in the history
This requires LibHeif version 1.17.0 or later, it will throw an
exception on older versions.
  • Loading branch information
0xC0000054 committed Nov 5, 2023
1 parent 7542c4d commit 0b1b190
Show file tree
Hide file tree
Showing 13 changed files with 848 additions and 11 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## Unreleased

## Added

* Support for reading and writing the inline and reference mask region items.
* Requires LibHeif 1.17.0 or later.

## v3.1.0 - 2023-05-16

## Added
Expand Down
4 changes: 2 additions & 2 deletions src/HeifContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ namespace LibHeifSharp
/// </summary>
/// <seealso cref="Disposable" />
/// <threadsafety static="true" instance="false"/>
public sealed class HeifContext : Disposable
public sealed class HeifContext : Disposable, IHeifContext
{
private LibHeifInitializationContext initializationContext;
private SafeHeifContext context;
Expand Down Expand Up @@ -845,7 +845,7 @@ public HeifRegionItem GetRegionItem(HeifRegionItemId id)
out var regionItem);
error.ThrowIfError();

return new HeifRegionItem(regionItem, id);
return new HeifRegionItem(regionItem, id, this);
}
else
{
Expand Down
29 changes: 29 additions & 0 deletions src/IHeifContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* .NET bindings for libheif.
* Copyright (c) 2020, 2021, 2022, 2023 Nicholas Hayes
*
* Portions Copyright (c) 2017 struktur AG, Dirk Farin <[email protected]>
*
* This file is part of libheif-sharp.
*
* libheif-sharp is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 3 of
* the License, or (at your option) any later version.
*
* libheif-sharp is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with libheif-sharp. If not, see <http://www.gnu.org/licenses/>.
*/

namespace LibHeifSharp
{
internal interface IHeifContext
{
HeifImageHandle GetImageHandle(HeifItemId imageId);
}
}
1 change: 1 addition & 0 deletions src/IgnoredWords.dic
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ ftyp
hdr
Heif
imir
inline
Interop
ipco
ipma
Expand Down
3 changes: 3 additions & 0 deletions src/Interop/LibHeif/NativeMethods/LibHeifNative.Image.cs
Original file line number Diff line number Diff line change
Expand Up @@ -137,5 +137,8 @@ internal static extern unsafe int heif_image_get_decoding_warnings(SafeHeifImage

[DllImport(DllName, CallingConvention = DllCallingConvention)]
internal static extern void heif_image_set_pixel_aspect_ratio(SafeHeifImage image, uint aspectH, uint aspectV);

[DllImport(DllName, CallingConvention = DllCallingConvention)]
internal static extern int heif_image_get_bits_per_pixel_range(SafeHeifImage image, HeifChannel channel);
}
}
38 changes: 38 additions & 0 deletions src/Interop/LibHeif/NativeMethods/LibHeifNative.Regions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,25 @@ internal static extern unsafe heif_error heif_region_get_polyline_points_transfo
double* points,
HeifItemId imageId);

[DllImport(DllName, CallingConvention = DllCallingConvention)]
internal static extern heif_error heif_region_get_referenced_mask_ID(heif_region region,
out int x,
out int y,
out uint width,
out uint height,
out HeifItemId maskItemId);

[DllImport(DllName, CallingConvention = DllCallingConvention)]
internal static extern UIntPtr heif_region_get_inline_mask_data_len(heif_region region);

[DllImport(DllName, CallingConvention = DllCallingConvention)]
internal static extern unsafe heif_error heif_region_get_inline_mask_data(heif_region region,
out int x,
out int y,
out uint width,
out uint height,
byte* maskData);

[DllImport(DllName, CallingConvention = DllCallingConvention)]
internal static extern heif_error heif_region_item_add_region_point(SafeHeifRegionItem handle,
int x,
Expand Down Expand Up @@ -150,5 +169,24 @@ internal static extern unsafe heif_error heif_region_item_add_region_polyline(Sa
int* points,
int pointCount,
IntPtr outRegion);

[DllImport(DllName, CallingConvention = DllCallingConvention)]
internal static extern unsafe heif_error heif_region_item_add_region_inline_mask_data(SafeHeifRegionItem handle,
int x,
int y,
uint width,
uint height,
byte* maskData,
UIntPtr maskDataLength,
IntPtr outRegion);

[DllImport(DllName, CallingConvention = DllCallingConvention)]
internal static extern unsafe heif_error heif_region_item_add_region_referenced_mask(SafeHeifRegionItem handle,
int x,
int y,
uint width,
uint height,
HeifItemId maskItemId,
IntPtr outRegion);
}
}
8 changes: 8 additions & 0 deletions src/LibHeifVersion.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,14 @@ internal static class LibHeifVersion
/// </value>
internal static bool Is1Point16Point2OrLater => LibHeifInfo.VersionNumber >= 0x01100200;

/// <summary>
/// Gets a value indicating whether the LibHeif version is at least 1.17.0.
/// </summary>
/// <value>
/// <c>true</c> if the LibHeif version is at least 1.17.0; otherwise, <c>false</c>.
/// </value>
internal static bool Is1Point17OrLater => LibHeifInfo.VersionNumber >= 0x01110000;

/// <summary>
/// Throws an exception if the LibHeif version is not supported.
/// </summary>
Expand Down
63 changes: 63 additions & 0 deletions src/Properties/Resources.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 22 additions & 0 deletions src/Properties/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,9 @@
<data name="CannotGetAllTransformationPropertyIds" xml:space="preserve">
<value>Could not get all of the transformation property ids.</value>
</data>
<data name="CannotGetInlineMaskDataSize" xml:space="preserve">
<value>Could not get the inline mask data size.</value>
</data>
<data name="CoerceStringValueToBooleanFailedFormat" xml:space="preserve">
<value>Unable to convert the {0} parameter to a Boolean.</value>
<comment>{0} is the parameter name.</comment>
Expand Down Expand Up @@ -200,6 +203,15 @@
<data name="ImageUndefinedIspeWidth" xml:space="preserve">
<value>The image has an undefined width.</value>
</data>
<data name="InlineMaskDataLargerThan2GB" xml:space="preserve">
<value>The inline mask data is larger than 2GB.</value>
</data>
<data name="InlineMaskImageMustBeEightBitsPerChannel" xml:space="preserve">
<value>The inline mask image Y channel must be 8-bit.</value>
</data>
<data name="InlineMaskImageMustHaveYChannel" xml:space="preserve">
<value>The inline mask image must have a Y channel.</value>
</data>
<data name="LibHeifVersionNotSupportedFormat" xml:space="preserve">
<value>LibHeif must be version {0}.{1}.{2} or later.</value>
<comment>{0} is the major version. {1} is the minor version. {2} is the maintenance version.</comment>
Expand All @@ -217,6 +229,10 @@
<value>'{0}' is an empty array.</value>
<comment>{0} is the parameter name.</comment>
</data>
<data name="ParameterMustBeGreaterThanOrEqualFormat" xml:space="preserve">
<value>'{0}' must be greater than or equal to {1}.</value>
<comment>{0} is the parameter name.</comment>
</data>
<data name="ParameterMustBePositive" xml:space="preserve">
<value>Must be a positive number.</value>
</data>
Expand Down Expand Up @@ -244,6 +260,12 @@
<data name="RegionAPINotSupported" xml:space="preserve">
<value>This method requires LibHeif version 1.16.0 or later.</value>
</data>
<data name="RegionMaskAPINotSupported" xml:space="preserve">
<value>Region mask support requires LibHeif 1.17.0 or later.</value>
</data>
<data name="RegionMaskBitmapTooLargeFormat" xml:space="preserve">
<value>The region mask bitmap dimensions are too large, width={0}, height={1}.</value>
</data>
<data name="StreamCannotReadAndSeek" xml:space="preserve">
<value>The stream must support reading and seeking.</value>
</data>
Expand Down
Loading

0 comments on commit 0b1b190

Please sign in to comment.