doxygen: blender/editors tagged.
[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(scene, 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         ot->name= "Save Screenshot"; /* weak: opname starting with 'save' makes filewindow give save-over */
186         ot->idname= "SCREEN_OT_screenshot";
187         
188         ot->invoke= screenshot_invoke;
189         ot->exec= screenshot_exec;
190         ot->poll= WM_operator_winactive;
191         ot->cancel= screenshot_cancel;
192         
193         ot->flag= 0;
194         
195         WM_operator_properties_filesel(ot, FOLDERFILE|IMAGEFILE, FILE_SPECIAL, FILE_SAVE, WM_FILESEL_FILEPATH);
196         RNA_def_boolean(ot->srna, "full", 1, "Full Screen", "");
197 }
198
199 /* *************** screenshot movie job ************************* */
200
201 typedef struct ScreenshotJob {
202         Scene *scene;
203         unsigned int *dumprect;
204         int x, y, dumpsx, dumpsy;
205         short *stop;
206         short *do_update;
207         ReportList reports;
208 } ScreenshotJob;
209
210
211 static void screenshot_freejob(void *sjv)
212 {
213         ScreenshotJob *sj= sjv;
214         
215         if(sj->dumprect)
216                 MEM_freeN(sj->dumprect);
217         
218         MEM_freeN(sj);
219 }
220
221
222 /* called before redraw notifiers, copies a new dumprect */
223 static void screenshot_updatejob(void *sjv)
224 {
225         ScreenshotJob *sj= sjv;
226         unsigned int *dumprect;
227         
228         if(sj->dumprect==NULL) {
229                 dumprect= MEM_mallocN(sizeof(int) * sj->dumpsx * sj->dumpsy, "dumprect");
230                 glReadPixels(sj->x, sj->y, sj->dumpsx, sj->dumpsy, GL_RGBA, GL_UNSIGNED_BYTE, dumprect);
231                 glFinish();
232                 
233                 sj->dumprect= dumprect;
234         }
235 }
236
237
238 /* only this runs inside thread */
239 static void screenshot_startjob(void *sjv, short *stop, short *do_update, float *UNUSED(progress))
240 {
241         ScreenshotJob *sj= sjv;
242         RenderData rd= sj->scene->r;
243         bMovieHandle *mh= BKE_get_movie_handle(sj->scene->r.imtype);
244         int cfra= 1;
245         
246         /* we need this as local variables for renderdata */
247         rd.frs_sec= U.scrcastfps;
248         rd.frs_sec_base= 1.0f;
249         
250         if(BKE_imtype_is_movie(rd.imtype)) {
251                 if(!mh->start_movie(sj->scene, &rd, sj->dumpsx, sj->dumpsy, &sj->reports)) {
252                         printf("screencast job stopped\n");
253                         return;
254                 }
255         }
256         else
257                 mh= NULL;
258         
259         sj->stop= stop;
260         sj->do_update= do_update;
261         
262         *do_update= 1; // wait for opengl rect
263         
264         while(*stop==0) {
265                 
266                 if(sj->dumprect) {
267                         
268                         if(mh) {
269                                 if(mh->append_movie(&rd, cfra, (int *)sj->dumprect, sj->dumpsx, sj->dumpsy, &sj->reports)) {
270                                         BKE_reportf(&sj->reports, RPT_INFO, "Appended frame: %d", cfra);
271                                         printf("Appended frame %d\n", cfra);
272                                 } else
273                                         break;
274                         }
275                         else {
276                                 ImBuf *ibuf= IMB_allocImBuf(sj->dumpsx, sj->dumpsy, rd.planes, 0);
277                                 char name[FILE_MAXDIR+FILE_MAXFILE];
278                                 int ok;
279                                 
280                                 BKE_makepicstring(name, rd.pic, cfra, rd.imtype, rd.scemode & R_EXTENSION, TRUE);
281                                 
282                                 ibuf->rect= sj->dumprect;
283                                 ok= BKE_write_ibuf(sj->scene, ibuf, name, rd.imtype, rd.subimtype, rd.quality);
284                                 
285                                 if(ok==0) {
286                                         printf("Write error: cannot save %s\n", name);
287                                         BKE_reportf(&sj->reports, RPT_INFO, "Write error: cannot save %s\n", name);
288                                         break;
289                                 }
290                                 else {
291                                         printf("Saved file: %s\n", name);
292                                         BKE_reportf(&sj->reports, RPT_INFO, "Saved file: %s", name);
293                                 }
294                                 
295                                 /* imbuf knows which rects are not part of ibuf */
296                                 IMB_freeImBuf(ibuf);    
297                         }
298                         
299                         MEM_freeN(sj->dumprect);
300                         sj->dumprect= NULL;
301                         
302                         *do_update= 1;
303                         
304                         cfra++;
305
306                 }
307                 else 
308                         PIL_sleep_ms(U.scrcastwait);
309         }
310         
311         if(mh)
312                 mh->end_movie();
313
314         BKE_report(&sj->reports, RPT_INFO, "Screencast job stopped");
315 }
316
317 static int screencast_exec(bContext *C, wmOperator *op)
318 {
319         bScreen *screen= CTX_wm_screen(C);
320         wmJob *steve= WM_jobs_get(CTX_wm_manager(C), CTX_wm_window(C), screen, "Screencast", 0);
321         ScreenshotJob *sj= MEM_callocN(sizeof(ScreenshotJob), "screenshot job");
322
323         /* setup sj */
324         if(RNA_boolean_get(op->ptr, "full")) {
325                 wmWindow *win= CTX_wm_window(C);
326                 sj->x= 0;
327                 sj->y= 0;
328                 sj->dumpsx= win->sizex;
329                 sj->dumpsy= win->sizey;
330         } 
331         else {
332                 ScrArea *curarea= CTX_wm_area(C);
333                 sj->x= curarea->totrct.xmin;
334                 sj->y= curarea->totrct.ymin;
335                 sj->dumpsx= curarea->totrct.xmax - sj->x;
336                 sj->dumpsy= curarea->totrct.ymax - sj->y;
337         }
338         sj->scene= CTX_data_scene(C);
339
340         BKE_reports_init(&sj->reports, RPT_PRINT);
341
342         /* setup job */
343         WM_jobs_customdata(steve, sj, screenshot_freejob);
344         WM_jobs_timer(steve, 0.1, 0, NC_SCREEN|ND_SCREENCAST);
345         WM_jobs_callbacks(steve, screenshot_startjob, NULL, screenshot_updatejob, NULL);
346         
347         WM_jobs_start(CTX_wm_manager(C), steve);
348         
349         WM_event_add_notifier(C, NC_SCREEN|ND_SCREENCAST, screen);
350         
351         return OPERATOR_FINISHED;
352 }
353
354 void SCREEN_OT_screencast(wmOperatorType *ot)
355 {
356         ot->name= "Make Screencast";
357         ot->idname= "SCREEN_OT_screencast";
358         
359         ot->invoke= WM_operator_confirm;
360         ot->exec= screencast_exec;
361         ot->poll= WM_operator_winactive;
362         
363         ot->flag= 0;
364         
365         RNA_def_property(ot->srna, "filepath", PROP_STRING, PROP_FILEPATH);
366         RNA_def_boolean(ot->srna, "full", 1, "Full Screen", "");
367 }
368
369
370