ImBuf: add crop function (move out of screendump.c)

This commit is contained in:
Campbell Barton 2019-07-04 21:15:18 +10:00
parent b2e3f23bec
commit c497a7efab
3 changed files with 74 additions and 19 deletions

View File

@ -110,24 +110,6 @@ static void screenshot_data_free(wmOperator *op)
}
}
static void screenshot_crop(ImBuf *ibuf, rcti crop)
{
unsigned int *to = ibuf->rect;
unsigned int *from = ibuf->rect + crop.ymin * ibuf->x + crop.xmin;
int crop_x = BLI_rcti_size_x(&crop);
int crop_y = BLI_rcti_size_y(&crop);
int y;
if (crop_x > 0 && crop_y > 0) {
for (y = 0; y < crop_y; y++, to += crop_x, from += ibuf->x) {
memmove(to, from, sizeof(unsigned int) * crop_x);
}
ibuf->x = crop_x;
ibuf->y = crop_y;
}
}
static int screenshot_exec(bContext *C, wmOperator *op)
{
ScreenshotData *scd = op->customdata;
@ -153,7 +135,8 @@ static int screenshot_exec(bContext *C, wmOperator *op)
/* crop to show only single editor */
if (!RNA_boolean_get(op->ptr, "full")) {
screenshot_crop(ibuf, scd->crop);
IMB_rect_crop(ibuf, &scd->crop);
scd->dumprect = ibuf->rect;
}
if (scd->im_format.planes == R_IMF_PLANES_BW) {

View File

@ -67,6 +67,7 @@
* \attention defined in ???
*/
struct ImBuf;
struct rcti;
/**
*
@ -222,6 +223,8 @@ void IMB_blend_color_byte(unsigned char dst[4],
IMB_BlendMode mode);
void IMB_blend_color_float(float dst[4], float src1[4], float src2[4], IMB_BlendMode mode);
void IMB_rect_crop(struct ImBuf *ibuf, const struct rcti *crop);
void IMB_rectclip(struct ImBuf *dbuf,
struct ImBuf *sbuf,
int *destx,

View File

@ -25,6 +25,7 @@
#include <stdlib.h>
#include "BLI_utildefines.h"
#include "BLI_rect.h"
#include "BLI_math_base.h"
#include "BLI_math_color.h"
#include "BLI_math_color_blend.h"
@ -35,6 +36,8 @@
#include "IMB_colormanagement.h"
#include "MEM_guardedalloc.h"
void IMB_blend_color_byte(unsigned char dst[4],
unsigned char src1[4],
unsigned char src2[4],
@ -207,6 +210,72 @@ void IMB_blend_color_float(float dst[4], float src1[4], float src2[4], IMB_Blend
}
}
/** Crop */
static void rect_crop_4bytes(void **buf_p, const int size_src[2], const rcti *crop)
{
if (*buf_p == NULL) {
return;
}
const int size_dst[2] = {
BLI_rcti_size_x(crop) + 1,
BLI_rcti_size_y(crop) + 1,
};
uint *src = *buf_p;
uint *dst = src + crop->ymin * size_src[0] + crop->xmin;
for (int y = 0; y < size_dst[1]; y++, src += size_dst[0], dst += size_src[0]) {
memmove(src, dst, sizeof(uint) * size_dst[0]);
}
*buf_p = MEM_reallocN(*buf_p, sizeof(uint) * size_dst[0] * size_dst[1]);
}
static void rect_crop_16bytes(void **buf_p, const int size_src[2], const rcti *crop)
{
if (*buf_p == NULL) {
return;
}
const int size_dst[2] = {
BLI_rcti_size_x(crop) + 1,
BLI_rcti_size_y(crop) + 1,
};
uint(*src)[4] = *buf_p;
uint(*dst)[4] = src + crop->ymin * size_src[0] + crop->xmin;
for (int y = 0; y < size_dst[1]; y++, src += size_dst[0], dst += size_src[0]) {
memmove(src, dst, sizeof(uint[4]) * size_dst[0]);
}
*buf_p = (void *)MEM_reallocN(*buf_p, sizeof(uint[4]) * size_dst[0] * size_dst[1]);
}
/**
* In-place image crop.
*/
void IMB_rect_crop(ImBuf *ibuf, const rcti *crop)
{
const int size_src[2] = {
ibuf->x,
ibuf->y,
};
const int size_dst[2] = {
BLI_rcti_size_x(crop) + 1,
BLI_rcti_size_y(crop) + 1,
};
BLI_assert(size_dst[0] > 0 && size_dst[0] > 0);
BLI_assert(crop->xmin >= 0 && crop->ymin >= 0);
BLI_assert(crop->xmax < ibuf->x && crop->ymax < ibuf->y);
if ((size_dst[0] == ibuf->x) && (size_dst[1] == ibuf->y)) {
return;
}
rect_crop_4bytes((void **)&ibuf->rect, size_src, crop);
rect_crop_4bytes((void **)&ibuf->zbuf, size_src, crop);
rect_crop_4bytes((void **)&ibuf->zbuf_float, size_src, crop);
rect_crop_16bytes((void **)&ibuf->rect_float, size_src, crop);
ibuf->x = size_dst[0];
ibuf->y = size_dst[1];
}
/* clipping */
void IMB_rectclip(ImBuf *dbuf,