fe2dc1fb9134686b6b8cec1a895cacb5cb506a79
[blender.git] / source / blender / editors / screen / screendump.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
19  * All rights reserved.
20  *
21  * Contributor(s): Blender Foundation
22  *
23  * ***** END GPL LICENSE BLOCK *****
24  * Making screendumps.
25  */
26
27 /** \file blender/editors/screen/screendump.c
28  *  \ingroup edscr
29  */
30
31
32 #include <string.h>
33
34 #include "MEM_guardedalloc.h"
35
36 #include "BLI_blenlib.h"
37 #include "BLI_utildefines.h"
38
39 #include "IMB_imbuf_types.h"
40 #include "IMB_imbuf.h"
41
42 #include "DNA_scene_types.h"
43 #include "DNA_screen_types.h"
44 #include "DNA_space_types.h"
45 #include "DNA_userdef_types.h"
46
47 #include "BKE_context.h"
48 #include "BKE_global.h"
49 #include "BKE_main.h"
50 #include "BKE_image.h"
51 #include "BKE_report.h"
52 #include "BKE_writeavi.h"
53
54 #include "BIF_gl.h"
55
56 #include "RNA_access.h"
57 #include "RNA_define.h"
58
59 #include "WM_types.h"
60 #include "WM_api.h"
61
62 #include "PIL_time.h"
63
64 #include "ED_screen_types.h"
65
66 #include "screen_intern.h"
67
68 typedef struct ScreenshotData {
69         unsigned int *dumprect;
70         int dumpsx, dumpsy;
71         rcti crop;
72 } ScreenshotData;
73
74 /* get shot from frontbuffer */
75 static unsigned int *screenshot(bContext *C, int *dumpsx, int *dumpsy)
76 {
77         wmWindow *win= CTX_wm_window(C);
78         int x=0, y=0;
79         unsigned int *dumprect= NULL;
80         
81         x= 0;
82         y= 0;
83         *dumpsx= win->sizex;
84         *dumpsy= win->sizey;
85
86         if (*dumpsx && *dumpsy) {
87                 
88                 dumprect= MEM_mallocN(sizeof(int) * (*dumpsx) * (*dumpsy), "dumprect");
89                 glReadBuffer(GL_FRONT);
90                 glReadPixels(x, y, *dumpsx, *dumpsy, GL_RGBA, GL_UNSIGNED_BYTE, dumprect);
91                 glFinish();
92                 glReadBuffer(GL_BACK);
93         }
94
95         return dumprect;
96 }
97
98 /* call from both exec and invoke */
99 static int screenshot_data_create(bContext *C, wmOperator *op)
100 {
101         unsigned int *dumprect;
102         int dumpsx, dumpsy;
103
104         /* do redraw so we don't show popups/menus */
105         WM_redraw_windows(C);
106         
107         dumprect= screenshot(C, &dumpsx, &dumpsy);
108
109         if(dumprect) {
110                 ScreenshotData *scd= MEM_callocN(sizeof(ScreenshotData), "screenshot");
111                 ScrArea *sa= CTX_wm_area(C);
112                 
113                 scd->dumpsx= dumpsx;
114                 scd->dumpsy= dumpsy;
115                 scd->dumprect= dumprect;
116                 if(sa)
117                         scd->crop= sa->totrct;
118                 op->customdata= scd;
119
120                 return TRUE;
121         }
122         else {
123                 op->customdata= NULL;
124                 return FALSE;
125         }
126 }
127
128 static void screenshot_data_free(wmOperator *op)
129 {
130         ScreenshotData *scd= op->customdata;
131
132         if(scd) {
133                 if(scd->dumprect)
134                         MEM_freeN(scd->dumprect);
135                 MEM_freeN(scd);
136                 op->customdata= NULL;
137         }
138 }
139
140 static void screenshot_crop(ImBuf *ibuf, rcti crop)
141 {
142         unsigned int *to= ibuf->rect;
143         unsigned int *from= ibuf->rect + crop.ymin*ibuf->x + crop.xmin;
144         int y, cropw= crop.xmax - crop.xmin, croph = crop.ymax - crop.ymin;
145
146         if(cropw > 0 && croph > 0) {
147                 for(y=0; y<croph; y++, to+=cropw, from+=ibuf->x)
148                         memmove(to, from, sizeof(unsigned int)*cropw);
149
150                 ibuf->x= cropw;
151                 ibuf->y= croph;
152         }
153 }
154
155 static int screenshot_exec(bContext *C, wmOperator *op)
156 {
157         ScreenshotData *scd= op->customdata;
158
159         if(scd == NULL) {
160                 /* when running exec directly */
161                 screenshot_data_create(C, op);
162                 scd= op->customdata;
163         }
164
165         if(scd) {
166                 if(scd->dumprect) {
167                         Scene *scene= CTX_data_scene(C);
168                         ImBuf *ibuf;
169                         char path[FILE_MAX];
170
171                         RNA_string_get(op->ptr, "filepath", path);
172
173                         BLI_strncpy(G.ima, path, sizeof(G.ima));
174                         BLI_path_abs(path, G.main->name);
175
176                         /* BKE_add_image_extension() checks for if extension was already set */
177                         if(scene->r.scemode & R_EXTENSION)
178                                 if(strlen(path)<FILE_MAX-5)
179                                         BKE_add_image_extension(path, scene->r.im_format.imtype);
180
181                         ibuf= IMB_allocImBuf(scd->dumpsx, scd->dumpsy, 24, 0);
182                         ibuf->rect= scd->dumprect;
183
184                         /* crop to show only single editor */
185                         if(!RNA_boolean_get(op->ptr, "full"))
186                                 screenshot_crop(ibuf, scd->crop);
187
188                         BKE_write_ibuf(ibuf, path, &scene->r.im_format);
189
190                         IMB_freeImBuf(ibuf);
191                 }
192         }
193
194         screenshot_data_free(op);
195         return OPERATOR_FINISHED;
196 }
197
198 static int screenshot_invoke(bContext *C, wmOperator *op, wmEvent *UNUSED(event))
199 {
200         if(screenshot_data_create(C, op)) {
201                 if(RNA_property_is_set(op->ptr, "filepath"))
202                         return screenshot_exec(C, op);
203                 
204                 RNA_string_set(op->ptr, "filepath", G.ima);
205                 
206                 WM_event_add_fileselect(C, op);
207         
208                 return OPERATOR_RUNNING_MODAL;
209         }       
210         return OPERATOR_CANCELLED;
211 }
212
213 static int screenshot_cancel(bContext *UNUSED(C), wmOperator *op)
214 {
215         screenshot_data_free(op);
216         return OPERATOR_CANCELLED;
217 }
218
219 void SCREEN_OT_screenshot(wmOperatorType *ot)
220 {
221         ot->name= "Save Screenshot"; /* weak: opname starting with 'save' makes filewindow give save-over */
222         ot->idname= "SCREEN_OT_screenshot";
223         
224         ot->invoke= screenshot_invoke;
225         ot->exec= screenshot_exec;
226         ot->poll= WM_operator_winactive;
227         ot->cancel= screenshot_cancel;
228         
229         ot->flag= 0;
230         
231         WM_operator_properties_filesel(ot, FOLDERFILE|IMAGEFILE, FILE_SPECIAL, FILE_SAVE, WM_FILESEL_FILEPATH);
232         RNA_def_boolean(ot->srna, "full", 1, "Full Screen", "");
233 }
234
235 /* *************** screenshot movie job ************************* */
236
237 typedef struct ScreenshotJob {
238         Main *bmain;
239         Scene *scene;
240         unsigned int *dumprect;
241         int x, y, dumpsx, dumpsy;
242         short *stop;
243         short *do_update;
244         ReportList reports;
245 } ScreenshotJob;
246
247
248 static void screenshot_freejob(void *sjv)
249 {
250         ScreenshotJob *sj= sjv;
251         
252         if(sj->dumprect)
253                 MEM_freeN(sj->dumprect);
254         
255         MEM_freeN(sj);
256 }
257
258
259 /* called before redraw notifiers, copies a new dumprect */
260 static void screenshot_updatejob(void *sjv)
261 {
262         ScreenshotJob *sj= sjv;
263         unsigned int *dumprect;
264         
265         if(sj->dumprect==NULL) {
266                 dumprect= MEM_mallocN(sizeof(int) * sj->dumpsx * sj->dumpsy, "dumprect");
267                 glReadPixels(sj->x, sj->y, sj->dumpsx, sj->dumpsy, GL_RGBA, GL_UNSIGNED_BYTE, dumprect);
268                 glFinish();
269                 
270                 sj->dumprect= dumprect;
271         }
272 }
273
274
275 /* only this runs inside thread */
276 static void screenshot_startjob(void *sjv, short *stop, short *do_update, float *UNUSED(progress))
277 {
278         ScreenshotJob *sj= sjv;
279         RenderData rd= sj->scene->r;
280         bMovieHandle *mh= BKE_get_movie_handle(sj->scene->r.im_format.imtype);
281         
282         /* we need this as local variables for renderdata */
283         rd.frs_sec= U.scrcastfps;
284         rd.frs_sec_base= 1.0f;
285         
286         if(BKE_imtype_is_movie(rd.im_format.imtype)) {
287                 if(!mh->start_movie(sj->scene, &rd, sj->dumpsx, sj->dumpsy, &sj->reports)) {
288                         printf("screencast job stopped\n");
289                         return;
290                 }
291         }
292         else
293                 mh= NULL;
294         
295         sj->stop= stop;
296         sj->do_update= do_update;
297         
298         *do_update= 1; // wait for opengl rect
299         
300         while(*stop==0) {
301                 
302                 if(sj->dumprect) {
303                         
304                         if(mh) {
305                                 if(mh->append_movie(&rd, rd.cfra, (int *)sj->dumprect, sj->dumpsx, sj->dumpsy, &sj->reports)) {
306                                         BKE_reportf(&sj->reports, RPT_INFO, "Appended frame: %d", rd.cfra);
307                                         printf("Appended frame %d\n", rd.cfra);
308                                 } else
309                                         break;
310                         }
311                         else {
312                                 ImBuf *ibuf= IMB_allocImBuf(sj->dumpsx, sj->dumpsy, rd.im_format.planes, 0);
313                                 char name[FILE_MAX];
314                                 int ok;
315                                 
316                                 BKE_makepicstring(name, rd.pic, sj->bmain->name, rd.cfra, rd.im_format.imtype, rd.scemode & R_EXTENSION, TRUE);
317                                 
318                                 ibuf->rect= sj->dumprect;
319                                 ok= BKE_write_ibuf(ibuf, name, &rd.im_format);
320                                 
321                                 if(ok==0) {
322                                         printf("Write error: cannot save %s\n", name);
323                                         BKE_reportf(&sj->reports, RPT_INFO, "Write error: cannot save %s\n", name);
324                                         break;
325                                 }
326                                 else {
327                                         printf("Saved file: %s\n", name);
328                                         BKE_reportf(&sj->reports, RPT_INFO, "Saved file: %s", name);
329                                 }
330                                 
331                                 /* imbuf knows which rects are not part of ibuf */
332                                 IMB_freeImBuf(ibuf);    
333                         }
334                         
335                         MEM_freeN(sj->dumprect);
336                         sj->dumprect= NULL;
337                         
338                         *do_update= 1;
339                         
340                         rd.cfra++;
341
342                 }
343                 else 
344                         PIL_sleep_ms(U.scrcastwait);
345         }
346         
347         if(mh)
348                 mh->end_movie();
349
350         BKE_report(&sj->reports, RPT_INFO, "Screencast job stopped");
351 }
352
353 static int screencast_exec(bContext *C, wmOperator *op)
354 {
355         bScreen *screen= CTX_wm_screen(C);
356         wmJob *steve= WM_jobs_get(CTX_wm_manager(C), CTX_wm_window(C), screen, "Screencast", 0);
357         ScreenshotJob *sj= MEM_callocN(sizeof(ScreenshotJob), "screenshot job");
358
359         /* setup sj */
360         if(RNA_boolean_get(op->ptr, "full")) {
361                 wmWindow *win= CTX_wm_window(C);
362                 sj->x= 0;
363                 sj->y= 0;
364                 sj->dumpsx= win->sizex;
365                 sj->dumpsy= win->sizey;
366         } 
367         else {
368                 ScrArea *curarea= CTX_wm_area(C);
369                 sj->x= curarea->totrct.xmin;
370                 sj->y= curarea->totrct.ymin;
371                 sj->dumpsx= curarea->totrct.xmax - sj->x;
372                 sj->dumpsy= curarea->totrct.ymax - sj->y;
373         }
374         sj->bmain= CTX_data_main(C);
375         sj->scene= CTX_data_scene(C);
376
377         BKE_reports_init(&sj->reports, RPT_PRINT);
378
379         /* setup job */
380         WM_jobs_customdata(steve, sj, screenshot_freejob);
381         WM_jobs_timer(steve, 0.1, 0, NC_SCREEN|ND_SCREENCAST);
382         WM_jobs_callbacks(steve, screenshot_startjob, NULL, screenshot_updatejob, NULL);
383         
384         WM_jobs_start(CTX_wm_manager(C), steve);
385         
386         WM_event_add_notifier(C, NC_SCREEN|ND_SCREENCAST, screen);
387         
388         return OPERATOR_FINISHED;
389 }
390
391 void SCREEN_OT_screencast(wmOperatorType *ot)
392 {
393         ot->name= "Make Screencast";
394         ot->idname= "SCREEN_OT_screencast";
395         
396         ot->invoke= WM_operator_confirm;
397         ot->exec= screencast_exec;
398         ot->poll= WM_operator_winactive;
399         
400         ot->flag= 0;
401         
402         RNA_def_property(ot->srna, "filepath", PROP_STRING, PROP_FILEPATH);
403         RNA_def_boolean(ot->srna, "full", 1, "Full Screen", "");
404 }
405
406
407