Final merge of HEAD (bf-blender) into the orange branch.
[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 #include "BIF_writeimage.h"
59
60 #include "BSE_filesel.h"
61
62 #include "mydevice.h"
63
64 static unsigned int *dumprect=0;
65 static int dumpsx, dumpsy;
66
67 void write_screendump(char *name);
68
69 void write_screendump(char *name)
70 {
71         ImBuf *ibuf;
72         
73         if(dumprect) {
74
75                 strcpy(G.ima, name);
76                 BLI_convertstringcode(name, G.sce, G.scene->r.cfra);
77                 
78                 if(saveover(name)) {
79                         waitcursor(1);
80                         
81                         ibuf= IMB_allocImBuf(dumpsx, dumpsy, 24, 0, 0);
82                         ibuf->rect= dumprect;
83                         
84                         if(G.scene->r.imtype== R_IRIS) ibuf->ftype= IMAGIC;
85                         else if(G.scene->r.imtype==R_IRIZ) ibuf->ftype= IMAGIC;
86                         else if(G.scene->r.imtype==R_TARGA) ibuf->ftype= TGA;
87                         else if(G.scene->r.imtype==R_RAWTGA) ibuf->ftype= RAWTGA;
88                         else if(G.scene->r.imtype==R_PNG) ibuf->ftype= PNG;
89                         else if((G.have_libtiff) && 
90                                 (G.scene->r.imtype==R_TIFF)) ibuf->ftype= TIF;
91 #ifdef WITH_OPENEXR
92                         else if(G.scene->r.imtype==R_OPENEXR) {
93                                 ibuf->ftype= OPENEXR;
94                                 if(G.scene->r.subimtype & R_OPENEXR_HALF)
95                                         ibuf->ftype |= OPENEXR_HALF;
96                                 ibuf->ftype |= (G.scene->r.quality & OPENEXR_COMPRESS);
97                         }
98 #endif
99                         else if(G.scene->r.imtype==R_HAMX) ibuf->ftype= AN_hamx;
100                         else if(ELEM5(G.scene->r.imtype, R_MOVIE, R_AVICODEC, R_AVIRAW, R_AVIJPEG, R_JPEG90)) {
101                                 ibuf->ftype= JPG|G.scene->r.quality;
102                         }
103                         else ibuf->ftype= TGA;  
104                         
105                         if(G.scene->r.planes == 8) IMB_cspace(ibuf, rgb_to_bw);
106                         
107                         IMB_saveiff(ibuf, name, IB_rect);
108                         IMB_freeImBuf(ibuf);
109                         
110                         waitcursor(0);
111                 }
112                 MEM_freeN(dumprect);
113                 dumprect= 0;
114         }
115 }
116
117 /* get dump from frontbuffer */
118 void BIF_screendump(int fscreen)
119 {
120         extern uiBut *UIbuttip; // interface.c
121         static int wasmenu= 0;
122         int x=0, y=0;
123         char imstr[64];
124
125         if(wasmenu && UIbuttip==NULL) {
126                 save_image_filesel_str(imstr);
127                 activate_fileselect(FILE_SPECIAL, imstr, G.ima, write_screendump);
128                 wasmenu= 0;
129                 return;
130         }
131         
132         dumpsx= 0;
133         dumpsy= 0;
134         
135         if(dumprect) MEM_freeN(dumprect);
136         dumprect= NULL;
137         
138         if(UIbuttip || (G.qual & LR_SHIFTKEY) || fscreen) {     /* full screen */
139                 x= 0;
140                 y= 0;
141                 
142                 dumpsx= G.curscreen->sizex;
143                 dumpsy= G.curscreen->sizey;
144                 
145         } 
146         else {
147                 {       /* a window */
148                         //int win= mywinget();
149
150                         //bwin_getsuborigin(win, &x, &y);
151                         //bwin_getsize(win, &dumpsx, &dumpsy);
152                         x= curarea->totrct.xmin;
153                         y= curarea->totrct.ymin;
154                         dumpsx= curarea->totrct.xmax-x;
155                         dumpsy= curarea->totrct.ymax-y;
156                 }
157         }
158         
159         if (dumpsx && dumpsy) {
160                 
161                 dumprect= MEM_mallocN(sizeof(int)*dumpsx*dumpsy, "dumprect");
162                 glReadBuffer(GL_FRONT);
163                 glReadPixels(x, y, dumpsx, dumpsy, GL_RGBA, GL_UNSIGNED_BYTE, dumprect);
164                 glFinish();
165                 glReadBuffer(GL_BACK);
166                 
167                 if(UIbuttip==NULL) {
168                         wasmenu= 0;
169                         save_image_filesel_str(imstr);
170                         activate_fileselect(FILE_SPECIAL, imstr, G.ima, write_screendump);
171                 }
172                 else wasmenu= 1;
173         }
174
175 }