Color Management, Stage 2: Switch color pipeline to use OpenColorIO
[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 "UI_interface.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         rcti crop;
74
75         ImageFormatData im_format;
76 } ScreenshotData;
77
78 /* get shot from frontbuffer */
79 static unsigned int *screenshot(bContext *C, int *dumpsx, int *dumpsy)
80 {
81         wmWindow *win = CTX_wm_window(C);
82         int x = 0, y = 0;
83         unsigned int *dumprect = NULL;
84
85         x = 0;
86         y = 0;
87         *dumpsx = win->sizex;
88         *dumpsy = win->sizey;
89
90         if (*dumpsx && *dumpsy) {
91                 
92                 dumprect = MEM_mallocN(sizeof(int) * (*dumpsx) * (*dumpsy), "dumprect");
93                 glReadBuffer(GL_FRONT);
94                 glReadPixels(x, y, *dumpsx, *dumpsy, GL_RGBA, GL_UNSIGNED_BYTE, dumprect);
95                 glFinish();
96                 glReadBuffer(GL_BACK);
97         }
98
99         return dumprect;
100 }
101
102 /* call from both exec and invoke */
103 static int screenshot_data_create(bContext *C, wmOperator *op)
104 {
105         unsigned int *dumprect;
106         int dumpsx, dumpsy;
107
108         /* do redraw so we don't show popups/menus */
109         WM_redraw_windows(C);
110         
111         dumprect = screenshot(C, &dumpsx, &dumpsy);
112
113         if (dumprect) {
114                 ScreenshotData *scd = MEM_callocN(sizeof(ScreenshotData), "screenshot");
115                 ScrArea *sa = CTX_wm_area(C);
116                 
117                 scd->dumpsx = dumpsx;
118                 scd->dumpsy = dumpsy;
119                 scd->dumprect = dumprect;
120                 if (sa) {
121                         scd->crop = sa->totrct;
122                 }
123
124                 BKE_imformat_defaults(&scd->im_format);
125
126                 op->customdata = scd;
127
128                 return TRUE;
129         }
130         else {
131                 op->customdata = NULL;
132                 return FALSE;
133         }
134 }
135
136 static void screenshot_data_free(wmOperator *op)
137 {
138         ScreenshotData *scd = op->customdata;
139
140         if (scd) {
141                 if (scd->dumprect)
142                         MEM_freeN(scd->dumprect);
143                 MEM_freeN(scd);
144                 op->customdata = NULL;
145         }
146 }
147
148 static void screenshot_crop(ImBuf *ibuf, rcti crop)
149 {
150         unsigned int *to = ibuf->rect;
151         unsigned int *from = ibuf->rect + crop.ymin * ibuf->x + crop.xmin;
152         int crop_x = BLI_RCT_SIZE_X(&crop);
153         int crop_y = BLI_RCT_SIZE_Y(&crop);
154         int y;
155
156         if (crop_x > 0 && crop_y > 0) {
157                 for (y = 0; y < crop_y; y++, to += crop_x, from += ibuf->x)
158                         memmove(to, from, sizeof(unsigned int) * crop_x);
159
160                 ibuf->x = crop_x;
161                 ibuf->y = crop_y;
162         }
163 }
164
165 static int screenshot_exec(bContext *C, wmOperator *op)
166 {
167         ScreenshotData *scd = op->customdata;
168
169         if (scd == NULL) {
170                 /* when running exec directly */
171                 screenshot_data_create(C, op);
172                 scd = op->customdata;
173         }
174
175         if (scd) {
176                 if (scd->dumprect) {
177                         ImBuf *ibuf;
178                         char path[FILE_MAX];
179
180                         RNA_string_get(op->ptr, "filepath", path);
181                         BLI_path_abs(path, G.main->name);
182
183                         /* operator ensures the extension */
184                         ibuf = IMB_allocImBuf(scd->dumpsx, scd->dumpsy, 24, 0);
185                         ibuf->rect = scd->dumprect;
186
187                         /* crop to show only single editor */
188                         if (!RNA_boolean_get(op->ptr, "full"))
189                                 screenshot_crop(ibuf, scd->crop);
190
191                         if (scd->im_format.planes == R_IMF_PLANES_BW) {
192                                 /* bw screenshot? - users will notice if it fails! */
193                                 IMB_color_to_bw(ibuf);
194                         }
195                         BKE_imbuf_write(ibuf, path, &scd->im_format);
196
197                         IMB_freeImBuf(ibuf);
198                 }
199         }
200
201         screenshot_data_free(op);
202         return OPERATOR_FINISHED;
203 }
204
205 static int screenshot_invoke(bContext *C, wmOperator *op, wmEvent *UNUSED(event))
206 {
207         if (screenshot_data_create(C, op)) {
208                 if (RNA_struct_property_is_set(op->ptr, "filepath"))
209                         return screenshot_exec(C, op);
210
211                 /* extension is added by 'screenshot_check' after */
212                 RNA_string_set(op->ptr, "filepath", G.relbase_valid ? G.main->name : "//screen");
213                 
214                 WM_event_add_fileselect(C, op);
215         
216                 return OPERATOR_RUNNING_MODAL;
217         }       
218         return OPERATOR_CANCELLED;
219 }
220
221 static int screenshot_check(bContext *UNUSED(C), wmOperator *op)
222 {
223         ScreenshotData *scd = op->customdata;
224         return WM_operator_filesel_ensure_ext_imtype(op, scd->im_format.imtype);
225 }
226
227 static int screenshot_cancel(bContext *UNUSED(C), wmOperator *op)
228 {
229         screenshot_data_free(op);
230         return OPERATOR_CANCELLED;
231 }
232
233 static int screenshot_draw_check_prop(PointerRNA *UNUSED(ptr), PropertyRNA *prop)
234 {
235         const char *prop_id = RNA_property_identifier(prop);
236
237         return !(strcmp(prop_id, "filepath") == 0);
238 }
239
240 static void screenshot_draw(bContext *UNUSED(C), wmOperator *op)
241 {
242         uiLayout *layout = op->layout;
243         ScreenshotData *scd = op->customdata;
244         PointerRNA ptr;
245
246         /* image template */
247         RNA_pointer_create(NULL, &RNA_ImageFormatSettings, &scd->im_format, &ptr);
248         uiTemplateImageSettings(layout, &ptr, TRUE);
249
250         /* main draw call */
251         RNA_pointer_create(NULL, op->type->srna, op->properties, &ptr);
252         uiDefAutoButsRNA(layout, &ptr, screenshot_draw_check_prop, '\0');
253 }
254
255
256 void SCREEN_OT_screenshot(wmOperatorType *ot)
257 {
258         ot->name = "Save Screenshot"; /* weak: opname starting with 'save' makes filewindow give save-over */
259         ot->idname = "SCREEN_OT_screenshot";
260         ot->description = "Capture a picture of the active area or whole Blender window";
261         
262         ot->invoke = screenshot_invoke;
263         ot->check = screenshot_check;
264         ot->exec = screenshot_exec;
265         ot->cancel = screenshot_cancel;
266         ot->ui = screenshot_draw;
267         ot->poll = WM_operator_winactive;
268         
269         ot->flag = 0;
270         
271         WM_operator_properties_filesel(ot, FOLDERFILE | IMAGEFILE, FILE_SPECIAL, FILE_SAVE,
272                                        WM_FILESEL_FILEPATH, FILE_DEFAULTDISPLAY);
273         RNA_def_boolean(ot->srna, "full", 1, "Full Screen", "Screenshot the whole Blender window");
274 }
275
276 /* *************** screenshot movie job ************************* */
277
278 typedef struct ScreenshotJob {
279         Main *bmain;
280         Scene *scene;
281         unsigned int *dumprect;
282         int x, y, dumpsx, dumpsy;
283         short *stop;
284         short *do_update;
285         ReportList reports;
286 } ScreenshotJob;
287
288
289 static void screenshot_freejob(void *sjv)
290 {
291         ScreenshotJob *sj = sjv;
292         
293         if (sj->dumprect)
294                 MEM_freeN(sj->dumprect);
295         
296         MEM_freeN(sj);
297 }
298
299
300 /* called before redraw notifiers, copies a new dumprect */
301 static void screenshot_updatejob(void *sjv)
302 {
303         ScreenshotJob *sj = sjv;
304         unsigned int *dumprect;
305         
306         if (sj->dumprect == NULL) {
307                 dumprect = MEM_mallocN(sizeof(int) * sj->dumpsx * sj->dumpsy, "dumprect");
308                 glReadPixels(sj->x, sj->y, sj->dumpsx, sj->dumpsy, GL_RGBA, GL_UNSIGNED_BYTE, dumprect);
309                 glFinish();
310                 
311                 sj->dumprect = dumprect;
312         }
313 }
314
315
316 /* only this runs inside thread */
317 static void screenshot_startjob(void *sjv, short *stop, short *do_update, float *UNUSED(progress))
318 {
319         ScreenshotJob *sj = sjv;
320         RenderData rd = sj->scene->r;
321         bMovieHandle *mh = BKE_movie_handle_get(sj->scene->r.im_format.imtype);
322         
323         /* we need this as local variables for renderdata */
324         rd.frs_sec = U.scrcastfps;
325         rd.frs_sec_base = 1.0f;
326         
327         if (BKE_imtype_is_movie(rd.im_format.imtype)) {
328                 if (!mh->start_movie(sj->scene, &rd, sj->dumpsx, sj->dumpsy, &sj->reports)) {
329                         printf("screencast job stopped\n");
330                         return;
331                 }
332         }
333         else
334                 mh = NULL;
335         
336         sj->stop = stop;
337         sj->do_update = do_update;
338         
339         *do_update = TRUE; /* wait for opengl rect */
340         
341         while (*stop == 0) {
342                 
343                 if (sj->dumprect) {
344                         
345                         if (mh) {
346                                 if (mh->append_movie(&rd, rd.sfra, rd.cfra, (int *)sj->dumprect,
347                                                      sj->dumpsx, sj->dumpsy, &sj->reports))
348                                 {
349                                         BKE_reportf(&sj->reports, RPT_INFO, "Appended frame: %d", rd.cfra);
350                                         printf("Appended frame %d\n", rd.cfra);
351                                 }
352                                 else {
353                                         break;
354                                 }
355                         }
356                         else {
357                                 ImBuf *ibuf = IMB_allocImBuf(sj->dumpsx, sj->dumpsy, rd.im_format.planes, 0);
358                                 char name[FILE_MAX];
359                                 int ok;
360                                 
361                                 BKE_makepicstring(name, rd.pic, sj->bmain->name, rd.cfra, rd.im_format.imtype, rd.scemode & R_EXTENSION, TRUE);
362                                 
363                                 ibuf->rect = sj->dumprect;
364                                 ok = BKE_imbuf_write(ibuf, name, &rd.im_format);
365                                 
366                                 if (ok == 0) {
367                                         printf("Write error: cannot save %s\n", name);
368                                         BKE_reportf(&sj->reports, RPT_INFO, "Write error: cannot save %s\n", name);
369                                         break;
370                                 }
371                                 else {
372                                         printf("Saved file: %s\n", name);
373                                         BKE_reportf(&sj->reports, RPT_INFO, "Saved file: %s", name);
374                                 }
375                                 
376                                 /* imbuf knows which rects are not part of ibuf */
377                                 IMB_freeImBuf(ibuf);    
378                         }
379                         
380                         MEM_freeN(sj->dumprect);
381                         sj->dumprect = NULL;
382                         
383                         *do_update = TRUE;
384                         
385                         rd.cfra++;
386
387                 }
388                 else 
389                         PIL_sleep_ms(U.scrcastwait);
390         }
391         
392         if (mh)
393                 mh->end_movie();
394
395         BKE_report(&sj->reports, RPT_INFO, "Screencast job stopped");
396 }
397
398 static int screencast_exec(bContext *C, wmOperator *op)
399 {
400         bScreen *screen = CTX_wm_screen(C);
401         wmJob *wm_job = WM_jobs_get(CTX_wm_manager(C), CTX_wm_window(C), screen, "Screencast", 0, WM_JOB_TYPE_SCREENCAST);
402         ScreenshotJob *sj = MEM_callocN(sizeof(ScreenshotJob), "screenshot job");
403
404         /* setup sj */
405         if (RNA_boolean_get(op->ptr, "full")) {
406                 wmWindow *win = CTX_wm_window(C);
407                 sj->x = 0;
408                 sj->y = 0;
409                 sj->dumpsx = win->sizex;
410                 sj->dumpsy = win->sizey;
411         } 
412         else {
413                 ScrArea *curarea = CTX_wm_area(C);
414                 sj->x = curarea->totrct.xmin;
415                 sj->y = curarea->totrct.ymin;
416                 sj->dumpsx = curarea->totrct.xmax - sj->x;
417                 sj->dumpsy = curarea->totrct.ymax - sj->y;
418         }
419         sj->bmain = CTX_data_main(C);
420         sj->scene = CTX_data_scene(C);
421
422         BKE_reports_init(&sj->reports, RPT_PRINT);
423
424         /* setup job */
425         WM_jobs_customdata_set(wm_job, sj, screenshot_freejob);
426         WM_jobs_timer(wm_job, 0.1, 0, NC_SCREEN | ND_SCREENCAST);
427         WM_jobs_callbacks(wm_job, screenshot_startjob, NULL, screenshot_updatejob, NULL);
428         
429         WM_jobs_start(CTX_wm_manager(C), wm_job);
430         
431         WM_event_add_notifier(C, NC_SCREEN | ND_SCREENCAST, screen);
432         
433         return OPERATOR_FINISHED;
434 }
435
436 void SCREEN_OT_screencast(wmOperatorType *ot)
437 {
438         ot->name = "Make Screencast";
439         ot->idname = "SCREEN_OT_screencast";
440         ot->description = "Capture a video of the active area or whole Blender window";
441         
442         ot->invoke = WM_operator_confirm;
443         ot->exec = screencast_exec;
444         ot->poll = WM_operator_winactive;
445         
446         ot->flag = 0;
447         
448         RNA_def_property(ot->srna, "filepath", PROP_STRING, PROP_FILEPATH);
449         RNA_def_boolean(ot->srna, "full", 1, "Full Screen", "Screencast the whole Blender window");
450 }