Lagrange
All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Modules Pages
load_image.h
1/*
2 * Copyright 2023 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
18namespace lagrange {
19namespace image_io {
20
22{
23 bool valid = false;
24 size_t width = 0;
25 size_t height = 0;
26 image::ImagePrecision precision = image::ImagePrecision::unknown;
27 image::ImageChannel channel = image::ImageChannel::unknown;
28 std::shared_ptr<image::ImageStorage> storage = nullptr;
29};
30
31// Load image. Storage type is determined by the image file type.
32LA_IMAGE_IO_API LoadImageResult load_image(const fs::path& path);
33
34// Load png or jpg image using stb library. Produces uint8 data.
35LA_IMAGE_IO_API LoadImageResult load_image_stb(const fs::path& path);
36
37// Note: #include <lagrange/image_io/exr.h> to use the full load_image_exr directly
38// Load exr image using tinyexr. Produces multiple data types.
39LA_IMAGE_IO_API LoadImageResult load_image_exr(const fs::path& path);
40
41// Load image from our custom binary format.
42LA_IMAGE_IO_API LoadImageResult load_image_bin(const fs::path& path);
43
44// Load image as the provided type/view. Converts if needed/possible. Returns true on success.
45template <typename T>
46bool load_image_as(const fs::path& path, image::ImageView<T>& img)
47{
48 LoadImageResult result = load_image(path);
49 if (!result.valid) return false;
50
51 if (image::ImageTraits<T>::precision == result.precision &&
52 image::ImageTraits<T>::channel == result.channel) {
53 return img.view(
54 result.storage,
55 result.storage->get_full_size()(0) / sizeof(T),
56 result.storage->get_full_size()(1),
57 sizeof(T),
58 1,
59 0,
60 0);
61 }
62
63 bool valid_conversion = false;
64#define LAGRANGE_TMP(PRECISION, CHANNEL, TYPE) \
65 if (image::ImagePrecision::PRECISION == result.precision && \
66 image::ImageChannel::CHANNEL == result.channel) { \
67 image::ImageView<TYPE> temp_image_view( \
68 result.storage, \
69 result.storage->get_full_size()(0) / sizeof(TYPE), \
70 result.storage->get_full_size()(1), \
71 sizeof(TYPE), \
72 1, \
73 0, \
74 0); \
75 valid_conversion = img.convert_from(temp_image_view, 1); \
76 }
77#define LAGRANGE_TMP_COMMA ,
78 LAGRANGE_TMP(uint8, one, unsigned char)
79 else LAGRANGE_TMP(uint8, three, Eigen::Matrix<unsigned char LAGRANGE_TMP_COMMA 3 LAGRANGE_TMP_COMMA 1>) else LAGRANGE_TMP(
80 uint8,
81 four,
82 Eigen::
83 Matrix<
84 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;
85#undef LAGRANGE_TMP
86#undef LAGRANGE_TMP_COMMA
87}
88
89} // namespace image_io
90} // namespace lagrange
Definition: ImageView.h:56
Main namespace for Lagrange.
Definition: AABBIGL.h:30
Definition: ImageType.h:58
Definition: load_image.h:22