/normxcorr/trunk

To get this branch, use:
bzr branch http://suren.me/webbzr/normxcorr/trunk

« back to all changes in this revision

Viewing changes to dict_hw/src/dict_image.cpp

  • Committer: Suren A. Chilingaryan
  • Date: 2009-12-13 02:20:05 UTC
  • Revision ID: csa@dside.dyndns.org-20091213022005-m932to8hhihwuw5r
Support for TIFF images in C code and stand-alone console application

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include <stdio.h>
 
2
#include <stdlib.h>
 
3
 
 
4
#include "normxcorr_hw.h"
 
5
#include "normxcorr_hw_msg.h"
 
6
 
 
7
#include "helpers.h"
 
8
 
 
9
#include "dict_image.h"
 
10
 
 
11
int dictImageFreeBuffer(DICTContext ctx, DICTImageType image_type, void *image_buf) {
 
12
    switch (image_type) {
 
13
#ifdef DICT_TIFF_SUPPORT
 
14
        case DICT_IMAGE_TIFF:
 
15
            _TIFFfree(image_buf);
 
16
        break;
 
17
#endif /* DICT_TIFF_SUPPORT */
 
18
    }
 
19
}
 
20
 
 
21
int dictImageFree(DICTContext ps) {
 
22
    if (ps->image_buf) dictImageFreeBuffer(ps, ps->image_type, ps->image_buf);
 
23
    if (ps->image) free(ps->image);
 
24
}
 
25
 
 
26
int dictReduceImage(DICTContext ps, void *img, int width, int height) {
 
27
    int size = width * height;
 
28
    unsigned char *res = ps->image;
 
29
 
 
30
#ifdef DICT_TIFF_SUPPORT
 
31
    unsigned char *image = (unsigned char*)img;
 
32
#endif /* DICT_TIFF_SUPPORT */
 
33
    
 
34
    switch (ps->image_type) {
 
35
#ifdef DICT_TIFF_SUPPORT
 
36
        case DICT_IMAGE_TIFF:
 
37
            if (ps->matlab_mode) {
 
38
                for (int i = 0; i < size; ++i) {
 
39
                    int col = i % height;
 
40
                    int lin = i / height;
 
41
                    int pos = 4*(col * width + lin);
 
42
                    res[i] = roundf(((double)(image[pos] + image[pos+1] + image[pos+2]))/3);
 
43
                }
 
44
            } else {
 
45
                for (int i = 0; i < size; ++i) {
 
46
                    int pos = 4 * i;
 
47
                    res[i] = roundf(((double)(image[pos] + image[pos+1] + image[pos+2]))/3);
 
48
                }
 
49
            }
 
50
        break;
 
51
#endif /* DICT_TIFF_SUPPORT */
 
52
    }
 
53
}
 
54
 
 
55
int dictImageLoadTemplateImage(DICTContext ps, const char *name) {
 
56
#ifdef DICT_TIFF_SUPPORT
 
57
    uint32_t w, h;
 
58
    
 
59
    TIFF* tif = TIFFOpen(name, "r");
 
60
    if (!tif) return DICT_ERROR_IMAGE;
 
61
    
 
62
    TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &w);
 
63
    TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &h);
 
64
    
 
65
    if (ps->image_buf) {
 
66
        dictImageFreeBuffer(ps, ps->image_type, ps->image_buf);
 
67
    }
 
68
    
 
69
    if (ps->image) {
 
70
        free(ps->image);
 
71
    }
 
72
 
 
73
    ps->image_type = DICT_IMAGE_TIFF;
 
74
    ps->image_buf = _TIFFmalloc(w * h * sizeof (uint32_t));
 
75
    ps->image = (unsigned char*)malloc(w * h * sizeof(unsigned char));
 
76
    
 
77
    ps->width = w;
 
78
    ps->height = h;
 
79
    
 
80
    if ((!ps->image)||(!ps->image_buf)) {
 
81
        reportError("Problem allocating memory for image %ix%i", w, h);
 
82
        return DICT_ERROR_MALLOC;
 
83
    }
 
84
 
 
85
    if (TIFFReadRGBAImage(tif, w, h, (uint32_t*)ps->image_buf, 0)) {
 
86
        dictReduceImage(ps, ps->image_buf, w, h);
 
87
    }
 
88
    
 
89
    TIFFClose(tif);
 
90
    return 0;
 
91
#else
 
92
    return DICT_ERROR_IMAGE;
 
93
#endif /* DICT_TIFF_SUPPORT */
 
94
}
 
95
 
 
96
int dictImageLoadImage(DICTContext ps, const char *name) {
 
97
#ifdef DICT_TIFF_SUPPORT
 
98
    TIFF* tif = TIFFOpen(name, "r");
 
99
    if (!tif) return DICT_ERROR_IMAGE;
 
100
 
 
101
    if (TIFFReadRGBAImage(tif, ps->width, ps->height, (uint32_t*)ps->image_buf, 0)) {
 
102
        dictReduceImage(ps, ps->image_buf, ps->width, ps->height);
 
103
    }
 
104
    TIFFClose(tif);
 
105
    return 0;
 
106
#else
 
107
    return DICT_ERROR_IMAGE;
 
108
#endif /* DICT_TIFF_SUPPORT */
 
109
}