aboutsummaryrefslogtreecommitdiff
path: root/src/cache.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/cache.c')
-rw-r--r--src/cache.c211
1 files changed, 138 insertions, 73 deletions
diff --git a/src/cache.c b/src/cache.c
index 8c328b3..8f05e05 100644
--- a/src/cache.c
+++ b/src/cache.c
@@ -1,7 +1,7 @@
/*
* cache.c
*
- * Save the reflection datablock to save having to recalculate it
+ * Save a list of features from images to save recalculation
*
* (c) 2007 Gordon Ball <gfb21@cam.ac.uk>
* Thomas White <taw27@cam.ac.uk>
@@ -18,103 +18,168 @@
#include <stdio.h>
#include <stdint.h>
#include <string.h>
+#include <assert.h>
-#include "reflections.h"
-#include "cache.h"
+#include "image.h"
-typedef struct struct_cacheheader {
- char top[16];
- int count;
- double scale;
+typedef struct {
+ char top[16];
+ uint32_t n_images;
} CacheHeader;
-ReflectionList *cache_load(const char *filename) {
- FILE *f;
- CacheHeader ch;
- ReflectionList *reflectionlist;
- size_t cachedreflection_size;
- int i;
-
- cachedreflection_size = sizeof(Reflection) - sizeof(Reflection *);
+int cache_save(ImageList *images, char *cache_filename) {
- reflectionlist = reflectionlist_new();
- f = fopen(filename, "rb");
- if ( !f ) {
- fprintf(stderr, "Couldn't open cache file\n");
- }
- if ( fread(&ch, sizeof(CacheHeader), 1, f) == 0 ) {
- fprintf(stderr, "Couldn't read cache header\n");
- fclose(f);
- return NULL;
+ FILE *fh;
+ CacheHeader ch;
+ uint32_t *index;
+ size_t index_size;
+ int i;
+
+ fh = fopen(cache_filename, "wb");
+ if ( !fh ) {
+ fprintf(stderr, "Couldn't open feature cache file.\n");
+ return -1;
}
- for ( i=0; i<ch.count; i++ ) {
+ memcpy(&ch.top, "DTR-1.0.6\0\0\0\0\0\0\0", 16);
+ ch.n_images = images->n_images;
+ fwrite(&ch, sizeof(CacheHeader), 1, fh);
- Reflection *cr;
+ index_size = images->n_images * 2 * sizeof(uint32_t);
+ index = malloc(index_size);
+ /* Dummy write to reserve space in the file */
+ memset(index, 0, index_size);
+ if ( fwrite(index, index_size, 1, fh) != 1 ) {
+ fprintf(stderr, "Couldn't perform dummy index write.\n");
+ fclose(fh);
+ return -1;
+ }
+
+ for ( i=0; i<images->n_images; i++ ) {
+
+ uint32_t size;
+
+ size = images->images[i].features->n_features * sizeof(ImageFeature);
+ index[2*i] = ftell(fh);
+ index[2*i + 1] = images->images[i].features->n_features;
- cr = malloc(sizeof(Reflection));
- if ( fread(cr, cachedreflection_size, 1, f) == 0 ) {
- fprintf(stderr, "Couldn't read reflections from cache\n");
- fclose(f);
- free(cr);
- reflectionlist_clear(reflectionlist);
- return NULL;
+ if ( fwrite(images->images[i].features->features, size, 1, fh) != 1 ) {
+ fprintf(stderr, "Couldn't write feature list for image %i.\n", i);
+ fclose(fh);
+ return -1;
}
-
- cr->next = NULL; /* Guarantee swift failure in the event of a screw-up */
- //printf("reading (%f,%f,%f) i=%f (%d,%d,%d) %d\n",cr->x,cr->y,cr->z,cr->intensity,cr->h,cr->k,cr->l,cr->type);
- reflection_add_from_reflection(reflectionlist, cr);
}
- fclose(f);
+ /* Actual write */
+ if ( fseek(fh, sizeof(CacheHeader), SEEK_SET) ) {
+ fprintf(stderr, "Couldn't seek for index write.\n");
+ fclose(fh);
+ return -1;
+ }
+ if ( fwrite(index, index_size, 1, fh) != 1 ) {
+ fprintf(stderr, "Couldn't write index write.\n");
+ fclose(fh);
+ return -1;
+ }
+
+ fclose(fh);
+
+ return 0;
- return reflectionlist;
}
-int cache_save(ReflectionList *reflectionlist, char *cache_filename) {
+int cache_load(ImageList *images, const char *filename) {
- FILE *f;
- CacheHeader ch;
- Reflection *r;
- int count;
- const char top[16] = "DTRCACHE\0\0\0\0\0\0\0\0";
- size_t cachedreflection_size;
-
- printf("Caching reflections to %s\n", cache_filename);
-
- cachedreflection_size = sizeof(Reflection) - sizeof(Reflection *);
-
- count = 0;
- r = reflectionlist->reflections;
- while ( r != NULL ) {
- count++;
- r = r->next;
- };
-
- f = fopen(cache_filename, "wb");
-
- if ( f == NULL ) {
- printf("Couldn't save reflection cache\n");
+ FILE *fh;
+ CacheHeader ch;
+ uint32_t *index;
+ size_t index_size;
+ int i;
+
+ fh = fopen(filename, "rb");
+ if ( !fh ) {
+ fprintf(stderr, "Couldn't open cache file.\n");
return -1;
}
- memcpy(&ch.top, &top, sizeof(top));
- ch.count = count;
- ch.scale = 0.; //temp, currently doesn't do anything
- fwrite(&ch, sizeof(CacheHeader), 1, f);
- r = reflectionlist->reflections;
- while ( r != NULL ) {
+ if ( fread(&ch, sizeof(CacheHeader), 1, fh) != 1 ) {
+ fprintf(stderr, "Couldn't read cache header.\n");
+ fclose(fh);
+ return -1;
+ }
+
+ /* Format check */
+ if ( strncmp(ch.top, "DTR-1.0.6\0", 8) != 0 ) {
+ fprintf(stderr, "Can't read this cache format.\n");
+ fclose(fh);
+ return -1;
+ }
+
+ /* Muppet check */
+ if ( ch.n_images != images->n_images ) {
+ fprintf(stderr, "Number of images in cache doesn't match.\n");
+ fclose(fh);
+ return -1;
+ }
+
+ index_size = images->n_images * 2 * sizeof(uint32_t);
+ index = malloc(index_size);
+ if ( fread(index, index_size, 1, fh) != 1 ) {
+ fprintf(stderr, "Couldn't read index.\n");
+ fclose(fh);
+ return -1;
+ }
+
+ for ( i=0; i<images->n_images; i++ ) {
+
+ uint32_t size;
+ uint32_t offs;
+ int j;
- fwrite(r, cachedreflection_size, 1, f); /* Write the reflection block, stopping just short of the "next" pointer */
- r = r->next;
+ offs = index[2*i];
+ size = index[2*i + 1];
- };
+ if ( fseek(fh, offs, SEEK_SET) ) {
+ fprintf(stderr, "Couldn't seek to feature list for image %i.\n", i);
+ fclose(fh);
+ return -1;
+ }
+
+ if ( images->images[i].features ) image_feature_list_free(images->images[i].features);
+ images->images[i].features = image_feature_list_new();
+ if ( !images->images[i].features ) {
+ fprintf(stderr, "Couldn't allocate feature list for image %i.\n", i);
+ fclose(fh);
+ return -1;
+ }
+
+ assert(images->images[i].features->features == NULL);
+ images->images[i].features->features = malloc(size*sizeof(ImageFeature));
+ if ( !images->images[i].features->features ) {
+ fprintf(stderr, "Couldn't allocate feature list block for image %i.\n", i);
+ fclose(fh);
+ return -1;
+ }
+
+ if ( fread(images->images[i].features->features, size*sizeof(ImageFeature), 1, fh) != 1 ) {
+ fprintf(stderr, "Couldn't read feature list for image %i.\n", i);
+ fclose(fh);
+ return -1;
+ }
+
+ images->images[i].features->n_features = size;
+
+ /* Set "parent" fields for all the features */
+ for ( j=0; j<images->images[i].features->n_features; j++ ) {
+ images->images[i].features->features[j].parent = &images->images[i];
+ }
+
+ }
- fclose(f);
+ fclose(fh);
return 0;
-
}