Blender V5.0
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.h"
11#include "BLI_listbase.h"
12#include "BLI_path_utils.hh"
13#include "BLI_string.h"
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
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)) {
37 }
38
39 char file_name[FILE_MAX];
40 SNPRINTF(file_name, "img_%p%s", image, file_ext[0]);
41
42 file_path = blender::io::usd::get_image_cache_file(file_name);
43 if (check_exist && BLI_exists(file_path.c_str())) {
45 return file_path;
46 }
47
48 opts.save_copy = true;
49 STRNCPY(opts.filepath, file_path.c_str());
50 if (BKE_image_save(nullptr, bmain, image, iuser, &opts)) {
51 CLOG_DEBUG(LOG_HYDRA_SCENE, "%s -> %s", image->id.name, file_path.c_str());
52 }
53 else {
54 CLOG_ERROR(LOG_HYDRA_SCENE, "Can't save %s", file_path.c_str());
55 file_path = "";
56 }
57 }
59 return file_path;
60}
61
62std::string cache_or_get_image_file(Main *bmain, Scene *scene, Image *image, ImageUser *iuser)
63{
64 char str[FILE_MAX];
65 std::string file_path;
66 bool do_check_extension = false;
67 if (image->source == IMA_SRC_GENERATED) {
68 file_path = cache_image_file(bmain, scene, image, iuser, false);
69 }
70 else if (BKE_image_has_packedfile(image)) {
71 do_check_extension = true;
72 std::string dir_path = blender::io::usd::image_cache_file_path();
73 char *cached_path;
74 char subfolder[FILE_MAXDIR];
75 SNPRINTF(subfolder, "unpack_%p", image);
77 char path[FILE_MAX];
79 path, sizeof(path), dir_path.c_str(), subfolder, BLI_path_basename(ipf->filepath));
82 dir_path.c_str(),
83 path,
84 ipf->packedfile,
86
87 /* Take first successfully unpacked image. */
88 if (cached_path != nullptr) {
89 if (file_path.empty()) {
90 file_path = cached_path;
91 }
93 }
94 }
95 }
96 else {
97 do_check_extension = true;
98 BKE_image_user_file_path_ex(bmain, iuser, image, str, false, true);
99 file_path = str;
100 }
101
102 if (do_check_extension && !pxr::HioImageRegistry::GetInstance().IsSupportedImageFile(file_path))
103 {
104 file_path = cache_image_file(bmain, scene, image, iuser, true);
105 }
106
107 CLOG_DEBUG(LOG_HYDRA_SCENE, "%s -> %s", image->id.name, file_path.c_str());
108 return file_path;
109}
110
111} // namespace blender::io::hydra
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)
bool BKE_image_has_packedfile(const Image *image)
void BKE_image_format_free(ImageFormatData *imf)
int BKE_image_path_ext_from_imformat(const ImageFormatData *im_format, const char *r_ext[BKE_IMAGE_PATH_EXT_MAX])
#define BKE_IMAGE_PATH_EXT_MAX
void BKE_image_format_copy(ImageFormatData *imf_dst, const ImageFormatData *imf_src)
bool BKE_image_save(ReportList *reports, Main *bmain, Image *ima, ImageUser *iuser, const ImageSaveOptions *opts)
bool BKE_image_save_options_init(ImageSaveOptions *opts, Main *bmain, Scene *scene, Image *ima, ImageUser *iuser, const bool guess_path, const bool save_as_render)
Definition image_save.cc:51
void BKE_image_save_options_free(ImageSaveOptions *opts)
const char * BKE_main_blendfile_path(const Main *bmain) ATTR_NONNULL()
Definition main.cc:887
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)
@ PF_WRITE_LOCAL
File and directory operations.
int BLI_exists(const char *path) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition storage.cc:360
#define LISTBASE_FOREACH(type, var, list)
void void void const char * BLI_path_basename(const char *path) ATTR_NONNULL(1) ATTR_WARN_UNUSED_RESULT
#define FILE_MAX
#define BLI_path_join(...)
const char * BLI_path_extension_or_end(const char *filepath) ATTR_NONNULL(1) ATTR_WARN_UNUSED_RESULT ATTR_RETURNS_NONNULL
#define FILE_MAXDIR
#define SNPRINTF(dst, format,...)
Definition BLI_string.h:604
char * STRNCPY(char(&dst)[N], const char *src)
Definition BLI_string.h:693
#define CLOG_ERROR(clg_ref,...)
Definition CLG_log.h:188
#define CLOG_DEBUG(clg_ref,...)
Definition CLG_log.h:191
@ IMA_SRC_GENERATED
#define str(s)
void MEM_freeN(void *vmemh)
Definition mallocn.cc:113
struct CLG_LogRef * LOG_HYDRA_SCENE
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)
static string cached_path
Definition path.cpp:60
char name[258]
Definition DNA_ID.h:432
ImageFormatData im_format
struct ListBase packedfiles
short source
struct ImageFormatData im_format
struct RenderData r