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