J-key render switching back, now with 10 slots.
[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(int val) {}
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(name, scene->r.imtype);
106
107         strcpy(str, name);
108         BLI_convertstringcode(str, G.sce);
109
110         if (confirm)
111                 overwrite = saveover(str);
112         else
113                 overwrite = 1;
114         
115         if(overwrite) {
116                 if(scene->r.imtype==R_MULTILAYER) {
117                         Render *re= RE_GetRender(scene->id.name, RE_SLOT_VIEW);
118                         RenderResult *rr= RE_AcquireResultRead(re);
119                         if(rr) 
120                                 RE_WriteRenderResult(rr, str, scene->r.quality);
121                         RE_ReleaseResult(re);
122                 }
123                 else {
124                         Render *re= RE_GetRender(scene->id.name, RE_SLOT_VIEW);
125                         RenderResult rres;
126                         ImBuf *ibuf;
127                         
128                         RE_AcquireResultImage(re, &rres);
129
130                         waitcursor(1); /* from screen.c */
131
132                         ibuf= IMB_allocImBuf(rres.rectx, rres.recty, scene->r.planes, 0, 0);
133                         ibuf->rect= (unsigned int *)rres.rect32;
134                         ibuf->rect_float= rres.rectf;
135                         ibuf->zbuf_float= rres.rectz;
136                         
137                         /* float factor for random dither, imbuf takes care of it */
138                         ibuf->dither= scene->r.dither_intensity;
139                         
140                         BKE_write_ibuf(scene, ibuf, str, scene->r.imtype, scene->r.subimtype, scene->r.quality);
141                         IMB_freeImBuf(ibuf);    /* imbuf knows rects are not part of ibuf */
142
143                         RE_ReleaseResultImage(re);
144                 }
145                 
146                 strcpy(G.ima, name);
147                 
148                 waitcursor(0);
149         }
150 }
151
152
153 void save_image_filesel_str(Scene *scene, char *str)
154 {
155         switch(scene->r.imtype) {
156                 case R_RADHDR:
157                         strcpy(str, "Save Radiance HDR");
158                         break;
159                 case R_FFMPEG:
160                 case R_H264:
161                 case R_XVID:
162                 case R_THEORA:
163                 case R_PNG:
164                         strcpy(str, "Save PNG");
165                         break;
166 #ifdef WITH_DDS
167                 case R_DDS:
168                         strcpy(str, "Save DDS");
169                         break;
170 #endif
171                 case R_BMP:
172                         strcpy(str, "Save BMP");
173                         break;
174                 case R_TIFF:
175                         if (G.have_libtiff)
176                                 strcpy(str, "Save TIFF");
177                         break;
178 #ifdef WITH_OPENEXR
179                 case R_OPENEXR:
180                         strcpy(str, "Save OpenEXR");
181                         break;
182 #endif
183                 case R_CINEON:
184                         strcpy(str, "Save Cineon");
185                         break;
186                 case R_DPX:
187                         strcpy(str, "Save DPX");
188                         break;
189                 case R_RAWTGA:
190                         strcpy(str, "Save Raw Targa");
191                         break;
192                 case R_IRIS:
193                         strcpy(str, "Save IRIS");
194                         break;
195                 case R_IRIZ:
196                         strcpy(str, "Save IRIS");
197                         break;
198                 case R_HAMX:
199                         strcpy(str, "Save HAMX");
200                         break;
201                 case R_TARGA:
202                         strcpy(str, "Save Targa");
203                         break;
204                 case R_MULTILAYER:
205                         strcpy(str, "Save Multi Layer EXR");
206                         break;
207 #ifdef WITH_OPENJPEG
208                 case R_JP2:
209                         strcpy(str, "Save JPEG2000");
210                         break;
211 #endif
212                         /* default we save jpeg, also for all movie formats */
213                 case R_JPEG90:
214                 case R_MOVIE:
215                 case R_AVICODEC:
216                 case R_AVIRAW:
217                 case R_AVIJPEG:
218                 default:
219                         strcpy(str, "Save JPEG");
220                         break;
221         }       
222 }
223
224 static void save_rendered_image_cb(char *name) 
225 {
226         save_rendered_image_cb_real(name, 1);
227 }
228
229 /* no fileselect, no confirm */
230 void BIF_save_rendered_image(char *name)
231 {
232         save_rendered_image_cb_real(name, 0);
233 }
234         
235 /* calls fileselect */
236 void BIF_save_rendered_image_fs(Scene *scene)
237 {
238         Render *re= RE_GetRender(scene->id.name, RE_SLOT_VIEW);
239         RenderResult rres;
240         
241         RE_AcquireResultImage(re, &rres);
242
243         if(!rres.rectf && !rres.rect32) {
244                 error("No image rendered");
245         } 
246         else {
247                 char dir[FILE_MAXDIR * 2], str[FILE_MAXFILE * 2];
248                 
249                 if(G.ima[0]==0) {
250                         strcpy(dir, G.sce);
251                         BLI_splitdirstring(dir, str);
252                         strcpy(G.ima, dir);
253                 }
254                 
255                 save_image_filesel_str(scene, str);
256                 activate_fileselect(FILE_SPECIAL, str, G.ima, save_rendered_image_cb);
257         }
258
259         RE_ReleaseResultImage(re);
260 }
261
262