-
-
Notifications
You must be signed in to change notification settings - Fork 450
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: build Image/ImageFrame from Texture2D (#1083)
* refactor: add ImageFormatExtension * feat: build Image/ImageFrame from Texture2D * refactor: format.NumberOfChannels * refactor: implement ByteDepth as a extension method * refactor: implement ChannelSize as a extension method * refactor: use new constructors in TextureFrame
- Loading branch information
Showing
7 changed files
with
160 additions
and
122 deletions.
There are no files selected for viewing
121 changes: 121 additions & 0 deletions
121
Packages/com.github.homuler.mediapipe/Runtime/Scripts/Extension/ImageFormatExtension.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
// Copyright (c) 2023 homuler | ||
// | ||
// Use of this source code is governed by an MIT-style | ||
// license that can be found in the LICENSE file or at | ||
// https://opensource.org/licenses/MIT. | ||
|
||
namespace Mediapipe | ||
{ | ||
public static class ImageFormatExtension | ||
{ | ||
/// <returns> | ||
/// The number of channels for a <paramref name="format" />. | ||
/// If channels don't make sense in the <paramref name="format" />, returns <c>0</c>. | ||
/// </returns> | ||
/// <remarks> | ||
/// Unlike the original implementation, this API won't signal SIGABRT. | ||
/// </remarks> | ||
public static int NumberOfChannels(this ImageFormat.Types.Format format) | ||
{ | ||
switch (format) | ||
{ | ||
case ImageFormat.Types.Format.Srgb: | ||
case ImageFormat.Types.Format.Srgb48: | ||
return 3; | ||
case ImageFormat.Types.Format.Srgba: | ||
case ImageFormat.Types.Format.Srgba64: | ||
case ImageFormat.Types.Format.Sbgra: | ||
return 4; | ||
case ImageFormat.Types.Format.Gray8: | ||
case ImageFormat.Types.Format.Gray16: | ||
return 1; | ||
case ImageFormat.Types.Format.Vec32F1: | ||
return 1; | ||
case ImageFormat.Types.Format.Vec32F2: | ||
return 2; | ||
case ImageFormat.Types.Format.Vec32F4: | ||
return 4; | ||
case ImageFormat.Types.Format.Lab8: | ||
return 3; | ||
case ImageFormat.Types.Format.Ycbcr420P: | ||
case ImageFormat.Types.Format.Ycbcr420P10: | ||
case ImageFormat.Types.Format.Unknown: | ||
default: | ||
return 0; | ||
} | ||
} | ||
|
||
/// <returns> | ||
/// The depth of each channel in bytes for a <paramref name="format" />. | ||
/// If channels don't make sense in the <paramref name="format" />, returns <c>0</c>. | ||
/// </returns> | ||
/// <remarks> | ||
/// Unlike the original implementation, this API won't signal SIGABRT. | ||
/// </remarks> | ||
public static int ByteDepth(this ImageFormat.Types.Format format) | ||
{ | ||
switch (format) | ||
{ | ||
case ImageFormat.Types.Format.Srgb: | ||
case ImageFormat.Types.Format.Srgba: | ||
case ImageFormat.Types.Format.Sbgra: | ||
return 1; | ||
case ImageFormat.Types.Format.Srgb48: | ||
case ImageFormat.Types.Format.Srgba64: | ||
return 2; | ||
case ImageFormat.Types.Format.Gray8: | ||
return 1; | ||
case ImageFormat.Types.Format.Gray16: | ||
return 2; | ||
case ImageFormat.Types.Format.Vec32F1: | ||
case ImageFormat.Types.Format.Vec32F2: | ||
case ImageFormat.Types.Format.Vec32F4: | ||
return 4; | ||
case ImageFormat.Types.Format.Lab8: | ||
return 1; | ||
case ImageFormat.Types.Format.Ycbcr420P: | ||
case ImageFormat.Types.Format.Ycbcr420P10: | ||
case ImageFormat.Types.Format.Unknown: | ||
default: | ||
return 0; | ||
} | ||
} | ||
|
||
/// <returns> | ||
/// The channel size for a <paramref name="format" />. | ||
/// If channels don't make sense in the <paramref name="format" />, returns <c>0</c>. | ||
/// </returns> | ||
/// <remarks> | ||
/// Unlike the original implementation, this API won't signal SIGABRT. | ||
/// </remarks> | ||
public static int ChannelSize(this ImageFormat.Types.Format format) | ||
{ | ||
switch (format) | ||
{ | ||
case ImageFormat.Types.Format.Srgb: | ||
case ImageFormat.Types.Format.Srgba: | ||
case ImageFormat.Types.Format.Sbgra: | ||
return sizeof(byte); | ||
case ImageFormat.Types.Format.Srgb48: | ||
case ImageFormat.Types.Format.Srgba64: | ||
return sizeof(ushort); | ||
case ImageFormat.Types.Format.Gray8: | ||
return sizeof(byte); | ||
case ImageFormat.Types.Format.Gray16: | ||
return sizeof(ushort); | ||
case ImageFormat.Types.Format.Vec32F1: | ||
case ImageFormat.Types.Format.Vec32F2: | ||
case ImageFormat.Types.Format.Vec32F4: | ||
// sizeof float may be wrong since it's platform-dependent, but we assume that it's constant across all supported platforms. | ||
return sizeof(float); | ||
case ImageFormat.Types.Format.Lab8: | ||
return sizeof(byte); | ||
case ImageFormat.Types.Format.Ycbcr420P: | ||
case ImageFormat.Types.Format.Ycbcr420P10: | ||
case ImageFormat.Types.Format.Unknown: | ||
default: | ||
return 0; | ||
} | ||
} | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
Packages/com.github.homuler.mediapipe/Runtime/Scripts/Extension/ImageFormatExtension.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.