05efdc125966119e824c1a5bc57e22ef67096b9c
[blender.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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 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_scene_types.h"
40 #include "DNA_screen_types.h"
41 #include "DNA_space_types.h"
42
43 #include "BKE_context.h"
44 #include "BKE_global.h"
45 #include "BKE_image.h"
46 #include "BKE_utildefines.h"
47
48 #include "RE_pipeline.h"
49
50 #include "file_intern.h"
51
52 /* XXX */
53 static void error(const char *dummy) {}
54 static void waitcursor(int val) {}
55 static void activate_fileselect(int d1, char *d2, char *d3, void *d4) {}
56 static int saveover(const char *dummy) {return 0;}
57 /* XXX */
58
59
60 /* ------------------------------------------------------------------------- */
61
62 /* callback for fileselect to save rendered image, renderresult was checked to exist */
63 static void save_rendered_image_cb_real(char *name, int confirm)
64 {
65         Scene *scene= NULL; // XXX
66         char str[FILE_MAXDIR+FILE_MAXFILE];
67         int overwrite;
68         
69         if(BLI_testextensie(name,".blend")) {
70                 error("Wrong filename");
71                 return;
72         }
73         
74         /* BKE_add_image_extension() checks for if extension was already set */
75         if(scene->r.scemode & R_EXTENSION) 
76                 if(strlen(name)<FILE_MAXDIR+FILE_MAXFILE-5)
77                         BKE_add_image_extension(name, scene->r.imtype);
78
79         strcpy(str, name);
80         BLI_path_abs(str, G.sce);
81
82         if (confirm)
83                 overwrite = saveover(str);
84         else
85                 overwrite = 1;
86         
87         if(overwrite) {
88                 if(scene->r.imtype==R_MULTILAYER) {
89                         Render *re= RE_GetRender(scene->id.name);
90                         RenderResult *rr= RE_AcquireResultRead(re);
91                         if(rr) 
92                                 RE_WriteRenderResult(rr, str, scene->r.quality);
93                         RE_ReleaseResult(re);
94                 }
95                 else {
96                         Render *re= RE_GetRender(scene->id.name);
97                         RenderResult rres;
98                         ImBuf *ibuf;
99                         
100                         RE_AcquireResultImage(re, &rres);
101
102                         waitcursor(1); /* from screen.c */
103
104                         ibuf= IMB_allocImBuf(rres.rectx, rres.recty, scene->r.planes, 0, 0);
105                         ibuf->rect= (unsigned int *)rres.rect32;
106                         ibuf->rect_float= rres.rectf;
107                         ibuf->zbuf_float= rres.rectz;
108                         
109                         /* float factor for random dither, imbuf takes care of it */
110                         ibuf->dither= scene->r.dither_intensity;
111                         
112                         BKE_write_ibuf(scene, ibuf, str, scene->r.imtype, scene->r.subimtype, scene->r.quality);
113                         IMB_freeImBuf(ibuf);    /* imbuf knows rects are not part of ibuf */
114
115                         RE_ReleaseResultImage(re);
116                 }
117                 
118                 strcpy(G.ima, name);
119                 
120                 waitcursor(0);
121         }
122 }
123
124
125 void save_image_filesel_str(Scene *scene, char *str)
126 {
127         switch(scene->r.imtype) {
128                 case R_RADHDR:
129                         strcpy(str, "Save Radiance HDR");
130                         break;
131                 case R_FFMPEG:
132                 case R_H264:
133                 case R_XVID:
134                 case R_THEORA:
135                 case R_PNG:
136                         strcpy(str, "Save PNG");
137                         break;
138 #ifdef WITH_DDS
139                 case R_DDS:
140                         strcpy(str, "Save DDS");
141                         break;
142 #endif
143                 case R_BMP:
144                         strcpy(str, "Save BMP");
145                         break;
146 #ifdef WITH_TIFF
147                 case R_TIFF:
148                         strcpy(str, "Save TIFF");
149                         break;
150 #endif
151 #ifdef WITH_OPENEXR
152                 case R_OPENEXR:
153                         strcpy(str, "Save OpenEXR");
154                         break;
155 #endif
156                 case R_CINEON:
157                         strcpy(str, "Save Cineon");
158                         break;
159                 case R_DPX:
160                         strcpy(str, "Save DPX");
161                         break;
162                 case R_RAWTGA:
163                         strcpy(str, "Save Raw Targa");
164                         break;
165                 case R_IRIS:
166                         strcpy(str, "Save IRIS");
167                         break;
168                 case R_IRIZ:
169                         strcpy(str, "Save IRIS");
170                         break;
171                 case R_HAMX:
172                         strcpy(str, "Save HAMX");
173                         break;
174                 case R_TARGA:
175                         strcpy(str, "Save Targa");
176                         break;
177                 case R_MULTILAYER:
178                         strcpy(str, "Save Multi Layer EXR");
179                         break;
180 #ifdef WITH_OPENJPEG
181                 case R_JP2:
182                         strcpy(str, "Save JPEG2000");
183                         break;
184 #endif
185                         /* default we save jpeg, also for all movie formats */
186                 case R_JPEG90:
187                 case R_AVICODEC:
188                 case R_AVIRAW:
189                 case R_AVIJPEG:
190                 default:
191                         strcpy(str, "Save JPEG");
192                         break;
193         }       
194 }
195
196 static void save_rendered_image_cb(char *name) 
197 {
198         save_rendered_image_cb_real(name, 1);
199 }
200
201 /* no fileselect, no confirm */
202 void BIF_save_rendered_image(char *name)
203 {
204         save_rendered_image_cb_real(name, 0);
205 }
206         
207 /* calls fileselect */
208 void BIF_save_rendered_image_fs(Scene *scene)
209 {
210         Render *re= RE_GetRender(scene->id.name);
211         RenderResult rres;
212         
213         RE_AcquireResultImage(re, &rres);
214
215         if(!rres.rectf && !rres.rect32) {
216                 error("No image rendered");
217         } 
218         else {
219                 char dir[FILE_MAXDIR * 2], str[FILE_MAXFILE * 2];
220                 
221                 if(G.ima[0]==0) {
222                         strcpy(dir, G.sce);
223                         BLI_splitdirstring(dir, str);
224                         strcpy(G.ima, dir);
225                 }
226                 
227                 save_image_filesel_str(scene, str);
228                 activate_fileselect(FILE_SPECIAL, str, G.ima, save_rendered_image_cb);
229         }
230
231         RE_ReleaseResultImage(re);
232 }
233
234