Blender V4.5
ImageExporter.cpp
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2010-2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include "COLLADABUURI.h"
10#include "COLLADASWImage.h"
11
12#include "DNA_image_types.h"
13
14#include "BKE_image.hh"
15#include "BKE_image_format.hh"
16#include "BKE_library.hh"
17#include "BKE_main.hh"
18
19#include "BLI_fileops.h"
20#include "BLI_path_utils.hh"
21#include "BLI_string.h"
22
23#include "IMB_imbuf_types.hh"
24
25#include "ImageExporter.h"
26
27ImagesExporter::ImagesExporter(COLLADASW::StreamWriter *sw,
28 BCExportSettings &export_settings,
29 KeyImageMap &key_image_map)
30 : COLLADASW::LibraryImages(sw), export_settings(export_settings), key_image_map(key_image_map)
31{
32 /* pass */
33}
34
35void ImagesExporter::export_UV_Image(Image *image, bool use_copies)
36{
37 std::string name(id_name(image));
38 std::string translated_name(translate_id(name));
39
40 ImBuf *imbuf = BKE_image_acquire_ibuf(image, nullptr, nullptr);
41 if (!imbuf) {
42 fprintf(stderr, "Collada export: image does not exist:\n%s\n", image->filepath);
43 return;
44 }
45
46 bool is_dirty = BKE_image_is_dirty(image);
47
48 ImageFormatData imageFormat;
49 BKE_image_format_from_imbuf(&imageFormat, imbuf);
50
51 short image_source = image->source;
52 bool is_generated = image_source == IMA_SRC_GENERATED;
53 bool is_packed = BKE_image_has_packedfile(image);
54
55 char export_path[FILE_MAX];
56 char source_path[FILE_MAX];
57 char export_dir[FILE_MAX];
58 char export_file[FILE_MAX];
59
60 /* Destination folder for exported assets */
61 BLI_path_split_dir_part(this->export_settings.get_filepath(), export_dir, sizeof(export_dir));
62
63 if (is_generated || is_dirty || use_copies || is_packed) {
64
65 /* make absolute destination path */
66
67 STRNCPY(export_file, name.c_str());
68 BKE_image_path_ext_from_imformat_ensure(export_file, sizeof(export_file), &imageFormat);
69
70 BLI_path_join(export_path, sizeof(export_path), export_dir, export_file);
71
73 }
74
75 if (is_generated || is_dirty || is_packed) {
76
77 /* This image in its current state only exists in Blender memory.
78 * So we have to export it. The export will keep the image state intact,
79 * so the exported file will not be associated with the image. */
80
81 if (BKE_imbuf_write_as(imbuf, export_path, &imageFormat, true) == false) {
82 fprintf(stderr, "Collada export: Cannot export image to:\n%s\n", export_path);
83 return;
84 }
85 STRNCPY(export_path, export_file);
86 }
87 else {
88
89 /* make absolute source path */
90 STRNCPY(source_path, image->filepath);
91 BLI_path_abs(source_path, ID_BLEND_PATH_FROM_GLOBAL(&image->id));
92 BLI_path_normalize(source_path);
93
94 if (use_copies) {
95
96 /* This image is already located on the file system.
97 * But we want to create copies here.
98 * To move images into the same export directory.
99 * NOTE: If an image is already located in the export folder,
100 * then skip the copy (as it would result in a file copy error). */
101
102 if (BLI_path_cmp(source_path, export_path) != 0) {
103 if (BLI_copy(source_path, export_path) != 0) {
104 fprintf(stderr,
105 "Collada export: Cannot copy image:\n source:%s\ndest :%s\n",
106 source_path,
107 export_path);
108 return;
109 }
110 }
111
112 STRNCPY(export_path, export_file);
113 }
114 else {
115
116 /* Do not make any copies, but use the source path directly as reference
117 * to the original image */
118
119 STRNCPY(export_path, source_path);
120 }
121 }
122
123 /* Set name also to mNameNC.
124 * This helps other viewers import files exported from Blender better. */
125 COLLADASW::Image img(COLLADABU::URI(COLLADABU::URI::nativePathToUri(export_path)),
126 translated_name,
127 translated_name);
128 img.add(mSW);
129 fprintf(stdout, "Collada export: Added image: %s\n", export_file);
130
131 BKE_image_release_ibuf(image, imbuf, nullptr);
132}
133
135{
136 bool use_texture_copies = this->export_settings.get_use_texture_copies();
137 openLibrary();
138
139 KeyImageMap::iterator iter;
140 for (iter = key_image_map.begin(); iter != key_image_map.end(); iter++) {
141
142 Image *image = iter->second;
143 export_UV_Image(image, use_texture_copies);
144 }
145
146 closeLibrary();
147}
ImBuf * BKE_image_acquire_ibuf(Image *ima, ImageUser *iuser, void **r_lock)
void BKE_image_release_ibuf(Image *ima, ImBuf *ibuf, void *lock)
bool BKE_image_is_dirty(Image *image)
bool BKE_image_has_packedfile(const Image *image)
bool BKE_imbuf_write_as(ImBuf *ibuf, const char *filepath, const ImageFormatData *imf, bool save_copy)
void BKE_image_format_from_imbuf(ImageFormatData *im_format, const ImBuf *imbuf)
int BKE_image_path_ext_from_imformat_ensure(char *filepath, size_t filepath_maxncpy, const ImageFormatData *im_format)
File and directory operations.
int BLI_copy(const char *path_src, const char *path_dst) ATTR_NONNULL()
bool BLI_file_ensure_parent_dir_exists(const char *filepath) ATTR_NONNULL(1)
Definition fileops_c.cc:429
bool BLI_path_abs(char path[FILE_MAX], const char *basepath) ATTR_NONNULL(1
#define FILE_MAX
int BLI_path_normalize(char *path) ATTR_NONNULL(1)
#define BLI_path_join(...)
void void BLI_path_split_dir_part(const char *filepath, char *dir, size_t dir_maxncpy) ATTR_NONNULL(1
#define BLI_path_cmp
char * STRNCPY(char(&dst)[N], const char *src)
Definition BLI_string.h:688
@ IMA_SRC_GENERATED
struct ImageFormatData ImageFormatData
ImagesExporter(COLLADASW::StreamWriter *sw, BCExportSettings &export_settings, KeyImageMap &key_image_map)
void exportImages(Scene *sce)
std::string translate_id(const char *idString)
std::string id_name(void *id)
std::map< std::string, Image * > KeyImageMap
#define ID_BLEND_PATH_FROM_GLOBAL(_id)
char filepath[1024]
short source