remove unused includes
[blender-staging.git] / source / blender / editors / screen / 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19  *
20  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
21  * All rights reserved.
22  *
23  * Contributor(s): Blender Foundation
24  *
25  * ***** END GPL LICENSE BLOCK *****
26  * Making screendumps.
27  */
28
29 #include <string.h>
30
31 #include "MEM_guardedalloc.h"
32
33 #include "BLI_blenlib.h"
34
35 #include "IMB_imbuf_types.h"
36 #include "IMB_imbuf.h"
37
38 #include "DNA_scene_types.h"
39 #include "DNA_screen_types.h"
40 #include "DNA_space_types.h"
41 #include "DNA_userdef_types.h"
42
43 #include "BKE_context.h"
44 #include "BKE_global.h"
45 #include "BKE_image.h"
46 #include "BKE_report.h"
47 #include "BKE_writeavi.h"
48
49 #include "BIF_gl.h"
50
51 #include "RNA_access.h"
52 #include "RNA_define.h"
53
54 #include "WM_types.h"
55 #include "WM_api.h"
56
57 #include "PIL_time.h"
58
59 #include "ED_screen_types.h"
60
61 #include "screen_intern.h"
62
63 typedef struct ScreenshotData {
64         unsigned int *dumprect;
65         int dumpsx, dumpsy;
66 } ScreenshotData;
67
68 static int screenshot_exec(bContext *C, wmOperator *op)
69 {
70         ScreenshotData *scd= op->customdata;
71         
72         if(scd && scd->dumprect) {
73                 Scene *scene= CTX_data_scene(C);
74                 ImBuf *ibuf;
75                 char path[FILE_MAX];
76         
77                 RNA_string_get(op->ptr, "filepath", path);
78         
79                 strcpy(G.ima, path);
80                 BLI_path_abs(path, G.sce);
81                 
82                 /* BKE_add_image_extension() checks for if extension was already set */
83                 if(scene->r.scemode & R_EXTENSION) 
84                         if(strlen(path)<FILE_MAXDIR+FILE_MAXFILE-5)
85                                 BKE_add_image_extension(path, scene->r.imtype);
86                 
87                 ibuf= IMB_allocImBuf(scd->dumpsx, scd->dumpsy, 24, 0, 0);
88                 ibuf->rect= scd->dumprect;
89                 
90                 BKE_write_ibuf(scene, ibuf, path, scene->r.imtype, scene->r.subimtype, scene->r.quality);
91
92                 IMB_freeImBuf(ibuf);
93
94                 MEM_freeN(scd->dumprect);
95                 MEM_freeN(scd);
96                 op->customdata= NULL;
97         }
98         return OPERATOR_FINISHED;
99 }
100
101 /* get shot from frontbuffer */
102 static unsigned int *screenshot(bContext *C, int *dumpsx, int *dumpsy, int fscreen)
103 {
104         wmWindow *win= CTX_wm_window(C);
105         ScrArea *curarea= CTX_wm_area(C);
106         int x=0, y=0;
107         unsigned int *dumprect= NULL;
108         
109         if(fscreen) {   /* full screen */
110                 x= 0;
111                 y= 0;
112                 *dumpsx= win->sizex;
113                 *dumpsy= win->sizey;
114         } 
115         else {
116                 x= curarea->totrct.xmin;
117                 y= curarea->totrct.ymin;
118                 *dumpsx= curarea->totrct.xmax-x;
119                 *dumpsy= curarea->totrct.ymax-y;
120         }
121
122         if (*dumpsx && *dumpsy) {
123                 
124                 dumprect= MEM_mallocN(sizeof(int) * (*dumpsx) * (*dumpsy), "dumprect");
125                 glReadBuffer(GL_FRONT);
126                 glReadPixels(x, y, *dumpsx, *dumpsy, GL_RGBA, GL_UNSIGNED_BYTE, dumprect);
127                 glFinish();
128                 glReadBuffer(GL_BACK);
129         }
130
131         return dumprect;
132 }
133
134
135 static int screenshot_invoke(bContext *C, wmOperator *op, wmEvent *event)
136 {
137         unsigned int *dumprect;
138         int dumpsx, dumpsy;
139         
140         dumprect= screenshot(C, &dumpsx, &dumpsy, RNA_boolean_get(op->ptr, "full"));
141         if(dumprect) {
142                 ScreenshotData *scd= MEM_callocN(sizeof(ScreenshotData), "screenshot");
143                 
144                 scd->dumpsx= dumpsx;
145                 scd->dumpsy= dumpsy;
146                 scd->dumprect= dumprect;
147                 op->customdata= scd;
148                 
149                 if(RNA_property_is_set(op->ptr, "filepath"))
150                         return screenshot_exec(C, op);
151                 
152                 RNA_string_set(op->ptr, "filepath", G.ima);
153                 
154                 WM_event_add_fileselect(C, op);
155         
156                 return OPERATOR_RUNNING_MODAL;
157         }       
158         return OPERATOR_CANCELLED;
159 }
160
161
162 void SCREEN_OT_screenshot(wmOperatorType *ot)
163 {
164         ot->name= "Save Screenshot"; /* weak: opname starting with 'save' makes filewindow give save-over */
165         ot->idname= "SCREEN_OT_screenshot";
166         
167         ot->invoke= screenshot_invoke;
168         ot->exec= screenshot_exec;
169         ot->poll= WM_operator_winactive;
170         
171         ot->flag= 0;
172         
173         WM_operator_properties_filesel(ot, FOLDERFILE|IMAGEFILE, FILE_SPECIAL, FILE_SAVE, WM_FILESEL_FILEPATH);
174         RNA_def_boolean(ot->srna, "full", 1, "Full Screen", "");
175 }
176
177 /* *************** screenshot movie job ************************* */
178
179 typedef struct ScreenshotJob {
180         Scene *scene;
181         unsigned int *dumprect;
182         int x, y, dumpsx, dumpsy;
183         short *stop;
184         short *do_update;
185         ReportList reports;
186 } ScreenshotJob;
187
188
189 static void screenshot_freejob(void *sjv)
190 {
191         ScreenshotJob *sj= sjv;
192         
193         if(sj->dumprect)
194                 MEM_freeN(sj->dumprect);
195         
196         MEM_freeN(sj);
197 }
198
199
200 /* called before redraw notifiers, copies a new dumprect */
201 static void screenshot_updatejob(void *sjv)
202 {
203         ScreenshotJob *sj= sjv;
204         unsigned int *dumprect;
205         
206         if(sj->dumprect==NULL) {
207                 dumprect= MEM_mallocN(sizeof(int) * sj->dumpsx * sj->dumpsy, "dumprect");
208                 glReadPixels(sj->x, sj->y, sj->dumpsx, sj->dumpsy, GL_RGBA, GL_UNSIGNED_BYTE, dumprect);
209                 glFinish();
210                 
211                 sj->dumprect= dumprect;
212         }
213 }
214
215
216 /* only this runs inside thread */
217 static void screenshot_startjob(void *sjv, short *stop, short *do_update, float *progress)
218 {
219         ScreenshotJob *sj= sjv;
220         RenderData rd= sj->scene->r;
221         bMovieHandle *mh= BKE_get_movie_handle(sj->scene->r.imtype);
222         int cfra= 1;
223         
224         /* we need this as local variables for renderdata */
225         rd.frs_sec= U.scrcastfps;
226         rd.frs_sec_base= 1.0f;
227         
228         if(BKE_imtype_is_movie(rd.imtype)) {
229                 if(!mh->start_movie(sj->scene, &rd, sj->dumpsx, sj->dumpsy, &sj->reports)) {
230                         printf("screencast job stopped\n");
231                         return;
232                 }
233         }
234         else
235                 mh= NULL;
236         
237         sj->stop= stop;
238         sj->do_update= do_update;
239         
240         *do_update= 1; // wait for opengl rect
241         
242         while(*stop==0) {
243                 
244                 if(sj->dumprect) {
245                         
246                         if(mh) {
247                                 if(mh->append_movie(&rd, cfra, (int *)sj->dumprect, sj->dumpsx, sj->dumpsy, &sj->reports)) {
248                                         BKE_reportf(&sj->reports, RPT_INFO, "Appended frame: %d", cfra);
249                                         printf("Appended frame %d\n", cfra);
250                                 } else
251                                         break;
252                         }
253                         else {
254                                 ImBuf *ibuf= IMB_allocImBuf(sj->dumpsx, sj->dumpsy, rd.planes, 0, 0);
255                                 char name[FILE_MAXDIR+FILE_MAXFILE];
256                                 int ok;
257                                 
258                                 BKE_makepicstring(name, rd.pic, cfra, rd.imtype, rd.scemode & R_EXTENSION);
259                                 
260                                 ibuf->rect= sj->dumprect;
261                                 ok= BKE_write_ibuf(sj->scene, ibuf, name, rd.imtype, rd.subimtype, rd.quality);
262                                 
263                                 if(ok==0) {
264                                         printf("Write error: cannot save %s\n", name);
265                                         BKE_reportf(&sj->reports, RPT_INFO, "Write error: cannot save %s\n", name);
266                                         break;
267                                 }
268                                 else {
269                                         printf("Saved file: %s\n", name);
270                                         BKE_reportf(&sj->reports, RPT_INFO, "Saved file: %s", name);
271                                 }
272                                 
273                                 /* imbuf knows which rects are not part of ibuf */
274                                 IMB_freeImBuf(ibuf);    
275                         }
276                         
277                         MEM_freeN(sj->dumprect);
278                         sj->dumprect= NULL;
279                         
280                         *do_update= 1;
281                         
282                         cfra++;
283
284                 }
285                 else 
286                         PIL_sleep_ms(U.scrcastwait);
287         }
288         
289         if(mh)
290                 mh->end_movie();
291
292         BKE_report(&sj->reports, RPT_INFO, "Screencast job stopped");
293 }
294
295 static int screencast_exec(bContext *C, wmOperator *op)
296 {
297         bScreen *screen= CTX_wm_screen(C);
298         wmJob *steve= WM_jobs_get(CTX_wm_manager(C), CTX_wm_window(C), screen, "Screencast", 0);
299         ScreenshotJob *sj= MEM_callocN(sizeof(ScreenshotJob), "screenshot job");
300
301         /* setup sj */
302         if(RNA_boolean_get(op->ptr, "full")) {
303                 wmWindow *win= CTX_wm_window(C);
304                 sj->x= 0;
305                 sj->y= 0;
306                 sj->dumpsx= win->sizex;
307                 sj->dumpsy= win->sizey;
308         } 
309         else {
310                 ScrArea *curarea= CTX_wm_area(C);
311                 sj->x= curarea->totrct.xmin;
312                 sj->y= curarea->totrct.ymin;
313                 sj->dumpsx= curarea->totrct.xmax - sj->x;
314                 sj->dumpsy= curarea->totrct.ymax - sj->y;
315         }
316         sj->scene= CTX_data_scene(C);
317
318         BKE_reports_init(&sj->reports, RPT_PRINT);
319
320         /* setup job */
321         WM_jobs_customdata(steve, sj, screenshot_freejob);
322         WM_jobs_timer(steve, 0.1, 0, NC_SCREEN|ND_SCREENCAST);
323         WM_jobs_callbacks(steve, screenshot_startjob, NULL, screenshot_updatejob, NULL);
324         
325         WM_jobs_start(CTX_wm_manager(C), steve);
326         
327         WM_event_add_notifier(C, NC_SCREEN|ND_SCREENCAST, screen);
328         
329         return OPERATOR_FINISHED;
330 }
331
332 void SCREEN_OT_screencast(wmOperatorType *ot)
333 {
334         ot->name= "Make Screencast";
335         ot->idname= "SCREEN_OT_screencast";
336         
337         ot->invoke= WM_operator_confirm;
338         ot->exec= screencast_exec;
339         ot->poll= WM_operator_winactive;
340         
341         ot->flag= 0;
342         
343         RNA_def_property(ot->srna, "filepath", PROP_STRING, PROP_FILEPATH);
344         RNA_def_boolean(ot->srna, "full", 1, "Full Screen", "");
345 }
346
347
348