/normxcorr/trunk

To get this branch, use:
bzr branch http://suren.me/webbzr/normxcorr/trunk
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#include <stdio.h>
#include <stdlib.h>

#include "normxcorr_hw.h"
#include "normxcorr_hw_msg.h"

#include "helpers.h"

#include "dict_image.h"

int dictImageFreeBuffer(DICTContext ctx, DICTImageType image_type, void *image_buf) {
    switch (image_type) {
#ifdef DICT_TIFF_SUPPORT
        case DICT_IMAGE_TIFF:
	    _TIFFfree(image_buf);
	break;
#endif /* DICT_TIFF_SUPPORT */
    }
}

int dictImageFree(DICTContext ps) {
    if (ps->image_buf) dictImageFreeBuffer(ps, ps->image_type, ps->image_buf);
    if (ps->image) free(ps->image);
}

int dictReduceImage(DICTContext ps, void *img, int width, int height) {
    int size = width * height;
    unsigned char *res = ps->image;

#ifdef DICT_TIFF_SUPPORT
    unsigned char *image = (unsigned char*)img;
#endif /* DICT_TIFF_SUPPORT */
    
    switch (ps->image_type) {
#ifdef DICT_TIFF_SUPPORT
        case DICT_IMAGE_TIFF:
	    if (ps->matlab_mode) {
		for (int i = 0; i < size; ++i) {
		    int col = i % height;
		    int lin = i / height;
		    int pos = 4*(col * width + lin);
		    res[i] = roundf(((double)(image[pos] + image[pos+1] + image[pos+2]))/3);
		}
	    } else {
	        for (int i = 0; i < size; ++i) {
		    int pos = 4 * i;
		    res[i] = roundf(((double)(image[pos] + image[pos+1] + image[pos+2]))/3);
		}
	    }
	break;
#endif /* DICT_TIFF_SUPPORT */
    }
}

int dictImageLoadTemplateImage(DICTContext ps, const char *name) {
#ifdef DICT_TIFF_SUPPORT
    uint32_t w, h;
    
    TIFF* tif = TIFFOpen(name, "r");
    if (!tif) return DICT_ERROR_IMAGE;
    
    TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &w);
    TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &h);
    
    if (ps->image_buf) {
	dictImageFreeBuffer(ps, ps->image_type, ps->image_buf);
    }
    
    if (ps->image) {
	free(ps->image);
    }

    ps->image_type = DICT_IMAGE_TIFF;
    ps->image_buf = _TIFFmalloc(w * h * sizeof (uint32_t));
    ps->image = (unsigned char*)malloc(w * h * sizeof(unsigned char));
    
    ps->width = w;
    ps->height = h;
    
    if ((!ps->image)||(!ps->image_buf)) {
	reportError("Problem allocating memory for image %ix%i", w, h);
	return DICT_ERROR_MALLOC;
    }

    if (TIFFReadRGBAImage(tif, w, h, (uint32_t*)ps->image_buf, 0)) {
	dictReduceImage(ps, ps->image_buf, w, h);
    }
    
    TIFFClose(tif);
    return 0;
#else
    return DICT_ERROR_IMAGE;
#endif /* DICT_TIFF_SUPPORT */
}

int dictImageLoadImage(DICTContext ps, const char *name) {
#ifdef DICT_TIFF_SUPPORT
    TIFF* tif = TIFFOpen(name, "r");
    if (!tif) return DICT_ERROR_IMAGE;

    if (TIFFReadRGBAImage(tif, ps->width, ps->height, (uint32_t*)ps->image_buf, 0)) {
	dictReduceImage(ps, ps->image_buf, ps->width, ps->height);
    }
    TIFFClose(tif);
    return 0;
#else
    return DICT_ERROR_IMAGE;
#endif /* DICT_TIFF_SUPPORT */
}