[Bf-blender-cvs] [4d62bb8fe57] master: GPencil: New Trace images using Potrace

Antonio Vazquez noreply at git.blender.org
Mon Sep 21 20:04:22 CEST 2020


Commit: 4d62bb8fe57ca431da669386d47ca185f3624c9a
Author: Antonio Vazquez
Date:   Mon Sep 21 19:53:36 2020 +0200
Branches: master
https://developer.blender.org/rB4d62bb8fe57ca431da669386d47ca185f3624c9a

GPencil: New Trace images using Potrace

This patch adds a new operator to convert a black and white image into
grease pencil strokes.

If the image is not B/W, an internal conversion is done.

This is the first operator using Potrace, but we expect to add more features in next Blender versions.

Reviewed By: HooglyBoogly

Maniphest Tasks: T79877

Differential Revision: https://developer.blender.org/D8951

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

M	release/scripts/startup/bl_ui/space_view3d.py
M	source/blender/blenkernel/BKE_gpencil.h
M	source/blender/blenkernel/intern/gpencil.c
M	source/blender/editors/gpencil/CMakeLists.txt
M	source/blender/editors/gpencil/gpencil_intern.h
M	source/blender/editors/gpencil/gpencil_ops.c
A	source/blender/editors/gpencil/gpencil_trace.h
A	source/blender/editors/gpencil/gpencil_trace_ops.c
A	source/blender/editors/gpencil/gpencil_trace_utils.c
M	source/blender/python/intern/CMakeLists.txt
M	source/blender/python/intern/bpy_app_build_options.c

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

diff --git a/release/scripts/startup/bl_ui/space_view3d.py b/release/scripts/startup/bl_ui/space_view3d.py
index 351654f61e4..888b4a62139 100644
--- a/release/scripts/startup/bl_ui/space_view3d.py
+++ b/release/scripts/startup/bl_ui/space_view3d.py
@@ -2260,6 +2260,11 @@ class VIEW3D_MT_object(Menu):
         else:
             layout.operator_menu_enum("object.convert", "target")
 
+        # Potrace lib dependency
+        if bpy.app.build_options.potrace:
+            layout.separator()
+            layout.operator("gpencil.trace_image")
+
         layout.separator()
 
         layout.menu("VIEW3D_MT_object_showhide")
diff --git a/source/blender/blenkernel/BKE_gpencil.h b/source/blender/blenkernel/BKE_gpencil.h
index f1912b14e8c..c484f0753a3 100644
--- a/source/blender/blenkernel/BKE_gpencil.h
+++ b/source/blender/blenkernel/BKE_gpencil.h
@@ -282,6 +282,8 @@ void BKE_gpencil_parent_matrix_get(const struct Depsgraph *depsgraph,
 
 void BKE_gpencil_update_layer_parent(const struct Depsgraph *depsgraph, struct Object *ob);
 
+int BKE_gpencil_material_find_index_by_name_prefix(struct Object *ob, const char *name_prefix);
+
 void BKE_gpencil_blend_read_data(struct BlendDataReader *reader, struct bGPdata *gpd);
 
 #ifdef __cplusplus
diff --git a/source/blender/blenkernel/intern/gpencil.c b/source/blender/blenkernel/intern/gpencil.c
index 03ac7e622e1..7bc3daf037f 100644
--- a/source/blender/blenkernel/intern/gpencil.c
+++ b/source/blender/blenkernel/intern/gpencil.c
@@ -2745,4 +2745,25 @@ void BKE_gpencil_update_layer_parent(const Depsgraph *depsgraph, Object *ob)
     }
   }
 }
+
+/**
+ * Find material by name prefix.
+ * \param ob: Object pointer
+ * \param name_prefix: Prefix name of the material
+ * \return  Index
+ */
+int BKE_gpencil_material_find_index_by_name_prefix(Object *ob, const char *name_prefix)
+{
+  const int name_prefix_len = strlen(name_prefix);
+  for (int i = 0; i < ob->totcol; i++) {
+    Material *ma = BKE_object_material_get(ob, i + 1);
+    if ((ma != NULL) && (ma->gp_style != NULL) &&
+        (STREQLEN(ma->id.name + 2, name_prefix, name_prefix_len))) {
+      return i;
+    }
+  }
+
+  return -1;
+}
+
 /** \} */
