[#18164] jpeg2000 patch, with some fixes from Peter too.
[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
84 /* callback for fileselect to save rendered image, renderresult was checked to exist */
85 static void save_rendered_image_cb_real(char *name, int confirm)
86 {
87         char str[FILE_MAXDIR+FILE_MAXFILE];
88         int overwrite;
89         
90         if(BLI_testextensie(name,".blend")) {
91                 error("Wrong filename");
92                 return;
93         }
94         
95         /* BKE_add_image_extension() checks for if extension was already set */
96         if(G.scene->r.scemode & R_EXTENSION) 
97                 if(strlen(name)<FILE_MAXDIR+FILE_MAXFILE-5)
98                         BKE_add_image_extension(name, G.scene->r.imtype);
99
100         strcpy(str, name);
101         BLI_convertstringcode(str, G.sce);
102         BLI_convertstringframe(str, G.scene->r.cfra); /* TODO - is this even used? */
103
104         if (confirm)
105                 overwrite = saveover(str);
106         else
107                 overwrite = 1;
108         
109         if(overwrite) {
110                 if(G.scene->r.imtype==R_MULTILAYER) {
111                         RenderResult *rr= RE_GetResult(RE_GetRender(G.scene->id.name));
112                         if(rr) 
113                                 RE_WriteRenderResult(rr, str, G.scene->r.quality);
114                 }
115                 else {
116                         RenderResult rres;
117                         ImBuf *ibuf;
118                         
119                         RE_GetResultImage(RE_GetRender(G.scene->id.name), &rres);
120
121                         waitcursor(1); /* from screen.c */
122
123                         ibuf= IMB_allocImBuf(rres.rectx, rres.recty, G.scene->r.planes, 0, 0);
124                         ibuf->rect= (unsigned int *)rres.rect32;
125                         ibuf->rect_float= rres.rectf;
126                         ibuf->zbuf_float= rres.rectz;
127                         
128                         /* float factor for random dither, imbuf takes care of it */
129                         ibuf->dither= G.scene->r.dither_intensity;
130                         
131                         BKE_write_ibuf(ibuf, str, G.scene->r.imtype, G.scene->r.subimtype, G.scene->r.quality);
132                         IMB_freeImBuf(ibuf);    /* imbuf knows rects are not part of ibuf */
133                 }
134                 
135                 strcpy(G.ima, name);
136                 
137                 waitcursor(0);
138         }
139 }
140
141
142 void save_image_filesel_str(char *str)
143 {
144         switch(G.scene->r.imtype) {
145                 case R_RADHDR:
146                         strcpy(str, "Save Radiance HDR");
147                         break;
148                 case R_FFMPEG:
149                 case R_PNG:
150                         strcpy(str, "Save PNG");
151                         break;
152 #ifdef WITH_DDS
153                 case R_DDS:
154                         strcpy(str, "Save DDS");
155                         break;
156 #endif
157                 case R_BMP:
158                         strcpy(str, "Save BMP");
159                         break;
160                 case R_TIFF:
161                         if (G.have_libtiff)
162                                 strcpy(str, "Save TIFF");
163                         break;
164 #ifdef WITH_OPENEXR
165                 case R_OPENEXR:
166                         strcpy(str, "Save OpenEXR");
167                         break;
168 #endif
169                 case R_CINEON:
170                         strcpy(str, "Save Cineon");
171                         break;
172                 case R_DPX:
173                         strcpy(str, "Save DPX");
174                         break;
175                 case R_RAWTGA:
176                         strcpy(str, "Save Raw Targa");
177                         break;
178                 case R_IRIS:
179                         strcpy(str, "Save IRIS");
180                         break;
181                 case R_IRIZ:
182                         strcpy(str, "Save IRIS");
183                         break;
184                 case R_HAMX:
185                         strcpy(str, "Save HAMX");
186                         break;
187                 case R_TARGA:
188                         strcpy(str, "Save Targa");
189                         break;
190                 case R_MULTILAYER:
191                         strcpy(str, "Save Multi Layer EXR");
192                         break;
193 #ifdef WITH_OPENJPEG
194                 case R_JP2:
195                         strcpy(str, "Save JPEG2000");
196                         break;
197 #endif
198                         /* default we save jpeg, also for all movie formats */
199                 case R_JPEG90:
200                 case R_MOVIE:
201                 case R_AVICODEC:
202                 case R_AVIRAW:
203                 case R_AVIJPEG:
204                 default:
205                         strcpy(str, "Save JPEG");
206                         break;
207         }       
208 }
209
210 static void save_rendered_image_cb(char *name) 
211 {
212         save_rendered_image_cb_real(name, 1);
213 }
214
215 /* no fileselect, no confirm */
216 void BIF_save_rendered_image(char *name)
217 {
218         save_rendered_image_cb_real(name, 0);
219 }
220         
221 /* calls fileselect */
222 void BIF_save_rendered_image_fs(void)
223 {
224         RenderResult rres;
225         
226         RE_GetResultImage(RE_GetRender(G.scene->id.name), &rres);
227
228         if(!rres.rectf && !rres.rect32) {
229                 error("No image rendered");
230         } 
231         else {
232                 char dir[FILE_MAXDIR * 2], str[FILE_MAXFILE * 2];
233                 
234                 if(G.ima[0]==0) {
235                         strcpy(dir, G.sce);
236                         BLI_splitdirstring(dir, str);
237                         strcpy(G.ima, dir);
238                 }
239                 
240                 save_image_filesel_str(str);
241                 activate_fileselect(FILE_SPECIAL, str, G.ima, save_rendered_image_cb);
242         }
243 }
244
245