Orange: Further cleanup of EXR saving
[blender.git] / source / blender / src / screendump.c
1 /**
2  * $Id$
3  *
4  * ***** BEGIN GPL/BL DUAL 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. The Blender
10  * Foundation also sells licenses for use in proprietary software under
11  * the Blender License.  See http://www.blender.org/BL/ for information
12  * about this.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software Foundation,
21  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22  *
23  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
24  * All rights reserved.
25  *
26  * The Original Code is: all of this file.
27  *
28  * Contributor(s): none yet.
29  *
30  * ***** END GPL/BL DUAL LICENSE BLOCK *****
31  * Making screendumps.
32  */
33
34 #include <string.h>
35
36 #include "MEM_guardedalloc.h"
37
38 #include "BLI_blenlib.h"
39
40 #include "IMB_imbuf_types.h"
41 #include "IMB_imbuf.h"
42
43 #include "DNA_scene_types.h"
44 #include "DNA_screen_types.h"
45 #include "DNA_space_types.h"
46
47 #include "BKE_utildefines.h"
48 #include "BKE_global.h"
49 #include "BKE_material.h"
50 #include "BKE_sca.h"
51
52 #include "BIF_gl.h"
53 #include "BIF_toolbox.h"
54 #include "BIF_mywindow.h"
55 #include "BIF_screen.h"
56 #include "BIF_toets.h"
57 #include "BIF_interface.h"
58
59 #include "BSE_filesel.h"
60
61 #include "mydevice.h"
62
63 static unsigned int *dumprect=0;
64 static int dumpsx, dumpsy;
65
66 void write_screendump(char *name);
67
68 void write_screendump(char *name)
69 {
70         ImBuf *ibuf;
71         
72         if(dumprect) {
73
74                 strcpy(G.ima, name);
75                 BLI_convertstringcode(name, G.sce, G.scene->r.cfra);
76                 
77                 if(saveover(name)) {
78                         waitcursor(1);
79                         
80                         ibuf= IMB_allocImBuf(dumpsx, dumpsy, 24, 0, 0);
81                         ibuf->rect= dumprect;
82                         
83                         if(G.scene->r.imtype== R_IRIS) ibuf->ftype= IMAGIC;
84                         else if(G.scene->r.imtype==R_IRIZ) ibuf->ftype= IMAGIC;
85                         else if(G.scene->r.imtype==R_TARGA) ibuf->ftype= TGA;
86                         else if(G.scene->r.imtype==R_RAWTGA) ibuf->ftype= RAWTGA;
87                         else if(G.scene->r.imtype==R_PNG) ibuf->ftype= PNG;
88                         else if((G.have_libtiff) && 
89                                 (G.scene->r.imtype==R_TIFF)) ibuf->ftype= TIF;
90 #ifdef WITH_OPENEXR
91                         else if(G.scene->r.imtype==R_OPENEXR) {
92                                 ibuf->ftype= OPENEXR;
93                                 if(G.scene->r.subimtype & R_OPENEXR_HALF)
94                                         ibuf->ftype |= OPENEXR_HALF;
95                                 ibuf->ftype |= (G.scene->r.quality & OPENEXR_COMPRESS);
96                         }
97 #endif
98                         else if(G.scene->r.imtype==R_HAMX) ibuf->ftype= AN_hamx;
99                         else if(ELEM5(G.scene->r.imtype, R_MOVIE, R_AVICODEC, R_AVIRAW, R_AVIJPEG, R_JPEG90)) {
100                                 ibuf->ftype= JPG|G.scene->r.quality;
101                         }
102                         else ibuf->ftype= TGA;  
103                         
104                         if(G.scene->r.planes == 8) IMB_cspace(ibuf, rgb_to_bw);
105                         
106                         IMB_saveiff(ibuf, name, IB_rect);
107                         IMB_freeImBuf(ibuf);
108                         
109                         waitcursor(0);
110                 }
111                 MEM_freeN(dumprect);
112                 dumprect= 0;
113         }
114 }
115
116 /* get dump from frontbuffer */
117 void BIF_screendump(int fscreen)
118 {
119         extern uiBut *UIbuttip; // interface.c
120         static int wasmenu= 0;
121         int x=0, y=0;
122         char imstr[64];
123
124         if(wasmenu && UIbuttip==NULL) {
125                 save_image_filesel_str(imstr);
126                 activate_fileselect(FILE_SPECIAL, imstr, G.ima, write_screendump);
127                 wasmenu= 0;
128                 return;
129         }
130         
131         dumpsx= 0;
132         dumpsy= 0;
133         
134         if(dumprect) MEM_freeN(dumprect);
135         dumprect= NULL;
136         
137         if(UIbuttip || (G.qual & LR_SHIFTKEY) || fscreen) {     /* full screen */
138                 x= 0;
139                 y= 0;
140                 
141                 dumpsx= G.curscreen->sizex;
142                 dumpsy= G.curscreen->sizey;
143                 
144         } 
145         else {
146                 {       /* a window */
147                         //int win= mywinget();
148
149                         //bwin_getsuborigin(win, &x, &y);
150                         //bwin_getsize(win, &dumpsx, &dumpsy);
151                         x= curarea->totrct.xmin;
152                         y= curarea->totrct.ymin;
153                         dumpsx= curarea->totrct.xmax-x;
154                         dumpsy= curarea->totrct.ymax-y;
155                 }
156         }
157         
158         if (dumpsx && dumpsy) {
159                 
160                 dumprect= MEM_mallocN(sizeof(int)*dumpsx*dumpsy, "dumprect");
161                 glReadBuffer(GL_FRONT);
162                 glReadPixels(x, y, dumpsx, dumpsy, GL_RGBA, GL_UNSIGNED_BYTE, dumprect);
163                 glFinish();
164                 glReadBuffer(GL_BACK);
165                 
166                 if(UIbuttip==NULL) {
167                         wasmenu= 0;
168                         save_image_filesel_str(imstr);
169                         activate_fileselect(FILE_SPECIAL, imstr, G.ima, write_screendump);
170                 }
171                 else wasmenu= 1;
172         }
173
174 }