Blender  V3.3
ImageExporter.cpp
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 
7 #include "COLLADABUURI.h"
8 #include "COLLADASWImage.h"
9 
10 #include "DNA_image_types.h"
11 #include "DNA_meshdata_types.h"
12 #include "DNA_texture_types.h"
13 
14 #include "BKE_customdata.h"
15 #include "BKE_global.h"
16 #include "BKE_image.h"
17 #include "BKE_image_format.h"
18 #include "BKE_main.h"
19 #include "BKE_mesh.h"
20 
21 #include "BLI_fileops.h"
22 #include "BLI_path_util.h"
23 #include "BLI_string.h"
24 
25 #include "IMB_imbuf_types.h"
26 
27 #include "ImageExporter.h"
28 #include "MaterialExporter.h"
29 
30 ImagesExporter::ImagesExporter(COLLADASW::StreamWriter *sw,
31  BCExportSettings &export_settings,
32  KeyImageMap &key_image_map)
33  : COLLADASW::LibraryImages(sw), export_settings(export_settings), key_image_map(key_image_map)
34 {
35  /* pass */
36 }
37 
38 void ImagesExporter::export_UV_Image(Image *image, bool use_copies)
39 {
40  std::string name(id_name(image));
41  std::string translated_name(translate_id(name));
42 
43  ImBuf *imbuf = BKE_image_acquire_ibuf(image, nullptr, nullptr);
44  if (!imbuf) {
45  fprintf(stderr, "Collada export: image does not exist:\n%s\n", image->filepath);
46  return;
47  }
48 
49  bool is_dirty = BKE_image_is_dirty(image);
50 
51  ImageFormatData imageFormat;
52  BKE_image_format_from_imbuf(&imageFormat, imbuf);
53 
54  short image_source = image->source;
55  bool is_generated = image_source == IMA_SRC_GENERATED;
56  bool is_packed = BKE_image_has_packedfile(image);
57 
58  char export_path[FILE_MAX];
59  char source_path[FILE_MAX];
60  char export_dir[FILE_MAX];
61  char export_file[FILE_MAX];
62 
63  /* Destination folder for exported assets */
64  BLI_split_dir_part(this->export_settings.get_filepath(), export_dir, sizeof(export_dir));
65 
66  if (is_generated || is_dirty || use_copies || is_packed) {
67 
68  /* make absolute destination path */
69 
70  BLI_strncpy(export_file, name.c_str(), sizeof(export_file));
71  BKE_image_path_ensure_ext_from_imformat(export_file, &imageFormat);
72 
73  BLI_join_dirfile(export_path, sizeof(export_path), export_dir, export_file);
74 
75  /* make dest directory if it doesn't exist */
76  BLI_make_existing_file(export_path);
77  }
78 
79  if (is_generated || is_dirty || is_packed) {
80 
81  /* This image in its current state only exists in Blender memory.
82  * So we have to export it. The export will keep the image state intact,
83  * so the exported file will not be associated with the image. */
84 
85  if (BKE_imbuf_write_as(imbuf, export_path, &imageFormat, true) == 0) {
86  fprintf(stderr, "Collada export: Cannot export image to:\n%s\n", export_path);
87  return;
88  }
89  BLI_strncpy(export_path, export_file, sizeof(export_path));
90  }
91  else {
92 
93  /* make absolute source path */
94  BLI_strncpy(source_path, image->filepath, sizeof(source_path));
95  BLI_path_abs(source_path, ID_BLEND_PATH_FROM_GLOBAL(&image->id));
96  BLI_path_normalize(nullptr, source_path);
97 
98  if (use_copies) {
99 
100  /* This image is already located on the file system.
101  * But we want to create copies here.
102  * To move images into the same export directory.
103  * NOTE: If an image is already located in the export folder,
104  * then skip the copy (as it would result in a file copy error). */
105 
106  if (BLI_path_cmp(source_path, export_path) != 0) {
107  if (BLI_copy(source_path, export_path) != 0) {
108  fprintf(stderr,
109  "Collada export: Cannot copy image:\n source:%s\ndest :%s\n",
110  source_path,
111  export_path);
112  return;
113  }
114  }
115 
116  BLI_strncpy(export_path, export_file, sizeof(export_path));
117  }
118  else {
119 
120  /* Do not make any copies, but use the source path directly as reference
121  * to the original image */
122 
123  BLI_strncpy(export_path, source_path, sizeof(export_path));
124  }
125  }
126 
127  /* Set name also to mNameNC.
128  * This helps other viewers import files exported from Blender better. */
129  COLLADASW::Image img(COLLADABU::URI(COLLADABU::URI::nativePathToUri(export_path)),
130  translated_name,
131  translated_name);
132  img.add(mSW);
133  fprintf(stdout, "Collada export: Added image: %s\n", export_file);
134 
135  BKE_image_release_ibuf(image, imbuf, nullptr);
136 }
137 
139 {
140  bool use_texture_copies = this->export_settings.get_use_texture_copies();
141  openLibrary();
142 
143  KeyImageMap::iterator iter;
144  for (iter = key_image_map.begin(); iter != key_image_map.end(); iter++) {
145 
146  Image *image = iter->second;
147  std::string uid(id_name(image));
148  std::string key = translate_id(uid);
149 
150  export_UV_Image(image, use_texture_copies);
151  }
152 
153  closeLibrary();
154 }
CustomData interface, see also DNA_customdata_types.h.
void BKE_image_release_ibuf(struct Image *ima, struct ImBuf *ibuf, void *lock)
int BKE_imbuf_write_as(struct ImBuf *ibuf, const char *name, const struct ImageFormatData *imf, bool save_copy)
struct ImBuf * BKE_image_acquire_ibuf(struct Image *ima, struct ImageUser *iuser, void **r_lock)
bool BKE_image_has_packedfile(const struct Image *image)
bool BKE_image_is_dirty(struct Image *image)
void BKE_image_format_from_imbuf(struct ImageFormatData *im_format, const struct ImBuf *imbuf)
int BKE_image_path_ensure_ext_from_imformat(char *string, const struct ImageFormatData *im_format)
File and directory operations.
int BLI_copy(const char *file, const char *to) ATTR_NONNULL()
Definition: fileops.c:1198
void BLI_split_dir_part(const char *string, char *dir, size_t dirlen)
Definition: path_util.c:1490
bool BLI_make_existing_file(const char *name)
Definition: path_util.c:1197
#define FILE_MAX
void BLI_path_normalize(const char *relabase, char *path) ATTR_NONNULL(2)
Definition: path_util.c:131
bool BLI_path_abs(char *path, const char *basepath) ATTR_NONNULL()
Definition: path_util.c:897
#define BLI_path_cmp
void BLI_join_dirfile(char *__restrict dst, size_t maxlen, const char *__restrict dir, const char *__restrict file) ATTR_NONNULL()
Definition: path_util.c:1531
char * BLI_strncpy(char *__restrict dst, const char *__restrict src, size_t maxncpy) ATTR_NONNULL()
Definition: string.c:64
#define ID_BLEND_PATH_FROM_GLOBAL(_id)
Definition: DNA_ID.h:561
@ IMA_SRC_GENERATED
struct Image Image
Contains defines and structs used throughout the imbuf module.
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
Definition: collada_utils.h:56
depth_tx normal_tx diffuse_light_tx specular_light_tx volume_light_tx environment_tx ambient_occlusion_tx aov_value_tx in_weight_img image(1, GPU_R32F, Qualifier::WRITE, ImageType::FLOAT_2D_ARRAY, "out_weight_img") .image(3
ccl_gpu_kernel_postfix ccl_global float int int int sw