Internal change

PiperOrigin-RevId: 488761646
This commit is contained in:
Camillo Lugaresi 2022-11-15 14:35:54 -08:00 committed by Copybara-Service
parent e65f21e2d8
commit 7a87546c30
2 changed files with 17 additions and 9 deletions

View File

@ -258,11 +258,8 @@ std::string GetTestFilePath(absl::string_view relative_path) {
return file::JoinPath(GetTestRootDir(), relative_path); return file::JoinPath(GetTestRootDir(), relative_path);
} }
absl::StatusOr<std::unique_ptr<ImageFrame>> LoadTestImage( absl::StatusOr<std::unique_ptr<ImageFrame>> DecodeTestImage(
absl::string_view path, ImageFormat::Format format) { absl::string_view encoded, ImageFormat::Format format) {
std::string encoded;
MP_RETURN_IF_ERROR(mediapipe::file::GetContents(path, &encoded));
// stbi_load determines the output pixel format based on the desired channels. // stbi_load determines the output pixel format based on the desired channels.
// 0 means "use whatever's in the file". // 0 means "use whatever's in the file".
int desired_channels = format == ImageFormat::UNKNOWN ? 0 int desired_channels = format == ImageFormat::UNKNOWN ? 0
@ -274,10 +271,10 @@ absl::StatusOr<std::unique_ptr<ImageFrame>> LoadTestImage(
<< "unsupported output format requested: " << format; << "unsupported output format requested: " << format;
int width, height, channels_in_file; int width, height, channels_in_file;
auto data = stbi_load_from_memory(reinterpret_cast<stbi_uc*>(encoded.data()), auto data = stbi_load_from_memory(
encoded.size(), &width, &height, reinterpret_cast<const stbi_uc*>(encoded.data()), encoded.size(), &width,
&channels_in_file, desired_channels); &height, &channels_in_file, desired_channels);
RET_CHECK(data) << "failed to decode image data from: " << path; RET_CHECK(data) << "failed to decode image data";
// If we didn't specify a desired format, it will be determined by what the // If we didn't specify a desired format, it will be determined by what the
// file contains. // file contains.
@ -295,6 +292,13 @@ absl::StatusOr<std::unique_ptr<ImageFrame>> LoadTestImage(
format, width, height, width * output_channels, data, stbi_image_free); format, width, height, width * output_channels, data, stbi_image_free);
} }
absl::StatusOr<std::unique_ptr<ImageFrame>> LoadTestImage(
absl::string_view path, ImageFormat::Format format) {
std::string encoded;
MP_RETURN_IF_ERROR(mediapipe::file::GetContents(path, &encoded));
return DecodeTestImage(encoded, format);
}
std::unique_ptr<ImageFrame> LoadTestPng(absl::string_view path, std::unique_ptr<ImageFrame> LoadTestPng(absl::string_view path,
ImageFormat::Format format) { ImageFormat::Format format) {
return nullptr; return nullptr;

View File

@ -81,6 +81,10 @@ std::string GetTestDataDir(absl::string_view package_base_path);
// Loads a binary graph from path. Returns true iff successful. // Loads a binary graph from path. Returns true iff successful.
bool LoadTestGraph(CalculatorGraphConfig* proto, const std::string& path); bool LoadTestGraph(CalculatorGraphConfig* proto, const std::string& path);
// Loads an image from memory.
absl::StatusOr<std::unique_ptr<ImageFrame>> DecodeTestImage(
absl::string_view encoded, ImageFormat::Format format = ImageFormat::SRGBA);
// Loads an image from path. // Loads an image from path.
absl::StatusOr<std::unique_ptr<ImageFrame>> LoadTestImage( absl::StatusOr<std::unique_ptr<ImageFrame>> LoadTestImage(
absl::string_view path, ImageFormat::Format format = ImageFormat::SRGBA); absl::string_view path, ImageFormat::Format format = ImageFormat::SRGBA);