Blender V5.3
screendump.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2001-2002 NaN Holding BV. All rights reserved.
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
9
10#include <cerrno>
11#include <cstring>
12
13#include "MEM_guardedalloc.h"
14
15#include "BLI_path_utils.hh"
16#include "BLI_rect.hh"
17#include "BLI_string_utf8.hh"
18#include "BLI_utildefines.hh"
19
20#include "IMB_imbuf.hh"
21#include "IMB_imbuf_types.hh"
22
23#include "DNA_scene_types.h"
24#include "DNA_screen_types.h"
25#include "DNA_space_types.h"
26
27#include "BKE_context.hh"
28#include "BKE_global.hh"
29#include "BKE_image.hh"
30#include "BKE_image_format.hh"
31#include "BKE_main.hh"
32#include "BKE_report.hh"
33#include "BKE_screen.hh"
34
35#include "BLT_translation.hh"
36
37#include "RNA_access.hh"
38#include "RNA_prototypes.hh"
39
40#include "UI_interface.hh"
42
43#include "WM_api.hh"
44#include "WM_types.hh"
45
46#include "screen_intern.hh"
47
48namespace blender {
49
51 /* Ownership transferred to ImBuf in exec function. */
52 uint8_t *dumprect = nullptr;
53 int dumpsx = 0, dumpsy = 0;
54 rcti crop = {};
55 bool use_crop = false;
56
58};
59
60/* call from both exec and invoke */
62{
63 int dumprect_size[2];
64
65 wmWindow *win = CTX_wm_window(C);
66
67 /* do redraw so we don't show popups/menus */
69
70 uint8_t *dumprect = WM_window_pixels_read(C, win, dumprect_size);
71
72 if (dumprect) {
73 ScreenshotData *scd = MEM_new<ScreenshotData>("screenshot");
74
75 scd->dumpsx = dumprect_size[0];
76 scd->dumpsy = dumprect_size[1];
77 scd->dumprect = dumprect;
78 if (area) {
79 scd->crop = area->totrct;
80 }
81
83
84 op->customdata = scd;
85
86 return true;
87 }
88 op->customdata = nullptr;
89 return false;
90}
91
93{
94 MEM_delete(static_cast<ScreenshotData *>(op->customdata));
95 op->customdata = nullptr;
96}
97
99{
100 const bool use_crop = STREQ(op->idname, "SCREEN_OT_screenshot_area");
101 ScreenshotData *scd = static_cast<ScreenshotData *>(op->customdata);
102 bool ok = false;
103
104 if (scd == nullptr) {
105 /* when running exec directly */
106 screenshot_data_create(C, op, use_crop ? CTX_wm_area(C) : nullptr);
107 scd = static_cast<ScreenshotData *>(op->customdata);
108 }
109
110 if (scd) {
111 if (scd->dumprect) {
112 ImBuf *ibuf;
113 char filepath[FILE_MAX];
114
115 RNA_string_get(op->ptr, "filepath", filepath);
117
118 /* operator ensures the extension */
119 ibuf = IMB_allocImBuf(scd->dumpsx, scd->dumpsy, ImBufFlags::Zero);
121 ibuf->assign_byte_data(scd->dumprect);
122 scd->dumprect = nullptr;
123
124 /* crop to show only single editor */
125 if (use_crop) {
126 IMB_crop(ibuf,
127 int2(scd->crop.xmin, scd->crop.ymin),
128 int2(BLI_rcti_size_x(&scd->crop) + 1, BLI_rcti_size_y(&scd->crop) + 1));
129 }
130
131 if ((scd->im_format.color_mode == ImColorMode::BW) &&
133 {
134 /* bw screenshot? - users will notice if it fails! */
135 IMB_color_to_bw(ibuf);
136 }
137 if (BKE_imbuf_write(ibuf, filepath, &scd->im_format)) {
138 ok = true;
139 }
140 else {
141 BKE_reportf(op->reports, RPT_ERROR, "Could not write image: %s", strerror(errno));
142 }
143
144 IMB_freeImBuf(ibuf);
145 }
146 }
147
149
151}
152
154{
155 const bool use_crop = STREQ(op->idname, "SCREEN_OT_screenshot_area");
156 ScrArea *area = nullptr;
157 if (use_crop) {
158 area = CTX_wm_area(C);
159 bScreen *screen = CTX_wm_screen(C);
160 ScrArea *area_test = BKE_screen_find_area_xy(screen, SPACE_TYPE_ANY, event->xy);
161 if (area_test != nullptr) {
162 area = area_test;
163 }
164 }
165
166 if (screenshot_data_create(C, op, area)) {
167 if (RNA_struct_property_is_set(op->ptr, "filepath")) {
168 return screenshot_exec(C, op);
169 }
170
171 /* extension is added by 'screenshot_check' after */
172 char filepath[FILE_MAX];
173 const char *blendfile_path = BKE_main_blendfile_path_from_global();
174 if (blendfile_path[0] != '\0') {
175 STRNCPY(filepath, blendfile_path);
176 BLI_path_extension_strip(filepath); /* Strip `.blend`. */
177 }
178 else {
179 /* As the file isn't saved, only set the name and let the file selector pick a directory. */
180 STRNCPY_UTF8(filepath, DATA_("screen"));
181 }
182 RNA_string_set(op->ptr, "filepath", filepath);
183
185
187 }
188 return OPERATOR_CANCELLED;
189}
190
191static bool screenshot_check(bContext * /*C*/, wmOperator *op)
192{
193 ScreenshotData *scd = static_cast<ScreenshotData *>(op->customdata);
195}
196
197static void screenshot_cancel(bContext * /*C*/, wmOperator *op)
198{
200}
201
203 PropertyRNA *prop,
204 void * /*user_data*/)
205{
206 const char *prop_id = RNA_property_identifier(prop);
207
208 return !STREQ(prop_id, "filepath");
209}
210
212{
213 ui::Layout &layout = *op->layout;
214 ScreenshotData *scd = static_cast<ScreenshotData *>(op->customdata);
215
216 layout.use_property_split_set(true);
217 layout.use_property_decorate_set(false);
218
219 /* image template */
220 PointerRNA ptr = RNA_pointer_create_discrete(nullptr, RNA_ImageFormatSettings, &scd->im_format);
221 uiTemplateImageSettings(&layout, C, &ptr, false);
222
223 /* main draw call */
224 uiDefAutoButsRNA(&layout,
225 op->ptr,
227 nullptr,
228 nullptr,
230 false);
231}
232
234{
235 if (G.background) {
236 return false;
237 }
238
239 return WM_operator_winactive(C);
240}
241
259
261{
262 ot->name = "Save Screenshot";
263 ot->idname = "SCREEN_OT_screenshot";
264 ot->description = "Capture a picture of the whole Blender window";
265
267
268 ot->flag = 0;
269}
270
272{
273 /* NOTE: the term "area" is a Blender internal name, "Editor" makes more sense for the UI. */
274 ot->name = "Save Screenshot (Editor)";
275 ot->idname = "SCREEN_OT_screenshot_area";
276 ot->description = "Capture a picture of an editor";
277
279
281}
282
283} // namespace blender
#define FILE_MAX
#define STRNCPY_UTF8(dst, src)
#define DATA_(msgid)
#define SPACE_TYPE_ANY
Read Blender memory allocation and freeing.
#define C
Definition RandGen.cpp:29
#define STREQ(a, b)
BMesh Iterators.
bool bool BLI_path_extension_strip(char *path) ATTR_NONNULL(1)
ScrArea * CTX_wm_area(const bContext *C)
char * STRNCPY(char(&dst)[N], const char *src)
bool BLI_path_abs(char path[FILE_MAX], const char *basepath) ATTR_NONNULL(1
static void screenshot_data_free(wmOperator *op)
Definition screendump.cc:92
static bool screenshot_check(bContext *, wmOperator *op)
const char * RNA_property_identifier(const PropertyRNA *prop)
void RNA_string_set(PointerRNA *ptr, const char *name, const char *value)
static void screenshot_draw(bContext *C, wmOperator *op)
static bool screenshot_draw_check_prop(PointerRNA *, PropertyRNA *prop, void *)
static void screenshot_cancel(bContext *, wmOperator *op)
wmWindow * CTX_wm_window(const bContext *C)
uint8_t * WM_window_pixels_read(bContext *C, wmWindow *win, int r_size[2])
Definition wm_draw.cc:1575
const char * BKE_main_blendfile_path_from_global()
Definition main.cc:1063
static wmOperatorStatus screenshot_exec(bContext *C, wmOperator *op)
Definition screendump.cc:98
static int screenshot_data_create(bContext *C, wmOperator *op, ScrArea *area)
Definition screendump.cc:61
void WM_operator_properties_filesel(wmOperatorType *ot, const int filter, const short type, const eFileSel_Action action, const eFileSel_Flag flag, const short display, const short sort)
void IMB_crop(ImBuf *ibuf, const int2 &rect_pos, const int2 &rect_size)
Definition rectop.cc:470
static wmOperatorStatus screenshot_invoke(bContext *C, wmOperator *op, const wmEvent *event)
Global G
Definition blender.cc:57
void SCREEN_OT_screenshot_area(wmOperatorType *ot)
@ R_IMF_IMTYPE_MULTILAYER
BLI_INLINE int BLI_rcti_size_y(const struct rcti *rct)
Definition BLI_rect.hh:207
void IMB_freeImBuf(ImBuf *ibuf)
VecBase< int32_t, 2 > int2
@ WM_FILESEL_FILEPATH
Definition WM_api.hh:1332
unsigned char uint8_t
Definition BKE_text.h:79
static void screen_screenshot_impl(wmOperatorType *ot)
void WM_redraw_windows(bContext *C)
Definition wm_draw.cc:1806
bool RNA_struct_property_is_set(PointerRNA *ptr, const char *identifier)
PointerRNA RNA_pointer_create_discrete(ID *id, StructRNA *type, void *data)
void IMB_color_to_bw(ImBuf *ibuf)
void uiTemplateImageSettings(ui::Layout *layout, bContext *C, PointerRNA *imfptr, bool color_management, const char *panel_idname=nullptr)
void BKE_image_format_init(ImageFormatData *imf)
ScrArea ScrArea * BKE_screen_find_area_xy(const bScreen *screen, int spacetype, const int xy[2]) ATTR_NONNULL(1
Definition screen.cc:1226
BLI_INLINE int BLI_rcti_size_x(const struct rcti *rct)
Definition BLI_rect.hh:203
ImBuf * IMB_allocImBuf(unsigned int x, unsigned int y, ImBufFlags flags)
void BKE_reportf(ReportList *reports, eReportType type, const char *format,...) ATTR_PRINTF_FORMAT(3
Definition report.cc:191
@ FILE_SAVE
Definition WM_api.hh:1342
bool WM_operator_winactive(bContext *C)
bScreen * CTX_wm_screen(const bContext *C)
@ OPTYPE_DEPENDS_ON_CURSOR
Definition WM_types.hh:228
std::string RNA_string_get(PointerRNA *ptr, const char *name)
static bool screenshot_poll(bContext *C)
bool BKE_imbuf_write(ImBuf *ibuf, const char *filepath, const ImageFormatData *imf)
void SCREEN_OT_screenshot(wmOperatorType *ot)
bool WM_operator_filesel_ensure_ext_imtype(wmOperator *op, const ImageFormatData *im_format)
void WM_event_add_fileselect(bContext *C, wmOperator *op)
ImColorMode color_mode
void assign_byte_data(uint8_t *data)
ImageFormatData im_format
Definition screendump.cc:57
void use_property_decorate_set(bool is_decorate)
void use_property_split_set(bool is_sep)
wmOperatorStatus(*) exec(bContext *C, wmOperator *op) ATTR_WARN_UNUSED_RESULT
Definition WM_types.hh:1125
bool(*) check(bContext *C, wmOperator *op)
Definition WM_types.hh:1133
const char * description
Definition WM_types.hh:1115
void(*) cancel(bContext *C, wmOperator *op)
Definition WM_types.hh:1149
void(*) ui(bContext *C, wmOperator *op)
Definition WM_types.hh:1176
bool(*) poll(bContext *C) ATTR_WARN_UNUSED_RESULT
Definition WM_types.hh:1165
wmOperatorStatus(*) invoke(bContext *C, wmOperator *op, const wmEvent *event) ATTR_WARN_UNUSED_RESULT
Definition WM_types.hh:1141
struct ReportList * reports