Lagrange
Loading...
Searching...
No Matches
load_image.h
1/*
2 * Copyright 2020 Adobe. All rights reserved.
3 * This file is licensed to you under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License. You may obtain a copy
5 * of the License at http://www.apache.org/licenses/LICENSE-2.0
6 *
7 * Unless required by applicable law or agreed to in writing, software distributed under
8 * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
9 * OF ANY KIND, either express or implied. See the License for the specific language
10 * governing permissions and limitations under the License.
11 */
12#pragma once
13
14#include <lagrange/fs/filesystem.h>
15#include <lagrange/image/ImageView.h>
16#include <lagrange/image_io/api.h>
17
18// clang-format off
19#include <lagrange/utils/warnoff.h>
20#include <spdlog/common.h>
21#include <lagrange/utils/warnon.h>
22// clang-format on
23
24namespace lagrange {
25namespace image_io {
26
28{
29 bool valid = false;
30 size_t width = 0;
31 size_t height = 0;
32 image::ImagePrecision precision = image::ImagePrecision::unknown;
33 image::ImageChannel channel = image::ImageChannel::unknown;
34 std::shared_ptr<image::ImageStorage> storage = nullptr;
35};
36
37// Load image. Storage type is determined by the image file type.
38LA_IMAGE_IO_API LoadImageResult
39load_image(const fs::path& path, spdlog::level::level_enum error_lvl = spdlog::level::err);
40
41// Load png or jpg image using stb library. Produces uint8 data.
42LA_IMAGE_IO_API LoadImageResult
43load_image_stb(const fs::path& path, spdlog::level::level_enum error_lvl = spdlog::level::err);
44
45// Note: #include <lagrange/image_io/exr.h> to use the full load_image_exr directly
46// Load exr image using tinyexr. Produces multiple data types.
47LA_IMAGE_IO_API LoadImageResult
48load_image_exr(const fs::path& path, spdlog::level::level_enum error_lvl = spdlog::level::err);
49
50// Load image from our custom binary format.
51LA_IMAGE_IO_API LoadImageResult
52load_image_bin(const fs::path& path, spdlog::level::level_enum error_lvl = spdlog::level::err);
53
54// Load image as the provided type/view. Converts if needed/possible. Returns true on success.
55template <typename T>
56bool load_image_as(const fs::path& path, image::ImageView<T>& img)
57{
58 LoadImageResult result = load_image(path);
59 if (!result.valid) return false;
60
61 if (image::ImageTraits<T>::precision == result.precision &&
62 image::ImageTraits<T>::channel == result.channel) {
63 return img.view(
64 result.storage,
65 result.storage->get_full_size()(0) / sizeof(T),
66 result.storage->get_full_size()(1),
67 sizeof(T),
68 1,
69 0,
70 0);
71 }
72
73 bool valid_conversion = false;
74#define LAGRANGE_TMP(PRECISION, CHANNEL, TYPE) \
75 if (image::ImagePrecision::PRECISION == result.precision && \
76 image::ImageChannel::CHANNEL == result.channel) { \
77 image::ImageView<TYPE> temp_image_view( \
78 result.storage, \
79 result.storage->get_full_size()(0) / sizeof(TYPE), \
80 result.storage->get_full_size()(1), \
81 sizeof(TYPE), \
82 1, \
83 0, \
84 0); \
85 valid_conversion = img.convert_from(temp_image_view, 1); \
86 }
87#define LAGRANGE_TMP_COMMA ,
88 LAGRANGE_TMP(uint8, one, unsigned char)
89 else LAGRANGE_TMP(uint8, three, Eigen::Matrix<unsigned char LAGRANGE_TMP_COMMA 3 LAGRANGE_TMP_COMMA 1>) else LAGRANGE_TMP(
90 uint8,
91 four,
92 Eigen::
93 Matrix<
94 unsigned char LAGRANGE_TMP_COMMA 4 LAGRANGE_TMP_COMMA 1>) else LAGRANGE_TMP(float32, one, float) else LAGRANGE_TMP(float32, three, Eigen::Vector3f) else LAGRANGE_TMP(float32, four, Eigen::Vector4f) else LAGRANGE_TMP(float64, one, double) else LAGRANGE_TMP(float64, three, Eigen::Vector3d) else LAGRANGE_TMP(float64, four, Eigen::Vector4d) return valid_conversion;
95#undef LAGRANGE_TMP
96#undef LAGRANGE_TMP_COMMA
97}
98
99} // namespace image_io
100} // namespace lagrange
Definition ImageView.h:56
Main namespace for Lagrange.
Definition load_image.h:28