summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBenjamin Franzke <benjaminfranzke@googlemail.com>2012-03-31 16:32:10 +0200
committerBenjamin Franzke <benjaminfranzke@googlemail.com>2012-03-31 16:33:46 +0200
commit9b65046df7f53e477f0b6ba3bf61a069178061d4 (patch)
treee306cf17562c2ef7a390d2fb79b853f721591c12
parent969f9a2be5e7c8abfb362d89fedb6b1f3e768155 (diff)
downloadcv-9b65046df7f53e477f0b6ba3bf61a069178061d4.tar.gz
cv-9b65046df7f53e477f0b6ba3bf61a069178061d4.tar.bz2
cv-9b65046df7f53e477f0b6ba3bf61a069178061d4.zip
roi: Move rubberband bounds check into do_roi
-rw-r--r--roi.c61
1 files changed, 31 insertions, 30 deletions
diff --git a/roi.c b/roi.c
index 173832c..9192472 100644
--- a/roi.c
+++ b/roi.c
@@ -16,18 +16,28 @@ struct roi {
GdkPixbuf *input, *modified_input;
- cairo_rectangle_t area;
-
gboolean doing_rubberband;
struct {
double x1, y1, x2, y2;
} rubberband;
};
-static void
+static inline void
+calc_rubberband_rect(struct roi *roi,
+ double *x, double *y,
+ double *width, double *height)
+{
+ *x = MIN(roi->rubberband.x1, roi->rubberband.x2);
+ *y = MIN(roi->rubberband.y1, roi->rubberband.y2);
+ *width = ABS(roi->rubberband.x1 - roi->rubberband.x2);
+ *height = ABS(roi->rubberband.y1 - roi->rubberband.y2);
+}
+
+static gboolean
do_roi(struct roi *roi)
{
GdkPixbuf *match;
+ cairo_rectangle_t area;
int x, y, i;
int width, height;
gint stride, nch;
@@ -39,18 +49,23 @@ do_roi(struct roi *roi)
gint min, max;
} interval[3];
+ calc_rubberband_rect(roi, &area.x, &area.y, &area.width, &area.height);
+
+ if (area.width < 1 || area.height < 1)
+ return FALSE;
+
match = gdk_pixbuf_new_subpixbuf(roi->input,
- roi->area.x,
- roi->area.y,
- roi->area.width,
- roi->area.height);
+ area.x,
+ area.y,
+ area.width,
+ area.height);
nch = gdk_pixbuf_get_n_channels(match);
stride = gdk_pixbuf_get_rowstride(match);
- s = roi->area.width * roi->area.height;
+ s = area.width * area.height;
for (y = 0, row = gdk_pixbuf_get_pixels(match);
- y < roi->area.height; ++y, row += stride) {
- for (x = 0, pix = row; x < roi->area.width; ++x, pix += nch) {
+ y < area.height; ++y, row += stride) {
+ for (x = 0, pix = row; x < area.width; ++x, pix += nch) {
for (i = 0; i < 3; ++i)
middle[i] += pix[i];
}
@@ -59,8 +74,8 @@ do_roi(struct roi *roi)
middle[i] /= s;
for (y = 0, row = gdk_pixbuf_get_pixels(match);
- y < roi->area.height; ++y, row += stride) {
- for (x = 0, pix = row; x < roi->area.width; ++x, pix += nch) {
+ y < area.height; ++y, row += stride) {
+ for (x = 0, pix = row; x < area.width; ++x, pix += nch) {
for (i = 0; i < 3; ++i)
sigma[i] +=
(pix[i] - middle[i]) *
@@ -105,6 +120,8 @@ do_roi(struct roi *roi)
}
g_object_unref(match);
+
+ return TRUE;
}
static gboolean
@@ -140,17 +157,6 @@ button_press_event(GtkWidget *widget, GdkEventButton *event, gpointer userdata)
}
}
-static inline void
-calc_rubberband_rect(struct roi *roi,
- double *x, double *y,
- double *width, double *height)
-{
- *x = MIN(roi->rubberband.x1, roi->rubberband.x2);
- *y = MIN(roi->rubberband.y1, roi->rubberband.y2);
- *width = ABS(roi->rubberband.x1 - roi->rubberband.x2);
- *height = ABS(roi->rubberband.y1 - roi->rubberband.y2);
-}
-
static gboolean
button_release_event(GtkWidget *widget, GdkEventButton *event, gpointer userdata)
{
@@ -159,15 +165,10 @@ button_release_event(GtkWidget *widget, GdkEventButton *event, gpointer userdata
if (event->button != GDK_BUTTON_PRIMARY)
return TRUE;
- calc_rubberband_rect(roi, &roi->area.x, &roi->area.y,
- &roi->area.width, &roi->area.height);
-
- if (roi->area.width > 0 && roi->area.height > 0)
- do_roi(roi);
-
roi->doing_rubberband = FALSE;
- gtk_widget_queue_draw(roi->drawing_area);
+ if (do_roi(roi))
+ gtk_widget_queue_draw(roi->drawing_area);
return FALSE;
}