[Bf-blender-cvs] [b5405cd285d] greasepencil-object: GPencil: Reorganize palette operators

Antonio Vazquez noreply at git.blender.org
Sun Nov 17 11:33:34 CET 2019


Commit: b5405cd285dc1b449a68465212d93325833461b4
Author: Antonio Vazquez
Date:   Sun Nov 17 11:33:20 2019 +0100
Branches: greasepencil-object
https://developer.blender.org/rBb5405cd285dc1b449a68465212d93325833461b4

GPencil: Reorganize palette operators

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

M	release/scripts/startup/bl_ui/space_view3d.py
M	source/blender/editors/gpencil/gpencil_intern.h
M	source/blender/editors/gpencil/gpencil_ops.c
M	source/blender/editors/gpencil/gpencil_vertex_ops.c

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

diff --git a/release/scripts/startup/bl_ui/space_view3d.py b/release/scripts/startup/bl_ui/space_view3d.py
index 75ce96d2dc2..f683df05c79 100644
--- a/release/scripts/startup/bl_ui/space_view3d.py
+++ b/release/scripts/startup/bl_ui/space_view3d.py
@@ -4597,7 +4597,7 @@ class VIEW3D_MT_gpencil_materials(Menu):
         layout.separator()
 
         layout.operator("gpencil.material_to_vertex_color", text="Convert Materials to Vertex Color")
-        layout.operator("gpencil.extract_palette", text="Extract Palette from Vertex Color")
+        layout.operator("gpencil.extract_palette_vertex", text="Extract Palette from Vertex Color")
 
 
 class VIEW3D_MT_gpencil_copy_layer(Menu):
diff --git a/source/blender/editors/gpencil/gpencil_intern.h b/source/blender/editors/gpencil/gpencil_intern.h
index db08e71279c..8a57dc0526a 100644
--- a/source/blender/editors/gpencil/gpencil_intern.h
+++ b/source/blender/editors/gpencil/gpencil_intern.h
@@ -506,8 +506,7 @@ void GPENCIL_OT_stroke_merge_by_distance(struct wmOperatorType *ot);
 void GPENCIL_OT_stroke_merge_material(struct wmOperatorType *ot);
 
 void GPENCIL_OT_material_to_vertex_color(struct wmOperatorType *ot);
-void GPENCIL_OT_extract_palette(struct wmOperatorType *ot);
-void GPENCIL_OT_extract_palette_from_image(struct wmOperatorType *ot);
+void GPENCIL_OT_extract_palette_vertex(struct wmOperatorType *ot);
 
 void GPENCIL_OT_transform_fill(struct wmOperatorType *ot);
 void GPENCIL_OT_reset_transform_fill(struct wmOperatorType *ot);
diff --git a/source/blender/editors/gpencil/gpencil_ops.c b/source/blender/editors/gpencil/gpencil_ops.c
index 0715237dabd..4340bf2187e 100644
--- a/source/blender/editors/gpencil/gpencil_ops.c
+++ b/source/blender/editors/gpencil/gpencil_ops.c
@@ -453,8 +453,7 @@ void ED_operatortypes_gpencil(void)
   WM_operatortype_append(GPENCIL_OT_stroke_merge_material);
 
   WM_operatortype_append(GPENCIL_OT_material_to_vertex_color);
-  WM_operatortype_append(GPENCIL_OT_extract_palette);
-  WM_operatortype_append(GPENCIL_OT_extract_palette_from_image);
+  WM_operatortype_append(GPENCIL_OT_extract_palette_vertex);
 
   WM_operatortype_append(GPENCIL_OT_transform_fill);
   WM_operatortype_append(GPENCIL_OT_reset_transform_fill);
diff --git a/source/blender/editors/gpencil/gpencil_vertex_ops.c b/source/blender/editors/gpencil/gpencil_vertex_ops.c
index 33975c398cb..71bdb67b6cc 100644
--- a/source/blender/editors/gpencil/gpencil_vertex_ops.c
+++ b/source/blender/editors/gpencil/gpencil_vertex_ops.c
@@ -24,8 +24,6 @@
 
 #include "MEM_guardedalloc.h"
 
-#include "IMB_imbuf_types.h"
-
 #include "BLI_blenlib.h"
 #include "BLI_ghash.h"
 #include "BLI_math.h"
@@ -39,7 +37,6 @@
 #include "BKE_context.h"
 #include "BKE_gpencil.h"
 #include "BKE_gpencil_modifier.h"
-#include "BKE_image.h"
 #include "BKE_main.h"
 #include "BKE_material.h"
 #include "BKE_paint.h"
