fix Collada: wrong usage of pointer and hidden redeclaration
[blender-staging.git] / source / blender / collada / ImageExporter.cpp
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * Contributor(s): Chingiz Dyussenov, Arystanbek Dyussenov, Jan Diederich, Tod Liverseed,
19  *                 Nathan Letwory
20  *
21  * ***** END GPL LICENSE BLOCK *****
22  */
23
24 /** \file blender/collada/ImageExporter.cpp
25  *  \ingroup collada
26  */
27
28
29 #include "COLLADABUURI.h"
30 #include "COLLADASWImage.h"
31
32 extern "C" {
33 #include "DNA_texture_types.h"
34 #include "DNA_image_types.h"
35 #include "DNA_meshdata_types.h"
36
37 #include "BKE_customdata.h" 
38 #include "BKE_global.h"
39 #include "BKE_image.h"
40 #include "BKE_main.h"
41 #include "BKE_mesh.h"
42 #include "BLI_fileops.h"
43 #include "BLI_path_util.h"
44 #include "BLI_string.h"
45 #include "IMB_imbuf_types.h"
46 }
47
48 #include "ImageExporter.h"
49 #include "MaterialExporter.h"
50
51
52 ImagesExporter::ImagesExporter(COLLADASW::StreamWriter *sw, const ExportSettings *export_settings) : COLLADASW::LibraryImages(sw), export_settings(export_settings)
53 {
54 }
55
56 void ImagesExporter::export_UV_Image(Image *image, bool use_copies) 
57 {
58         std::string id(id_name(image));
59         std::string translated_id(translate_id(id));
60         bool not_yet_exported = find(mImages.begin(), mImages.end(), translated_id) == mImages.end();
61
62         if (not_yet_exported) {
63
64                 ImBuf *imbuf       = BKE_image_acquire_ibuf(image, NULL, NULL);
65                 if (!imbuf) {
66                         fprintf(stderr, "Collada export: image does not exist:\n%s\n", image->name);
67                         return;
68                 }
69
70                 bool  is_dirty     = (imbuf->userflags & IB_BITMAPDIRTY) != 0;
71
72                 ImageFormatData imageFormat;
73                 BKE_imbuf_to_image_format(&imageFormat, imbuf);
74
75                 short image_source = image->source;
76                 bool  is_generated = image_source == IMA_SRC_GENERATED;
77                 bool  is_packed    = BKE_image_has_packedfile(image);
78
79                 char export_path[FILE_MAX];
80                 char source_path[FILE_MAX];
81                 char export_dir[FILE_MAX];
82                 char export_file[FILE_MAX];
83
84                 // Destination folder for exported assets
85                 BLI_split_dir_part(this->export_settings->filepath, export_dir, sizeof(export_dir));
86
87                 if (is_generated || is_dirty || use_copies || is_packed) {
88
89                         // make absolute destination path
90
91                         BLI_strncpy(export_file, id.c_str(), sizeof(export_file));
92                         BKE_image_path_ensure_ext_from_imformat(export_file, &imageFormat);
93
94                         BLI_join_dirfile(export_path, sizeof(export_path), export_dir, export_file);
95
96                         // make dest directory if it doesn't exist
97                         BLI_make_existing_file(export_path);
98                 }
99
100                 if (is_generated || is_dirty || is_packed) {
101
102                         // This image in its current state only exists in Blender memory.
103                         // So we have to export it. The export will keep the image state intact,
104                         // so the exported file will not be associated with the image.
105
106                         if (BKE_imbuf_write_as(imbuf, export_path, &imageFormat, true) == 0) {
107                                 fprintf(stderr, "Collada export: Cannot export image to:\n%s\n", export_path);
108                                 return;
109                         }
110                         BLI_strncpy(export_path, export_file, sizeof(export_path));
111                 }
112                 else {
113
114                         // make absolute source path
115                         BLI_strncpy(source_path, image->name, sizeof(source_path));
116                         BLI_path_abs(source_path, G.main->name);
117                         BLI_cleanup_path(NULL, source_path);
118
119                         if (use_copies) {
120                         
121                                 // This image is already located on the file system.
122                                 // But we want to create copies here.
123                                 // To move images into the same export directory.
124                                 // Note: If an image is already located in the export folder,
125                                 // then skip the copy (as it would result in a file copy error).
126
127                                 if (BLI_path_cmp(source_path, export_path) != 0) {
128                                         if (BLI_copy(source_path, export_path) != 0) {
129                                                 fprintf(stderr, "Collada export: Cannot copy image:\n source:%s\ndest :%s\n", source_path, export_path);
130                                                 return;
131                                         }
132                                 }
133
134                                 BLI_strncpy(export_path, export_file, sizeof(export_path));
135
136                         }
137                         else {
138
139                                 // Do not make any copies, but use the source path directly as reference
140                                 // to the original image
141
142                                 BLI_strncpy(export_path, source_path, sizeof(export_path));
143                         }
144                 }
145
146                 /* set name also to mNameNC. This helps other viewers import files exported from Blender better */
147                 COLLADASW::Image img(COLLADABU::URI(COLLADABU::URI::nativePathToUri(export_path)), translated_id, translated_id); 
148                 img.add(mSW);
149                 fprintf(stdout, "Collada export: Added image: %s\n", export_file);
150                 mImages.push_back(translated_id);
151
152                 BKE_image_release_ibuf(image, imbuf, NULL);
153         }
154 }
155
156 void ImagesExporter::export_UV_Images()
157 {
158         std::set<Image *> uv_textures;
159         LinkNode *node;
160         bool use_texture_copies = this->export_settings->use_texture_copies;
161         bool active_uv_only     = this->export_settings->active_uv_only;
162
163         for (node = this->export_settings->export_set; node; node = node->next) {
164                 Object *ob = (Object *)node->link;
165                 if (ob->type == OB_MESH) {
166                         Mesh *me     = (Mesh *) ob->data;
167                         BKE_mesh_tessface_ensure(me);
168                         int active_uv_layer = CustomData_get_active_layer_index(&me->pdata, CD_MTEXPOLY);
169                         for (int i = 0; i < me->pdata.totlayer; i++) {
170                                 if (me->pdata.layers[i].type == CD_MTEXPOLY) {
171                                         if (!active_uv_only || active_uv_layer == i)
172                                         {
173                                                 MTexPoly *txface = (MTexPoly *)me->pdata.layers[i].data;
174                                                 for (int j = 0; j < me->totpoly; j++, txface++) {
175
176                                                         Image *ima = txface->tpage;
177                                                         if (ima == NULL)
178                                                                 continue;
179
180                                                         bool not_in_list = uv_textures.find(ima) == uv_textures.end();
181                                                         if (not_in_list) {
182                                                                         uv_textures.insert(ima);
183                                                                         export_UV_Image(ima, use_texture_copies);
184                                                         }
185                                                 }
186                                         }
187                                 }
188                         }
189                 }
190         }
191 }
192
193 /* ============================================================
194  * Check if there are any images to be exported
195  * Returns true as soon as an object is detected that
196  * either has an UV Texture assigned, or has a material
197  * assigned that uses an Image Texture.
198  * ============================================================
199  */
200 bool ImagesExporter::hasImages(Scene *sce)
201 {
202         LinkNode *node;
203         
204         for (node = this->export_settings->export_set; node; node = node->next) {
205                 Object *ob = (Object *)node->link;
206
207                 for (int a = 0; a < ob->totcol; a++) {
208                         Material *ma = give_current_material(ob, a + 1);
209
210                         // no material, but check all of the slots
211                         if (!ma) continue;
212                         int b;
213                         for (b = 0; b < MAX_MTEX; b++) {
214                                 MTex *mtex = ma->mtex[b];
215                                 if (mtex && mtex->tex && mtex->tex->ima) return true;
216                         }
217
218                 }
219                 if (ob->type == OB_MESH) {
220                         Mesh *me     = (Mesh *) ob->data;
221                         BKE_mesh_tessface_ensure(me);
222                         bool has_uvs = (bool)CustomData_has_layer(&me->fdata, CD_MTFACE);
223                         if (has_uvs) {
224                                 int num_layers = CustomData_number_of_layers(&me->fdata, CD_MTFACE);
225                                 for (int a = 0; a < num_layers; a++) {
226                                         MTFace *tface = (MTFace *)CustomData_get_layer_n(&me->fdata, CD_MTFACE, a);
227                                         Image *img = tface->tpage;
228                                         if (img) return true;
229                                 }
230                         }
231                 }
232
233         }
234         return false;
235 }
236
237 void ImagesExporter::exportImages(Scene *sce)
238 {
239         openLibrary();
240
241         MaterialFunctor mf;
242         if (this->export_settings->export_texture_type == BC_TEXTURE_TYPE_MAT) {
243                 mf.forEachMaterialInExportSet<ImagesExporter>(sce, *this, this->export_settings->export_set);
244         }
245         else {
246                 export_UV_Images();
247         }
248
249         closeLibrary();
250 }
251
252
253
254 void ImagesExporter::operator()(Material *ma, Object *ob)
255 {
256         int a;
257         bool use_texture_copies = this->export_settings->use_texture_copies;
258         for (a = 0; a < MAX_MTEX; a++) {
259                 MTex *mtex = ma->mtex[a];
260                 if (mtex && mtex->tex && mtex->tex->ima) {
261                         Image *image = mtex->tex->ima;
262                         export_UV_Image(image, use_texture_copies);
263                 }
264         }
265 }