Blender V5.3
imbuf_py_api.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
10
11#include <Python.h>
12
13#include "BLI_rect.hh"
14#include "BLI_utildefines.hh"
15
16#include "DNA_space_enums.h"
17#include "py_capi_utils.hh"
18
19#include "python_compat.hh" /* IWYU pragma: keep. */
20
21#include "imbuf_py_api.hh" /* own include */
22
25
26/* File IO */
27#include "BLI_fileops.hh"
28
29#include <cerrno>
30#include <fcntl.h>
31#include <optional>
32
33#define IMB_FTYPE_DEFAULT IMB_FTYPE_PNG
34
35namespace blender {
36
37static PyObject *BPyInit_imbuf_types();
38
39static PyObject *Py_ImBuf_CreatePyObject(ImBuf *ibuf);
40
41struct Py_ImBuf;
42
43#define PY_IMBUF_BUFFER_TYPE_DOC \
44 " :param buffer_type: The buffer type.\n" \
45 " :type buffer_type: Literal['BYTE', 'FLOAT']\n"
46
48 {int(ImBufFlags::ByteData), "BYTE"},
49 {int(ImBufFlags::FloatData), "FLOAT"},
50 {0, nullptr},
51};
52
53const static char *py_imbuf_type_none = "NONE";
54
56 PyObject_HEAD
64 std::optional<rcti> region;
65};
66extern PyTypeObject Py_ImBufBuffer_Type;
67
69 PyObject_HEAD
71};
72extern PyTypeObject Py_ImBufFileType_Type;
73
74/* -------------------------------------------------------------------- */
80
81static std::optional<int> py_imbuf_ftype_from_string(const char *str)
82{
83 const eImbFileType ftype = IMB_ftype_from_id(str);
84 if (ftype == IMB_FTYPE_NONE && !STREQ(str, py_imbuf_type_none)) {
85 return std::nullopt;
86 }
87 return ftype;
88}
89
91
92/* -------------------------------------------------------------------- */
95
96struct Py_ImBuf {
97 PyObject_HEAD
98 /* can be nullptr */
102};
103
105{
106 if (self->ibuf) [[likely]] {
107 return 0;
108 }
109
110 PyErr_Format(
111 PyExc_ReferenceError, "ImBuf data of type %.200s has been freed", Py_TYPE(self)->tp_name);
112 return -1;
113}
114
115#define PY_IMBUF_CHECK_OBJ(obj) \
116 if (py_imbuf_valid_check(obj) == -1) [[unlikely]] { \
117 return nullptr; \
118 } \
119 ((void)0)
120#define PY_IMBUF_CHECK_INT(obj) \
121 if (py_imbuf_valid_check(obj) == -1) [[unlikely]] { \
122 return -1; \
123 } \
124 ((void)0)
125
126#define PY_IMBUF_CHECK_BUFFER_USERS_OBJ(obj) \
127 if ((obj)->buffer_users > 0) [[unlikely]] { \
128 PyErr_SetString(PyExc_BufferError, \
129 "ImBuf cannot be modified while pixel buffers are exported"); \
130 return nullptr; \
131 } \
132 ((void)0)
133
135{
136 /* Should not be possible, but avoid crashing on corrupt data. */
138 PyErr_WarnFormat(PyExc_RuntimeWarning, 1, "unknown file type enum: %d", ftype);
139}
140
147{
148 if (ftype == IMB_FTYPE_NONE) {
149 return py_imbuf_type_none;
150 }
151 const char *id = IMB_ftype_to_id(ftype);
152 if (id == nullptr) [[unlikely]] {
154 return py_imbuf_type_none;
155 }
156 return id;
157}
158
166static bool py_imbuf_region_sanitize(const ImBuf *ibuf, rcti *region)
167{
168 BLI_rcti_sanitize(region);
169
170 CLAMP(region->xmin, 0, ibuf->x);
171 CLAMP(region->xmax, 0, ibuf->x);
172 CLAMP(region->ymin, 0, ibuf->y);
173 CLAMP(region->ymax, 0, ibuf->y);
174
175 return !((region->xmin == 0 && region->ymin == 0) &&
176 (region->xmax == ibuf->x && region->ymax == ibuf->y));
177}
178
184static std::optional<ImBufFlags> py_imbuf_buffer_type(const ImBuf *ibuf)
185{
186 if (ibuf->float_data() != nullptr) {
188 }
189 if (ibuf->byte_data() != nullptr) {
191 }
192 return std::nullopt;
193}
194
198static std::optional<ImBufFlags> py_imbuf_buffer_type_or_error(const ImBuf *ibuf)
199{
200 std::optional<ImBufFlags> buffer_type = py_imbuf_buffer_type(ibuf);
201 if (buffer_type == std::nullopt) [[unlikely]] {
202 PyErr_SetString(PyExc_ValueError, "ImBuf has no pixel data");
203 }
204 return buffer_type;
205}
206
212{
213 return ibuf->flags | (ibuf->float_data() ? ImBufFlags::FloatData : ImBufFlags::ByteData);
214}
215
220#define PY_IMBUF_CHECK_BUFFER_TYPE_OBJ(obj, buffer_type_var) \
221 PY_IMBUF_CHECK_OBJ(obj); \
222 const std::optional<ImBufFlags> _buffer_type_or_none = py_imbuf_buffer_type_or_error( \
223 (obj)->ibuf); \
224 if (_buffer_type_or_none == std::nullopt) [[unlikely]] { \
225 return nullptr; \
226 } \
227 const ImBufFlags buffer_type_var = *(_buffer_type_or_none)
228
230
231/* -------------------------------------------------------------------- */
234
236 /* Wrap. */
237 py_imbuf_resize_doc,
238 ".. method:: resize(size, *, method='FAST')\n"
239 "\n"
240 " Resize the image in-place.\n"
241 "\n"
242 " :param size: New size.\n"
243 " :type size: tuple[int, int]\n"
244 " :param method: Method of resizing ('FAST', 'BILINEAR').\n"
245 " :type method: str\n");
246static PyObject *py_imbuf_resize(Py_ImBuf *self, PyObject *args, PyObject *kw)
247{
248 PY_IMBUF_CHECK_OBJ(self);
250
251 int size[2];
252
253 enum { FAST, BILINEAR };
254 const PyC_StringEnumItems method_items[] = {
255 {FAST, "FAST"},
256 {BILINEAR, "BILINEAR"},
257 {0, nullptr},
258 };
259 PyC_StringEnum method = {method_items, FAST};
260
261 static const char *_keywords[] = {
262 "size",
263 "method",
264 nullptr,
265 };
266 static _PyArg_Parser _parser = {
267 "(ii)" /* `size` */
268 "|$" /* Optional, keyword only arguments. */
269 "O&" /* `method` */
270 ":resize",
271 _keywords,
272 nullptr,
273 };
274 if (!_PyArg_ParseTupleAndKeywordsFast(
275 args, kw, &_parser, &size[0], &size[1], PyC_ParseStringEnum, &method))
276 {
277 return nullptr;
278 }
279 if (size[0] <= 0 || size[1] <= 0) {
280 PyErr_Format(PyExc_ValueError, "resize: Image size cannot be below 1 (%d, %d)", UNPACK2(size));
281 return nullptr;
282 }
283
284 if (method.value_found == FAST) {
285 IMB_scale(self->ibuf, UNPACK2(size), IMBScaleFilter::Nearest, false);
286 }
287 else if (method.value_found == BILINEAR) {
288 IMB_scale(self->ibuf, UNPACK2(size), IMBScaleFilter::Box, false);
289 }
290 else {
292 }
293 Py_RETURN_NONE;
294}
295
297 /* Wrap. */
298 py_imbuf_crop_doc,
299 ".. method:: crop(min, max)\n"
300 "\n"
301 " Crop the image in-place.\n"
302 "\n"
303 " :param min: Minimum pixel coordinates (X, Y), inclusive.\n"
304 " :type min: tuple[int, int]\n"
305 " :param max: Maximum pixel coordinates (X, Y), inclusive.\n"
306 " :type max: tuple[int, int]\n");
307static PyObject *py_imbuf_crop(Py_ImBuf *self, PyObject *args, PyObject *kw)
308{
309 PY_IMBUF_CHECK_OBJ(self);
311
312 rcti crop;
313
314 static const char *_keywords[] = {
315 "min",
316 "max",
317 nullptr,
318 };
319 static _PyArg_Parser _parser = {
320 "(II)" /* `min` */
321 "(II)" /* `max` */
322 ":crop",
323 _keywords,
324 nullptr,
325 };
326 if (!_PyArg_ParseTupleAndKeywordsFast(
327 args, kw, &_parser, &crop.xmin, &crop.ymin, &crop.xmax, &crop.ymax))
328 {
329 return nullptr;
330 }
331
332 if (/* X range. */
333 !(crop.xmin >= 0 && crop.xmax < self->ibuf->x) ||
334 /* Y range. */
335 !(crop.ymin >= 0 && crop.ymax < self->ibuf->y) ||
336 /* X order. */
337 !(crop.xmin <= crop.xmax) ||
338 /* Y order. */
339 !(crop.ymin <= crop.ymax))
340 {
341 PyErr_SetString(PyExc_ValueError, "ImBuf crop min/max not in range");
342 return nullptr;
343 }
344
345 IMB_crop(self->ibuf,
346 int2(crop.xmin, crop.ymin),
347 int2(BLI_rcti_size_x(&crop) + 1, BLI_rcti_size_y(&crop) + 1));
348 Py_RETURN_NONE;
349}
350
352 /* Wrap. */
353 py_imbuf_copy_doc,
354 ".. method:: copy()\n"
355 "\n"
356 " Return a copy of the image.\n"
357 "\n"
358 " :return: A copy of the image.\n"
359 " :rtype: :class:`ImBuf`\n");
360static PyObject *py_imbuf_copy(Py_ImBuf *self)
361{
362 PY_IMBUF_CHECK_OBJ(self);
363 ImBuf *ibuf_copy = IMB_dupImBuf(self->ibuf);
364
365 if (ibuf_copy == nullptr) [[unlikely]] {
366 PyErr_SetString(PyExc_MemoryError,
367 "ImBuf.copy(): "
368 "failed to allocate memory");
369 return nullptr;
370 }
371 return Py_ImBuf_CreatePyObject(ibuf_copy);
372}
373
374static PyObject *py_imbuf_deepcopy(Py_ImBuf *self, PyObject *args)
375{
376 if (!PyC_CheckArgs_DeepCopy(args)) {
377 return nullptr;
378 }
379 return py_imbuf_copy(self);
380}
381
383 /* Wrap. */
384 py_imbuf_with_buffer_doc,
385 ".. method:: with_buffer(*, write=False, region=None)\n"
386 "\n"
387 " Return a context manager that yields a :class:`memoryview` "
388 "of the image's pixel data, shaped ``(height, width, channels)``.\n"
389 "\n"
390 " Usage::\n"
391 "\n"
392 " with image.with_buffer(write=True) as buf:\n"
393 " buf[0, 0, 0] = 255 # set red channel of pixel (0, 0)\n"
394 "\n"
395 " :param write: When true the buffer is writable.\n"
396 " :type write: bool\n"
397 " :param region: Optional sub-region ``((x_min, y_min), (x_max, y_max))``, "
398 "clamped to image bounds. When set the shape becomes "
399 "``(region_height, region_width, channels)``.\n"
400 " :type region: tuple[tuple[int, int], tuple[int, int]] | None\n"
401 " :return: A context manager yielding a :class:`memoryview` of pixel data.\n"
402 " :rtype: :class:`ImBufBuffer`\n");
403static PyObject *py_imbuf_with_buffer(Py_ImBuf *self, PyObject *args, PyObject *kw)
404{
405 PY_IMBUF_CHECK_OBJ(self);
406
407 bool write = false;
408 std::optional<rcti> region;
409
410 static const char *_keywords[] = {
411 "write",
412 "region",
413 nullptr,
414 };
415 static _PyArg_Parser _parser = {
416 "|$" /* Optional, keyword only arguments. */
417 "O&" /* `write` */
418 "O&" /* `region` */
419 ":with_buffer",
420 _keywords,
421 nullptr,
422 };
423 if (!_PyArg_ParseTupleAndKeywordsFast(
424 args, kw, &_parser, PyC_ParseBool, &write, PyC_ParseOptionalRectI, &region))
425 {
426 return nullptr;
427 }
428
429 ImBuf *ibuf = self->ibuf;
430
431 bool use_region = false;
432 if (region.has_value()) {
433 use_region = py_imbuf_region_sanitize(ibuf, &region.value());
434 }
435
436 Py_ImBufBuffer *ctx = PyObject_GC_New(Py_ImBufBuffer, &Py_ImBufBuffer_Type);
437 if (ctx == nullptr) [[unlikely]] {
438 return nullptr;
439 }
440 Py_INCREF(self);
441 ctx->py_ibuf = self;
442 ctx->is_entered = false;
443 ctx->writable = write;
444 ctx->region = use_region ? region : std::nullopt;
445 return reinterpret_cast<PyObject *>(ctx);
446}
447
449 /* Wrap. */
450 py_imbuf_convert_buffer_type_doc,
451 ".. method:: convert_buffer_type(buffer_type)\n"
452 "\n"
453 " Convert the image's pixel buffer to the given type.\n"
454 " When the image is already of the given type this is a no-op.\n"
455 " The previous buffer is freed.\n"
457static PyObject *py_imbuf_convert_buffer_type(Py_ImBuf *self, PyObject *args, PyObject *kw)
458{
460 PY_IMBUF_CHECK_BUFFER_TYPE_OBJ(self, source_buffer_type);
461
462 PyC_StringEnum buffer_type = {py_imbuf_buffer_mode_items, -1};
463
464 static const char *_keywords[] = {
465 "buffer_type",
466 nullptr,
467 };
468 static _PyArg_Parser _parser = {
469 "O&" /* `buffer_type` (required) */
470 ":convert_buffer_type",
471 _keywords,
472 nullptr,
473 };
474 if (!_PyArg_ParseTupleAndKeywordsFast(args, kw, &_parser, PyC_ParseStringEnum, &buffer_type)) {
475 return nullptr;
476 }
477
478 ImBuf *ibuf = self->ibuf;
479 if (ImBufFlags(buffer_type.value_found) == source_buffer_type) {
480 Py_RETURN_NONE;
481 }
482
483 if (ImBufFlags(buffer_type.value_found) == ImBufFlags::ByteData) {
485 if (ibuf->byte_data() == nullptr) [[unlikely]] {
486 PyErr_SetString(PyExc_MemoryError, "failed to allocate byte buffer");
487 return nullptr;
488 }
490 }
491 else {
493 if (ibuf->float_data() == nullptr) [[unlikely]] {
494 PyErr_SetString(PyExc_MemoryError, "failed to allocate float buffer");
495 return nullptr;
496 }
498 }
499 Py_RETURN_NONE;
500}
501
503 /* Wrap. */
504 py_imbuf_free_doc,
505 ".. method:: free()\n"
506 "\n"
507 " Clear image data immediately (causing an error on re-use).\n");
508static PyObject *py_imbuf_free(Py_ImBuf *self)
509{
511 if (self->ibuf) {
512 IMB_freeImBuf(self->ibuf);
513 self->ibuf = nullptr;
514 }
515 Py_RETURN_NONE;
516}
517
518#ifdef __GNUC__
519# ifdef __clang__
520# pragma clang diagnostic push
521# pragma clang diagnostic ignored "-Wcast-function-type"
522# else
523# pragma GCC diagnostic push
524# pragma GCC diagnostic ignored "-Wcast-function-type"
525# endif
526#endif
527
528static PyMethodDef Py_ImBuf_methods[] = {
529 {"resize",
530 reinterpret_cast<PyCFunction>(py_imbuf_resize),
531 METH_VARARGS | METH_KEYWORDS,
532 py_imbuf_resize_doc},
533 {"crop",
534 reinterpret_cast<PyCFunction>(py_imbuf_crop),
535 METH_VARARGS | METH_KEYWORDS,
536 const_cast<char *>(py_imbuf_crop_doc)},
537 {"with_buffer",
538 reinterpret_cast<PyCFunction>(py_imbuf_with_buffer),
539 METH_VARARGS | METH_KEYWORDS,
540 py_imbuf_with_buffer_doc},
541 {"convert_buffer_type",
542 reinterpret_cast<PyCFunction>(py_imbuf_convert_buffer_type),
543 METH_VARARGS | METH_KEYWORDS,
544 py_imbuf_convert_buffer_type_doc},
545 {"free", reinterpret_cast<PyCFunction>(py_imbuf_free), METH_NOARGS, py_imbuf_free_doc},
546 {"copy", reinterpret_cast<PyCFunction>(py_imbuf_copy), METH_NOARGS, py_imbuf_copy_doc},
547 {"__copy__", reinterpret_cast<PyCFunction>(py_imbuf_copy), METH_NOARGS, py_imbuf_copy_doc},
548 {"__deepcopy__",
549 reinterpret_cast<PyCFunction>(py_imbuf_deepcopy),
550 METH_VARARGS,
551 py_imbuf_copy_doc},
552 {nullptr, nullptr, 0, nullptr},
553};
554
555#ifdef __GNUC__
556# ifdef __clang__
557# pragma clang diagnostic pop
558# else
559# pragma GCC diagnostic pop
560# endif
561#endif
562
564
565/* -------------------------------------------------------------------- */
568
570 /* Wrap. */
571 py_imbuf_size_doc,
572 "Size of the image in pixels.\n"
573 "\n"
574 ":type: tuple[int, int]\n");
575static PyObject *py_imbuf_size_get(Py_ImBuf *self, void * /*closure*/)
576{
577 PY_IMBUF_CHECK_OBJ(self);
578 ImBuf *ibuf = self->ibuf;
579 return PyC_Tuple_Pack_I32({ibuf->x, ibuf->y});
580}
581
583 /* Wrap. */
584 py_imbuf_ppm_doc,
585 "Pixels per meter.\n"
586 "\n"
587 ":type: tuple[float, float]\n");
588static PyObject *py_imbuf_ppm_get(Py_ImBuf *self, void * /*closure*/)
589{
590 PY_IMBUF_CHECK_OBJ(self);
591 ImBuf *ibuf = self->ibuf;
592 return PyC_Tuple_Pack_F64({ibuf->ppm[0], ibuf->ppm[1]});
593}
594
595static int py_imbuf_ppm_set(Py_ImBuf *self, PyObject *value, void * /*closure*/)
596{
597 PY_IMBUF_CHECK_INT(self);
598 double ppm[2];
599
600 if (PyC_AsArray(ppm, sizeof(*ppm), value, 2, &PyFloat_Type, "ppm") == -1) {
601 return -1;
602 }
603
604 if (ppm[0] <= 0.0 || ppm[1] <= 0.0) {
605 PyErr_SetString(PyExc_ValueError, "invalid ppm value");
606 return -1;
607 }
608
609 ImBuf *ibuf = self->ibuf;
610 ibuf->ppm[0] = ppm[0];
611 ibuf->ppm[1] = ppm[1];
612 return 0;
613}
614
616 /* Wrap. */
617 py_imbuf_filepath_doc,
618 "Filepath associated with this image.\n"
619 "\n"
620 ":type: str | bytes\n");
621static PyObject *py_imbuf_filepath_get(Py_ImBuf *self, void * /*closure*/)
622{
623 PY_IMBUF_CHECK_OBJ(self);
624 ImBuf *ibuf = self->ibuf;
625 return PyC_UnicodeFromStdStr(ibuf->filepath);
626}
627
628static int py_imbuf_filepath_set(Py_ImBuf *self, PyObject *value, void * /*closure*/)
629{
630 PY_IMBUF_CHECK_INT(self);
631
632 ImBuf *ibuf = self->ibuf;
633 const Py_ssize_t value_str_len_max = FILE_MAX;
634 PyObject *value_coerce = nullptr;
635 Py_ssize_t value_str_len;
636 const char *value_str = PyC_UnicodeAsBytesAndSize(value, &value_str_len, &value_coerce);
637 if (value_str == nullptr) [[unlikely]] {
638 return -1;
639 }
640 if (value_str_len >= value_str_len_max) {
641 PyErr_Format(PyExc_TypeError, "filepath length over %zd", value_str_len_max - 1);
642 Py_XDECREF(value_coerce);
643 return -1;
644 }
645 ibuf->filepath = value_str;
646 Py_XDECREF(value_coerce);
647 return 0;
648}
649
651 /* Wrap. */
652 py_imbuf_planes_doc,
653 "Number of bits per pixel for the byte buffer.\n"
654 "Used when reading and writing image files.\n"
655 "\n"
656 "- 8: Gray-scale.\n"
657 "- 16: Gray-scale with alpha.\n"
658 "- 24: RGB.\n"
659 "- 32: RGBA.\n"
660 "\n"
661 ".. note::\n"
662 "\n"
663 " This value may be set by the file format on load,\n"
664 " and determines how many channels are written on save.\n"
665 "\n"
666 ":type: int\n");
667static PyObject *py_imbuf_planes_get(Py_ImBuf *self, void * /*closure*/)
668{
669 PY_IMBUF_CHECK_OBJ(self);
670 const ImBuf *ibuf = self->ibuf;
671 return PyLong_FromLong(ibuf->color_mode_channels_get() * 8);
672}
673
675 /* Wrap. */
676 py_imbuf_channels_doc,
677 "Number of color channels.\n"
678 "\n"
679 ":type: int\n");
680static PyObject *py_imbuf_channels_get(Py_ImBuf *self, void * /*closure*/)
681{
682 PY_IMBUF_CHECK_OBJ(self);
683 ImBuf *imbuf = self->ibuf;
684 return PyLong_FromLong(imbuf->channels);
685}
686
688 /* Wrap. */
689 py_imbuf_quality_doc,
690 "Quality for formats that support lossy compression (0 - 100, clamped).\n"
691 "\n"
692 ":type: int\n");
693static PyObject *py_imbuf_quality_get(Py_ImBuf *self, void * /*closure*/)
694{
695 PY_IMBUF_CHECK_OBJ(self);
696 return PyLong_FromLong(self->ibuf->foptions.quality);
697}
698static int py_imbuf_quality_set(Py_ImBuf *self, PyObject *value, void * /*closure*/)
699{
700 PY_IMBUF_CHECK_INT(self);
701 const int quality = PyC_Long_AsI32(value);
702 if (quality == -1 && PyErr_Occurred()) {
703 return -1;
704 }
705 self->ibuf->foptions.quality = char(std::clamp(quality, 0, 100));
706 return 0;
707}
708
710 /* Wrap. */
711 py_imbuf_compress_doc,
712 "Compression level for formats that support lossless compression levels (0 - 100, clamped).\n"
713 "\n"
714 ":type: int\n");
715static PyObject *py_imbuf_compress_get(Py_ImBuf *self, void * /*closure*/)
716{
717 PY_IMBUF_CHECK_OBJ(self);
718 return PyLong_FromLong(self->ibuf->foptions.compress);
719}
720static int py_imbuf_compress_set(Py_ImBuf *self, PyObject *value, void * /*closure*/)
721{
722 PY_IMBUF_CHECK_INT(self);
723 const int compress = PyC_Long_AsI32(value);
724 if (compress == -1 && PyErr_Occurred()) {
725 return -1;
726 }
727 self->ibuf->foptions.compress = char(std::clamp(compress, 0, 100));
728 return 0;
729}
730
732 /* Wrap. */
733 py_imbuf_file_type_doc,
734 "The file type identifier.\n"
735 "\n"
736 ":type: str\n");
737static PyObject *py_imbuf_file_type_get(Py_ImBuf *self, void * /*closure*/)
738{
739 PY_IMBUF_CHECK_OBJ(self);
740 const char *id = py_imbuf_ftype_to_id_with_fallback(self->ibuf->ftype);
741 return PyUnicode_FromString(id);
742}
743
744static int py_imbuf_file_type_set(Py_ImBuf *self, PyObject *value, void * /*closure*/)
745{
746 PY_IMBUF_CHECK_INT(self);
747 const char *str = PyUnicode_AsUTF8(value);
748 if (!str) {
749 return -1;
750 }
751 const std::optional<int> ftype = py_imbuf_ftype_from_string(str);
752 if (!ftype) {
753 PyErr_Format(PyExc_ValueError, "unknown file type: '%.200s'", str);
754 return -1;
755 }
756 self->ibuf->ftype = eImbFileType(*ftype);
757 return 0;
758}
759
761 /* Wrap. */
762 py_imbuf_buffer_type_doc,
763 "Type of the image's pixel buffer (``'BYTE'`` or ``'FLOAT'``).\n"
764 "\n"
765 ":type: str\n");
766static PyObject *py_imbuf_buffer_type_get(Py_ImBuf *self, void * /*closure*/)
767{
768 PY_IMBUF_CHECK_BUFFER_TYPE_OBJ(self, buffer_type);
769 return PyUnicode_FromString(
771}
772
773static PyGetSetDef Py_ImBuf_getseters[] = {
774 {"size",
775 reinterpret_cast<getter>(py_imbuf_size_get),
776 static_cast<setter>(nullptr),
777 py_imbuf_size_doc,
778 nullptr},
779 {"ppm",
780 reinterpret_cast<getter>(py_imbuf_ppm_get),
781 reinterpret_cast<setter>(py_imbuf_ppm_set),
782 py_imbuf_ppm_doc,
783 nullptr},
784 {"filepath",
785 reinterpret_cast<getter>(py_imbuf_filepath_get),
786 reinterpret_cast<setter>(py_imbuf_filepath_set),
787 py_imbuf_filepath_doc,
788 nullptr},
789 {"planes",
790 reinterpret_cast<getter>(py_imbuf_planes_get),
791 nullptr,
792 py_imbuf_planes_doc,
793 nullptr},
794 {"channels",
795 reinterpret_cast<getter>(py_imbuf_channels_get),
796 nullptr,
797 py_imbuf_channels_doc,
798 nullptr},
799 {"file_type",
800 reinterpret_cast<getter>(py_imbuf_file_type_get),
801 reinterpret_cast<setter>(py_imbuf_file_type_set),
802 py_imbuf_file_type_doc,
803 nullptr},
804 {"quality",
805 reinterpret_cast<getter>(py_imbuf_quality_get),
806 reinterpret_cast<setter>(py_imbuf_quality_set),
807 py_imbuf_quality_doc,
808 nullptr},
809 {"compress",
810 reinterpret_cast<getter>(py_imbuf_compress_get),
811 reinterpret_cast<setter>(py_imbuf_compress_set),
812 py_imbuf_compress_doc,
813 nullptr},
814 {"buffer_type",
815 reinterpret_cast<getter>(py_imbuf_buffer_type_get),
816 nullptr,
817 py_imbuf_buffer_type_doc,
818 nullptr},
819 {nullptr, nullptr, nullptr, nullptr, nullptr} /* Sentinel */
820};
821
823
824/* -------------------------------------------------------------------- */
827
828static void py_imbuf_dealloc(Py_ImBuf *self)
829{
830 ImBuf *ibuf = self->ibuf;
831 if (ibuf != nullptr) {
832 IMB_freeImBuf(self->ibuf);
833 self->ibuf = nullptr;
834 }
835 PyObject_DEL(self);
836}
837
838static PyObject *py_imbuf_repr(Py_ImBuf *self)
839{
840 const ImBuf *ibuf = self->ibuf;
841 if (ibuf != nullptr) {
842 return PyUnicode_FromFormat("<imbuf: address=%p, filepath='%s', size=(%d, %d)>",
843 ibuf,
844 ibuf->filepath.c_str(),
845 ibuf->x,
846 ibuf->y);
847 }
848
849 return PyUnicode_FromString("<imbuf: address=0x0>");
850}
851
852static Py_hash_t py_imbuf_hash(Py_ImBuf *self)
853{
854 return Py_HashPointer(self->ibuf);
855}
856
857PyTypeObject Py_ImBuf_Type = {
858 /*ob_base*/ PyVarObject_HEAD_INIT(nullptr, 0)
859 /*tp_name*/ "ImBuf",
860 /*tp_basicsize*/ sizeof(Py_ImBuf),
861 /*tp_itemsize*/ 0,
862 /*tp_dealloc*/ reinterpret_cast<destructor>(py_imbuf_dealloc),
863 /*tp_vectorcall_offset*/ 0,
864 /*tp_getattr*/ nullptr,
865 /*tp_setattr*/ nullptr,
866 /*tp_as_async*/ nullptr,
867 /*tp_repr*/ reinterpret_cast<reprfunc>(py_imbuf_repr),
868 /*tp_as_number*/ nullptr,
869 /*tp_as_sequence*/ nullptr,
870 /*tp_as_mapping*/ nullptr,
871 /*tp_hash*/ reinterpret_cast<hashfunc>(py_imbuf_hash),
872 /*tp_call*/ nullptr,
873 /*tp_str*/ nullptr,
874 /*tp_getattro*/ nullptr,
875 /*tp_setattro*/ nullptr,
876 /*tp_as_buffer*/ nullptr,
877 /*tp_flags*/ Py_TPFLAGS_DEFAULT,
878 /*tp_doc*/ nullptr,
879 /*tp_traverse*/ nullptr,
880 /*tp_clear*/ nullptr,
881 /*tp_richcompare*/ nullptr,
882 /*tp_weaklistoffset*/ 0,
883 /*tp_iter*/ nullptr,
884 /*tp_iternext*/ nullptr,
885 /*tp_methods*/ Py_ImBuf_methods,
886 /*tp_members*/ nullptr,
887 /*tp_getset*/ Py_ImBuf_getseters,
888 /*tp_base*/ nullptr,
889 /*tp_dict*/ nullptr,
890 /*tp_descr_get*/ nullptr,
891 /*tp_descr_set*/ nullptr,
892 /*tp_dictoffset*/ 0,
893 /*tp_init*/ nullptr,
894 /*tp_alloc*/ nullptr,
895 /*tp_new*/ nullptr,
896 /*tp_free*/ nullptr,
897 /*tp_is_gc*/ nullptr,
898 /*tp_bases*/ nullptr,
899 /*tp_mro*/ nullptr,
900 /*tp_cache*/ nullptr,
901 /*tp_subclasses*/ nullptr,
902 /*tp_weaklist*/ nullptr,
903 /*tp_del*/ nullptr,
904 /*tp_version_tag*/ 0,
905 /*tp_finalize*/ nullptr,
906 /*tp_vectorcall*/ nullptr,
907};
908
909static PyObject *Py_ImBuf_CreatePyObject(ImBuf *ibuf)
910{
911 Py_ImBuf *self = PyObject_New(Py_ImBuf, &Py_ImBuf_Type);
912 self->ibuf = ibuf;
913 self->buffer_users = 0;
914 return reinterpret_cast<PyObject *>(self);
915}
916
918
919/* -------------------------------------------------------------------- */
922
924{
925 if (self->is_entered && self->py_ibuf) {
926 self->py_ibuf->buffer_users--;
927 BLI_assert(self->py_ibuf->buffer_users >= 0);
928 }
929 PyObject_GC_UnTrack(self);
930 Py_CLEAR(self->py_ibuf);
931 PyObject_GC_Del(self);
932}
933
934static int py_imbuf_buffer_traverse(Py_ImBufBuffer *self, visitproc visit, void *arg)
935{
936 Py_VISIT(self->py_ibuf);
937 return 0;
938}
939
941{
942 /* Release the buffer lock, dealloc can't once the reference is cleared. */
943 if (self->is_entered && self->py_ibuf) {
944 self->is_entered = false;
945 self->py_ibuf->buffer_users--;
946 BLI_assert(self->py_ibuf->buffer_users >= 0);
947 }
948 Py_CLEAR(self->py_ibuf);
949 return 0;
950}
951
953{
954 const ImBuf *ibuf = self->py_ibuf ? self->py_ibuf->ibuf : nullptr;
955 if (ibuf != nullptr) {
956 return PyUnicode_FromFormat(
957 "<ImBufBuffer: size=(%d, %d), entered=%d>", ibuf->x, ibuf->y, int(self->is_entered));
958 }
959 return PyUnicode_FromString("<ImBufBuffer: invalid>");
960}
961
963{
964 if (self->is_entered) [[unlikely]] {
965 PyErr_SetString(PyExc_RuntimeError, "ImBufBuffer context is already entered");
966 return nullptr;
967 }
968 PY_IMBUF_CHECK_BUFFER_TYPE_OBJ(self->py_ibuf, buffer_type);
969 ImBuf *ibuf = self->py_ibuf->ibuf;
970 const bool is_byte = (buffer_type == ImBufFlags::ByteData);
971
972 const int channels = is_byte ? 4 : (ibuf->channels ? ibuf->channels : 4);
973 const Py_ssize_t itemsize = is_byte ? 1 : Py_ssize_t(sizeof(float));
974 const char *format = is_byte ? "B" : "f";
975
976 /* The image may have been resized or cropped since the context was created. */
977 std::optional<rcti> region_clamped = self->region;
978 if (region_clamped && !py_imbuf_region_sanitize(ibuf, &*region_clamped)) {
979 region_clamped.reset();
980 }
981
982 /* `(height, width, channels)` view - the memoryview's natural image shape.
983 * The full image is C-contiguous; a region uses the full image's row stride
984 * to point at a sub-rectangle without copying. */
985 Py_ssize_t shape[3];
986 Py_ssize_t strides[3];
987 Py_ssize_t offset = 0;
988
989 if (region_clamped == std::nullopt) {
990 shape[0] = ibuf->y;
991 shape[1] = ibuf->x;
992 }
993 else {
994 const rcti &region = *region_clamped;
995 shape[0] = region.ymax - region.ymin;
996 shape[1] = region.xmax - region.xmin;
997 offset = (Py_ssize_t(region.ymin) * ibuf->x + region.xmin) * channels * itemsize;
998 }
999 shape[2] = channels;
1000 strides[0] = Py_ssize_t(ibuf->x) * channels * itemsize;
1001 strides[1] = Py_ssize_t(channels) * itemsize;
1002 strides[2] = itemsize;
1003
1004 Py_buffer pybuf;
1005 memset(&pybuf, 0, sizeof(pybuf));
1006 pybuf.buf = is_byte ? static_cast<void *>(ibuf->byte_data_for_write() + offset) :
1007 static_cast<void *>(
1008 reinterpret_cast<char *>(ibuf->float_data_for_write()) + offset);
1009 pybuf.len = shape[0] * shape[1] * shape[2] * itemsize;
1010 pybuf.itemsize = itemsize;
1011 pybuf.readonly = !self->writable;
1012 pybuf.format = const_cast<char *>(format);
1013 pybuf.ndim = 3;
1014 pybuf.shape = shape;
1015 pybuf.strides = strides;
1016 PyObject *memview = PyMemoryView_FromBuffer(&pybuf);
1017
1018 if (memview == nullptr) [[unlikely]] {
1019 return nullptr;
1020 }
1021
1022 self->is_entered = true;
1023 self->py_ibuf->buffer_users++;
1024 return memview;
1025}
1026
1027static PyObject *py_imbuf_buffer_exit(Py_ImBufBuffer *self, PyObject * /*args*/)
1028{
1029 if (self->is_entered && self->py_ibuf) {
1030 self->is_entered = false;
1031 self->py_ibuf->buffer_users--;
1032 BLI_assert(self->py_ibuf->buffer_users >= 0);
1033 }
1034 Py_RETURN_NONE;
1035}
1036
1037#ifdef __GNUC__
1038# ifdef __clang__
1039# pragma clang diagnostic push
1040# pragma clang diagnostic ignored "-Wcast-function-type"
1041# else
1042# pragma GCC diagnostic push
1043# pragma GCC diagnostic ignored "-Wcast-function-type"
1044# endif
1045#endif
1046
1047static PyMethodDef Py_ImBufBuffer_methods[] = {
1048 {"__enter__", reinterpret_cast<PyCFunction>(py_imbuf_buffer_enter), METH_NOARGS, nullptr},
1049 {"__exit__", reinterpret_cast<PyCFunction>(py_imbuf_buffer_exit), METH_VARARGS, nullptr},
1050 {nullptr, nullptr, 0, nullptr},
1051};
1052
1053#ifdef __GNUC__
1054# ifdef __clang__
1055# pragma clang diagnostic pop
1056# else
1057# pragma GCC diagnostic pop
1058# endif
1059#endif
1060
1061PyTypeObject Py_ImBufBuffer_Type = {
1062 /*ob_base*/ PyVarObject_HEAD_INIT(nullptr, 0)
1063 /*tp_name*/ "ImBufBuffer",
1064 /*tp_basicsize*/ sizeof(Py_ImBufBuffer),
1065 /*tp_itemsize*/ 0,
1066 /*tp_dealloc*/ reinterpret_cast<destructor>(py_imbuf_buffer_dealloc),
1067 /*tp_vectorcall_offset*/ 0,
1068 /*tp_getattr*/ nullptr,
1069 /*tp_setattr*/ nullptr,
1070 /*tp_as_async*/ nullptr,
1071 /*tp_repr*/ reinterpret_cast<reprfunc>(py_imbuf_buffer_repr),
1072 /*tp_as_number*/ nullptr,
1073 /*tp_as_sequence*/ nullptr,
1074 /*tp_as_mapping*/ nullptr,
1075 /*tp_hash*/ PyObject_HashNotImplemented,
1076 /*tp_call*/ nullptr,
1077 /*tp_str*/ nullptr,
1078 /*tp_getattro*/ nullptr,
1079 /*tp_setattro*/ nullptr,
1080 /*tp_as_buffer*/ nullptr,
1081 /*tp_flags*/ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,
1082 /*tp_doc*/ nullptr,
1083 /*tp_traverse*/ reinterpret_cast<traverseproc>(py_imbuf_buffer_traverse),
1084 /*tp_clear*/ reinterpret_cast<inquiry>(py_imbuf_buffer_clear),
1085 /*tp_richcompare*/ nullptr,
1086 /*tp_weaklistoffset*/ 0,
1087 /*tp_iter*/ nullptr,
1088 /*tp_iternext*/ nullptr,
1089 /*tp_methods*/ Py_ImBufBuffer_methods,
1090 /*tp_members*/ nullptr,
1091 /*tp_getset*/ nullptr,
1092 /*tp_base*/ nullptr,
1093 /*tp_dict*/ nullptr,
1094 /*tp_descr_get*/ nullptr,
1095 /*tp_descr_set*/ nullptr,
1096 /*tp_dictoffset*/ 0,
1097 /*tp_init*/ nullptr,
1098 /*tp_alloc*/ nullptr,
1099 /*tp_new*/ nullptr,
1100 /*tp_free*/ nullptr,
1101 /*tp_is_gc*/ nullptr,
1102 /*tp_bases*/ nullptr,
1103 /*tp_mro*/ nullptr,
1104 /*tp_cache*/ nullptr,
1105 /*tp_subclasses*/ nullptr,
1106 /*tp_weaklist*/ nullptr,
1107 /*tp_del*/ nullptr,
1108 /*tp_version_tag*/ 0,
1109 /*tp_finalize*/ nullptr,
1110 /*tp_vectorcall*/ nullptr,
1111};
1112
1114
1115/* -------------------------------------------------------------------- */
1118
1120 /* Wrap. */
1121 py_imbuf_file_type_id_doc,
1122 "The identifier for this image file type (e.g. ``\"PNG\"``, ``\"JPEG\"``).\n"
1123 "\n"
1124 ":type: str\n");
1125static PyObject *py_imbuf_file_type_id_get(Py_ImBufFileType *self, void * /*closure*/)
1126{
1127 const char *id = py_imbuf_ftype_to_id_with_fallback(self->ftype);
1128 return PyUnicode_FromString(id);
1129}
1130
1132 /* Wrap. */
1133 py_imbuf_file_type_file_extensions_doc,
1134 "The file extensions associated with this image file type (e.g. ``(\".jpg\", \".jpeg\")``).\n"
1135 "\n"
1136 ":type: tuple[str, ...]\n");
1137static PyObject *py_imbuf_file_type_file_extensions_get(Py_ImBufFileType *self, void * /*closure*/)
1138{
1139 const char **ext = IMB_ftype_file_extensions(self->ftype);
1140 if (!ext) {
1141 return PyTuple_New(0);
1142 }
1143 int len = 0;
1144 for (const char **p = ext; *p; p++) {
1145 len++;
1146 }
1147 PyObject *tuple = PyTuple_New(len);
1148 for (int i = 0; i < len; i++) {
1149 PyTuple_SET_ITEM(tuple, i, PyUnicode_FromString(ext[i]));
1150 }
1151 return tuple;
1152}
1153
1155{
1157 return PyBool_FromLong((IMB_ftype_capability_read(self->ftype) & flag) !=
1159}
1160
1162{
1164 return PyBool_FromLong((IMB_ftype_capability_write(self->ftype) & flag) !=
1166}
1167
1169 /* Wrap. */
1170 py_imbuf_file_type_has_read_file_doc,
1171 "True when images of this file type can be read from a file.\n"
1172 "\n"
1173 ":type: bool\n");
1174
1176 /* Wrap. */
1177 py_imbuf_file_type_has_write_file_doc,
1178 "True when images of this file type can be written to a file.\n"
1179 "\n"
1180 ":type: bool\n");
1181
1183 /* Wrap. */
1184 py_imbuf_file_type_has_read_memory_doc,
1185 "True when images of this file type can be read from memory.\n"
1186 "\n"
1187 ":type: bool\n");
1188
1190 /* Wrap. */
1191 py_imbuf_file_type_has_write_memory_doc,
1192 "True when images of this file type can be written to memory.\n"
1193 "\n"
1194 ":type: bool\n");
1195
1196static PyGetSetDef Py_ImBufFileType_getseters[] = {
1197 {"id",
1198 reinterpret_cast<getter>(py_imbuf_file_type_id_get),
1199 nullptr,
1200 py_imbuf_file_type_id_doc,
1201 nullptr},
1202 {"file_extensions",
1203 reinterpret_cast<getter>(py_imbuf_file_type_file_extensions_get),
1204 nullptr,
1205 py_imbuf_file_type_file_extensions_doc,
1206 nullptr},
1207 {"has_read_file",
1208 reinterpret_cast<getter>(py_imbuf_file_type_capability_read_get),
1209 nullptr,
1210 py_imbuf_file_type_has_read_file_doc,
1212 {"has_write_file",
1213 reinterpret_cast<getter>(py_imbuf_file_type_capability_write_get),
1214 nullptr,
1215 py_imbuf_file_type_has_write_file_doc,
1217 {"has_read_memory",
1218 reinterpret_cast<getter>(py_imbuf_file_type_capability_read_get),
1219 nullptr,
1220 py_imbuf_file_type_has_read_memory_doc,
1222 {"has_write_memory",
1223 reinterpret_cast<getter>(py_imbuf_file_type_capability_write_get),
1224 nullptr,
1225 py_imbuf_file_type_has_write_memory_doc,
1227 {nullptr},
1228};
1229
1231{
1232 const char *id = py_imbuf_ftype_to_id_with_fallback(self->ftype);
1233 return PyUnicode_FromFormat("<ImBufFileType: id='%s'>", id);
1234}
1235
1237{
1238 return self->ftype;
1239}
1240
1242 /*ob_base*/ PyVarObject_HEAD_INIT(nullptr, 0)
1243 /*tp_name*/ "ImBufFileType",
1244 /*tp_basicsize*/ sizeof(Py_ImBufFileType),
1245 /*tp_itemsize*/ 0,
1246 /*tp_dealloc*/ nullptr,
1247 /*tp_vectorcall_offset*/ 0,
1248 /*tp_getattr*/ nullptr,
1249 /*tp_setattr*/ nullptr,
1250 /*tp_as_async*/ nullptr,
1251 /*tp_repr*/ reinterpret_cast<reprfunc>(py_imbuf_file_type_repr),
1252 /*tp_as_number*/ nullptr,
1253 /*tp_as_sequence*/ nullptr,
1254 /*tp_as_mapping*/ nullptr,
1255 /*tp_hash*/ reinterpret_cast<hashfunc>(py_imbuf_file_type_hash),
1256 /*tp_call*/ nullptr,
1257 /*tp_str*/ nullptr,
1258 /*tp_getattro*/ nullptr,
1259 /*tp_setattro*/ nullptr,
1260 /*tp_as_buffer*/ nullptr,
1261 /*tp_flags*/ Py_TPFLAGS_DEFAULT,
1262 /*tp_doc*/ nullptr,
1263 /*tp_traverse*/ nullptr,
1264 /*tp_clear*/ nullptr,
1265 /*tp_richcompare*/ nullptr,
1266 /*tp_weaklistoffset*/ 0,
1267 /*tp_iter*/ nullptr,
1268 /*tp_iternext*/ nullptr,
1269 /*tp_methods*/ nullptr,
1270 /*tp_members*/ nullptr,
1271 /*tp_getset*/ Py_ImBufFileType_getseters,
1272 /*tp_base*/ nullptr,
1273 /*tp_dict*/ nullptr,
1274 /*tp_descr_get*/ nullptr,
1275 /*tp_descr_set*/ nullptr,
1276 /*tp_dictoffset*/ 0,
1277 /*tp_init*/ nullptr,
1278 /*tp_alloc*/ nullptr,
1279 /*tp_new*/ nullptr,
1280 /*tp_free*/ nullptr,
1281 /*tp_is_gc*/ nullptr,
1282 /*tp_bases*/ nullptr,
1283 /*tp_mro*/ nullptr,
1284 /*tp_cache*/ nullptr,
1285 /*tp_subclasses*/ nullptr,
1286 /*tp_weaklist*/ nullptr,
1287 /*tp_del*/ nullptr,
1288 /*tp_version_tag*/ 0,
1289 /*tp_finalize*/ nullptr,
1290 /*tp_vectorcall*/ nullptr,
1291};
1292
1294
1295/* -------------------------------------------------------------------- */
1298
1300 /* Wrap. */
1301 M_imbuf_new_doc,
1302 ".. function:: new(size, *, planes=32, buffer_type='BYTE')\n"
1303 "\n"
1304 " Create a new image.\n"
1305 "\n"
1306 " :param size: The size of the image in pixels.\n"
1307 " :type size: tuple[int, int]\n"
1308 " :param planes: Number of bits per pixel.\n"
1309 " :type planes: Literal[8, 16, 24, 32]\n" PY_IMBUF_BUFFER_TYPE_DOC
1310 " :return: The newly created image.\n"
1311 " :rtype: :class:`ImBuf`\n");
1312static PyObject *M_imbuf_new(PyObject * /*self*/, PyObject *args, PyObject *kw)
1313{
1314 int size[2];
1315 int planes = 32;
1317 static const char *_keywords[] = {
1318 "size",
1319 "planes",
1320 "buffer_type",
1321 nullptr,
1322 };
1323 static _PyArg_Parser _parser = {
1324 "(ii)" /* `size` */
1325 "|$" /* Optional, keyword only arguments. */
1326 "i" /* `planes` */
1327 "O&" /* `buffer_type` */
1328 ":new",
1329 _keywords,
1330 nullptr,
1331 };
1332 if (!_PyArg_ParseTupleAndKeywordsFast(
1333 args, kw, &_parser, &size[0], &size[1], &planes, PyC_ParseStringEnum, &buffer_type))
1334 {
1335 return nullptr;
1336 }
1337 if (size[0] <= 0 || size[1] <= 0) {
1338 PyErr_Format(PyExc_ValueError, "new: Image size cannot be below 1 (%d, %d)", UNPACK2(size));
1339 return nullptr;
1340 }
1341
1342 const ImBufFlags flags = ImBufFlags(buffer_type.value_found);
1343 ImColorMode color_mode = ImColorMode::RGBA;
1344 if (planes == 8) {
1345 color_mode = ImColorMode::BW;
1346 }
1347 else if (planes == 16) {
1348 color_mode = ImColorMode::BW_A;
1349 }
1350 else if (planes == 24) {
1351 color_mode = ImColorMode::RGB;
1352 }
1353 else if (planes == 32) {
1354 color_mode = ImColorMode::RGBA;
1355 }
1356 else {
1357 PyErr_Format(PyExc_ValueError, "new: planes must be 8, 16, 24 or 32, got %d", planes);
1358 return nullptr;
1359 }
1360
1361 ImBuf *ibuf = IMB_allocImBuf(UNPACK2(size), flags);
1362 if (ibuf == nullptr) {
1363 PyErr_Format(PyExc_ValueError, "new: Unable to create image (%d, %d)", UNPACK2(size));
1364 return nullptr;
1365 }
1366 ibuf->color_mode = color_mode;
1367 return Py_ImBuf_CreatePyObject(ibuf);
1368}
1369
1370static PyObject *imbuf_load_impl(const char *filepath)
1371{
1372 const int file = BLI_open(filepath, O_BINARY | O_RDONLY, 0);
1373 if (file == -1) {
1374 PyErr_Format(PyExc_IOError, "load: %s, failed to open file '%s'", strerror(errno), filepath);
1375 return nullptr;
1376 }
1377
1380
1381 close(file);
1382
1383 if (ibuf == nullptr) {
1384 PyErr_Format(
1385 PyExc_ValueError, "load: Unable to recognize image format for file '%s'", filepath);
1386 return nullptr;
1387 }
1388
1389 ibuf->filepath = filepath;
1390
1391 return Py_ImBuf_CreatePyObject(ibuf);
1392}
1393
1395 /* Wrap. */
1396 M_imbuf_load_doc,
1397 ".. function:: load(filepath)\n"
1398 "\n"
1399 " Load an image from a file.\n"
1400 "\n"
1401 " :param filepath: The filepath of the image.\n"
1402 " :type filepath: str | bytes\n"
1403 " :return: The newly loaded image.\n"
1404 " :rtype: :class:`ImBuf`\n");
1405static PyObject *M_imbuf_load(PyObject * /*self*/, PyObject *args, PyObject *kw)
1406{
1407 PyC_UnicodeAsBytesAndSize_Data filepath_data = {nullptr};
1408
1409 static const char *_keywords[] = {
1410 "filepath",
1411 nullptr,
1412 };
1413 static _PyArg_Parser _parser = {
1414 "O&" /* `filepath` */
1415 ":load",
1416 _keywords,
1417 nullptr,
1418 };
1419 if (!_PyArg_ParseTupleAndKeywordsFast(
1420 args, kw, &_parser, PyC_ParseUnicodeAsBytesAndSize, &filepath_data))
1421 {
1422 return nullptr;
1423 }
1424
1425 PyObject *result = imbuf_load_impl(filepath_data.value);
1426 Py_XDECREF(filepath_data.value_coerce);
1427 return result;
1428}
1429
1430static PyObject *imbuf_load_from_memory_impl(const char *buffer,
1431 const size_t buffer_size,
1432 ImBufFlags flags)
1433{
1435 reinterpret_cast<const uchar *>(buffer), buffer_size, flags, "<imbuf.load_from_buffer>");
1436
1437 if (ibuf == nullptr) {
1438 PyErr_SetString(PyExc_ValueError, "load_from_buffer: Unable to load image from memory");
1439 return nullptr;
1440 }
1441
1442 return Py_ImBuf_CreatePyObject(ibuf);
1443}
1444
1446 /* Wrap. */
1447 M_imbuf_load_from_buffer_doc,
1448 ".. function:: load_from_buffer(buffer)\n"
1449 "\n"
1450 " Load an image from a buffer.\n"
1451 "\n"
1452 " :param buffer: A buffer containing the image data.\n"
1453 " :type buffer: collections.abc.Buffer\n"
1454 " :return: The newly loaded image.\n"
1455 " :rtype: :class:`ImBuf`\n");
1456static PyObject *M_imbuf_load_from_buffer(PyObject * /*self*/, PyObject *args, PyObject *kw)
1457{
1458 PyObject *buffer_py_ob;
1459
1460 static const char *_keywords[] = {
1461 "buffer",
1462 nullptr,
1463 };
1464 static _PyArg_Parser _parser = {
1465 "O" /* `buffer` */
1466 ":load_from_buffer",
1467 _keywords,
1468 nullptr,
1469 };
1470 if (!_PyArg_ParseTupleAndKeywordsFast(args, kw, &_parser, &buffer_py_ob)) {
1471 return nullptr;
1472 }
1473
1474 PyObject *result = nullptr;
1476
1477 /* This supports `PyBytes`, no need for a separate check. */
1478 if (PyObject_CheckBuffer(buffer_py_ob)) {
1479 Py_buffer pybuffer;
1480 if (PyObject_GetBuffer(buffer_py_ob, &pybuffer, PyBUF_SIMPLE) == -1) {
1481 return nullptr;
1482 }
1484 reinterpret_cast<const char *>(pybuffer.buf), pybuffer.len, flags);
1485
1486 PyBuffer_Release(&pybuffer);
1487 }
1488 else {
1489 PyErr_Format(PyExc_TypeError,
1490 "load_from_buffer: expected a buffer, unsupported type %.200s",
1491 Py_TYPE(buffer_py_ob)->tp_name);
1492 return nullptr;
1493 }
1494 return result;
1495}
1496
1497static PyObject *imbuf_write_impl(ImBuf *ibuf, const char *filepath)
1498{
1499 const bool ok = IMB_save_image(ibuf, filepath, py_imbuf_write_flags(ibuf));
1500 if (ok == false) {
1501 PyErr_Format(
1502 PyExc_IOError, "write: Unable to write image file (%s) '%s'", strerror(errno), filepath);
1503 return nullptr;
1504 }
1505 Py_RETURN_NONE;
1506}
1507
1508static PyObject *py_imbuf_write_impl(Py_ImBuf *py_imb, const char *filepath_or_null)
1509{
1510 if (py_imbuf_valid_check(py_imb) == -1) [[unlikely]] {
1511 return nullptr;
1512 }
1513
1516 {
1517 const char *id = py_imbuf_ftype_to_id_with_fallback(py_imb->ibuf->ftype);
1518 PyErr_Format(
1519 PyExc_ValueError, "write: file type '%.200s' does not support writing to a file", id);
1520 return nullptr;
1521 }
1522
1523 if (filepath_or_null == nullptr) {
1524 /* Argument omitted, use images path. */
1525 filepath_or_null = py_imb->ibuf->filepath.c_str();
1526 }
1527 return imbuf_write_impl(py_imb->ibuf, filepath_or_null);
1528}
1529
1531 /* Wrap. */
1532 M_imbuf_write_doc,
1533 ".. function:: write(image, *, filepath=None)\n"
1534 "\n"
1535 " Write an image.\n"
1536 "\n"
1537 " :param image: The image to write.\n"
1538 " :type image: :class:`ImBuf`\n"
1539 " :param filepath: Optional filepath of the image (fallback to the image's file path).\n"
1540 " :type filepath: str | bytes | None\n");
1541static PyObject *M_imbuf_write(PyObject * /*self*/, PyObject *args, PyObject *kw)
1542{
1543 Py_ImBuf *py_imb;
1544 PyC_UnicodeAsBytesAndSize_Data filepath_data = {nullptr};
1545
1546 static const char *_keywords[] = {
1547 "image",
1548 "filepath",
1549 nullptr,
1550 };
1551 static _PyArg_Parser _parser = {
1552 "O!" /* `image` */
1553 "|$" /* Optional, keyword only arguments. */
1554 "O&" /* `filepath` */
1555 ":write",
1556 _keywords,
1557 nullptr,
1558 };
1559 if (!_PyArg_ParseTupleAndKeywordsFast(args,
1560 kw,
1561 &_parser,
1563 &py_imb,
1565 &filepath_data))
1566 {
1567 return nullptr;
1568 }
1569
1570 PyObject *result = py_imbuf_write_impl(py_imb, filepath_data.value);
1571 Py_XDECREF(filepath_data.value_coerce);
1572 return result;
1573}
1574
1578static PyObject *imbuf_write_to_buffer_impl(ImBuf *ibuf, PyObject *file)
1579{
1580 if (ibuf->ftype == IMB_FTYPE_NONE) {
1581 ibuf->ftype = IMB_FTYPE_DEFAULT;
1582 }
1584 if (encoded.is_empty()) {
1585 PyErr_SetString(PyExc_RuntimeError, "write_to_buffer: failed to write image to memory");
1586 return nullptr;
1587 }
1588
1589 PyObject *memview = PyMemoryView_FromMemory(
1590 reinterpret_cast<char *>(encoded.data()), Py_ssize_t(encoded.size()), PyBUF_READ);
1591 if (!memview) {
1592 return nullptr;
1593 }
1594
1595 /* Handles missing attribute, non-callable attribute, and write errors. */
1596 PyObject *result = PyObject_CallMethod(file, "write", "O", memview);
1597 Py_DECREF(memview);
1598
1599 if (!result) {
1600 return nullptr;
1601 }
1602 Py_DECREF(result);
1603
1604 Py_RETURN_NONE;
1605}
1606
1608 /* Wrap. */
1609 M_imbuf_write_to_buffer_doc,
1610 ".. function:: write_to_buffer(image, file)\n"
1611 "\n"
1612 " Write an image to a file-like object.\n"
1613 "\n"
1614 " :param image: The image to write.\n"
1615 " :type image: :class:`ImBuf`\n"
1616 " :param file: A writable file-like object (e.g. :class:`io.BytesIO`).\n"
1617 " :type file: :class:`BinaryIO`\n");
1618static PyObject *M_imbuf_write_to_buffer(PyObject * /*self*/, PyObject *args, PyObject *kw)
1619{
1620 Py_ImBuf *py_imb;
1621 PyObject *file;
1622
1623 static const char *_keywords[] = {
1624 "image",
1625 "file",
1626 nullptr,
1627 };
1628 static _PyArg_Parser _parser = {
1629 "O!" /* `image` */
1630 "O" /* `file` */
1631 ":write_to_buffer",
1632 _keywords,
1633 nullptr,
1634 };
1635 if (!_PyArg_ParseTupleAndKeywordsFast(args, kw, &_parser, &Py_ImBuf_Type, &py_imb, &file)) {
1636 return nullptr;
1637 }
1638 PY_IMBUF_CHECK_OBJ(py_imb);
1639
1642 {
1643 const char *id = py_imbuf_ftype_to_id_with_fallback(py_imb->ibuf->ftype);
1644 PyErr_Format(PyExc_ValueError,
1645 "write_to_buffer: file type '%.200s' does not support writing to memory",
1646 id);
1647 return nullptr;
1648 }
1649
1650 /* Work on a copy to avoid mutating the original (ftype).
1651 * This could be avoided by making the encoded buffer free function public. */
1652 ImBuf *ibuf = IMB_dupImBuf(py_imb->ibuf);
1653 if (!ibuf) {
1654 PyErr_SetString(PyExc_MemoryError, "write_to_buffer: failed to duplicate image buffer");
1655 return nullptr;
1656 }
1657 PyObject *result = imbuf_write_to_buffer_impl(ibuf, file);
1658 IMB_freeImBuf(ibuf);
1659 return result;
1660}
1661
1663 /* Wrap. */
1664 M_imbuf_file_type_from_buffer_doc,
1665 ".. function:: file_type_from_buffer(buffer)\n"
1666 "\n"
1667 " Detect the image file type from a buffer.\n"
1668 "\n"
1669 " :param buffer: A buffer containing image data.\n"
1670 " :type buffer: collections.abc.Buffer\n"
1671 " :return: The detected file type, or None if unrecognized.\n"
1672 " :rtype: :class:`ImBufFileType` | None\n");
1673static PyObject *M_imbuf_file_type_from_buffer(PyObject * /*self*/, PyObject *args, PyObject *kw)
1674{
1675 PyObject *buffer_py_ob;
1676
1677 static const char *_keywords[] = {
1678 "buffer",
1679 nullptr,
1680 };
1681 static _PyArg_Parser _parser = {
1682 "O" /* `buffer` */
1683 ":file_type_from_buffer",
1684 _keywords,
1685 nullptr,
1686 };
1687 if (!_PyArg_ParseTupleAndKeywordsFast(args, kw, &_parser, &buffer_py_ob)) {
1688 return nullptr;
1689 }
1690
1691 if (!PyObject_CheckBuffer(buffer_py_ob)) {
1692 PyErr_Format(PyExc_TypeError,
1693 "file_type_from_buffer: expected a buffer, unsupported type %.200s",
1694 Py_TYPE(buffer_py_ob)->tp_name);
1695 return nullptr;
1696 }
1697
1698 Py_buffer pybuffer;
1699 if (PyObject_GetBuffer(buffer_py_ob, &pybuffer, PyBUF_SIMPLE) == -1) {
1700 return nullptr;
1701 }
1703 reinterpret_cast<const uchar *>(pybuffer.buf), pybuffer.len);
1704 PyBuffer_Release(&pybuffer);
1705
1706 if (ftype == IMB_FTYPE_NONE) {
1707 Py_RETURN_NONE;
1708 }
1709
1711 val->ftype = ftype;
1712 return reinterpret_cast<PyObject *>(val);
1713}
1714
1716
1717/* -------------------------------------------------------------------- */
1720
1721#ifdef __GNUC__
1722# ifdef __clang__
1723# pragma clang diagnostic push
1724# pragma clang diagnostic ignored "-Wcast-function-type"
1725# else
1726# pragma GCC diagnostic push
1727# pragma GCC diagnostic ignored "-Wcast-function-type"
1728# endif
1729#endif
1730
1731static PyMethodDef IMB_methods[] = {
1732 {"new",
1733 reinterpret_cast<PyCFunction>(M_imbuf_new),
1734 METH_VARARGS | METH_KEYWORDS,
1735 M_imbuf_new_doc},
1736 {"load",
1737 reinterpret_cast<PyCFunction>(M_imbuf_load),
1738 METH_VARARGS | METH_KEYWORDS,
1739 M_imbuf_load_doc},
1740 {"load_from_buffer",
1741 reinterpret_cast<PyCFunction>(M_imbuf_load_from_buffer),
1742 METH_VARARGS | METH_KEYWORDS,
1743 M_imbuf_load_from_buffer_doc},
1744 {"write",
1745 reinterpret_cast<PyCFunction>(M_imbuf_write),
1746 METH_VARARGS | METH_KEYWORDS,
1747 M_imbuf_write_doc},
1748 {"write_to_buffer",
1749 reinterpret_cast<PyCFunction>(M_imbuf_write_to_buffer),
1750 METH_VARARGS | METH_KEYWORDS,
1751 M_imbuf_write_to_buffer_doc},
1752 {"file_type_from_buffer",
1753 reinterpret_cast<PyCFunction>(M_imbuf_file_type_from_buffer),
1754 METH_VARARGS | METH_KEYWORDS,
1755 M_imbuf_file_type_from_buffer_doc},
1756 {nullptr, nullptr, 0, nullptr},
1757};
1758
1759#ifdef __GNUC__
1760# ifdef __clang__
1761# pragma clang diagnostic pop
1762# else
1763# pragma GCC diagnostic pop
1764# endif
1765#endif
1766
1768 /* Wrap. */
1769 IMB_doc,
1770 "This module provides access to Blender's image manipulation API.\n"
1771 "\n"
1772 "It provides access to image buffers outside of Blender's\n"
1773 ":class:`bpy.types.Image` data-block context.\n");
1774static PyModuleDef IMB_module_def = {
1775 /*m_base*/ PyModuleDef_HEAD_INIT,
1776 /*m_name*/ "imbuf",
1777 /*m_doc*/ IMB_doc,
1778 /*m_size*/ 0,
1779 /*m_methods*/ IMB_methods,
1780 /*m_slots*/ nullptr,
1781 /*m_traverse*/ nullptr,
1782 /*m_clear*/ nullptr,
1783 /*m_free*/ nullptr,
1784};
1785
1786PyObject *BPyInit_imbuf()
1787{
1788 if (PyType_Ready(&Py_ImBufFileType_Type) < 0) {
1789 return nullptr;
1790 }
1791
1792 PyObject *mod;
1793 PyObject *submodule;
1794 PyObject *sys_modules = PyImport_GetModuleDict();
1795
1796 mod = PyModule_Create(&IMB_module_def);
1797
1798 /* `imbuf.types` */
1799 PyModule_AddObject(mod, "types", (submodule = BPyInit_imbuf_types()));
1800 PyC_Module_AddToSysModules(sys_modules, submodule);
1801
1802 /* `imbuf.file_types` (read-only dict of supported file type identifiers). */
1803 {
1804 PyObject *dict = _PyDict_NewPresized(IMB_FTYPE_LAST + 1);
1805 for (int ftype = 0; ftype <= IMB_FTYPE_LAST; ftype++) {
1806 const char *id = (ftype != IMB_FTYPE_NONE) ? IMB_ftype_to_id(eImbFileType(ftype)) :
1808 if (id) {
1810 val->ftype = eImbFileType(ftype);
1811 PyDict_SetItemString(dict, id, reinterpret_cast<PyObject *>(val));
1812 Py_DECREF(val);
1813 }
1814 }
1815 PyObject *proxy = PyDictProxy_New(dict);
1816 PyModule_AddObject(mod, "file_types", proxy);
1817 Py_DECREF(dict);
1818 }
1819
1820 return mod;
1821}
1822
1824
1825/* -------------------------------------------------------------------- */
1831
1833 /* Wrap. */
1834 IMB_types_doc,
1835 "This module provides access to image buffer types.\n"
1836 "\n"
1837 ".. note::\n"
1838 "\n"
1839 " Image buffer is also the structure used by :class:`bpy.types.Image`\n"
1840 " ID type to store and manipulate image data at runtime.\n");
1841static PyModuleDef IMB_types_module_def = {
1842 /*m_base*/ PyModuleDef_HEAD_INIT,
1843 /*m_name*/ "imbuf.types",
1844 /*m_doc*/ IMB_types_doc,
1845 /*m_size*/ 0,
1846 /*m_methods*/ nullptr,
1847 /*m_slots*/ nullptr,
1848 /*m_traverse*/ nullptr,
1849 /*m_clear*/ nullptr,
1850 /*m_free*/ nullptr,
1851};
1852
1854{
1855 if (PyType_Ready(&Py_ImBuf_Type) < 0) {
1856 return nullptr;
1857 }
1858
1859 PyObject *submodule = PyModule_Create(&IMB_types_module_def);
1860
1861 PyModule_AddType(submodule, &Py_ImBuf_Type);
1862 PyModule_AddType(submodule, &Py_ImBufBuffer_Type);
1863 PyModule_AddType(submodule, &Py_ImBufFileType_Type);
1864
1865 return submodule;
1866}
1867
1869
1870/* -------------------------------------------------------------------- */
1873
1874ImBuf *BPy_ImBuf_FromPyObject(PyObject *py_imbuf)
1875{
1876 /* The caller must ensure this. */
1877 BLI_assert(Py_TYPE(py_imbuf) == &Py_ImBuf_Type);
1878
1879 if (py_imbuf_valid_check(reinterpret_cast<Py_ImBuf *>(py_imbuf)) == -1) {
1880 return nullptr;
1881 }
1882
1883 return (reinterpret_cast<Py_ImBuf *>(py_imbuf))->ibuf;
1884}
1885
1887
1888} // namespace blender
File and directory operations.
#define O_BINARY
#define FILE_MAX
#define IMB_FTYPE_LAST
#define CLAMP(a, b, c)
#define UNPACK2(a)
#define BLI_assert_unreachable()
#define POINTER_FROM_INT(i)
#define POINTER_AS_INT(i)
#define STREQ(a, b)
int64_t size() const
bool is_empty() const
#define str(s)
VecBase< float, D > constexpr mod(VecOp< float, D >, VecOp< float, D >) RET
#define PY_IMBUF_CHECK_BUFFER_USERS_OBJ(obj)
#define PY_IMBUF_BUFFER_TYPE_DOC
#define PY_IMBUF_CHECK_OBJ(obj)
#define IMB_FTYPE_DEFAULT
#define PY_IMBUF_CHECK_INT(obj)
#define PY_IMBUF_CHECK_BUFFER_TYPE_OBJ(obj, buffer_type_var)
ccl_device_inline dual1 len(const ccl_private dual< T > &a)
Definition math_dual.h:263
BMesh Iterators.
static PyObject * M_imbuf_file_type_from_buffer(PyObject *, PyObject *args, PyObject *kw)
static const char * py_imbuf_type_none
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
eImbFileType IMB_ftype_from_id(const char *id)
Definition filetype.cc:354
const char ** IMB_ftype_file_extensions(eImbFileType ftype)
Definition filetype.cc:364
void IMB_free_byte_pixels(ImBuf *ibuf)
int PyC_ParseBool(PyObject *o, void *p)
static int py_imbuf_quality_set(Py_ImBuf *self, PyObject *value, void *)
static PyObject * py_imbuf_crop(Py_ImBuf *self, PyObject *args, PyObject *kw)
static PyGetSetDef Py_ImBufFileType_getseters[]
static PyObject * py_imbuf_quality_get(Py_ImBuf *self, void *)
PyTypeObject Py_ImBufFileType_Type
static PyObject * M_imbuf_write(PyObject *, PyObject *args, PyObject *kw)
static PyObject * M_imbuf_load_from_buffer(PyObject *, PyObject *args, PyObject *kw)
int PyC_CheckArgs_DeepCopy(PyObject *args)
static int py_imbuf_file_type_set(Py_ImBuf *self, PyObject *value, void *)
static PyObject * py_imbuf_buffer_exit(Py_ImBufBuffer *self, PyObject *)
static PyObject * imbuf_load_impl(const char *filepath)
Vector< uint8_t > IMB_save_image_to_buffer(ImBuf *ibuf, ImBufFlags flags)
Definition writeimage.cc:64
static PyObject * py_imbuf_free(Py_ImBuf *self)
const char * IMB_ftype_to_id(eImbFileType ftype)
Definition filetype.cc:348
static Py_hash_t py_imbuf_file_type_hash(Py_ImBufFileType *self)
static PyObject * M_imbuf_write_to_buffer(PyObject *, PyObject *args, PyObject *kw)
PyDoc_STRVAR(bpy_bm_new_doc, ".. function:: new(*, use_operators=True)\n" "\n" " :param use_operators: Support calling operators in :mod:`bmesh.ops` (uses some " "extra memory per vert/edge/face).\n" " :type use_operators: bool\n" " :return: Return a new, empty BMesh.\n" " :rtype: :class:`bmesh.types.BMesh`\n")
int PyC_AsArray(void *array, const size_t array_item_size, PyObject *value, const Py_ssize_t length, const PyTypeObject *type, const char *error_prefix)
static PyObject * BPyInit_imbuf_types()
PyTypeObject Py_ImBuf_Type
static PyObject * py_imbuf_size_get(Py_ImBuf *self, void *)
static PyObject * py_imbuf_with_buffer(Py_ImBuf *self, PyObject *args, PyObject *kw)
static std::optional< ImBufFlags > py_imbuf_buffer_type_or_error(const ImBuf *ibuf)
int BLI_open(const char *filepath, int oflag, int pmode) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
static PyObject * py_imbuf_compress_get(Py_ImBuf *self, void *)
static PyObject * py_imbuf_planes_get(Py_ImBuf *self, void *)
PyObject * PyC_Tuple_Pack_F64(const Span< double > values)
void BLI_rcti_sanitize(struct rcti *rect)
Definition rct.cc:460
static void py_imbuf_warn_corrupt_ftype(const eImbFileType ftype)
void IMB_crop(ImBuf *ibuf, const int2 &rect_pos, const int2 &rect_size)
Definition rectop.cc:470
ImBuf * IMB_dupImBuf(const ImBuf *ibuf1)
static PyObject * py_imbuf_deepcopy(Py_ImBuf *self, PyObject *args)
PyObject * PyC_Tuple_Pack_I32(const Span< int > values)
static ImBufFlags py_imbuf_write_flags(const ImBuf *ibuf)
bool IMB_save_image(ImBuf *ibuf, const char *filepath, ImBufFlags flags)
Definition writeimage.cc:40
void IMB_float_from_byte(ImBuf *ibuf)
static PyObject * M_imbuf_new(PyObject *, PyObject *args, PyObject *kw)
static std::optional< int > py_imbuf_ftype_from_string(const char *str)
static int py_imbuf_buffer_clear(Py_ImBufBuffer *self)
BLI_INLINE int BLI_rcti_size_y(const struct rcti *rct)
Definition BLI_rect.hh:207
static PyObject * py_imbuf_filepath_get(Py_ImBuf *self, void *)
void IMB_freeImBuf(ImBuf *ibuf)
VecBase< int32_t, 2 > int2
static PyObject * py_imbuf_file_type_file_extensions_get(Py_ImBufFileType *self, void *)
unsigned char uchar
Definition BKE_text.h:58
static PyModuleDef IMB_types_module_def
PyObject * BPyInit_imbuf()
static PyObject * imbuf_load_from_memory_impl(const char *buffer, const size_t buffer_size, ImBufFlags flags)
static PyObject * imbuf_write_to_buffer_impl(ImBuf *ibuf, PyObject *file)
static PyMethodDef IMB_methods[]
static PyMethodDef Py_ImBufBuffer_methods[]
static PyObject * py_imbuf_buffer_enter(Py_ImBufBuffer *self)
static int py_imbuf_valid_check(Py_ImBuf *self)
static PyObject * M_imbuf_load(PyObject *, PyObject *args, PyObject *kw)
static PyObject * py_imbuf_convert_buffer_type(Py_ImBuf *self, PyObject *args, PyObject *kw)
eImFileTypeCapability IMB_ftype_capability_read(eImbFileType ftype)
Definition filetype.cc:370
static int py_imbuf_filepath_set(Py_ImBuf *self, PyObject *value, void *)
static PyObject * py_imbuf_file_type_id_get(Py_ImBufFileType *self, void *)
static const PyC_StringEnumItems py_imbuf_buffer_mode_items[]
static int py_imbuf_compress_set(Py_ImBuf *self, PyObject *value, void *)
static PyObject * py_imbuf_repr(Py_ImBuf *self)
int PyC_ParseUnicodeAsBytesAndSize(PyObject *o, void *p)
static PyObject * py_imbuf_channels_get(Py_ImBuf *self, void *)
static PyObject * py_imbuf_file_type_repr(Py_ImBufFileType *self)
BLI_INLINE int BLI_rcti_size_x(const struct rcti *rct)
Definition BLI_rect.hh:203
static PyObject * py_imbuf_copy(Py_ImBuf *self)
static PyObject * py_imbuf_file_type_capability_write_get(Py_ImBufFileType *self, void *flag_p)
const char * PyC_StringEnum_FindIDFromValue(const PyC_StringEnumItems *items, const int value)
static bool py_imbuf_region_sanitize(const ImBuf *ibuf, rcti *region)
eImFileTypeCapability IMB_ftype_capability_write(eImbFileType ftype)
Definition filetype.cc:376
ImBuf * IMB_allocImBuf(unsigned int x, unsigned int y, ImBufFlags flags)
static PyMethodDef Py_ImBuf_methods[]
static void py_imbuf_dealloc(Py_ImBuf *self)
static PyObject * Py_ImBuf_CreatePyObject(ImBuf *ibuf)
ImBuf * IMB_load_image_from_memory(const unsigned char *mem, size_t size, ImBufFlags flags, const char *descr, const char *filepath=nullptr, ColorManagedColorspaceSettings *colorspace_settings=nullptr)
Definition readimage.cc:132
static std::optional< ImBufFlags > py_imbuf_buffer_type(const ImBuf *ibuf)
eImbFileType IMB_test_image_type_from_memory(const unsigned char *buf, size_t buf_size)
static PyObject * py_imbuf_buffer_type_get(Py_ImBuf *self, void *)
void IMB_byte_from_float(ImBuf *ibuf)
static PyObject * imbuf_write_impl(ImBuf *ibuf, const char *filepath)
static PyModuleDef IMB_module_def
int PyC_ParseOptionalRectI(PyObject *o, void *p)
static const char * py_imbuf_ftype_to_id_with_fallback(const eImbFileType ftype)
int PyC_Module_AddToSysModules(PyObject *sys_modules, PyObject *module)
static int py_imbuf_buffer_traverse(Py_ImBufBuffer *self, visitproc visit, void *arg)
PyObject * PyC_UnicodeFromStdStr(const std::string &str)
static Py_hash_t py_imbuf_hash(Py_ImBuf *self)
int PyC_ParseStringEnum(PyObject *o, void *p)
static PyObject * py_imbuf_resize(Py_ImBuf *self, PyObject *args, PyObject *kw)
static PyGetSetDef Py_ImBuf_getseters[]
PyTypeObject Py_ImBufBuffer_Type
static int py_imbuf_ppm_set(Py_ImBuf *self, PyObject *value, void *)
bool IMB_scale(ImBuf *ibuf, int2 new_size, IMBScaleFilter filter, bool threaded=true)
Definition scaling.cc:467
int PyC_ParseUnicodeAsBytesAndSize_OrNone(PyObject *o, void *p)
static void py_imbuf_buffer_dealloc(Py_ImBufBuffer *self)
ImBuf * BPy_ImBuf_FromPyObject(PyObject *py_imbuf)
static PyObject * py_imbuf_file_type_capability_read_get(Py_ImBufFileType *self, void *flag_p)
void IMB_free_float_pixels(ImBuf *ibuf)
static PyObject * py_imbuf_file_type_get(Py_ImBuf *self, void *)
static PyObject * py_imbuf_write_impl(Py_ImBuf *py_imb, const char *filepath_or_null)
const char * PyC_UnicodeAsBytesAndSize(PyObject *py_str, Py_ssize_t *r_size, PyObject **r_coerce)
ImBuf * IMB_load_image_from_file_descriptor(int file, ImBufFlags flags, const char *filepath=nullptr, ColorManagedColorspaceSettings *colorspace_settings=nullptr)
Definition readimage.cc:167
static PyObject * py_imbuf_ppm_get(Py_ImBuf *self, void *)
static PyObject * py_imbuf_buffer_repr(Py_ImBufBuffer *self)
header-only compatibility defines.
#define Py_HashPointer
ImbFormatOptions foptions
const uint8_t * byte_data() const
uint8_t * byte_data_for_write()
Definition allocimbuf.cc:60
const float * float_data() const
std::string filepath
ImColorMode color_mode
eImbFileType ftype
float * float_data_for_write()
Definition allocimbuf.cc:79
int color_mode_channels_get() const
std::optional< rcti > region
PyObject_HEAD Py_ImBuf * py_ibuf
PyObject_HEAD eImbFileType ftype
PyObject_HEAD ImBuf * ibuf