[Bf-blender-cvs] [10bf4b35035] greasepencil-object: GPencil: Basic structure of trace

Antonio Vazquez noreply at git.blender.org
Thu Apr 9 18:56:32 CEST 2020


Commit: 10bf4b35035bbbbf012874a1dd72b9bde7e3a6b8
Author: Antonio Vazquez
Date:   Wed Apr 8 15:49:08 2020 +0200
Branches: greasepencil-object
https://developer.blender.org/rB10bf4b35035bbbbf012874a1dd72b9bde7e3a6b8

GPencil: Basic structure of trace

Still is not tracing, but all pieces are in place

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

M	extern/potrace/src/potracelib.h
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

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

diff --git a/extern/potrace/src/potracelib.h b/extern/potrace/src/potracelib.h
index ae2f18c7ebc..d1b21982140 100644
--- a/extern/potrace/src/potracelib.h
+++ b/extern/potrace/src/potracelib.h
@@ -78,7 +78,7 @@ typedef struct potrace_dpoint_s potrace_dpoint_t;
 struct potrace_curve_s {
   int n;                    /* number of segments */
   int *tag;                 /* tag[n]: POTRACE_CURVETO or POTRACE_CORNER */
-  potrace_dpoint_t (*c)[3]; /* c[n][3]: control points. 
+  potrace_dpoint_t (*c)[3]; /* c[n][3]: control points.
 			       c[n][0] is unused for tag[n]=POTRACE_CORNER */
 };
 typedef struct potrace_curve_s potrace_curve_t;
@@ -96,7 +96,7 @@ struct potrace_path_s {
 
   struct potrace_privpath_s *priv;  /* private state */
 };
-typedef struct potrace_path_s potrace_path_t;  
+typedef struct potrace_path_s potrace_path_t;
 
 /* ---------------------------------------------------------------------- */
 /* Potrace state */
