Patch from GSR that a) fixes a whole bunch of GPL/BL license
[blender.git] / source / blender / src / screendump.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  * Making screendumps.
29  */
30
31 #include <string.h>
32
33 #include "MEM_guardedalloc.h"
34
35 #include "BLI_blenlib.h"
36
37 #include "IMB_imbuf_types.h"
38 #include "IMB_imbuf.h"
39
40 #include "DNA_scene_types.h"
41 #include "DNA_screen_types.h"
42 #include "DNA_space_types.h"
43
44 #include "BKE_utildefines.h"
45 #include "BKE_global.h"
46 #include "BKE_image.h"
47 #include "BKE_material.h"
48 #include "BKE_sca.h"
49
50 #include "BIF_gl.h"
51 #include "BIF_toolbox.h"
52 #include "BIF_mywindow.h"
53 #include "BIF_screen.h"
54 #include "BIF_toets.h"
55 #include "BIF_interface.h"
56 #include "BIF_writeimage.h"
57
58 #include "BSE_filesel.h"
59
60 #include "mydevice.h"
61
62 static unsigned int *dumprect=0;
63 static int dumpsx, dumpsy;
64
65 void write_screendump(char *name)
66 {
67         ImBuf *ibuf;
68         
69         if(dumprect) {
70
71                 strcpy(G.ima, name);
72                 BLI_convertstringcode(name, G.sce, G.scene->r.cfra);
73                 
74                 /* BKE_add_image_extension() checks for if extension was already set */
75                 if(G.scene->r.scemode & R_EXTENSION) 
76                         if(strlen(name)<FILE_MAXDIR+FILE_MAXFILE-5)
77                                 BKE_add_image_extension(name, G.scene->r.imtype);
78                 
79                 if(saveover(name)) {
80                         waitcursor(1);
81                         
82                         ibuf= IMB_allocImBuf(dumpsx, dumpsy, 24, 0, 0);
83                         ibuf->rect= dumprect;
84                         
85                         if(G.scene->r.planes == 8) IMB_cspace(ibuf, rgb_to_bw);
86                         
87                         BKE_write_ibuf(ibuf, name, G.scene->r.imtype, G.scene->r.subimtype, G.scene->r.quality);
88
89                         IMB_freeImBuf(ibuf);
90                         
91                         waitcursor(0);
92                 }
93                 MEM_freeN(dumprect);
94                 dumprect= NULL;
95         }
96 }
97
98 /* get dump from frontbuffer */
99 void BIF_screendump(int fscreen)
100 {
101         extern int uiIsMenu(int *x, int *y, int *sizex, int *sizey);
102         int ismenu;
103         static int wasmenu= 0;
104         int x=0, y=0;
105         char imstr[64];
106
107         /* this sets dumpsx/y to zero if ismenu==0 */
108         ismenu= uiIsMenu(&x, &y, &dumpsx, &dumpsy);
109         
110         if(wasmenu && !ismenu) {
111                 save_image_filesel_str(imstr);
112                 strcat(imstr, " (Menu)");
113                 activate_fileselect(FILE_SPECIAL, imstr, G.ima, write_screendump);
114                 wasmenu= 0;
115                 return;
116         }
117         
118         if(dumprect) MEM_freeN(dumprect);
119         dumprect= NULL;
120         
121         if((G.qual & LR_SHIFTKEY) || fscreen) { /* full screen */
122                 x= 0;
123                 y= 0;
124                 
125                 dumpsx= G.curscreen->sizex;
126                 dumpsy= G.curscreen->sizey;
127                 
128         } 
129         else {
130                 if(ismenu==0) { /* a window */
131                         //int win= mywinget();
132
133                         //bwin_getsuborigin(win, &x, &y);
134                         //bwin_getsize(win, &dumpsx, &dumpsy);
135                         x= curarea->totrct.xmin;
136                         y= curarea->totrct.ymin;
137                         dumpsx= curarea->totrct.xmax-x;
138                         dumpsy= curarea->totrct.ymax-y;
139                 }
140         }
141         
142         if (dumpsx && dumpsy) {
143                 
144                 dumprect= MEM_mallocN(sizeof(int)*dumpsx*dumpsy, "dumprect");
145                 glReadBuffer(GL_FRONT);
146                 glReadPixels(x, y, dumpsx, dumpsy, GL_RGBA, GL_UNSIGNED_BYTE, dumprect);
147                 glFinish();
148                 glReadBuffer(GL_BACK);
149                 
150                 if(ismenu==0) {
151                         wasmenu= 0;
152                         save_image_filesel_str(imstr);
153                         activate_fileselect(FILE_SPECIAL, imstr, G.ima, write_screendump);
154                 }
155                 else wasmenu= 1;
156         }
157
158 }