4 * ***** BEGIN GPL LICENSE BLOCK *****
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.
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.
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.
20 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
21 * All rights reserved.
23 * Contributor(s): Blender Foundation
25 * ***** END GPL LICENSE BLOCK *****
29 /** \file blender/editors/screen/screendump.c
36 #include "MEM_guardedalloc.h"
38 #include "BLI_blenlib.h"
39 #include "BLI_utildefines.h"
41 #include "IMB_imbuf_types.h"
42 #include "IMB_imbuf.h"
44 #include "DNA_scene_types.h"
45 #include "DNA_screen_types.h"
46 #include "DNA_space_types.h"
47 #include "DNA_userdef_types.h"
49 #include "BKE_context.h"
50 #include "BKE_global.h"
52 #include "BKE_image.h"
53 #include "BKE_report.h"
54 #include "BKE_writeavi.h"
58 #include "RNA_access.h"
59 #include "RNA_define.h"
66 #include "ED_screen_types.h"
68 #include "screen_intern.h"
70 typedef struct ScreenshotData {
71 unsigned int *dumprect;
75 static int screenshot_exec(bContext *C, wmOperator *op)
77 ScreenshotData *scd= op->customdata;
81 Scene *scene= CTX_data_scene(C);
85 RNA_string_get(op->ptr, "filepath", path);
88 BLI_path_abs(path, G.main->name);
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);
95 ibuf= IMB_allocImBuf(scd->dumpsx, scd->dumpsy, 24, 0);
96 ibuf->rect= scd->dumprect;
98 BKE_write_ibuf(ibuf, path, scene->r.imtype, scene->r.subimtype, scene->r.quality);
102 MEM_freeN(scd->dumprect);
105 op->customdata= NULL;
107 return OPERATOR_FINISHED;
110 /* get shot from frontbuffer */
111 static unsigned int *screenshot(bContext *C, int *dumpsx, int *dumpsy, int fscreen)
113 wmWindow *win= CTX_wm_window(C);
114 ScrArea *curarea= CTX_wm_area(C);
116 unsigned int *dumprect= NULL;
118 if(fscreen) { /* full screen */
125 x= curarea->totrct.xmin;
126 y= curarea->totrct.ymin;
127 *dumpsx= curarea->totrct.xmax-x;
128 *dumpsy= curarea->totrct.ymax-y;
131 if (*dumpsx && *dumpsy) {
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);
137 glReadBuffer(GL_BACK);
144 static int screenshot_invoke(bContext *C, wmOperator *op, wmEvent *UNUSED(event))
146 unsigned int *dumprect;
149 dumprect= screenshot(C, &dumpsx, &dumpsy, RNA_boolean_get(op->ptr, "full"));
151 ScreenshotData *scd= MEM_callocN(sizeof(ScreenshotData), "screenshot");
155 scd->dumprect= dumprect;
158 if(RNA_property_is_set(op->ptr, "filepath"))
159 return screenshot_exec(C, op);
161 RNA_string_set(op->ptr, "filepath", G.ima);
163 WM_event_add_fileselect(C, op);
165 return OPERATOR_RUNNING_MODAL;
167 return OPERATOR_CANCELLED;
170 static int screenshot_cancel(bContext *UNUSED(C), wmOperator *op)
172 ScreenshotData *scd= op->customdata;
176 MEM_freeN(scd->dumprect);
178 op->customdata= NULL;
180 return OPERATOR_CANCELLED;
183 void SCREEN_OT_screenshot(wmOperatorType *ot)
187 ot->name= "Save Screenshot"; /* weak: opname starting with 'save' makes filewindow give save-over */
188 ot->idname= "SCREEN_OT_screenshot";
190 ot->invoke= screenshot_invoke;
191 ot->exec= screenshot_exec;
192 ot->poll= WM_operator_winactive;
193 ot->cancel= screenshot_cancel;
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 */
202 /* *************** screenshot movie job ************************* */
204 typedef struct ScreenshotJob {
206 unsigned int *dumprect;
207 int x, y, dumpsx, dumpsy;
214 static void screenshot_freejob(void *sjv)
216 ScreenshotJob *sj= sjv;
219 MEM_freeN(sj->dumprect);
225 /* called before redraw notifiers, copies a new dumprect */
226 static void screenshot_updatejob(void *sjv)
228 ScreenshotJob *sj= sjv;
229 unsigned int *dumprect;
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);
236 sj->dumprect= dumprect;
241 /* only this runs inside thread */
242 static void screenshot_startjob(void *sjv, short *stop, short *do_update, float *UNUSED(progress))
244 ScreenshotJob *sj= sjv;
245 RenderData rd= sj->scene->r;
246 bMovieHandle *mh= BKE_get_movie_handle(sj->scene->r.imtype);
249 /* we need this as local variables for renderdata */
250 rd.frs_sec= U.scrcastfps;
251 rd.frs_sec_base= 1.0f;
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");
263 sj->do_update= do_update;
265 *do_update= 1; // wait for opengl rect
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);
279 ImBuf *ibuf= IMB_allocImBuf(sj->dumpsx, sj->dumpsy, rd.planes, 0);
280 char name[FILE_MAXDIR+FILE_MAXFILE];
283 BKE_makepicstring(name, rd.pic, cfra, rd.imtype, rd.scemode & R_EXTENSION, TRUE);
285 ibuf->rect= sj->dumprect;
286 ok= BKE_write_ibuf(ibuf, name, rd.imtype, rd.subimtype, rd.quality);
289 printf("Write error: cannot save %s\n", name);
290 BKE_reportf(&sj->reports, RPT_INFO, "Write error: cannot save %s\n", name);
294 printf("Saved file: %s\n", name);
295 BKE_reportf(&sj->reports, RPT_INFO, "Saved file: %s", name);
298 /* imbuf knows which rects are not part of ibuf */
302 MEM_freeN(sj->dumprect);
311 PIL_sleep_ms(U.scrcastwait);
317 BKE_report(&sj->reports, RPT_INFO, "Screencast job stopped");
320 static int screencast_exec(bContext *C, wmOperator *op)
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");
327 if(RNA_boolean_get(op->ptr, "full")) {
328 wmWindow *win= CTX_wm_window(C);
331 sj->dumpsx= win->sizex;
332 sj->dumpsy= win->sizey;
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;
341 sj->scene= CTX_data_scene(C);
343 BKE_reports_init(&sj->reports, RPT_PRINT);
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);
350 WM_jobs_start(CTX_wm_manager(C), steve);
352 WM_event_add_notifier(C, NC_SCREEN|ND_SCREENCAST, screen);
354 return OPERATOR_FINISHED;
357 void SCREEN_OT_screencast(wmOperatorType *ot)
359 ot->name= "Make Screencast";
360 ot->idname= "SCREEN_OT_screencast";
362 ot->invoke= WM_operator_confirm;
363 ot->exec= screencast_exec;
364 ot->poll= WM_operator_winactive;
368 RNA_def_property(ot->srna, "filepath", PROP_STRING, PROP_FILEPATH);
369 RNA_def_boolean(ot->srna, "full", 1, "Full Screen", "");