Blender V5.3
source/blender/io/usd/hydra/image.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2011-2022 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#include "image.hh"
6#include "usd_private.hh"
7
8#include <pxr/imaging/hio/imageRegistry.h>
9
10#include "BLI_fileops.hh"
12#include "BLI_path_utils.hh"
13#include "BLI_string.hh"
14
15#include "BKE_image.hh"
16#include "BKE_image_format.hh"
17#include "BKE_image_save.hh"
18#include "BKE_main.hh"
19#include "BKE_packedFile.hh"
20
21#include "scene_index.hh"
22
23namespace blender::io::hydra {
24
25static std::string cache_image_file(
26 Main *bmain, Scene *scene, Image *image, ImageUser *iuser, bool check_exist)
27{
28 std::string file_path;
30 if (BKE_image_save_options_init(&opts, bmain, scene, image, iuser, false, false)) {
31 const char *file_ext[BKE_IMAGE_PATH_EXT_MAX];
32 file_ext[0] = BLI_path_extension_or_end(image->id.name);
33 if (!pxr::HioImageRegistry::GetInstance().IsSupportedImageFile(image->id.name)) {
36 }
37
38 char file_name[FILE_MAX];
39 SNPRINTF(file_name, "img_%p%s", image, file_ext[0]);
40
41 file_path = io::usd::get_image_cache_file(file_name);
42 if (check_exist && BLI_exists(file_path.c_str())) {
44 return file_path;
45 }
46
47 opts.save_copy = true;
48 STRNCPY(opts.filepath, file_path.c_str());
49 if (BKE_image_save(nullptr, bmain, image, iuser, &opts)) {
50 CLOG_DEBUG(LOG_HYDRA_SCENE_INDEX, "%s -> %s", image->id.name, file_path.c_str());
51 }
52 else {
53 CLOG_ERROR(LOG_HYDRA_SCENE_INDEX, "Can't save %s", file_path.c_str());
54 file_path = "";
55 }
56 }
58 return file_path;
59}
60
61std::string cache_or_get_image_file(Main *bmain, Scene *scene, Image *image, ImageUser *iuser)
62{
63 char str[FILE_MAX];
64 std::string file_path;
65 bool do_check_extension = false;
66 if (image->source == IMA_SRC_GENERATED) {
67 file_path = cache_image_file(bmain, scene, image, iuser, false);
68 }
70 do_check_extension = true;
71 std::string dir_path = io::usd::image_cache_file_path();
72 char *cached_path;
73 char subfolder[FILE_MAXDIR];
74 SNPRINTF(subfolder, "unpack_%p", image);
75 for (ImagePackedFile &ipf : image->packedfiles) {
76 char path[FILE_MAX];
78 path, sizeof(path), dir_path.c_str(), subfolder, BLI_path_basename(ipf.filepath));
79 cached_path = BKE_packedfile_unpack_to_file(nullptr,
81 dir_path.c_str(),
82 path,
83 ipf.packedfile,
85
86 /* Take first successfully unpacked image. */
87 if (cached_path != nullptr) {
88 if (file_path.empty()) {
89 file_path = cached_path;
90 }
91 MEM_delete(cached_path);
92 }
93 }
94 }
95 else {
96 do_check_extension = true;
97 BKE_image_user_file_path_ex(bmain, iuser, image, str, false, true);
98 file_path = str;
99 }
100
101 if (do_check_extension && !pxr::HioImageRegistry::GetInstance().IsSupportedImageFile(file_path))
102 {
103 file_path = cache_image_file(bmain, scene, image, iuser, true);
104 }
105
106 CLOG_DEBUG(LOG_HYDRA_SCENE_INDEX, "%s -> %s", image->id.name, file_path.c_str());
107 return file_path;
108}
109
110} // namespace blender::io::hydra
#define BKE_IMAGE_PATH_EXT_MAX
File and directory operations.
#define FILE_MAX
#define BLI_path_join(...)
#define FILE_MAXDIR
#define SNPRINTF(dst, format,...)
#define CLOG_ERROR(clg_ref,...)
Definition CLG_log.h:188
#define CLOG_DEBUG(clg_ref,...)
Definition CLG_log.h:191
#define str(s)
CLG_LogRef * LOG_HYDRA_SCENE_INDEX
std::string cache_or_get_image_file(Main *bmain, Scene *scene, Image *image, ImageUser *iuser)
static std::string cache_image_file(Main *bmain, Scene *scene, Image *image, ImageUser *iuser, bool check_exist)
std::string image_cache_file_path()
std::string get_image_cache_file(const std::string &file_name, bool mkdir)
const char * BLI_path_extension_or_end(const char *filepath) ATTR_NONNULL(1) ATTR_WARN_UNUSED_RESULT ATTR_RETURNS_NONNULL
char * STRNCPY(char(&dst)[N], const char *src)
void BKE_image_format_set(ImageFormatData *imf, ID *owner_id, const char imtype)
bool BLI_exists(const char *path) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition storage.cc:404
bool BKE_image_save_options_init(ImageSaveOptions *opts, Main *bmain, Scene *scene, Image *ima, ImageUser *iuser, bool guess_path, bool save_as_render)
Definition image_save.cc:51
const char * BKE_main_blendfile_path(const Main *bmain) ATTR_NONNULL()
Definition main.cc:1058
void void void const char * BLI_path_basename(const char *path) ATTR_NONNULL(1) ATTR_WARN_UNUSED_RESULT
char * BKE_packedfile_unpack_to_file(ReportList *reports, const char *ref_file_name, const char *abs_name, const char *local_name, PackedFile *pf, enum ePF_FileStatus how)
void BKE_image_save_options_free(ImageSaveOptions *opts)
bool BKE_image_has_packedfile(const Image *image)
int BKE_image_path_ext_from_imformat(const ImageFormatData *im_format, const char *r_ext[BKE_IMAGE_PATH_EXT_MAX])
bool BKE_image_save(ReportList *reports, Main *bmain, Image *ima, ImageUser *iuser, const ImageSaveOptions *opts)
void BKE_image_user_file_path_ex(const Main *bmain, const ImageUser *iuser, const Image *ima, char *filepath, const bool resolve_udim, const bool resolve_multiview)
struct PackedFile * packedfile