From 687f70ceca88324fb8f376e086a55b341dd34e6f Mon Sep 17 00:00:00 2001 From: Campbell Barton Date: Mon, 6 Sep 2021 20:04:26 +1000 Subject: ImBuf: add IMB_allocFromBufferOwn that takes ownership of the buffer Avoids duplicating the image buffer when saving thumbnails. --- source/blender/imbuf/IMB_imbuf.h | 8 ++++++++ source/blender/imbuf/intern/allocimbuf.c | 35 ++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+) (limited to 'source/blender/imbuf') diff --git a/source/blender/imbuf/IMB_imbuf.h b/source/blender/imbuf/IMB_imbuf.h index 4ad7aa98484..dd8e6549e24 100644 --- a/source/blender/imbuf/IMB_imbuf.h +++ b/source/blender/imbuf/IMB_imbuf.h @@ -148,6 +148,14 @@ struct ImBuf *IMB_allocImBuf(unsigned int x, bool IMB_initImBuf( struct ImBuf *ibuf, unsigned int x, unsigned int y, unsigned char planes, unsigned int flags); +/** + * Create a copy of a pixel buffer and wrap it to a new ImBuf + * (transferring ownership to the in imbuf). + * \attention Defined in allocimbuf.c + */ +struct ImBuf *IMB_allocFromBufferOwn( + unsigned int *rect, float *rectf, unsigned int w, unsigned int h, unsigned int channels); + /** * Create a copy of a pixel buffer and wrap it to a new ImBuf * \attention Defined in allocimbuf.c diff --git a/source/blender/imbuf/intern/allocimbuf.c b/source/blender/imbuf/intern/allocimbuf.c index d327981f583..c2bff9ddc51 100644 --- a/source/blender/imbuf/intern/allocimbuf.c +++ b/source/blender/imbuf/intern/allocimbuf.c @@ -430,6 +430,41 @@ bool imb_addrectImBuf(ImBuf *ibuf) return false; } +/** + * \param take_ownership: When true, the buffers become owned by the resulting image. + */ +struct ImBuf *IMB_allocFromBufferOwn( + unsigned int *rect, float *rectf, unsigned int w, unsigned int h, unsigned int channels) +{ + ImBuf *ibuf = NULL; + + if (!(rect || rectf)) { + return NULL; + } + + ibuf = IMB_allocImBuf(w, h, 32, 0); + + ibuf->channels = channels; + + /* Avoid #MEM_dupallocN since the buffers might not be allocated using guarded-allocation. */ + if (rectf) { + BLI_assert(MEM_allocN_len(rectf) == sizeof(float[4]) * w * h); + ibuf->rect_float = rectf; + + ibuf->flags |= IB_rectfloat; + ibuf->mall |= IB_rectfloat; + } + if (rect) { + BLI_assert(MEM_allocN_len(rect) == sizeof(uchar[4]) * w * h); + ibuf->rect = rect; + + ibuf->flags |= IB_rect; + ibuf->mall |= IB_rect; + } + + return ibuf; +} + struct ImBuf *IMB_allocFromBuffer(const unsigned int *rect, const float *rectf, unsigned int w, -- cgit v1.2.3