Filling in branch from trunk
[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_image.h"
50 #include "BKE_material.h"
51 #include "BKE_sca.h"
52
53 #include "BIF_gl.h"
54 #include "BIF_toolbox.h"
55 #include "BIF_mywindow.h"
56 #include "BIF_screen.h"
57 #include "BIF_toets.h"
58 #include "BIF_interface.h"
59 #include "BIF_writeimage.h"
60
61 #include "BSE_filesel.h"
62
63 #include "mydevice.h"
64
65 static unsigned int *dumprect=0;
66 static int dumpsx, dumpsy;
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                 /* BKE_add_image_extension() checks for if extension was already set */
78                 if(G.scene->r.scemode & R_EXTENSION) 
79                         if(strlen(name)<FILE_MAXDIR+FILE_MAXFILE-5)
80                                 BKE_add_image_extension(name, G.scene->r.imtype);
81                 
82                 if(saveover(name)) {
83                         waitcursor(1);
84                         
85                         ibuf= IMB_allocImBuf(dumpsx, dumpsy, 24, 0, 0);
86                         ibuf->rect= dumprect;
87                         
88                         if(G.scene->r.planes == 8) IMB_cspace(ibuf, rgb_to_bw);
89                         
90                         BKE_write_ibuf(ibuf, name, G.scene->r.imtype, G.scene->r.subimtype, G.scene->r.quality);
91
92                         IMB_freeImBuf(ibuf);
93                         
94                         waitcursor(0);
95                 }
96                 MEM_freeN(dumprect);
97                 dumprect= NULL;
98         }
99 }
100
101 /* get dump from frontbuffer */
102 void BIF_screendump(int fscreen)
103 {
104         extern int uiIsMenu(int *x, int *y, int *sizex, int *sizey);
105         int ismenu;
106         static int wasmenu= 0;
107         int x=0, y=0;
108         char imstr[64];
109
110         /* this sets dumpsx/y to zero if ismenu==0 */
111         ismenu= uiIsMenu(&x, &y, &dumpsx, &dumpsy);
112         
113         if(wasmenu && !ismenu) {
114                 save_image_filesel_str(imstr);
115                 strcat(imstr, " (Menu)");
116                 activate_fileselect(FILE_SPECIAL, imstr, G.ima, write_screendump);
117                 wasmenu= 0;
118                 return;
119         }
120         
121         if(dumprect) MEM_freeN(dumprect);
122         dumprect= NULL;
123         
124         if((G.qual & LR_SHIFTKEY) || fscreen) { /* full screen */
125                 x= 0;
126                 y= 0;
127                 
128                 dumpsx= G.curscreen->sizex;
129                 dumpsy= G.curscreen->sizey;
130                 
131         } 
132         else {
133                 if(ismenu==0) { /* a window */
134                         //int win= mywinget();
135
136                         //bwin_getsuborigin(win, &x, &y);
137                         //bwin_getsize(win, &dumpsx, &dumpsy);
138                         x= curarea->totrct.xmin;
139                         y= curarea->totrct.ymin;
140                         dumpsx= curarea->totrct.xmax-x;
141                         dumpsy= curarea->totrct.ymax-y;
142                 }
143         }
144         
145         if (dumpsx && dumpsy) {
146                 
147                 dumprect= MEM_mallocN(sizeof(int)*dumpsx*dumpsy, "dumprect");
148                 glReadBuffer(GL_FRONT);
149                 glReadPixels(x, y, dumpsx, dumpsy, GL_RGBA, GL_UNSIGNED_BYTE, dumprect);
150                 glFinish();
151                 glReadBuffer(GL_BACK);
152                 
153                 if(ismenu==0) {
154                         wasmenu= 0;
155                         save_image_filesel_str(imstr);
156                         activate_fileselect(FILE_SPECIAL, imstr, G.ima, write_screendump);
157                 }
158                 else wasmenu= 1;
159         }
160
161 }