diff --git a/source/blender/editors/gpencil/CMakeLists.txt b/source/blender/editors/gpencil/CMakeLists.txt
index 20408327105..7bf8a93a97c 100644
--- a/source/blender/editors/gpencil/CMakeLists.txt
+++ b/source/blender/editors/gpencil/CMakeLists.txt
@@ -29,6 +29,7 @@ set(INC
   ../../windowmanager
   ../../../../intern/glew-mx
   ../../../../intern/guardedalloc
+  ../../../../extern/potrace/src
 )
 
 set(SRC
@@ -60,6 +61,7 @@ set(SRC
   gpencil_weight_paint.c
 
   gpencil_intern.h
+  gpencil_trace.h
 )
 
 set(LIB
@@ -67,6 +69,20 @@ set(LIB
   bf_blenlib
 )
 
+if(WITH_POTRACE)
+  list(APPEND SRC
+    gpencil_trace_ops.c
+    gpencil_trace_utils.c
+  )
+  list(APPEND INC
+    ${POTRACE_INCLUDE_DIRS}
+  )
+  list(APPEND LIB
+    ${POTRACE_LIBRARIES}
+  )
+  add_definitions(-DWITH_POTRACE)
+endif()
+
 if(WITH_INTERNATIONAL)
   add_definitions(-DWITH_INTERNATIONAL)
 endif()
diff --git a/source/blender/editors/gpencil/gpencil_intern.h b/source/blender/editors/gpencil/gpencil_intern.h
index f45321b6b20..e3e2199f8a3 100644
--- a/source/blender/editors/gpencil/gpencil_intern.h
+++ b/source/blender/editors/gpencil/gpencil_intern.h
@@ -492,6 +492,7 @@ void GPENCIL_OT_convert(struct wmOperatorType *ot);
 void GPENCIL_OT_bake_mesh_animation(struct wmOperatorType *ot);
 
 void GPENCIL_OT_image_to_grease_pencil(struct wmOperatorType *ot);
+void GPENCIL_OT_trace_image(struct wmOperatorType *ot);
 
 enum {
   GP_STROKE_JOIN = -1,
diff --git a/source/blender/editors/gpencil/gpencil_ops.c b/source/blender/editors/gpencil/gpencil_ops.c
index 211ff895e67..9f7725e01f5 100644
--- a/source/blender/editors/gpencil/gpencil_ops.c
+++ b/source/blender/editors/gpencil/gpencil_ops.c
@@ -606,7 +606,9 @@ void ED_operatortypes_gpencil(void)
   WM_operatortype_append(GPENCIL_OT_bake_mesh_animation);
 
   WM_operatortype_append(GPENCIL_OT_image_to_grease_pencil);
-
+#ifdef WITH_POTRACE
+  WM_operatortype_append(GPENCIL_OT_trace_image);
+#endif
   WM_operatortype_append(GPENCIL_OT_stroke_arrange);
   WM_operatortype_append(GPENCIL_OT_stroke_change_color);
   WM_operatortype_append(GPENCIL_OT_material_lock_unused);
diff --git a/source/blender/editors/gpencil/gpencil_trace.h b/source/blender/editors/gpencil/gpencil_trace.h
new file mode 100644
index 00000000000..7ac35ee5e04
--- /dev/null
+++ b/source/blender/editors/gpencil/gpencil_trace.h
@@ -0,0 +1,79 @@
+/*
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ *
+ * The Original Code is Copyright (C) 2020 Blender Foundation.
+ * All rights reserved.
+ */
+
+/** \file
+ * \ingroup edgpencil
+ */
+
+#ifndef __GPENCIL_TRACE_H__
+#define __GPENCIL_TRACE_H__
+
+/* internal exports only */
+struct bGPDframe;
+struct FILE;
+struct ImBuf;
+struct Main;
+struct Object;
+
+#include "potracelib.h"
+
+/* Potrace macros for writing individual bitmap pixels. */
+#define BM_WORDSIZE ((int)sizeof(potrace_word))
+#define BM_WORDBITS (8 * BM_WORDSIZE)
+#define BM_HIBIT (((potrace_word)1) << (BM_WORDBITS - 1))
+#define BM_ALLBITS (~(potrace_word)0)
+
+#define bm_scanline(bm, y) ((bm)->map + (y) * (bm)->dy)
+#define bm_index(bm, x, y) (&bm_scanline(bm, y)[(x) / BM_WORDBITS])
+#define bm_mask(x) (BM_HIBIT >> ((x) & (BM_WORDBITS - 1)))
+#define bm_range(x, a) ((int)(x) >= 0 && (int)(x) < (a))
+#define bm_safe(bm, x, y) (bm_range(x, (bm)->w) && bm_range(y, (bm)->h))
+
+#define BM_UGET(bm, x, y) ((*bm_index(bm, x, y) & bm_mask(x)) != 0)
+#define BM_USET(bm, x, y) (*bm_index(bm, x, y) |= bm_mask(x))
+#define BM_UCLR(bm, x, y) (*bm_index(bm, x, y) &= ~bm_mask(x))
+#define BM_UINV(bm, x, y) (*bm_index(bm, x, y) ^= bm_mask(x))
+#define BM_UPUT(bm, x, y, b) ((b) ? BM_USET(bm, x, y) : BM_UCLR(bm, x, y))
+#define BM_GET(bm, x, y) (bm_safe(bm, x, y) ? BM_UGET(bm, x, y) : 0)
+#define BM_SET(bm, x, y) (bm_safe(bm, x, y) ? BM_USET(bm, x, y) : 0)
+#define BM_CLR(bm, x, y) (bm_safe(bm, x, y) ? BM_UCLR(bm, x, y) : 0)
+#define BM_INV(bm, x, y) (bm_safe(bm, x, y) ? BM_UINV(bm, x, y) : 0)
+#define BM_PUT(bm, x, y, b) (bm_safe(bm, x, y) ? BM_UPUT(bm, x, y, b) : 0)
+
+void ED_gpencil_trace_bitmap_print(FILE *f, const potrace_bitmap_t *bm);
+
+potrace_bitmap_t *ED_gpencil_trace_bitmap_new(int32_t w, int32_t h);
+void ED_gpencil_trace_bitmap_free(const potrace_bitmap_t *bm);
+void ED_gpencil_trace_bitmap_invert(const potrace_bitmap_t *bm);
+
+void ED_gpencil_trace_image_to_bitmap(struct ImBuf *ibuf,
+                                      const potrace_bitmap_t *bm,
+                                      const float threshold);
+
+void ED_gpencil_trace_data_to_strokes(struct Main *bmain,
+                                      potrace_state_t *st,
+                                      struct Object *ob,
+                                      struct bGPDframe *gpf,
+                                      int32_t offset[2],
+                                      const float scale,
+                                      const float sample,
+                                      const int32_t resolution,
+                                      const int32_t thickness);
+
+#endif /* __GPENCIL_TRACE_H__ */
diff --git a/source/blender/editors/gpencil/gpencil_trace_ops.c b/source/blender/editors/gpencil/gpencil_trace_ops.c
new file mode 100644
index 00000000000..4391abee5a1
--- /dev/null
+++ b/source/blender/editors/gpencil/gpencil_trace_ops.c
@@ -0,0 +1,313 @@
+/*
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ *
+ * The Original Code is Copyright (C) 2020 Blender Foundation
+ * All rights reserved.
+ */
+
+/** \file
+ * \ingroup edgpencil
+ */
+
+#include "MEM_guardedalloc.h"
+
+#include "BLI_blenlib.h"
+#include "BLI_math.h"
+
+#include "BLT_translation.h"
+
+#include "DNA_gpencil_types.h"
+#include "DNA_scene_types.h"
+#include "DNA_screen_types.h"
+#include "DNA_space_types.h"
+
+#include "BKE_context.h"
+#include "BKE_duplilist.h"
+#include "BKE_gpencil.h"
+#include "BKE_image.h"
+#include "BKE_main.h"
+#include "BKE_material.h"
+#include "BKE_object.h"
+#include "BKE_report.h"
+
+#include "DEG_depsgraph.h"
+#include "DEG_depsgraph_query.h"
+
+#include "WM_api.h"
+#include "WM_types.h"
+
+#include "RNA_access.h"
+#include "RNA_define.h"
+
+#include "IMB_imbuf.h"
+#include "IMB_imbuf_types.h"
+
+#include "ED_gpencil.h"
+#include "ED_object.h"
+
+#include "gpencil_intern.h"
+#include "gpencil_trace.h"
+#include "potracelib.h"
+
+/**
+ * Trace a image.
+ * \param C: Context
+ * \param op: Operator
+ * \param ob: Grease pencil object, can be NULL
+ * \param ima: Image
+ * \param gpf: Destination frame
+ */
+static bool gpencil_trace_image(
+    bContext *C, wmOperator *op, Object *ob, Image *ima, bGPDframe *gpf)
+{
+  Main *bmain = CTX_data_main(C);
+
+  potrace_bitmap_t *bm = NULL;
+  potrace_param_t *param = NULL

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list