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 image from a memory buffer (PNG, JPEG, or other stb-supported formats).
42LA_IMAGE_IO_API LoadImageResult load_image_from_buffer(
43 const void* buffer,
44 size_t size,
45 spdlog::level::level_enum error_lvl = spdlog::level::err);
46
47// Load png or jpg image using stb library. Produces uint8 data.
48LA_IMAGE_IO_API LoadImageResult
49load_image_stb(const fs::path& path, spdlog::level::level_enum error_lvl = spdlog::level::err);
50
51// Note: #include <lagrange/image_io/exr.h> to use the full load_image_exr directly
52// Load exr image using tinyexr. Produces multiple data types.
53LA_IMAGE_IO_API LoadImageResult
54load_image_exr(const fs::path& path, spdlog::level::level_enum error_lvl = spdlog::level::err);
55
56// Load image from our custom binary format.
57LA_IMAGE_IO_API LoadImageResult
58load_image_bin(const fs::path& path, spdlog::level::level_enum error_lvl = spdlog::level::err);
59
60// Load image as the provided type/view. Converts if needed/possible. Returns true on success.
61template <typename T>
62bool load_image_as(const fs::path& path, image::ImageView<T>& img)
63{
64 LoadImageResult result = load_image(path);
65 if (!result.valid) return false;
66
67 if (image::ImageTraits<T>::precision == result.precision &&
68 image::ImageTraits<T>::channel == result.channel) {
69 return img.view(
70 result.storage,
71 result.storage->get_full_size()(0) / sizeof(T),
72 result.storage->get_full_size()(1),
73 sizeof(T),
74 1,
75 0,
76 0);
77 }
78
79 bool valid_conversion = false;
80#define LAGRANGE_TMP(PRECISION, CHANNEL, TYPE) \
81 if (image::ImagePrecision::PRECISION == result.precision && \
82 image::ImageChannel::CHANNEL == result.channel) { \
83 image::ImageView<TYPE> temp_image_view( \
84 result.storage, \
85 result.storage->get_full_size()(0) / sizeof(TYPE), \
86 result.storage->get_full_size()(1), \
87 sizeof(TYPE), \
88 1, \
89 0, \
90 0); \
91 valid_conversion = img.convert_from(temp_image_view, 1); \
92 }
93#define LAGRANGE_TMP_COMMA ,
94 // clang-format off
95 LAGRANGE_TMP(uint8, one, unsigned char)
96 else LAGRANGE_TMP(uint8, three, Eigen::Matrix<unsigned char LAGRANGE_TMP_COMMA 3 LAGRANGE_TMP_COMMA 1>)
97 else LAGRANGE_TMP(uint8, four, Eigen::Matrix<unsigned char LAGRANGE_TMP_COMMA 4 LAGRANGE_TMP_COMMA 1>)
98 else LAGRANGE_TMP(uint16, one, uint16_t)
99 else LAGRANGE_TMP(uint16, three, Eigen::Matrix<uint16_t LAGRANGE_TMP_COMMA 3 LAGRANGE_TMP_COMMA 1>)
100 else LAGRANGE_TMP(uint16, four, Eigen::Matrix<uint16_t LAGRANGE_TMP_COMMA 4 LAGRANGE_TMP_COMMA 1>)
101 else LAGRANGE_TMP(float32, one, float)
102 else LAGRANGE_TMP(float32, three, Eigen::Vector3f)
103 else LAGRANGE_TMP(float32, four, Eigen::Vector4f)
104 else LAGRANGE_TMP(float64, one, double)
105 else LAGRANGE_TMP(float64, three, Eigen::Vector3d)
106 else LAGRANGE_TMP(float64, four, Eigen::Vector4d)
107 // clang-format on
108 return valid_conversion;
109#undef LAGRANGE_TMP
110#undef LAGRANGE_TMP_COMMA
111}
112
113} // namespace image_io
114} // namespace lagrange
Definition ImageView.h:56
Main namespace for Lagrange.
Definition load_image.h:28