@@ -499,89 +496,8 @@ void GPENCIL_OT_vertex_color_set(wmOperatorType *ot)
   RNA_def_float(ot->srna, "factor", 1.0f, 0.001f, 1.0f, "Factor", "Mix Factor", 0.001f, 1.0f);
 }
 
-typedef struct GPHue {
-  float rgb[3];
-  float hue;
-  float sat;
-} GPHue;
-
-/* helper function to sort  using qsort */
-static int gpencil_compare_hue_sat(const void *a1, const void *a2)
-{
-  const GPHue *ps1 = a1, *ps2 = a2;
-  int a = ps1->hue * 1e6 + ps1->sat * 1e3;
-  int b = ps2->hue * 1e6 + ps2->sat * 1e3;
-
-  if (a < b) {
-    return -1;
-  }
-  else if (a > b) {
-    return 1;
-  }
-
-  return 0;
-}
-
-static bool gpencil_create_palette_from_hash(Main *bmain, GHash *color_table)
-{
-  GPHue *color_array = NULL;
-  GPHue *col_elm = NULL;
-  bool done = false;
-
-  int totpal = BLI_ghash_len(color_table);
-
-  if (totpal > 0) {
-    color_array = MEM_calloc_arrayN(totpal, sizeof(GPHue), __func__);
-    /* Put all colors in an array. */
-    GHashIterator gh_iter;
-    int t = 0;
-    GHASH_ITER (gh_iter, color_table) {
-      const uint col = POINTER_AS_INT(BLI_ghashIterator_getValue(&gh_iter));
-      float r, g, b;
-      float h, s, v;
-      cpack_to_rgb(col, &r, &g, &b);
-      rgb_to_hsv(r, g, b, &h, &s, &v);
-
-      col_elm = &color_array[t];
-      col_elm->rgb[0] = r;
-      col_elm->rgb[1] = g;
-      col_elm->rgb[2] = b;
-      col_elm->hue = h;
-      col_elm->sat = s;
-      t++;
-    }
-  }
-
-  /* Create the Palette. */
-  if (totpal > 0) {
-    /* Sort by Hue and saturation. */
-    qsort(color_array, totpal, sizeof(GPHue), gpencil_compare_hue_sat);
-
-    Palette *palette = BKE_palette_add(bmain, "Palette");
-    if (palette) {
-      for (int i = 0; i < totpal; i++) {
-        col_elm = &color_array[i];
-        PaletteColor *palcol = BKE_palette_color_add(palette);
-        if (palcol) {
-          copy_v3_v3(palcol->rgb, col_elm->rgb);
-        }
-      }
-      done = true;
-    }
-  }
-  else {
-    done = false;
-  }
-
-  if (totpal > 0) {
-    MEM_SAFE_FREE(color_array);
-  }
-
-  return done;
-}
-
-/* Helper to extract color for palette. */
-static bool gp_extract_palette(bContext *C, const bool selected, const int threshold)
+/* Helper to extract color from vertex color to create a palette. */
+static bool gp_extract_palette_from_vertex(bContext *C, const bool selected, const int threshold)
 {
   Main *bmain = CTX_data_main(C);
   Object *ob = CTX_data_active_object(C);
@@ -661,7 +577,7 @@ static bool gp_extract_palette(bContext *C, const bool selected, const int thres
   CTX_DATA_END;
 
   /* Create the Palette. */
-  done = gpencil_create_palette_from_hash(bmain, color_table);
+  done = BKE_palette_from_hash(bmain, color_table);
 
   /* Free memory. */
   BLI_ghash_free(color_table, NULL, NULL);
@@ -670,17 +586,6 @@ static bool gp_extract_palette(bContext *C, const bool selected, const int thres
 }
 
 /* Convert Materials to Vertex Color. */
-static bool gp_material_to_vertex_poll(bContext *C)
-{
-  /* only supported with grease pencil objects */
-  Object *ob = CTX_data_active_object(C);
-  if ((ob == NULL) || (ob->type != OB_GPENCIL)) {
-    return false;
-  }
-
-  return true;
-}
-
 typedef struct GPMatArray {
   uint key;
   Material *ma;
@@ -753,6 +658,17 @@ static uint get_material_type(MaterialGPencilStyle *gp_style,
   return r_i;
 }
 
+static bool gp_material_to_vertex_poll(bContext *C)
+{
+  /* only supported with grease pencil objects */
+  Object *ob = CTX_data_active_object(C);
+  if ((ob == NULL) || (ob->type != OB_GPENCIL)) {
+    return false;
+  }
+
+  return true;
+}
+
 static int gp_material_to_vertex_exec(bContext *C, wmOperator *op)
 {
   Main *bmain = CTX_data_main(C);
@@ -896,7 +812,7 @@ static int gp_material_to_vertex_exec(bContext *C, wmOperator *op)
 
   /* Generate a Palette. */
   if (palette) {
-    gp_extract_palette(C, selected, 1);
+    gp_extract_palette_from_vertex(C, selected, 1);
   }
 
   /* Clean unused materials. */
@@ -934,7 +850,7 @@ void GPENCIL_OT_material_to_vertex_color(wmOperatorType *ot)
 }
 
 /* Extract Palette from Vertex Color. */
-static bool gp_extract_palette_poll(bContext *C)
+static bool gp_extract_palette_vertex_poll(bContext *C)
 {
   /* only supported with grease pencil objects */
   Object *ob = CTX_data_active_object(C);
@@ -945,12 +861,12 @@ static bool gp_extract_palette_poll(bContext *C)
   return true;
 }
 
-static int gp_extract_palette_exec(bContext *C, wmOperator *op)
+static int gp_extract_palette_vertex_exec(bContext *C, wmOperator *op)
 {
   const bool selected = RNA_boolean_get(op->ptr, "selected");
   const int threshold = RNA_int_get(op->ptr, "threshold");
 
-  if (gp_extract_palette(C, selected, threshold)) {
+  if (gp_extract_palette_from_vertex(C, selected, threshold)) {
     BKE_reportf(op->reports, RPT_INFO, "Palette created");
   }
   else {
@@ -960,16 +876,16 @@ static int gp_extract_palette_exec(bContext *C, wmOperator *op)
   return OPERATOR_FINISHED;
 }
 
-void GPENCIL_OT_extract_palette(wmOperatorType *ot)
+void GPENCIL_OT_extract_palette_vertex(wmOperatorType *ot)
 {
   /* identifiers */
   ot->name = "Extract Palette from Vertex Color";
-  ot->idname = "GPENCIL_OT_extract_palette";
-  ot->description = "Extract all colors used in Vertex and create a Palette";
+  ot->idname = "GPENCIL_OT_extract_palette_vertex";
+  ot->description = "Extract all colors used in Grease Pencil Vertex and create a Palette";
 
   /* api callbacks */
-  ot->exec = gp_extract_palette_exec;
-  ot->poll = gp_extract_palette_poll;
+  ot->exec = gp_extract_palette_vertex_exec;
+  ot->poll = gp_extract_palette_vertex_poll;
 
   /* flags */
   ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
@@ -979,104 +895,3 @@ void GPENCIL_OT_extract_palette(wmOperatorType *ot)
       ot->srna, "selected", false, "Only Selected", "Convert only selected strokes");
   RNA_def_int(ot->srna, "threshold", 1, 1, 4, "Threshold", "", 1, 4);
 }
-
-/* Extract Palette from Image. */
-static bool gp_extract_palette_img_poll(bContext *C)
-{
-  SpaceLink *sl = CTX_wm_space_data(C);
-  if (sl->spacetype == SPACE_IMAGE) {
-    return true;
-  }
-
-  return false;
-}
-
-/* return pixel data (rgba) at index */
-static void get_pixel(const ImBuf *ibuf, const int idx, float r_col[3])
-{
-  if (ibuf->rect_float) {
-    const float *frgba = &ibuf->rect_float[idx * 4];
-    copy_v3_v3(r_col, frgba);
-  }
-  else if (ibuf->rect) {
-    float r, g, b;
-    uint *cp = &ibuf->rect[idx];
-    uint a = *cp;
-    cpack_to_rgb(a, &r, &g, &b);
-    r_col[0] = r;
-    r_col[1] = g;
-    r_col[2] = b;
-  }
-  else {
-    BLI_assert(!"gpencil_fill.c - get_pixel() non-float case is used!");
-    zero_v4(r_col);
-  }
-}
-
-static int gp_extract_palette_img_exec(bContext *C, wmOperator *op)
-{
-  const int threshold = RNA_int_get(op->ptr, "threshold");
-
-  Main *bmain = CTX_data_main(C);
-  bool done = false;
-  int totpal = 0;
-
-  SpaceImage *sima = CTX_wm_space_image(C);
-  Image *image = sima->image;
-  ImageUser iuser = sima->iuser;
-  void *lock;
-  ImBuf *ibuf;
-  GHash *color_table = BLI_ghash_int_new(__func__);
-
-  ibuf = BKE_image_acquire_ibuf(image, &iuser, &lock);
-
-  if (ibuf->rect) {
-    const int maxpixel = (ibuf->x * ibuf->y) - 1;
-
-    /* Extract all colors. */
-    for (int v = maxpixel; v != 0; v--) {
-      float col[3];
-      get_pixel(ibuf, v, col);
-
-      const float range = pow(10.0f, threshold);

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list