Blender V4.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.h"
11#include "BLI_path_utils.hh"
12#include "BLI_string.h"
13
14#include "BKE_appdir.hh"
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 "IMB_imbuf.hh"
22#include "IMB_imbuf_types.hh"
23
25
26namespace blender::io::hydra {
27
28static std::string cache_image_file(
29 Main *bmain, Scene *scene, Image *image, ImageUser *iuser, bool check_exist)
30{
31 std::string file_path;
33 if (BKE_image_save_options_init(&opts, bmain, scene, image, iuser, false, false)) {
34 char file_name[32];
35 const char *r_ext = BLI_path_extension_or_end(image->id.name);
36 if (!pxr::HioImageRegistry::GetInstance().IsSupportedImageFile(image->id.name)) {
37 BKE_image_path_ext_from_imformat(&scene->r.im_format, &r_ext);
39 BKE_image_format_copy(&opts.im_format, &scene->r.im_format);
40 }
41
42 SNPRINTF(file_name, "img_%p%s", image, r_ext);
43
44 file_path = blender::io::usd::get_image_cache_file(file_name);
45 if (check_exist && BLI_exists(file_path.c_str())) {
47 return file_path;
48 }
49
50 opts.save_copy = true;
51 STRNCPY(opts.filepath, file_path.c_str());
52 if (BKE_image_save(nullptr, bmain, image, iuser, &opts)) {
53 CLOG_INFO(LOG_HYDRA_SCENE, 1, "%s -> %s", image->id.name, file_path.c_str());
54 }
55 else {
56 CLOG_ERROR(LOG_HYDRA_SCENE, "Can't save %s", file_path.c_str());
57 file_path = "";
58 }
59 }
61 return file_path;
62}
63
64std::string cache_or_get_image_file(Main *bmain, Scene *scene, Image *image, ImageUser *iuser)
65{
66 char str[FILE_MAX];
67 std::string file_path;
68 bool do_check_extension = false;
69 if (image->source == IMA_SRC_GENERATED) {
70 file_path = cache_image_file(bmain, scene, image, iuser, false);
71 }
72 else if (BKE_image_has_packedfile(image)) {
73 do_check_extension = true;
74 std::string dir_path = blender::io::usd::image_cache_file_path();
75 char *cached_path;
76 char subfolder[FILE_MAXDIR];
77 SNPRINTF(subfolder, "unpack_%p", image);
78 LISTBASE_FOREACH (ImagePackedFile *, ipf, &image->packedfiles) {
79 char path[FILE_MAX];
81 path, sizeof(path), dir_path.c_str(), subfolder, BLI_path_basename(ipf->filepath));
84 dir_path.c_str(),
85 path,
86 ipf->packedfile,
88
89 /* Take first successfully unpacked image. */
90 if (cached_path != nullptr) {
91 if (file_path.empty()) {
92 file_path = cached_path;
93 }
95 }
96 }
97 }
98 else {
99 do_check_extension = true;
100 BKE_image_user_file_path_ex(bmain, iuser, image, str, false, true);
101 file_path = str;
102 }
103
104 if (do_check_extension && !pxr::HioImageRegistry::GetInstance().IsSupportedImageFile(file_path))
105 {
106 file_path = cache_image_file(bmain, scene, image, iuser, true);
107 }
108
109 CLOG_INFO(LOG_HYDRA_SCENE, 1, "%s -> %s", image->id.name, file_path.c_str());
110 return file_path;
111}
112
113} // 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])
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:82
void BKE_image_save_options_free(ImageSaveOptions *opts)
const char * BKE_main_blendfile_path(const Main *bmain) ATTR_NONNULL()
Definition main.cc:832
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:350
#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 STRNCPY(dst, src)
Definition BLI_string.h:593
#define SNPRINTF(dst, format,...)
Definition BLI_string.h:597
#define CLOG_ERROR(clg_ref,...)
Definition CLG_log.h:182
#define CLOG_INFO(clg_ref, level,...)
Definition CLG_log.h:179
@ IMA_SRC_GENERATED
Contains defines and structs used throughout the imbuf module.
#define str(s)
void MEM_freeN(void *vmemh)
Definition mallocn.cc:105
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
ImageFormatData im_format