From 3e36cbb3deb317c903c32c7361bbcca9535a1aa9 Mon Sep 17 00:00:00 2001 From: Campbell Barton Date: Wed, 26 Oct 2016 20:11:09 +1100 Subject: Cleanup: move bitmap drawing into its own module Bitmap drawing is out-of-scope for a general math API, move to BLI_bitmap_draw_2d. --- source/blender/blenkernel/intern/tracking.c | 1 + source/blender/blenlib/BLI_bitmap_draw_2d.h | 37 +++++ source/blender/blenlib/BLI_math_geom.h | 5 - source/blender/blenlib/CMakeLists.txt | 2 + source/blender/blenlib/intern/bitmap_draw_2d.c | 181 ++++++++++++++++++++++ source/blender/blenlib/intern/math_geom.c | 136 ---------------- source/blender/editors/mesh/editmesh_select.c | 1 + source/blender/editors/sculpt_paint/paint_mask.c | 1 + source/blender/editors/space_view3d/view3d_edit.c | 1 + source/blender/windowmanager/intern/wm_gesture.c | 1 + 10 files changed, 225 insertions(+), 141 deletions(-) create mode 100644 source/blender/blenlib/BLI_bitmap_draw_2d.h create mode 100644 source/blender/blenlib/intern/bitmap_draw_2d.c (limited to 'source') diff --git a/source/blender/blenkernel/intern/tracking.c b/source/blender/blenkernel/intern/tracking.c index a86606f1099..29750cf2183 100644 --- a/source/blender/blenkernel/intern/tracking.c +++ b/source/blender/blenkernel/intern/tracking.c @@ -44,6 +44,7 @@ #include "DNA_scene_types.h" #include "BLI_utildefines.h" +#include "BLI_bitmap_draw_2d.h" #include "BLI_ghash.h" #include "BLI_math.h" #include "BLI_math_base.h" diff --git a/source/blender/blenlib/BLI_bitmap_draw_2d.h b/source/blender/blenlib/BLI_bitmap_draw_2d.h new file mode 100644 index 00000000000..d447c5823e2 --- /dev/null +++ b/source/blender/blenlib/BLI_bitmap_draw_2d.h @@ -0,0 +1,37 @@ +/* + * ***** BEGIN GPL LICENSE BLOCK ***** + * + * 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. + * + * ***** END GPL LICENSE BLOCK ***** + */ + +#ifndef __BLI_BITMAP_DRAW_2D_H__ +#define __BLI_BITMAP_DRAW_2D_H__ + +/** \file BLI_bitmap_draw_2d.h + * \ingroup bli + */ + +void plot_line_v2v2i( + const int p1[2], const int p2[2], + bool (*callback)(int, int, void *), void *userData); + +void fill_poly_v2i_n( + const int xmin, const int ymin, const int xmax, const int ymax, + const int polyXY[][2], const int polyCorners, + void (*callback)(int x, int x_end, int y, void *), void *userData); + +#endif /* __BLI_BITMAP_DRAW_2D_H__ */ diff --git a/source/blender/blenlib/BLI_math_geom.h b/source/blender/blenlib/BLI_math_geom.h index 84a25f533bf..514b0300274 100644 --- a/source/blender/blenlib/BLI_math_geom.h +++ b/source/blender/blenlib/BLI_math_geom.h @@ -325,11 +325,6 @@ bool clip_segment_v3_plane_n( const float p1[3], const float p2[3], const float plane_array[][4], const int plane_tot, float r_p1[3], float r_p2[3]); -void plot_line_v2v2i(const int p1[2], const int p2[2], bool (*callback)(int, int, void *), void *userData); -void fill_poly_v2i_n( - const int xmin, const int ymin, const int xmax, const int ymax, - const int polyXY[][2], const int polyCorners, - void (*callback)(int x, int x_end, int y, void *), void *userData); /****************************** Interpolation ********************************/ /* tri or quad, d can be NULL */ diff --git a/source/blender/blenlib/CMakeLists.txt b/source/blender/blenlib/CMakeLists.txt index 9978d1d19af..6e717a3ae7e 100644 --- a/source/blender/blenlib/CMakeLists.txt +++ b/source/blender/blenlib/CMakeLists.txt @@ -56,6 +56,7 @@ set(SRC intern/array_store_utils.c intern/array_utils.c intern/astar.c + intern/bitmap_draw_2d.c intern/boxpack2d.c intern/buffer.c intern/callbacks.c @@ -127,6 +128,7 @@ set(SRC BLI_array_utils.h BLI_astar.h BLI_bitmap.h + BLI_bitmap_draw_2d.h BLI_blenlib.h BLI_boxpack2d.h BLI_buffer.h diff --git a/source/blender/blenlib/intern/bitmap_draw_2d.c b/source/blender/blenlib/intern/bitmap_draw_2d.c new file mode 100644 index 00000000000..06d21855197 --- /dev/null +++ b/source/blender/blenlib/intern/bitmap_draw_2d.c @@ -0,0 +1,181 @@ +/* + * ***** BEGIN GPL LICENSE BLOCK ***** + * + * 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) 2001-2002 by NaN Holding BV. + * All rights reserved. + * + * The Original Code is: some of this file. + * + * ***** END GPL LICENSE BLOCK ***** + * */ + +/** \file blender/blenlib/intern/bitmap_draw_2d.c + * \ingroup bli + * + * Utility functions for primitive drawing operations. + */ + +#include "MEM_guardedalloc.h" + +#include "BLI_bitmap_draw_2d.h" + +#include "BLI_utildefines.h" + +#include "BLI_strict_flags.h" + +/** + * Plot a line from \a p1 to \a p2 (inclusive). + */ +void plot_line_v2v2i( + const int p1[2], const int p2[2], + bool (*callback)(int, int, void *), void *userData) +{ + /* Bresenham's line algorithm. */ + int x1 = p1[0]; + int y1 = p1[1]; + int x2 = p2[0]; + int y2 = p2[1]; + + int ix; + int iy; + + /* if x1 == x2 or y1 == y2, then it does not matter what we set here */ + int delta_x = (x2 > x1 ? ((void)(ix = 1), x2 - x1) : ((void)(ix = -1), x1 - x2)) << 1; + int delta_y = (y2 > y1 ? ((void)(iy = 1), y2 - y1) : ((void)(iy = -1), y1 - y2)) << 1; + + if (callback(x1, y1, userData) == 0) { + return; + } + + if (delta_x >= delta_y) { + /* error may go below zero */ + int error = delta_y - (delta_x >> 1); + + while (x1 != x2) { + if (error >= 0) { + if (error || (ix > 0)) { + y1 += iy; + error -= delta_x; + } + /* else do nothing */ + } + /* else do nothing */ + + x1 += ix; + error += delta_y; + + if (callback(x1, y1, userData) == 0) { + return; + } + } + } + else { + /* error may go below zero */ + int error = delta_x - (delta_y >> 1); + + while (y1 != y2) { + if (error >= 0) { + if (error || (iy > 0)) { + x1 += ix; + error -= delta_y; + } + /* else do nothing */ + } + /* else do nothing */ + + y1 += iy; + error += delta_x; + + if (callback(x1, y1, userData) == 0) { + return; + } + } + } +} + +/** + * \param callback: Takes the x, y coords and x-span (\a x_end is not inclusive), + * note that \a x_end will always be greater than \a x, so we can use: + * + * \code{.c} + * do { + * func(x, y); + * } while (++x != x_end); + * \endcode + */ +void fill_poly_v2i_n( + const int xmin, const int ymin, const int xmax, const int ymax, + const int verts[][2], const int nr, + void (*callback)(int x, int x_end, int y, void *), void *userData) +{ + /* Originally by Darel Rex Finley, 2007. + */ + + int nodes, pixel_y, i, j, swap; + int *node_x = MEM_mallocN(sizeof(*node_x) * (size_t)(nr + 1), __func__); + + /* Loop through the rows of the image. */ + for (pixel_y = ymin; pixel_y < ymax; pixel_y++) { + + /* Build a list of nodes. */ + nodes = 0; j = nr - 1; + for (i = 0; i < nr; i++) { + if ((verts[i][1] < pixel_y && verts[j][1] >= pixel_y) || + (verts[j][1] < pixel_y && verts[i][1] >= pixel_y)) + { + node_x[nodes++] = (int)(verts[i][0] + + ((double)(pixel_y - verts[i][1]) / (verts[j][1] - verts[i][1])) * + (verts[j][0] - verts[i][0])); + } + j = i; + } + + /* Sort the nodes, via a simple "Bubble" sort. */ + i = 0; + while (i < nodes - 1) { + if (node_x[i] > node_x[i + 1]) { + SWAP_TVAL(swap, node_x[i], node_x[i + 1]); + if (i) i--; + } + else { + i++; + } + } + + /* Fill the pixels between node pairs. */ + for (i = 0; i < nodes; i += 2) { + if (node_x[i] >= xmax) break; + if (node_x[i + 1] > xmin) { + if (node_x[i ] < xmin) node_x[i ] = xmin; + if (node_x[i + 1] > xmax) node_x[i + 1] = xmax; + +#if 0 + /* for many x/y calls */ + for (j = node_x[i]; j < node_x[i + 1]; j++) { + callback(j - xmin, pixel_y - ymin, userData); + } +#else + /* for single call per x-span */ + if (node_x[i] < node_x[i + 1]) { + callback(node_x[i] - xmin, node_x[i + 1] - xmin, pixel_y - ymin, userData); + } +#endif + } + } + } + MEM_freeN(node_x); +} diff --git a/source/blender/blenlib/intern/math_geom.c b/source/blender/blenlib/intern/math_geom.c index dd30f267f78..f31d0935b77 100644 --- a/source/blender/blenlib/intern/math_geom.c +++ b/source/blender/blenlib/intern/math_geom.c @@ -2885,142 +2885,6 @@ bool clip_segment_v3_plane_n( return true; } -void plot_line_v2v2i(const int p1[2], const int p2[2], bool (*callback)(int, int, void *), void *userData) -{ - int x1 = p1[0]; - int y1 = p1[1]; - int x2 = p2[0]; - int y2 = p2[1]; - - int ix; - int iy; - - /* if x1 == x2 or y1 == y2, then it does not matter what we set here */ - int delta_x = (x2 > x1 ? ((void)(ix = 1), x2 - x1) : ((void)(ix = -1), x1 - x2)) << 1; - int delta_y = (y2 > y1 ? ((void)(iy = 1), y2 - y1) : ((void)(iy = -1), y1 - y2)) << 1; - - if (callback(x1, y1, userData) == 0) { - return; - } - - if (delta_x >= delta_y) { - /* error may go below zero */ - int error = delta_y - (delta_x >> 1); - - while (x1 != x2) { - if (error >= 0) { - if (error || (ix > 0)) { - y1 += iy; - error -= delta_x; - } - /* else do nothing */ - } - /* else do nothing */ - - x1 += ix; - error += delta_y; - - if (callback(x1, y1, userData) == 0) { - return; - } - } - } - else { - /* error may go below zero */ - int error = delta_x - (delta_y >> 1); - - while (y1 != y2) { - if (error >= 0) { - if (error || (iy > 0)) { - x1 += ix; - error -= delta_y; - } - /* else do nothing */ - } - /* else do nothing */ - - y1 += iy; - error += delta_x; - - if (callback(x1, y1, userData) == 0) { - return; - } - } - } -} - -/** - * \param callback: Takes the x, y coords and x-span (\a x_end is not inclusive), - * note that \a x_end will always be greater than \a x, so we can use: - * - * \code{.c} - * do { - * func(x, y); - * } while (++x != x_end); - * \endcode - */ -void fill_poly_v2i_n( - const int xmin, const int ymin, const int xmax, const int ymax, - const int verts[][2], const int nr, - void (*callback)(int x, int x_end, int y, void *), void *userData) -{ - /* originally by Darel Rex Finley, 2007 */ - - int nodes, pixel_y, i, j, swap; - int *node_x = MEM_mallocN(sizeof(*node_x) * (size_t)(nr + 1), __func__); - - /* Loop through the rows of the image. */ - for (pixel_y = ymin; pixel_y < ymax; pixel_y++) { - - /* Build a list of nodes. */ - nodes = 0; j = nr - 1; - for (i = 0; i < nr; i++) { - if ((verts[i][1] < pixel_y && verts[j][1] >= pixel_y) || - (verts[j][1] < pixel_y && verts[i][1] >= pixel_y)) - { - node_x[nodes++] = (int)(verts[i][0] + - ((double)(pixel_y - verts[i][1]) / (verts[j][1] - verts[i][1])) * - (verts[j][0] - verts[i][0])); - } - j = i; - } - - /* Sort the nodes, via a simple "Bubble" sort. */ - i = 0; - while (i < nodes - 1) { - if (node_x[i] > node_x[i + 1]) { - SWAP_TVAL(swap, node_x[i], node_x[i + 1]); - if (i) i--; - } - else { - i++; - } - } - - /* Fill the pixels between node pairs. */ - for (i = 0; i < nodes; i += 2) { - if (node_x[i] >= xmax) break; - if (node_x[i + 1] > xmin) { - if (node_x[i ] < xmin) node_x[i ] = xmin; - if (node_x[i + 1] > xmax) node_x[i + 1] = xmax; - -#if 0 - /* for many x/y calls */ - for (j = node_x[i]; j < node_x[i + 1]; j++) { - callback(j - xmin, pixel_y - ymin, userData); - } -#else - /* for single call per x-span */ - if (node_x[i] < node_x[i + 1]) { - callback(node_x[i] - xmin, node_x[i + 1] - xmin, pixel_y - ymin, userData); - } -#endif - } - } - } - MEM_freeN(node_x); -} - /****************************** Axis Utils ********************************/ /** diff --git a/source/blender/editors/mesh/editmesh_select.c b/source/blender/editors/mesh/editmesh_select.c index 5d5731a7e16..1004b0a54b5 100644 --- a/source/blender/editors/mesh/editmesh_select.c +++ b/source/blender/editors/mesh/editmesh_select.c @@ -32,6 +32,7 @@ #include "MEM_guardedalloc.h" #include "BLI_bitmap.h" +#include "BLI_bitmap_draw_2d.h" #include "BLI_listbase.h" #include "BLI_linklist.h" #include "BLI_linklist_stack.h" diff --git a/source/blender/editors/sculpt_paint/paint_mask.c b/source/blender/editors/sculpt_paint/paint_mask.c index a47b9a0b936..5471a39cf82 100644 --- a/source/blender/editors/sculpt_paint/paint_mask.c +++ b/source/blender/editors/sculpt_paint/paint_mask.c @@ -37,6 +37,7 @@ #include "BIF_glutil.h" +#include "BLI_bitmap_draw_2d.h" #include "BLI_math_matrix.h" #include "BLI_math_geom.h" #include "BLI_utildefines.h" diff --git a/source/blender/editors/space_view3d/view3d_edit.c b/source/blender/editors/space_view3d/view3d_edit.c index 9b8ca2d26da..3121a8fd90b 100644 --- a/source/blender/editors/space_view3d/view3d_edit.c +++ b/source/blender/editors/space_view3d/view3d_edit.c @@ -41,6 +41,7 @@ #include "MEM_guardedalloc.h" +#include "BLI_bitmap_draw_2d.h" #include "BLI_blenlib.h" #include "BLI_kdopbvh.h" #include "BLI_math.h" diff --git a/source/blender/windowmanager/intern/wm_gesture.c b/source/blender/windowmanager/intern/wm_gesture.c index 1357729e898..1f30aa17f23 100644 --- a/source/blender/windowmanager/intern/wm_gesture.c +++ b/source/blender/windowmanager/intern/wm_gesture.c @@ -37,6 +37,7 @@ #include "MEM_guardedalloc.h" +#include "BLI_bitmap_draw_2d.h" #include "BLI_blenlib.h" #include "BLI_math.h" #include "BLI_utildefines.h" -- cgit v1.2.3