Blender  V2.93
screendump.c
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  *
16  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
17  * All rights reserved.
18  * Making screendumps.
19  */
20 
25 #include <errno.h>
26 #include <string.h>
27 
28 #include "MEM_guardedalloc.h"
29 
30 #include "BLI_blenlib.h"
31 #include "BLI_utildefines.h"
32 
33 #include "IMB_imbuf.h"
34 #include "IMB_imbuf_types.h"
35 
36 #include "DNA_scene_types.h"
37 #include "DNA_screen_types.h"
38 #include "DNA_space_types.h"
39 
40 #include "BKE_context.h"
41 #include "BKE_global.h"
42 #include "BKE_image.h"
43 #include "BKE_main.h"
44 #include "BKE_report.h"
45 
46 #include "RNA_access.h"
47 #include "RNA_define.h"
48 
49 #include "UI_interface.h"
50 
51 #include "WM_api.h"
52 #include "WM_types.h"
53 
54 #include "screen_intern.h"
55 
56 typedef struct ScreenshotData {
58  int dumpsx, dumpsy;
60 
63 
64 /* call from both exec and invoke */
66 {
67  int dumprect_size[2];
68 
70  wmWindow *win = CTX_wm_window(C);
71 
72  /* do redraw so we don't show popups/menus */
74 
75  uint *dumprect = WM_window_pixels_read(wm, win, dumprect_size);
76 
77  if (dumprect) {
78  ScreenshotData *scd = MEM_callocN(sizeof(ScreenshotData), "screenshot");
80 
81  scd->dumpsx = dumprect_size[0];
82  scd->dumpsy = dumprect_size[1];
83  scd->dumprect = dumprect;
84  if (area) {
85  scd->crop = area->totrct;
86  }
87 
89 
90  op->customdata = scd;
91 
92  return true;
93  }
94  op->customdata = NULL;
95  return false;
96 }
97 
99 {
100  ScreenshotData *scd = op->customdata;
101 
102  if (scd) {
103  if (scd->dumprect) {
104  MEM_freeN(scd->dumprect);
105  }
106  MEM_freeN(scd);
107  op->customdata = NULL;
108  }
109 }
110 
112 {
113  ScreenshotData *scd = op->customdata;
114  bool ok = false;
115 
116  if (scd == NULL) {
117  /* when running exec directly */
119  scd = op->customdata;
120  }
121 
122  if (scd) {
123  if (scd->dumprect) {
124  ImBuf *ibuf;
125  char path[FILE_MAX];
126 
127  RNA_string_get(op->ptr, "filepath", path);
129 
130  /* operator ensures the extension */
131  ibuf = IMB_allocImBuf(scd->dumpsx, scd->dumpsy, 24, 0);
132  ibuf->rect = scd->dumprect;
133 
134  /* crop to show only single editor */
135  if (!RNA_boolean_get(op->ptr, "full")) {
136  IMB_rect_crop(ibuf, &scd->crop);
137  scd->dumprect = ibuf->rect;
138  }
139 
140  if (scd->im_format.planes == R_IMF_PLANES_BW) {
141  /* bw screenshot? - users will notice if it fails! */
142  IMB_color_to_bw(ibuf);
143  }
144  if (BKE_imbuf_write(ibuf, path, &scd->im_format)) {
145  ok = true;
146  }
147  else {
148  BKE_reportf(op->reports, RPT_ERROR, "Could not write image: %s", strerror(errno));
149  }
150 
151  IMB_freeImBuf(ibuf);
152  }
153  }
154 
156 
158 }
159 
160 static int screenshot_invoke(bContext *C, wmOperator *op, const wmEvent *UNUSED(event))
161 {
162  if (screenshot_data_create(C, op)) {
163  if (RNA_struct_property_is_set(op->ptr, "filepath")) {
164  return screenshot_exec(C, op);
165  }
166 
167  /* extension is added by 'screenshot_check' after */
168  char filepath[FILE_MAX] = "//screen";
169  if (G.relbase_valid) {
170  BLI_strncpy(filepath, BKE_main_blendfile_path_from_global(), sizeof(filepath));
171  BLI_path_extension_replace(filepath, sizeof(filepath), ""); /* strip '.blend' */
172  }
173  RNA_string_set(op->ptr, "filepath", filepath);
174 
176 
177  return OPERATOR_RUNNING_MODAL;
178  }
179  return OPERATOR_CANCELLED;
180 }
181 
183 {
184  ScreenshotData *scd = op->customdata;
186 }
187 
189 {
191 }
192 
194  PropertyRNA *prop,
195  void *UNUSED(user_data))
196 {
197  const char *prop_id = RNA_property_identifier(prop);
198 
199  return !(STREQ(prop_id, "filepath"));
200 }
201 
203 {
204  uiLayout *layout = op->layout;
206  ScreenshotData *scd = op->customdata;
207 
208  uiLayoutSetPropSep(layout, true);
209  uiLayoutSetPropDecorate(layout, false);
210 
211  /* image template */
212  PointerRNA ptr;
214  uiTemplateImageSettings(layout, &ptr, false);
215 
216  /* main draw call */
217  RNA_pointer_create(&wm->id, op->type->srna, op->properties, &ptr);
220 }
221 
223 {
224  if (G.background) {
225  return false;
226  }
227 
228  return WM_operator_winactive(C);
229 }
230 
232 {
233  ot->name = "Save Screenshot";
234  ot->idname = "SCREEN_OT_screenshot";
235  ot->description = "Capture a picture of the active area or whole Blender window";
236 
241  ot->ui = screenshot_draw;
243 
244  ot->flag = 0;
245 
248  FILE_SPECIAL,
249  FILE_SAVE,
254  "full",
255  1,
256  "Full Screen",
257  "Capture the whole window (otherwise only capture the active area)");
258 }
struct ScrArea * CTX_wm_area(const bContext *C)
Definition: context.c:714
struct wmWindowManager * CTX_wm_manager(const bContext *C)
Definition: context.c:689
struct wmWindow * CTX_wm_window(const bContext *C)
Definition: context.c:699
void BKE_imformat_defaults(struct ImageFormatData *im_format)
Definition: image.c:1785
int BKE_imbuf_write(struct ImBuf *ibuf, const char *name, const struct ImageFormatData *imf)
const char * BKE_main_blendfile_path_from_global(void)
Definition: main.c:439
void BKE_reportf(ReportList *reports, ReportType type, const char *format,...) ATTR_PRINTF_FORMAT(3
#define FILE_MAX
bool BLI_path_extension_replace(char *path, size_t maxlen, const char *ext) ATTR_NONNULL()
Definition: path_util.c:1571
bool BLI_path_abs(char *path, const char *basepath) ATTR_NONNULL()
Definition: path_util.c:1016
char * BLI_strncpy(char *__restrict dst, const char *__restrict src, const size_t maxncpy) ATTR_NONNULL()
Definition: string.c:108
unsigned int uint
Definition: BLI_sys_types.h:83
#define UNUSED(x)
#define STREQ(a, b)
#define R_IMF_PLANES_BW
@ FILE_SORT_DEFAULT
@ FILE_SPECIAL
@ FILE_TYPE_FOLDER
@ FILE_TYPE_IMAGE
@ FILE_SAVE
@ FILE_DEFAULTDISPLAY
@ OPERATOR_CANCELLED
@ OPERATOR_FINISHED
@ OPERATOR_RUNNING_MODAL
void IMB_color_to_bw(struct ImBuf *ibuf)
Definition: divers.c:842
struct ImBuf * IMB_allocImBuf(unsigned int x, unsigned int y, unsigned char planes, unsigned int flags)
Definition: allocimbuf.c:478
void IMB_rect_crop(struct ImBuf *ibuf, const struct rcti *crop)
void IMB_freeImBuf(struct ImBuf *ibuf)
Definition: allocimbuf.c:211
Contains defines and structs used throughout the imbuf module.
Read Guarded memory(de)allocation.
StructRNA RNA_ImageFormatSettings
#define C
Definition: RandGen.cpp:39
eAutoPropButsReturn uiDefAutoButsRNA(uiLayout *layout, struct PointerRNA *ptr, bool(*check_prop)(struct PointerRNA *ptr, struct PropertyRNA *prop, void *user_data), void *user_data, struct PropertyRNA *prop_activate_init, eButLabelAlign label_align, const bool compact)
void uiTemplateImageSettings(uiLayout *layout, struct PointerRNA *imfptr, bool color_management)
void uiLayoutSetPropSep(uiLayout *layout, bool is_sep)
@ UI_BUT_LABEL_ALIGN_NONE
void uiLayoutSetPropDecorate(uiLayout *layout, bool is_sep)
#define WM_FILESEL_FILEPATH
Definition: WM_api.h:537
void * user_data
void(* MEM_freeN)(void *vmemh)
Definition: mallocn.c:41
void *(* MEM_callocN)(size_t len, const char *str)
Definition: mallocn.c:45
static void area(int d1, int d2, int e1, int e2, float weights[2])
void RNA_pointer_create(ID *id, StructRNA *type, void *data, PointerRNA *r_ptr)
Definition: rna_access.c:146
void RNA_string_set(PointerRNA *ptr, const char *name, const char *value)
Definition: rna_access.c:6550
const char * RNA_property_identifier(const PropertyRNA *prop)
Definition: rna_access.c:1145
void RNA_string_get(PointerRNA *ptr, const char *name, char *value)
Definition: rna_access.c:6514
bool RNA_struct_property_is_set(PointerRNA *ptr, const char *identifier)
Definition: rna_access.c:6685
bool RNA_boolean_get(PointerRNA *ptr, const char *name)
Definition: rna_access.c:6261
PropertyRNA * RNA_def_boolean(StructOrFunctionRNA *cont_, const char *identifier, bool default_value, const char *ui_name, const char *ui_description)
Definition: rna_define.c:3481
struct ScreenshotData ScreenshotData
static bool screenshot_poll(bContext *C)
Definition: screendump.c:222
void SCREEN_OT_screenshot(wmOperatorType *ot)
Definition: screendump.c:231
static void screenshot_draw(bContext *C, wmOperator *op)
Definition: screendump.c:202
static int screenshot_invoke(bContext *C, wmOperator *op, const wmEvent *UNUSED(event))
Definition: screendump.c:160
static void screenshot_cancel(bContext *UNUSED(C), wmOperator *op)
Definition: screendump.c:188
static int screenshot_exec(bContext *C, wmOperator *op)
Definition: screendump.c:111
static void screenshot_data_free(wmOperator *op)
Definition: screendump.c:98
static bool screenshot_draw_check_prop(PointerRNA *UNUSED(ptr), PropertyRNA *prop, void *UNUSED(user_data))
Definition: screendump.c:193
static bool screenshot_check(bContext *UNUSED(C), wmOperator *op)
Definition: screendump.c:182
static int screenshot_data_create(bContext *C, wmOperator *op)
Definition: screendump.c:65
unsigned int * rect
ImageFormatData im_format
Definition: screendump.c:61
uint * dumprect
Definition: screendump.c:57
int(* invoke)(struct bContext *, struct wmOperator *, const struct wmEvent *) ATTR_WARN_UNUSED_RESULT
Definition: WM_types.h:752
const char * name
Definition: WM_types.h:721
const char * idname
Definition: WM_types.h:723
bool(* poll)(struct bContext *) ATTR_WARN_UNUSED_RESULT
Definition: WM_types.h:776
void(* cancel)(struct bContext *, struct wmOperator *)
Definition: WM_types.h:760
struct StructRNA * srna
Definition: WM_types.h:802
const char * description
Definition: WM_types.h:726
void(* ui)(struct bContext *, struct wmOperator *)
Definition: WM_types.h:787
bool(* check)(struct bContext *, struct wmOperator *)
Definition: WM_types.h:744
int(* exec)(struct bContext *, struct wmOperator *) ATTR_WARN_UNUSED_RESULT
Definition: WM_types.h:736
struct ReportList * reports
IDProperty * properties
struct uiLayout * layout
struct wmOperatorType * type
struct PointerRNA * ptr
#define G(x, y, z)
void WM_redraw_windows(bContext *C)
Definition: wm_draw.c:1105
void WM_event_add_fileselect(bContext *C, wmOperator *op)
PointerRNA * ptr
Definition: wm_files.c:3157
wmOperatorType * ot
Definition: wm_files.c:3156
void WM_operator_properties_filesel(wmOperatorType *ot, int filter, short type, short action, short flag, short display, short sort)
bool WM_operator_filesel_ensure_ext_imtype(wmOperator *op, const struct ImageFormatData *im_format)
bool WM_operator_winactive(bContext *C)
uint * WM_window_pixels_read(wmWindowManager *wm, wmWindow *win, int r_size[2])
Definition: wm_window.c:1989