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