2.50: svn merge https://svn.blender.org/svnroot/bf-blender/trunk/blender -r19323...
[blender-staging.git] / source / blender / editors / space_file / writeimage.c
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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19  *
20  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
21  * All rights reserved.
22  *
23  * The Original Code is: all of this file.
24  *
25  * Contributor(s): none yet.
26  *
27  * ***** END GPL LICENSE BLOCK *****
28  */
29
30 #include <string.h>
31
32 #include "MEM_guardedalloc.h"
33
34 #include "IMB_imbuf.h"
35 #include "IMB_imbuf_types.h"
36
37 #include "BLI_blenlib.h"
38
39 #include "DNA_image_types.h"
40 #include "DNA_scene_types.h"
41 #include "DNA_screen_types.h"
42 #include "DNA_space_types.h"
43 #include "DNA_texture_types.h"
44
45 #include "BKE_context.h"
46 #include "BKE_global.h"
47 #include "BKE_image.h"
48 #include "BKE_utildefines.h"
49
50 #include "RE_pipeline.h"
51
52 #include "file_intern.h"
53
54 /* XXX */
55 static void error() {}
56 static void waitcursor() {}
57 static void activate_fileselect() {}
58 static int saveover() {return 0;}
59 /* XXX */
60
61
62 /* ------------------------------------------------------------------------- */
63
64
65 void BIF_save_envmap(Scene *scene, EnvMap *env, char *str)
66 {
67         ImBuf *ibuf;
68 /*      extern rectcpy(); */
69         int dx;
70         
71         /* all interactive stuff is handled in buttons.c */
72         if(env->cube[0]==NULL) return;
73                 
74         dx= env->cube[0]->x;
75         ibuf= IMB_allocImBuf(3*dx, 2*dx, 24, IB_rect, 0);
76         
77         IMB_rectcpy(ibuf, env->cube[0], 0, 0, 0, 0, dx, dx);
78         IMB_rectcpy(ibuf, env->cube[1], dx, 0, 0, 0, dx, dx);
79         IMB_rectcpy(ibuf, env->cube[2], 2*dx, 0, 0, 0, dx, dx);
80         IMB_rectcpy(ibuf, env->cube[3], 0, dx, 0, 0, dx, dx);
81         IMB_rectcpy(ibuf, env->cube[4], dx, dx, 0, 0, dx, dx);
82         IMB_rectcpy(ibuf, env->cube[5], 2*dx, dx, 0, 0, dx, dx);
83         
84         BKE_write_ibuf(scene, ibuf, str, scene->r.imtype, scene->r.subimtype, scene->r.quality);
85         IMB_freeImBuf(ibuf);
86 }
87
88
89
90 /* callback for fileselect to save rendered image, renderresult was checked to exist */
91 static void save_rendered_image_cb_real(char *name, int confirm)
92 {
93         Scene *scene= NULL; // XXX
94         char str[FILE_MAXDIR+FILE_MAXFILE];
95         int overwrite;
96         
97         if(BLI_testextensie(name,".blend")) {
98                 error("Wrong filename");
99                 return;
100         }
101         
102         /* BKE_add_image_extension() checks for if extension was already set */
103         if(scene->r.scemode & R_EXTENSION) 
104                 if(strlen(name)<FILE_MAXDIR+FILE_MAXFILE-5)
105                         BKE_add_image_extension(scene, name, scene->r.imtype);
106
107         strcpy(str, name);
108         BLI_convertstringcode(str, G.sce);
109         BLI_convertstringframe(str, scene->r.cfra); /* TODO - is this even used? */
110
111         if (confirm)
112                 overwrite = saveover(str);
113         else
114                 overwrite = 1;
115         
116         if(overwrite) {
117                 if(scene->r.imtype==R_MULTILAYER) {
118                         RenderResult *rr= RE_GetResult(RE_GetRender(scene->id.name));
119                         if(rr) 
120                                 RE_WriteRenderResult(rr, str, scene->r.quality);
121                 }
122                 else {
123                         RenderResult rres;
124                         ImBuf *ibuf;
125                         
126                         RE_GetResultImage(RE_GetRender(scene->id.name), &rres);
127
128                         waitcursor(1); /* from screen.c */
129
130                         ibuf= IMB_allocImBuf(rres.rectx, rres.recty, scene->r.planes, 0, 0);
131                         ibuf->rect= (unsigned int *)rres.rect32;
132                         ibuf->rect_float= rres.rectf;
133                         ibuf->zbuf_float= rres.rectz;
134                         
135                         /* float factor for random dither, imbuf takes care of it */
136                         ibuf->dither= scene->r.dither_intensity;
137                         
138                         BKE_write_ibuf(scene, ibuf, str, scene->r.imtype, scene->r.subimtype, scene->r.quality);
139                         IMB_freeImBuf(ibuf);    /* imbuf knows rects are not part of ibuf */
140                 }
141                 
142                 strcpy(G.ima, name);
143                 
144                 waitcursor(0);
145         }
146 }
147
148
149 void save_image_filesel_str(Scene *scene, char *str)
150 {
151         switch(scene->r.imtype) {
152                 case R_RADHDR:
153                         strcpy(str, "Save Radiance HDR");
154                         break;
155                 case R_FFMPEG:
156                 case R_PNG:
157                         strcpy(str, "Save PNG");
158                         break;
159 #ifdef WITH_DDS
160                 case R_DDS:
161                         strcpy(str, "Save DDS");
162                         break;
163 #endif
164                 case R_BMP:
165                         strcpy(str, "Save BMP");
166                         break;
167                 case R_TIFF:
168                         if (G.have_libtiff)
169                                 strcpy(str, "Save TIFF");
170                         break;
171 #ifdef WITH_OPENEXR
172                 case R_OPENEXR:
173                         strcpy(str, "Save OpenEXR");
174                         break;
175 #endif
176                 case R_CINEON:
177                         strcpy(str, "Save Cineon");
178                         break;
179                 case R_DPX:
180                         strcpy(str, "Save DPX");
181                         break;
182                 case R_RAWTGA:
183                         strcpy(str, "Save Raw Targa");
184                         break;
185                 case R_IRIS:
186                         strcpy(str, "Save IRIS");
187                         break;
188                 case R_IRIZ:
189                         strcpy(str, "Save IRIS");
190                         break;
191                 case R_HAMX:
192                         strcpy(str, "Save HAMX");
193                         break;
194                 case R_TARGA:
195                         strcpy(str, "Save Targa");
196                         break;
197                 case R_MULTILAYER:
198                         strcpy(str, "Save Multi Layer EXR");
199                         break;
200 #ifdef WITH_OPENJPEG
201                 case R_JP2:
202                         strcpy(str, "Save JPEG2000");
203                         break;
204 #endif
205                         /* default we save jpeg, also for all movie formats */
206                 case R_JPEG90:
207                 case R_MOVIE:
208                 case R_AVICODEC:
209                 case R_AVIRAW:
210                 case R_AVIJPEG:
211                 default:
212                         strcpy(str, "Save JPEG");
213                         break;
214         }       
215 }
216
217 static void save_rendered_image_cb(char *name) 
218 {
219         save_rendered_image_cb_real(name, 1);
220 }
221
222 /* no fileselect, no confirm */
223 void BIF_save_rendered_image(char *name)
224 {
225         save_rendered_image_cb_real(name, 0);
226 }
227         
228 /* calls fileselect */
229 void BIF_save_rendered_image_fs(Scene *scene)
230 {
231         RenderResult rres;
232         
233         RE_GetResultImage(RE_GetRender(scene->id.name), &rres);
234
235         if(!rres.rectf && !rres.rect32) {
236                 error("No image rendered");
237         } 
238         else {
239                 char dir[FILE_MAXDIR * 2], str[FILE_MAXFILE * 2];
240                 
241                 if(G.ima[0]==0) {
242                         strcpy(dir, G.sce);
243                         BLI_splitdirstring(dir, str);
244                         strcpy(G.ima, dir);
245                 }
246                 
247                 save_image_filesel_str(scene, str);
248                 activate_fileselect(FILE_SPECIAL, str, G.ima, save_rendered_image_cb);
249         }
250 }
251
252