/src/libvips/libvips/arithmetic/hough.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* hough transform |
2 | | * |
3 | | * 7/3/14 |
4 | | * - from hist_find.c |
5 | | */ |
6 | | |
7 | | /* |
8 | | |
9 | | This file is part of VIPS. |
10 | | |
11 | | VIPS is free software; you can redistribute it and/or modify |
12 | | it under the terms of the GNU Lesser General Public License as published by |
13 | | the Free Software Foundation; either version 2 of the License, or |
14 | | (at your option) any later version. |
15 | | |
16 | | This program is distributed in the hope that it will be useful, |
17 | | but WITHOUT ANY WARRANTY; without even the implied warranty of |
18 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
19 | | GNU Lesser General Public License for more details. |
20 | | |
21 | | You should have received a copy of the GNU Lesser General Public License |
22 | | along with this program; if not, write to the Free Software |
23 | | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
24 | | 02110-1301 USA |
25 | | |
26 | | */ |
27 | | |
28 | | /* |
29 | | |
30 | | These files are distributed with VIPS - http://www.vips.ecs.soton.ac.uk |
31 | | |
32 | | */ |
33 | | |
34 | | #ifdef HAVE_CONFIG_H |
35 | | #include <config.h> |
36 | | #endif /*HAVE_CONFIG_H*/ |
37 | | #include <glib/gi18n-lib.h> |
38 | | |
39 | | #include <string.h> |
40 | | |
41 | | #include <vips/vips.h> |
42 | | |
43 | | #include "statistic.h" |
44 | | #include "hough.h" |
45 | | |
46 | | G_DEFINE_ABSTRACT_TYPE(VipsHough, vips_hough, VIPS_TYPE_STATISTIC); |
47 | | |
48 | | static VipsImage * |
49 | | vips_hough_new_accumulator(VipsHough *hough) |
50 | 0 | { |
51 | 0 | VipsHoughClass *class = VIPS_HOUGH_GET_CLASS(hough); |
52 | 0 | VipsStatistic *statistic = VIPS_STATISTIC(hough); |
53 | |
|
54 | 0 | VipsImage *accumulator; |
55 | |
|
56 | 0 | accumulator = vips_image_new_memory(); |
57 | |
|
58 | 0 | if (vips_image_pipelinev(accumulator, |
59 | 0 | VIPS_DEMAND_STYLE_ANY, statistic->ready, NULL) || |
60 | 0 | class->init_accumulator(hough, accumulator) || |
61 | 0 | vips_image_write_prepare(accumulator)) { |
62 | 0 | g_object_unref(accumulator); |
63 | 0 | return NULL; |
64 | 0 | } |
65 | | |
66 | 0 | return accumulator; |
67 | 0 | } |
68 | | |
69 | | static int |
70 | | vips_hough_build(VipsObject *object) |
71 | 0 | { |
72 | 0 | VipsObjectClass *class = VIPS_OBJECT_GET_CLASS(object); |
73 | 0 | VipsStatistic *statistic = VIPS_STATISTIC(object); |
74 | 0 | VipsHough *hough = (VipsHough *) object; |
75 | |
|
76 | 0 | VipsImage *out; |
77 | | |
78 | | /* Mono only, we use the bands dimension of the output image for |
79 | | * a parameter. |
80 | | */ |
81 | 0 | if (statistic->in) |
82 | 0 | if (vips_check_mono(class->nickname, statistic->in)) |
83 | 0 | return -1; |
84 | | |
85 | 0 | if (!(out = vips_hough_new_accumulator(hough))) |
86 | 0 | return -1; |
87 | 0 | g_object_set(object, |
88 | 0 | "out", out, |
89 | 0 | NULL); |
90 | |
|
91 | 0 | if (VIPS_OBJECT_CLASS(vips_hough_parent_class)->build(object)) |
92 | 0 | return -1; |
93 | | |
94 | 0 | return 0; |
95 | 0 | } |
96 | | |
97 | | /* Build a new accumulator. |
98 | | */ |
99 | | static void * |
100 | | vips_hough_start(VipsStatistic *statistic) |
101 | 0 | { |
102 | 0 | VipsHough *hough = (VipsHough *) statistic; |
103 | |
|
104 | 0 | VipsImage *accumulator; |
105 | |
|
106 | 0 | if (!(accumulator = vips_hough_new_accumulator(hough))) |
107 | 0 | return NULL; |
108 | | |
109 | 0 | return (void *) accumulator; |
110 | 0 | } |
111 | | |
112 | | /* Add our finished accumulator to the main area. |
113 | | */ |
114 | | static int |
115 | | vips_hough_stop(VipsStatistic *statistic, void *seq) |
116 | 0 | { |
117 | 0 | VipsImage *accumulator = (VipsImage *) seq; |
118 | 0 | VipsHough *hough = (VipsHough *) statistic; |
119 | |
|
120 | 0 | if (vips_draw_image(hough->out, accumulator, 0, 0, |
121 | 0 | "mode", VIPS_COMBINE_MODE_ADD, |
122 | 0 | NULL)) { |
123 | 0 | g_object_unref(accumulator); |
124 | 0 | return -1; |
125 | 0 | } |
126 | | |
127 | 0 | g_object_unref(accumulator); |
128 | |
|
129 | 0 | return 0; |
130 | 0 | } |
131 | | |
132 | | static int |
133 | | vips_hough_scan(VipsStatistic *statistic, |
134 | | void *seq, int x, int y, void *in, int n) |
135 | 0 | { |
136 | 0 | VipsHough *hough = (VipsHough *) statistic; |
137 | 0 | VipsHoughClass *class = VIPS_HOUGH_GET_CLASS(hough); |
138 | 0 | VipsImage *accumulator = (VipsImage *) seq; |
139 | 0 | VipsPel *p = (VipsPel *) in; |
140 | |
|
141 | 0 | int i; |
142 | |
|
143 | 0 | for (i = 0; i < n; i++) |
144 | 0 | if (p[i]) |
145 | 0 | class->vote(hough, accumulator, x + i, y); |
146 | |
|
147 | 0 | return 0; |
148 | 0 | } |
149 | | |
150 | | #define UC VIPS_FORMAT_UCHAR |
151 | | |
152 | | /* Input image is cast to this format. |
153 | | */ |
154 | | static const VipsBandFormat vips_hough_format_table[10] = { |
155 | | /* Band format: UC C US S UI I F X D DX */ |
156 | | /* Promotion: */ UC, UC, UC, UC, UC, UC, UC, UC, UC, UC |
157 | | }; |
158 | | |
159 | | static void |
160 | | vips_hough_class_init(VipsHoughClass *class) |
161 | 1 | { |
162 | 1 | GObjectClass *gobject_class = (GObjectClass *) class; |
163 | 1 | VipsObjectClass *object_class = (VipsObjectClass *) class; |
164 | 1 | VipsStatisticClass *sclass = VIPS_STATISTIC_CLASS(class); |
165 | | |
166 | 1 | gobject_class->set_property = vips_object_set_property; |
167 | 1 | gobject_class->get_property = vips_object_get_property; |
168 | | |
169 | 1 | object_class->nickname = "hough"; |
170 | 1 | object_class->description = _("find hough transform"); |
171 | 1 | object_class->build = vips_hough_build; |
172 | | |
173 | 1 | sclass->start = vips_hough_start; |
174 | 1 | sclass->scan = vips_hough_scan; |
175 | 1 | sclass->stop = vips_hough_stop; |
176 | 1 | sclass->format_table = vips_hough_format_table; |
177 | | |
178 | 1 | VIPS_ARG_IMAGE(class, "out", 100, |
179 | 1 | _("Output"), |
180 | 1 | _("Output image"), |
181 | 1 | VIPS_ARGUMENT_REQUIRED_OUTPUT, |
182 | 1 | G_STRUCT_OFFSET(VipsHough, out)); |
183 | 1 | } |
184 | | |
185 | | static void |
186 | | vips_hough_init(VipsHough *hough) |
187 | 0 | { |
188 | 0 | } |