- remove all obsolete inclusions of BLI_winstuff.h (due to recent changes)
[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 #ifdef HAVE_CONFIG_H
37 #include <config.h>
38 #endif
39
40 #include "MEM_guardedalloc.h"
41
42 #include "BLI_blenlib.h"
43
44 #include "IMB_imbuf_types.h"
45 #include "IMB_imbuf.h"
46
47 #include "DNA_scene_types.h"
48 #include "DNA_screen_types.h"
49 #include "DNA_space_types.h"
50
51 #include "BKE_utildefines.h"
52 #include "BKE_global.h"
53 #include "BKE_material.h"
54 #include "BKE_sca.h"
55
56 #include "BIF_gl.h"
57 #include "BIF_toolbox.h"
58 #include "BIF_mywindow.h"
59 #include "BIF_screen.h"
60 #include "BIF_toets.h"
61 #include "BIF_interface.h"
62
63 #include "BSE_filesel.h"
64
65 #include "mydevice.h"
66
67 static unsigned int *dumprect=0;
68 static int dumpsx, dumpsy;
69
70 void write_screendump(char *name);
71
72 void write_screendump(char *name)
73 {
74         ImBuf *ibuf;
75         
76         if(dumprect) {
77
78                 strcpy(G.ima, name);
79                 BLI_convertstringcode(name, G.sce, G.scene->r.cfra);
80                 
81                 if(saveover(name)) {
82                         waitcursor(1);
83                         
84                         ibuf= IMB_allocImBuf(dumpsx, dumpsy, 24, 0, 0);
85                         ibuf->rect= dumprect;
86                         
87                         if(G.scene->r.imtype== R_IRIS) ibuf->ftype= IMAGIC;
88                         else if(G.scene->r.imtype==R_IRIZ) ibuf->ftype= IMAGIC;
89                         else if(G.scene->r.imtype==R_TARGA) ibuf->ftype= TGA;
90                         else if(G.scene->r.imtype==R_RAWTGA) ibuf->ftype= RAWTGA;
91                         else if(G.scene->r.imtype==R_PNG) ibuf->ftype= PNG;
92                         else if(G.scene->r.imtype==R_HAMX) ibuf->ftype= AN_hamx;
93                         else if(ELEM5(G.scene->r.imtype, R_MOVIE, R_AVICODEC, R_AVIRAW, R_AVIJPEG, R_JPEG90)) {
94                                 ibuf->ftype= JPG|G.scene->r.quality;
95                         }
96                         else ibuf->ftype= TGA;  
97                         
98                         if(G.scene->r.planes == 8) IMB_cspace(ibuf, rgb_to_bw);
99                         
100                         IMB_saveiff(ibuf, name, IB_rect);
101                         IMB_freeImBuf(ibuf);
102                         
103                         waitcursor(0);
104                 }
105                 MEM_freeN(dumprect);
106                 dumprect= 0;
107         }
108 }
109
110 /* get dump from frontbuffer */
111 void BIF_screendump(int fscreen)
112 {
113         extern uiBut *UIbuttip; // interface.c
114         static int wasmenu= 0;
115         int x=0, y=0;
116         char imstr[64];
117
118         if(wasmenu && UIbuttip==NULL) {
119                 save_image_filesel_str(imstr);
120                 activate_fileselect(FILE_SPECIAL, imstr, G.ima, write_screendump);
121                 wasmenu= 0;
122                 return;
123         }
124         
125         dumpsx= 0;
126         dumpsy= 0;
127         
128         if(dumprect) MEM_freeN(dumprect);
129         dumprect= NULL;
130         
131         if(UIbuttip || (G.qual & LR_SHIFTKEY) || fscreen) {     /* full screen */
132                 x= 0;
133                 y= 0;
134                 
135                 dumpsx= G.curscreen->sizex;
136                 dumpsy= G.curscreen->sizey;
137                 
138         } 
139         else {
140                 {       /* a window */
141                         //int win= mywinget();
142
143                         //bwin_getsuborigin(win, &x, &y);
144                         //bwin_getsize(win, &dumpsx, &dumpsy);
145                         x= curarea->totrct.xmin;
146                         y= curarea->totrct.ymin;
147                         dumpsx= curarea->totrct.xmax-x;
148                         dumpsy= curarea->totrct.ymax-y;
149                 }
150         }
151         
152         if (dumpsx && dumpsy) {
153                 
154                 dumprect= MEM_mallocN(sizeof(int)*dumpsx*dumpsy, "dumprect");
155                 glReadBuffer(GL_FRONT);
156                 glReadPixels(x, y, dumpsx, dumpsy, GL_RGBA, GL_UNSIGNED_BYTE, dumprect);
157                 glFinish();
158                 glReadBuffer(GL_BACK);
159                 
160                 if(UIbuttip==NULL) {
161                         wasmenu= 0;
162                         save_image_filesel_str(imstr);
163                         activate_fileselect(FILE_SPECIAL, imstr, G.ima, write_screendump);
164                 }
165                 else wasmenu= 1;
166         }
167
168 }