From 65a2016becbb165afa4fdf16742f3586f83d0f30 Mon Sep 17 00:00:00 2001 From: Matthias Vogelgesang Date: Thu, 17 Mar 2011 13:55:53 +0100 Subject: Allow resize of image dimensions --- test/control.c | 34 +++++++++++++++++++++++++++++----- 1 file changed, 29 insertions(+), 5 deletions(-) (limited to 'test') diff --git a/test/control.c b/test/control.c index 449c4ea..c220e21 100644 --- a/test/control.c +++ b/test/control.c @@ -1,10 +1,12 @@ #include #include #include +#include #include "uca.h" #include "uca-cam.h" + typedef struct { guchar *buffer, *pixels; gboolean running; @@ -12,16 +14,18 @@ typedef struct { GdkPixbuf *pixbuf; int width; int height; - int bits; + int pixel_size; struct uca_camera *cam; struct uca *u; -} ThreadData ; +} ThreadData; + typedef struct { ThreadData *thread_data; GtkTreeStore *tree_store; } ValueCellData; + enum { COLUMN_NAME = 0, COLUMN_VALUE, @@ -30,6 +34,7 @@ enum { NUM_COLUMNS }; + void convert_8bit_to_rgb(guchar *output, guchar *input, int width, int height) { for (int x = 0; x < width; x++) { @@ -56,6 +61,22 @@ void convert_16bit_to_rgb(guchar *output, guchar *input, int width, int height) } } +void reallocate_buffers(ThreadData *td, int width, int height) +{ + const size_t num_bytes = width * height * td->pixel_size; + + g_object_unref(td->pixbuf); + g_free(td->buffer); + + td->pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, FALSE, 8, width, height); + td->buffer = (guchar *) g_malloc(num_bytes); + td->width = width; + td->height = height; + td->pixels = gdk_pixbuf_get_pixels(td->pixbuf); + gtk_image_set_from_pixbuf(GTK_IMAGE(td->image), td->pixbuf); + memset(td->buffer, 0, num_bytes); +} + static gboolean delete_event(GtkWidget *widget, GdkEvent *event, gpointer data) { return FALSE; @@ -76,9 +97,9 @@ void *grab_thread(void *args) while (data->running) { cam->grab(cam, (char *) data->buffer); - if (data->bits == 8) + if (data->pixel_size == 1) convert_8bit_to_rgb(data->pixels, data->buffer, data->width, data->height); - else if (data->bits == 16) + else if (data->pixel_size == 2) convert_16bit_to_rgb(data->pixels, data->buffer, data->width, data->height); gdk_threads_enter(); @@ -129,6 +150,9 @@ static void on_valuecell_edited(GtkCellRendererText *renderer, gchar *path, gcha /* TODO: extensive value checking */ uint32_t val = (uint32_t) g_ascii_strtoull(new_text, NULL, 10); cam->set_property(cam, prop_id, &val); + if ((prop_id == UCA_PROP_WIDTH) || (prop_id == UCA_PROP_HEIGHT)) + reallocate_buffers(value_data->thread_data, cam->frame_width, cam->frame_height); + gtk_tree_store_set(value_data->tree_store, &iter, COLUMN_VALUE, new_text, -1); } } @@ -320,10 +344,10 @@ int main(int argc, char *argv[]) td.pixels = gdk_pixbuf_get_pixels(pixbuf); td.width = width; td.height = height; - td.bits = bits_per_sample; td.cam = cam; td.u = u; td.running = FALSE; + td.pixel_size = pixel_size; g_signal_connect(window, "delete-event", G_CALLBACK (delete_event), NULL); -- cgit v1.2.3