fbe7111bb58bf5132a7541b99425cc82caf8f478
[blender.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 "BKE_utildefines.h"
43 #include "BLI_fileops.h"
44 #include "BLI_path_util.h"
45 #include "BLI_string.h"
46 #include "IMB_imbuf_types.h"
47 }
48
49 #include "ImageExporter.h"
50 #include "MaterialExporter.h"
51
52
53 ImagesExporter::ImagesExporter(COLLADASW::StreamWriter *sw, const ExportSettings *export_settings) : COLLADASW::LibraryImages(sw), export_settings(export_settings)
54 {
55 }
56
57 void ImagesExporter::export_UV_Image(Image *image, bool use_copies) 
58 {
59         std::string name(id_name(image));
60         std::string translated_name(translate_id(name));
61         bool not_yet_exported = find(mImages.begin(), mImages.end(), translated_name) == mImages.end();
62
63         if (not_yet_exported) {
64
65                 ImBuf *imbuf       = BKE_image_get_ibuf(image, NULL);
66                 bool  is_dirty     = imbuf->userflags & IB_BITMAPDIRTY;
67
68                 ImageFormatData imageFormat;
69                 BKE_imbuf_to_image_format(&imageFormat, imbuf);
70
71                 short image_source = image->source;
72                 bool  is_generated = image_source == IMA_SRC_GENERATED;
73
74                 char export_path[FILE_MAX];
75                 char source_path[FILE_MAX];
76                 char export_dir[FILE_MAX];
77                 char export_file[FILE_MAX];
78
79                 // Destination folder for exported assets
80                 BLI_split_dir_part(this->export_settings->filepath, export_dir, sizeof(export_dir));
81
82                 if (is_generated || is_dirty || use_copies) {
83
84                         // make absolute destination path
85
86                         BLI_strncpy(export_file, name.c_str(), sizeof(export_file));
87                         BKE_add_image_extension(export_file, imageFormat.imtype);
88
89                         BLI_join_dirfile(export_path, sizeof(export_path), export_dir, export_file);
90
91                         // make dest directory if it doesn't exist
92                         BLI_make_existing_file(export_path);
93                 }
94
95                 if (is_generated || is_dirty) {
96
97                         // This image in its current state only exists in Blender memory.
98                         // So we have to export it. The export will keep the image state intact,
99                         // so the exported file will not be associated with the image.
100
101                         if (BKE_imbuf_write_as(imbuf, export_path, &imageFormat, true) == 0) {
102                                 fprintf(stderr, "Collada export: Cannot export image to:\n%s\n", export_path);
103                         }
104                         BLI_strncpy(export_path, export_file, sizeof(export_path));
105                 }
106                 else {
107
108                         // make absolute source path
109                         BLI_strncpy(source_path, image->name, sizeof(source_path));
110                         BLI_path_abs(source_path, G.main->name);
111                         BLI_cleanup_path(NULL, source_path);
112
113                         if (use_copies) {
114                         
115                                 // This image is already located on the file system.
116                                 // But we want to create copies here.
117                                 // To avoid overwroting images with same file name but
118                                 // differenet source locations
119
120                                 if (BLI_copy(source_path, export_path) != 0) {
121                                         fprintf(stderr, "Collada export: Cannot copy image:\n source:%s\ndest :%s\n", source_path, export_path);
122                                 }
123
124                                 BLI_strncpy(export_path, export_file, sizeof(export_path));
125
126                         }
127                         else {
128
129                                 // Do not make any vopies, but use the source path directly as reference
130                                 // to the original image
131
132                                 BLI_strncpy(export_path, source_path, sizeof(export_path));
133                         }
134                 }
135
136                 COLLADASW::Image img(COLLADABU::URI(COLLADABU::URI::nativePathToUri(export_path)), translated_name, translated_name); /* set name also to mNameNC. This helps other viewers import files exported from Blender better */
137                 img.add(mSW);
138                 fprintf(stdout, "Collada export: Added image: %s\n",export_file);
139                 mImages.push_back(translated_name);
140         }
141 }
142
143 void ImagesExporter::export_UV_Images()
144 {
145         std::set<Image *> uv_textures;
146         LinkNode *node;
147         bool use_copies = this->export_settings->use_texture_copies;
148         for (node=this->export_settings->export_set; node; node=node->next) {
149                 Object *ob = (Object *)node->link;
150                 if (ob->type == OB_MESH && ob->totcol) {
151                         Mesh *me     = (Mesh *) ob->data;
152                         BKE_mesh_tessface_ensure(me);
153                         for (int i = 0; i < me->pdata.totlayer; i++) {
154                                 if (me->pdata.layers[i].type == CD_MTEXPOLY) {
155                                         MTexPoly *txface = (MTexPoly *)me->pdata.layers[i].data;
156                                         MFace *mface = me->mface;
157                                         for (int j = 0; j < me->totpoly; j++, mface++, txface++) {
158
159                                                 Image *ima = txface->tpage;
160                                                 if (ima == NULL)
161                                                         continue;
162
163                                                 bool not_in_list = uv_textures.find(ima)==uv_textures.end();
164                                                 if (not_in_list) {
165                                                                 uv_textures.insert(ima);
166                                                                 export_UV_Image(ima, use_copies);
167                                                 }
168                                         }
169                                 }
170                         }
171                 }
172         }
173 }
174
175
176 bool ImagesExporter::hasImages(Scene *sce)
177 {
178         LinkNode *node;
179         
180         for (node=this->export_settings->export_set; node; node=node->next) {
181                 Object *ob = (Object *)node->link;
182                 int a;
183                 for (a = 0; a < ob->totcol; a++) {
184                         Material *ma = give_current_material(ob, a + 1);
185
186                         // no material, but check all of the slots
187                         if (!ma) continue;
188                         int b;
189                         for (b = 0; b < MAX_MTEX; b++) {
190                                 MTex *mtex = ma->mtex[b];
191                                 if (mtex && mtex->tex && mtex->tex->ima) return true;
192                         }
193
194                 }
195                 if (ob->type == OB_MESH) {
196                         Mesh *me     = (Mesh *) ob->data;
197                         BKE_mesh_tessface_ensure(me);
198                         bool has_uvs = (bool)CustomData_has_layer(&me->fdata, CD_MTFACE);
199                         if (has_uvs) {
200                                 int num_layers = CustomData_number_of_layers(&me->fdata, CD_MTFACE);
201                                 for (int a = 0; a < num_layers; a++) {
202                                         MTFace *tface = (MTFace *)CustomData_get_layer_n(&me->fdata, CD_MTFACE, a);
203                                         Image *img = tface->tpage;
204                                         if(img) return true;
205                                 }
206                         }
207                 }
208
209         }
210         return false;
211 }
212
213 void ImagesExporter::exportImages(Scene *sce)
214 {
215         openLibrary();
216
217         MaterialFunctor mf;
218         if (this->export_settings->include_material_textures) {
219                 mf.forEachMaterialInExportSet<ImagesExporter>(sce, *this, this->export_settings->export_set);
220         }
221
222         if (this->export_settings->include_uv_textures) {
223                 export_UV_Images();
224         }
225
226         closeLibrary();
227 }
228
229
230
231 void ImagesExporter::operator()(Material *ma, Object *ob)
232 {
233         int a;
234         bool use_texture_copies = this->export_settings->use_texture_copies;
235         for (a = 0; a < MAX_MTEX; a++) {
236                 MTex *mtex = ma->mtex[a];
237                 if (mtex && mtex->tex && mtex->tex->ima) {
238                         Image *image = mtex->tex->ima;
239                         export_UV_Image(image, use_texture_copies);
240                 }
241         }
242 }