@@ -105,7 +105,7 @@ typedef struct potrace_path_s potrace_path_t;
 #define POTRACE_STATUS_INCOMPLETE 1
 
 struct potrace_state_s {
-  int status;                       
+  int status;
   potrace_path_t *plist;            /* vector data */
 
   struct potrace_privstate_s *priv; /* private state */
@@ -122,7 +122,7 @@ potrace_param_t *potrace_param_default(void);
 void potrace_param_free(potrace_param_t *p);
 
 /* trace a bitmap */
-potrace_state_t *potrace_trace(const potrace_param_t *param, 
+potrace_state_t *potrace_trace(const potrace_param_t *param,
 			       const potrace_bitmap_t *bm);
 
 /* free a Potrace state */
diff --git a/source/blender/editors/gpencil/CMakeLists.txt b/source/blender/editors/gpencil/CMakeLists.txt
index 67bb423c540..c521b0dac2b 100644
--- a/source/blender/editors/gpencil/CMakeLists.txt
+++ b/source/blender/editors/gpencil/CMakeLists.txt
@@ -57,6 +57,8 @@ set(SRC
   gpencil_primitive.c
   gpencil_sculpt_paint.c
   gpencil_select.c
+  gpencil_trace_ops.c
+  gpencil_trace_utils.c
   gpencil_undo.c
   gpencil_utils.c
   gpencil_uv.c
@@ -65,6 +67,7 @@ set(SRC
   gpencil_weight_paint.c
 
   gpencil_intern.h
+  gpencil_trace.h
 )
 
 set(LIB
diff --git a/source/blender/editors/gpencil/gpencil_intern.h b/source/blender/editors/gpencil/gpencil_intern.h
index ded2c2e9bd8..9d07dd69e43 100644
--- a/source/blender/editors/gpencil/gpencil_intern.h
+++ b/source/blender/editors/gpencil/gpencil_intern.h
@@ -469,6 +469,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 a370e55d266..f11fd389764 100644
--- a/source/blender/editors/gpencil/gpencil_ops.c
+++ b/source/blender/editors/gpencil/gpencil_ops.c
@@ -606,6 +606,7 @@ void ED_operatortypes_gpencil(void)
   WM_operatortype_append(GPENCIL_OT_bake_mesh_animation);
 
   WM_operatortype_append(GPENCIL_OT_image_to_grease_pencil);
+  WM_operatortype_append(GPENCIL_OT_trace_image);
 
   WM_operatortype_append(GPENCIL_OT_stroke_arrange);
   WM_operatortype_append(GPENCIL_OT_stroke_change_color);
diff --git a/source/blender/editors/gpencil/gpencil_trace.h b/source/blender/editors/gpencil/gpencil_trace.h
new file mode 100644
index 00000000000..126a8926bff
--- /dev/null
+++ b/source/blender/editors/gpencil/gpencil_trace.h
@@ -0,0 +1,67 @@
+/*
+ * 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) 2009 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 Object;
+
+#include "potracelib.h"
+
+/* 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_bm_print(FILE *f, const potrace_bitmap_t *bm);
+
+potrace_bitmap_t *ED_gpencil_trace_bm_new(int w, int h);
+void ED_gpencil_trace_bm_free(const potrace_bitmap_t *bm);
+void ED_gpencil_trace_bm_invert(const potrace_bitmap_t *bm);
+
+void ED_gpencil_trace_image_to_bm(struct ImBuf *ibuf, const potrace_bitmap_t *bm);
+void ED_gpencil_trace_data_to_gp(potrace_state_t *st, struct Object *ob, struct bGPDframe *gpf);
+
+#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..a6a62231fe4
--- /dev/null
+++ b/source/blender/editors/gpencil/gpencil_trace_ops.c
@@ -0,0 +1,195 @@
+/*
+ * 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) 2008, Blender Foundation
+ * This is a new part of Blender
+ * Operator for converting Grease Pencil data to geometry
+ */
+
+/** \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 "gpencil_trace.h"
+#include "potracelib.h"
+
+/* Trace Image to Grease Pencil. */
+static bool gp_trace_image_poll(bContext *C)
+{
+  SpaceLink *sl = CTX_wm_space_data(C);
+  if ((sl != NULL) && (sl->spacetype == SPACE_IMAGE)) {
+    SpaceImage *sima = CTX_wm_space_image(C);
+    return (sima->image != NULL);
+  }
+
+  return false;
+}
+
+static int gp_trace_image_exec(bContext *C, wmOperator *op)
+{
+  Main *bmain = CTX_data_main(C);
+  View3D *v3d = CTX_wm_view3d(C);
+  SpaceImage *sima = CTX_wm_space_image(C);
+  Object *ob_gpencil = NULL;
+
+  potrace_bitmap_t *bm = NULL;
+  potrace_param_t *param = NULL;
+  potrace_state_t *st = NULL;
+
+  char target[64];
+  RNA_string_get(op->ptr, "target", target);
+  const int frame_target = RNA_int_get(op->ptr, "frame_target");
+
+  ImBuf *ibuf;
+  void *lock;
+  ibuf = BKE_image_acquire_ibuf(sima->image, NULL, &lock);
+
+  /* Create an empty BW bitmap. */
+  bm = ED_gpencil_trace_bm_new(ibuf->x, ibuf->y);
+  if (!bm) {
+    return OPERATOR_CANCELLED;
+  }
+
+  /* Set tracing parameters, starting from defaults */
+  param = potrace_param_default();
+  if (!param) {
+    return OPERATOR_CANCELLED;
+  }
+  param->turdsize = 0;
+
+  /* Create a new grease pencil object in origin. */
+  if (STREQ(target, "*NEW")) {
+    ushort local_view_bits = (v3d && v3d->localvd) ? v3d->local_view_uuid : 0;
+    float loc[3] = {0.0f, 0.0f, 0.0f};
+    ob_gpencil = ED_gpencil_add_object(C, loc, local_view_bits);
+  }
+  else {
+    ob_gpencil = BLI_findstring(&bmain->objects, target, offsetof(ID, name) + 2);
+  }
+
+  if ((ob_gpencil == NULL) || (ob_gpencil->type != OB_GPENCIL)) {
+    BKE_report(op->reports, RPT_ERROR, "Target grease pencil object not valid");
+    return OPERATOR_CANCELLED;
+  }
+  bGPdata *gpd = (bGPdata *)ob_gpencil->data;
+
+  /* If the object has materials means it was created in a previous run. */
+  if (ob_gpencil->totcol == 0) {
+    const float default_color[4] = {0.0f, 0.0f, 0.0f, 1.0f};
+    int r_idx;
+    Material *mat_gp = BKE_gpencil_object_mat

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list