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