diff --git a/mediapipe/tasks/ios/test/vision/utils/BUILD b/mediapipe/tasks/ios/test/vision/utils/BUILD new file mode 100644 index 000000000..cf7626dbd --- /dev/null +++ b/mediapipe/tasks/ios/test/vision/utils/BUILD @@ -0,0 +1,13 @@ +package(default_visibility = ["//mediapipe/tasks:internal"]) + +licenses(["notice"]) + +objc_library( + name = "MPPImageTestUtils", + srcs = ["sources/MPPImage+TestUtils.m"], + hdrs = ["sources/MPPImage+TestUtils.h"], + module_name = "MPPImageTestUtils", + deps = [ + "//mediapipe/tasks/ios/vision/core:MPPImage", + ], +) diff --git a/mediapipe/tasks/ios/test/vision/utils/sources/MPPImage+TestUtils.h b/mediapipe/tasks/ios/test/vision/utils/sources/MPPImage+TestUtils.h new file mode 100644 index 000000000..7f521e85d --- /dev/null +++ b/mediapipe/tasks/ios/test/vision/utils/sources/MPPImage+TestUtils.h @@ -0,0 +1,51 @@ +// Copyright 2023 The MediaPipe Authors. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#import + +#import "mediapipe/tasks/ios/vision/core/sources/MPPImage.h" + +NS_ASSUME_NONNULL_BEGIN + +/** + * Helper utility for initializing `MPPImage` for MediaPipe iOS vision library tests. + */ +@interface MPPImage (TestUtils) +/** + * Loads an image from a file in an app bundle into a `MPPImage` object. + * + * @param classObject The specified class associated with the bundle containing + * the file to be loaded. + * @param name Name of the image file. + * @param type Extenstion of the image file. + * + * @return The `MPPImage` object contains the loaded image. This method returns + * nil if it cannot load the image. + */ ++ (nullable MPPImage *)imageFromBundleWithClass:(Class)classObject + fileName:(NSString *)name + ofType:(NSString *)type + error:(NSError **)error + NS_SWIFT_NAME(imageFromBundle(class:filename:type:)); + ++ (nullable MPPImage *)imageFromBundleWithClass:(Class)classObject + fileName:(NSString *)name + ofType:(NSString *)type + orientation:(UIImageOrientation)imageOrientation + error:(NSError **)error + NS_SWIFT_NAME(imageFromBundle(class:filename:type:orientation:)); + +@end + +NS_ASSUME_NONNULL_END diff --git a/mediapipe/tasks/ios/test/vision/utils/sources/MPPImage+TestUtils.m b/mediapipe/tasks/ios/test/vision/utils/sources/MPPImage+TestUtils.m new file mode 100644 index 000000000..926d7a900 --- /dev/null +++ b/mediapipe/tasks/ios/test/vision/utils/sources/MPPImage+TestUtils.m @@ -0,0 +1,46 @@ +// Copyright 2023 The MediaPipe Authors. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#import "mediapipe/tasks/ios/test/vision/utils/sources/MPPImage+TestUtils.h" + +@implementation MPPImage (TestUtils) + ++ (nullable MPPImage *)imageFromBundleWithClass:(Class)classObject + fileName:(NSString *)name + ofType:(NSString *)type + error:(NSError **)error { + NSString *imagePath = [[NSBundle bundleForClass:classObject] pathForResource:name ofType:type]; + if (!imagePath) return nil; + + UIImage *image = [[UIImage alloc] initWithContentsOfFile:imagePath]; + if (!image) return nil; + + return [[MPPImage alloc] initWithUIImage:image error:error]; +} + ++ (nullable MPPImage *)imageFromBundleWithClass:(Class)classObject + fileName:(NSString *)name + ofType:(NSString *)type + orientation:(UIImageOrientation)imageOrientation + error:(NSError **)error { + NSString *imagePath = [[NSBundle bundleForClass:classObject] pathForResource:name ofType:type]; + if (!imagePath) return nil; + + UIImage *image = [[UIImage alloc] initWithContentsOfFile:imagePath]; + if (!image) return nil; + + return [[MPPImage alloc] initWithUIImage:image orientation:imageOrientation error:error]; +} + +@end