Blender  V2.93
ImageExporter.cpp
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  */
16 
21 #include "COLLADABUURI.h"
22 #include "COLLADASWImage.h"
23 
24 #include "DNA_image_types.h"
25 #include "DNA_meshdata_types.h"
26 #include "DNA_texture_types.h"
27 
28 #include "BKE_customdata.h"
29 #include "BKE_global.h"
30 #include "BKE_image.h"
31 #include "BKE_main.h"
32 #include "BKE_mesh.h"
33 
34 #include "BLI_fileops.h"
35 #include "BLI_path_util.h"
36 #include "BLI_string.h"
37 
38 #include "IMB_imbuf_types.h"
39 
40 #include "ImageExporter.h"
41 #include "MaterialExporter.h"
42 
43 ImagesExporter::ImagesExporter(COLLADASW::StreamWriter *sw,
44  BCExportSettings &export_settings,
45  KeyImageMap &key_image_map)
46  : COLLADASW::LibraryImages(sw), export_settings(export_settings), key_image_map(key_image_map)
47 {
48  /* pass */
49 }
50 
51 void ImagesExporter::export_UV_Image(Image *image, bool use_copies)
52 {
53  std::string name(id_name(image));
54  std::string translated_name(translate_id(name));
55 
56  ImBuf *imbuf = BKE_image_acquire_ibuf(image, nullptr, nullptr);
57  if (!imbuf) {
58  fprintf(stderr, "Collada export: image does not exist:\n%s\n", image->filepath);
59  return;
60  }
61 
62  bool is_dirty = BKE_image_is_dirty(image);
63 
64  ImageFormatData imageFormat;
65  BKE_imbuf_to_image_format(&imageFormat, imbuf);
66 
67  short image_source = image->source;
68  bool is_generated = image_source == IMA_SRC_GENERATED;
69  bool is_packed = BKE_image_has_packedfile(image);
70 
71  char export_path[FILE_MAX];
72  char source_path[FILE_MAX];
73  char export_dir[FILE_MAX];
74  char export_file[FILE_MAX];
75 
76  /* Destination folder for exported assets */
77  BLI_split_dir_part(this->export_settings.get_filepath(), export_dir, sizeof(export_dir));
78 
79  if (is_generated || is_dirty || use_copies || is_packed) {
80 
81  /* make absolute destination path */
82 
83  BLI_strncpy(export_file, name.c_str(), sizeof(export_file));
84  BKE_image_path_ensure_ext_from_imformat(export_file, &imageFormat);
85 
86  BLI_join_dirfile(export_path, sizeof(export_path), export_dir, export_file);
87 
88  /* make dest directory if it doesn't exist */
89  BLI_make_existing_file(export_path);
90  }
91 
92  if (is_generated || is_dirty || is_packed) {
93 
94  /* This image in its current state only exists in Blender memory.
95  * So we have to export it. The export will keep the image state intact,
96  * so the exported file will not be associated with the image. */
97 
98  if (BKE_imbuf_write_as(imbuf, export_path, &imageFormat, true) == 0) {
99  fprintf(stderr, "Collada export: Cannot export image to:\n%s\n", export_path);
100  return;
101  }
102  BLI_strncpy(export_path, export_file, sizeof(export_path));
103  }
104  else {
105 
106  /* make absolute source path */
107  BLI_strncpy(source_path, image->filepath, sizeof(source_path));
108  BLI_path_abs(source_path, ID_BLEND_PATH_FROM_GLOBAL(&image->id));
109  BLI_path_normalize(nullptr, source_path);
110 
111  if (use_copies) {
112 
113  /* This image is already located on the file system.
114  * But we want to create copies here.
115  * To move images into the same export directory.
116  * Note: If an image is already located in the export folder,
117  * then skip the copy (as it would result in a file copy error). */
118 
119  if (BLI_path_cmp(source_path, export_path) != 0) {
120  if (BLI_copy(source_path, export_path) != 0) {
121  fprintf(stderr,
122  "Collada export: Cannot copy image:\n source:%s\ndest :%s\n",
123  source_path,
124  export_path);
125  return;
126  }
127  }
128 
129  BLI_strncpy(export_path, export_file, sizeof(export_path));
130  }
131  else {
132 
133  /* Do not make any copies, but use the source path directly as reference
134  * to the original image */
135 
136  BLI_strncpy(export_path, source_path, sizeof(export_path));
137  }
138  }
139 
140  /* Set name also to mNameNC.
141  * This helps other viewers import files exported from Blender better. */
142  COLLADASW::Image img(COLLADABU::URI(COLLADABU::URI::nativePathToUri(export_path)),
143  translated_name,
144  translated_name);
145  img.add(mSW);
146  fprintf(stdout, "Collada export: Added image: %s\n", export_file);
147 
148  BKE_image_release_ibuf(image, imbuf, nullptr);
149 }
150 
152 {
153  bool use_texture_copies = this->export_settings.get_use_texture_copies();
154  openLibrary();
155 
156  KeyImageMap::iterator iter;
157  for (iter = key_image_map.begin(); iter != key_image_map.end(); iter++) {
158 
159  Image *image = iter->second;
160  std::string uid(id_name(image));
161  std::string key = translate_id(uid);
162 
163  export_UV_Image(image, use_texture_copies);
164  }
165 
166  closeLibrary();
167 }
CustomData interface, see also DNA_customdata_types.h.
void BKE_image_release_ibuf(struct Image *ima, struct ImBuf *ibuf, void *lock)
Definition: image.c:5113
int BKE_imbuf_write_as(struct ImBuf *ibuf, const char *name, struct ImageFormatData *imf, const bool save_copy)
Definition: image.c:3015
bool BKE_image_has_packedfile(struct Image *image)
Definition: image.c:5627
struct ImBuf * BKE_image_acquire_ibuf(struct Image *ima, struct ImageUser *iuser, void **r_lock)
Definition: image.c:5100
void BKE_imbuf_to_image_format(struct ImageFormatData *im_format, const struct ImBuf *imbuf)
int BKE_image_path_ensure_ext_from_imformat(char *string, const struct ImageFormatData *im_format)
bool BKE_image_is_dirty(struct Image *image)
Definition: image.c:5681
File and directory operations.
int BLI_copy(const char *file, const char *to) ATTR_NONNULL()
Definition: fileops.c:1307
void BLI_split_dir_part(const char *string, char *dir, const size_t dirlen)
Definition: path_util.c:1682
bool BLI_make_existing_file(const char *name)
Definition: path_util.c:1347
#define FILE_MAX
void BLI_path_normalize(const char *relabase, char *path) ATTR_NONNULL(2)
Definition: path_util.c:173
void BLI_join_dirfile(char *__restrict dst, const size_t maxlen, const char *__restrict dir, const char *__restrict file) ATTR_NONNULL()
Definition: path_util.c:1737
bool BLI_path_abs(char *path, const char *basepath) ATTR_NONNULL()
Definition: path_util.c:1016
#define BLI_path_cmp
char * BLI_strncpy(char *__restrict dst, const char *__restrict src, const size_t maxncpy) ATTR_NONNULL()
Definition: string.c:108
#define ID_BLEND_PATH_FROM_GLOBAL(_id)
Definition: DNA_ID.h:421
struct Image Image
@ IMA_SRC_GENERATED
Contains defines and structs used throughout the imbuf module.
char filepath[1024]
short source
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:70