From 9c7602733b9f64ee5e3ced0a5c081bfb854d8c3d Mon Sep 17 00:00:00 2001 From: Benjamin Franzke Date: Thu, 5 Apr 2012 10:00:00 +0200 Subject: capture: Add keybindings to stop/continue capturing --- capture.c | 118 ++++++++++++++++++++++++++++++++++++-------------------------- 1 file changed, 68 insertions(+), 50 deletions(-) diff --git a/capture.c b/capture.c index 8576e20..49d360b 100644 --- a/capture.c +++ b/capture.c @@ -25,8 +25,59 @@ struct ct { struct { double x1, y1, x2, y2; } rubberband; + + int idle_source; }; +static gboolean +capture(gpointer userdata) +{ + struct ct *ct = userdata; + IplImage *image; + guchar *row1, *row2, *p1, *p2; + int x, y; + + image = cvQueryFrame(ct->capture); + if (!image) { + fprintf(stderr, "ERROR: frame is null...\n"); + exit(EXIT_FAILURE); + } + + if (!ct->surface || + cairo_image_surface_get_width(ct->surface) != image->width || + cairo_image_surface_get_height(ct->surface) != image->height) { + if (ct->surface) + cairo_surface_destroy(ct->surface); + ct->surface = + cairo_image_surface_create(CAIRO_FORMAT_RGB24, + image->width, image->height); + } + + row1 = (guchar *) image->imageData; + row2 = cairo_image_surface_get_data(ct->surface); + for (y = 0; y < image->height; ++y) { + p1 = row1; + p2 = row2; + + for (x = 0; x < image->width; ++x) { + /* + p2[0] = p1[0]; + p2[1] = p1[1]; + p2[2] = p1[2]; + p2[3] = 0; + */ + memcpy(p2, p1, 3); + + p1 += image->nChannels; + p2 += 4; + } + row1 += image->widthStep; + row2 += cairo_image_surface_get_stride(ct->surface); + } + gtk_widget_queue_draw(ct->drawing_area); + return TRUE; +} + static inline void calc_rubberband_rect(struct ct *ct, double *x, double *y, @@ -87,9 +138,23 @@ button_release_event(GtkWidget *widget, GdkEventButton *event, gpointer userdata static gboolean key_event(GtkWidget *widget, GdkEventKey *event, gpointer userdata) { + struct ct *ct = userdata; + switch (event->type) { case GDK_KEY_PRESS: switch (event->keyval) { + case GDK_KEY_s: + case GDK_KEY_S: + if (ct->idle_source) { + g_source_remove(ct->idle_source); + ct->idle_source = 0; + } + break; + case GDK_KEY_c: + case GDK_KEY_C: + if (!ct->idle_source) + ct->idle_source = g_idle_add(capture, ct); + break; case GDK_KEY_q: case GDK_KEY_Q: gtk_main_quit(); @@ -148,55 +213,6 @@ draw_cb(GtkWidget *widget, cairo_t *cr, gpointer userdata) return FALSE; } -static gboolean -capture(gpointer userdata) -{ - struct ct *ct = userdata; - IplImage *image; - guchar *row1, *row2, *p1, *p2; - int x, y; - - image = cvQueryFrame(ct->capture); - if (!image) { - fprintf(stderr, "ERROR: frame is null...\n"); - exit(EXIT_FAILURE); - } - - if (!ct->surface || - cairo_image_surface_get_width(ct->surface) != image->width || - cairo_image_surface_get_height(ct->surface) != image->height) { - if (ct->surface) - cairo_surface_destroy(ct->surface); - ct->surface = - cairo_image_surface_create(CAIRO_FORMAT_RGB24, - image->width, image->height); - } - - row1 = (guchar *) image->imageData; - row2 = cairo_image_surface_get_data(ct->surface); - for (y = 0; y < image->height; ++y) { - p1 = row1; - p2 = row2; - - for (x = 0; x < image->width; ++x) { - /* - p2[0] = p1[0]; - p2[1] = p1[1]; - p2[2] = p1[2]; - p2[3] = 0; - */ - memcpy(p2, p1, 3); - - p1 += image->nChannels; - p2 += 4; - } - row1 += image->widthStep; - row2 += cairo_image_surface_get_stride(ct->surface); - } - gtk_widget_queue_draw(ct->drawing_area); - return TRUE; -} - int main(int argc, char *argv[]) { @@ -251,10 +267,12 @@ main(int argc, char *argv[]) g_signal_connect(ct.drawing_area, "motion-notify-event", G_CALLBACK(motion_notify_event), &ct); - g_idle_add(capture, &ct); + ct.idle_source = g_idle_add(capture, &ct); gtk_widget_show_all(ct.window); gtk_main(); + if (ct.idle_source) + g_source_remove(ct.idle_source); cvReleaseCapture(&ct.capture); return 0; -- cgit