doxygen: add newline after \file
[blender.git] / source / blender / collada / ImageExporter.cpp
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
17 /** \file
18  * \ingroup collada
19  */
20
21
22 #include "COLLADABUURI.h"
23 #include "COLLADASWImage.h"
24
25 extern "C" {
26 #include "DNA_texture_types.h"
27 #include "DNA_image_types.h"
28 #include "DNA_meshdata_types.h"
29
30 #include "BKE_customdata.h"
31 #include "BKE_global.h"
32 #include "BKE_image.h"
33 #include "BKE_main.h"
34 #include "BKE_mesh.h"
35
36 #include "BLI_fileops.h"
37 #include "BLI_path_util.h"
38 #include "BLI_string.h"
39
40 #include "IMB_imbuf_types.h"
41 }
42
43 #include "ImageExporter.h"
44 #include "MaterialExporter.h"
45
46
47 ImagesExporter::ImagesExporter(COLLADASW::StreamWriter *sw, const ExportSettings *export_settings, KeyImageMap &key_image_map) :
48         COLLADASW::LibraryImages(sw),
49         export_settings(export_settings),
50         key_image_map(key_image_map)
51 {
52 }
53
54 void ImagesExporter::export_UV_Image(Image *image, bool use_copies)
55 {
56         std::string name(id_name(image));
57         std::string translated_name(translate_id(name));
58
59         ImBuf *imbuf       = BKE_image_acquire_ibuf(image, NULL, NULL);
60         if (!imbuf) {
61                 fprintf(stderr, "Collada export: image does not exist:\n%s\n", image->name);
62                 return;
63         }
64
65         bool  is_dirty     = (imbuf->userflags & IB_BITMAPDIRTY) != 0;
66
67         ImageFormatData imageFormat;
68         BKE_imbuf_to_image_format(&imageFormat, imbuf);
69
70         short image_source = image->source;
71         bool  is_generated = image_source == IMA_SRC_GENERATED;
72         bool  is_packed    = BKE_image_has_packedfile(image);
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 || is_packed) {
83
84                 // make absolute destination path
85
86                 BLI_strncpy(export_file, name.c_str(), sizeof(export_file));
87                 BKE_image_path_ensure_ext_from_imformat(export_file, &imageFormat);
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 || is_packed) {
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                         return;
104                 }
105                 BLI_strncpy(export_path, export_file, sizeof(export_path));
106         }
107         else {
108
109                 // make absolute source path
110                 BLI_strncpy(source_path, image->name, sizeof(source_path));
111                 BLI_path_abs(source_path, BKE_main_blendfile_path_from_global());
112                 BLI_cleanup_path(NULL, source_path);
113
114                 if (use_copies) {
115
116                         // This image is already located on the file system.
117                         // But we want to create copies here.
118                         // To move images into the same export directory.
119                         // Note: If an image is already located in the export folder,
120                         // then skip the copy (as it would result in a file copy error).
121
122                         if (BLI_path_cmp(source_path, export_path) != 0) {
123                                 if (BLI_copy(source_path, export_path) != 0) {
124                                         fprintf(stderr, "Collada export: Cannot copy image:\n source:%s\ndest :%s\n", source_path, export_path);
125                                         return;
126                                 }
127                         }
128
129                         BLI_strncpy(export_path, export_file, sizeof(export_path));
130
131                 }
132                 else {
133
134                         // Do not make any copies, but use the source path directly as reference
135                         // to the original image
136
137                         BLI_strncpy(export_path, source_path, sizeof(export_path));
138                 }
139         }
140
141         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 */
142         img.add(mSW);
143         fprintf(stdout, "Collada export: Added image: %s\n", export_file);
144
145         BKE_image_release_ibuf(image, imbuf, NULL);
146 }
147
148 void ImagesExporter::exportImages(Scene *sce)
149 {
150         bool use_texture_copies = this->export_settings->use_texture_copies;
151         openLibrary();
152
153         KeyImageMap::iterator iter;
154         for (iter = key_image_map.begin(); iter != key_image_map.end(); iter++) {
155
156                 Image *image = iter->second;
157                 std::string uid(id_name(image));
158                 std::string key = translate_id(uid);
159
160                 export_UV_Image(image, use_texture_copies);
161         }
162
163         closeLibrary();
164 }