aboutsummaryrefslogtreecommitdiff
path: root/src/objects.c
diff options
context:
space:
mode:
authorThomas White <taw@bitwiz.org.uk>2011-10-13 23:31:15 +0200
committerThomas White <taw@bitwiz.org.uk>2011-10-13 23:31:15 +0200
commit2e0f09d78f28c576d9a10dfcd1eeaae81e3baa07 (patch)
tree9be7b14eefb4451ac6c2a2dccce24625dc3bf7af /src/objects.c
parent7c54f36c7a5352dfd34322397ed322bb377b1abb (diff)
Add image handling basics
Diffstat (limited to 'src/objects.c')
-rw-r--r--src/objects.c86
1 files changed, 74 insertions, 12 deletions
diff --git a/src/objects.c b/src/objects.c
index 4a1b3db..e926dcc 100644
--- a/src/objects.c
+++ b/src/objects.c
@@ -28,31 +28,93 @@
#include <stdlib.h>
#include <string.h>
#include <assert.h>
+#include <gdk-pixbuf/gdk-pixbuf.h>
#include "presentation.h"
#include "objects.h"
#include "mainwindow.h"
-struct object *add_image_object(struct slide *s, double x, double y,
- const char *filename,
- double width, double height)
+struct image_store
{
- struct object *new;
+ int n_images;
+ struct image *images;
+};
- new = NULL; /* FIXME! Generally */
- new->x = x; new->y = y;
- new->bb_width = width;
- new->bb_height = height;
+static struct image *add_image_to_store(struct image_store *is, char *filename)
+{
+ struct image *images_new;
+ int idx;
+ GError *error = NULL;
+ int w, h;
+
+ images_new = realloc(is->images, (is->n_images+1)*sizeof(struct image));
+ if ( images_new == NULL ) {
+ fprintf(stderr, "Couldn't allocate memory for image.\n");
+ return NULL;
+ }
+ is->images = images_new;
+ idx = is->n_images++;
+
+ gdk_pixbuf_get_file_info(filename, &w, &h);
- if ( add_object_to_slide(s, new) ) {
- delete_object(new);
+ /* FIXME: If image is huge, load a smaller version */
+ is->images[idx].pb = gdk_pixbuf_new_from_file(filename, &error);
+ if ( is->images[idx].pb == NULL ) {
+ fprintf(stderr, "Failed to load image '%s'\n", filename);
+ is->n_images--;
return NULL;
}
- s->object_seq++;
+ is->images[idx].filename = filename;
+ is->images[idx].refcount = 1;
+ is->images[idx].width = w;
+ is->images[idx].height = h;
+
+ return &is->images[idx];
+}
+
+
+static struct image *find_filename(struct image_store *is, const char *filename)
+{
+ int i;
+
+ for ( i=0; i<is->n_images; i++ ) {
+ if ( strcmp(is->images[i].filename, filename) == 0 ) {
+ return &is->images[i];
+ }
+ }
+
+ return NULL;
+}
+
+
+struct image *get_image(struct image_store *is, char *filename)
+{
+ struct image *image;
+
+ image = find_filename(is, filename);
+ if ( image == NULL ) {
+ image = add_image_to_store(is, filename);
+ } else {
+ image->refcount++;
+ }
+
+ return image;
+}
+
+
+struct image_store *image_store_new()
+{
+ struct image_store *is;
+
+ is = calloc(1, sizeof(*is));
+ if ( is == NULL ) return NULL;
+
+ is->images = NULL;
+ is->n_images = 0;
- return new;
+ return is;
}