Commit: c497a7efabe73514cd051a02b0574e179b0efeb8
Author: Campbell Barton
Date:   Thu Jul 4 21:15:18 2019 +1000
Branches: master
https://developer.blender.org/rBc497a7efabe73514cd051a02b0574e179b0efeb8

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

===================================================================

M       source/blender/editors/screen/screendump.c
M       source/blender/imbuf/IMB_imbuf.h
M       source/blender/imbuf/intern/rectop.c

===================================================================

diff --git a/source/blender/editors/screen/screendump.c 
b/source/blender/editors/screen/screendump.c
index e5552314a6e..005c76d26b0 100644
--- a/source/blender/editors/screen/screendump.c
+++ b/source/blender/editors/screen/screendump.c
@@ -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) {
diff --git a/source/blender/imbuf/IMB_imbuf.h b/source/blender/imbuf/IMB_imbuf.h
index 8574f33bce6..fbaa9b06d3b 100644
--- a/source/blender/imbuf/IMB_imbuf.h
+++ b/source/blender/imbuf/IMB_imbuf.h
@@ -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,
diff --git a/source/blender/imbuf/intern/rectop.c 
b/source/blender/imbuf/intern/rectop.c
index 25390b73737..76918c216f7 100644
--- a/source/blender/imbuf/intern/rectop.c
+++ b/source/blender/imbuf/intern/rectop.c
@@ -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,

_______________________________________________
Bf-blender-cvs mailing list
[email protected]
https://lists.blender.org/mailman/listinfo/bf-blender-cvs

Reply via email to