doxygen: blender/collada tagged.
[blender-staging.git] / source / blender / collada / ImageExporter.cpp
1 /*
2  * $Id$
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19  *
20  * Contributor(s): Chingiz Dyussenov, Arystanbek Dyussenov, Jan Diederich, Tod Liverseed,
21  *                 Nathan Letwory
22  *
23  * ***** END GPL LICENSE BLOCK *****
24  */
25
26 /** \file blender/collada/ImageExporter.cpp
27  *  \ingroup collada
28  */
29
30
31 #include "COLLADABUURI.h"
32 #include "COLLADASWImage.h"
33
34 #include "ImageExporter.h"
35 #include "MaterialExporter.h"
36
37 #include "DNA_texture_types.h"
38
39 #include "BKE_global.h"
40 #include "BKE_main.h"
41 #include "BKE_utildefines.h"
42 #include "BLI_fileops.h"
43 #include "BLI_path_util.h"
44 #include "BLI_string.h"
45
46 ImagesExporter::ImagesExporter(COLLADASW::StreamWriter *sw, const char* filename) : COLLADASW::LibraryImages(sw), mfilename(filename)
47 {}
48
49 void ImagesExporter::exportImages(Scene *sce)
50 {
51         openLibrary();
52         MaterialFunctor mf;
53         mf.forEachMaterialInScene<ImagesExporter>(sce, *this);
54
55         closeLibrary();
56 }
57
58 void ImagesExporter::operator()(Material *ma, Object *ob)
59 {
60         int a;
61         for (a = 0; a < MAX_MTEX; a++) {
62                 MTex *mtex = ma->mtex[a];
63                 if (mtex && mtex->tex && mtex->tex->ima) {
64
65                         Image *image = mtex->tex->ima;
66                         std::string name(id_name(image));
67                         name = translate_id(name);
68                         char rel[FILE_MAX];
69                         char abs[FILE_MAX];
70                         char src[FILE_MAX];
71                         char dir[FILE_MAX];
72                         
73                         BLI_split_dirfile(mfilename, dir, NULL);
74
75                         BKE_rebase_path(abs, sizeof(abs), rel, sizeof(rel), G.main->name, image->name, dir);
76
77                         if (abs[0] != '\0') {
78
79                                 // make absolute source path
80                                 BLI_strncpy(src, image->name, sizeof(src));
81                                 BLI_path_abs(src, G.main->name);
82
83                                 // make dest directory if it doesn't exist
84                                 BLI_make_existing_file(abs);
85                         
86                                 if (BLI_copy_fileops(src, abs) != 0) {
87                                         fprintf(stderr, "Cannot copy image to file's directory. \n");
88                                 }
89                         } 
90                         
91                         if (find(mImages.begin(), mImages.end(), name) == mImages.end()) {
92                                 COLLADASW::Image img(COLLADABU::URI(COLLADABU::URI::nativePathToUri(rel)), name, name); /* set name also to mNameNC. This helps other viewers import files exported from Blender better */
93                                 img.add(mSW);
94
95                                 mImages.push_back(name);
96                         }
97                 }
98         }
99 }