d32a1854
David Mayerich
added framework f...
|
1
2
|
#ifndef STIM_IMAGE_H
#define STIM_IMAGE_H
|
1cb3eb53
David Mayerich
modified image.h ...
|
3
4
5
|
#ifdef JPEG_FOUND
#define cimg_use_jpeg //necessary for JPG files
#endif
|
fbf0ab02
David Mayerich
added support for...
|
6
7
|
#include "CImg.h"
|
912d9073
David Mayerich
added vector.h to...
|
8
|
#include <vector>
|
d32a1854
David Mayerich
added framework f...
|
9
|
#include <iostream>
|
7b3948ab
David Mayerich
added support for...
|
10
|
namespace stim{
|
d32a1854
David Mayerich
added framework f...
|
11
12
13
14
15
16
17
18
|
//This static class provides the STIM interface for loading images
// Use this interface for all image management - that way the actual library can be changed without problems
//currently this interface uses CImg
// T = data type (usually unsigned char)
template <class T>
class image{
|
7b3948ab
David Mayerich
added support for...
|
19
|
cimg_library::CImg<T> img;
|
d32a1854
David Mayerich
added framework f...
|
20
21
22
|
public:
|
7b3948ab
David Mayerich
added support for...
|
23
24
25
26
27
28
29
30
31
|
//default constructor
image(){
}
//constructor (load an image file)
image(std::string filename){
img.load(filename.c_str());
}
|
41acaf5d
David Mayerich
added a CUDA Gaus...
|
32
33
34
35
36
|
/// Constructor initializes an image to a given size
image(unsigned int x, unsigned int y = 1, unsigned int z = 1){
img = cimg_library::CImg<T>(x, y, z);
}
|
d32a1854
David Mayerich
added framework f...
|
37
38
|
//Load an image from a file
void load(std::string filename){
|
7b3948ab
David Mayerich
added support for...
|
39
|
img.load(filename.c_str());
|
d32a1854
David Mayerich
added framework f...
|
40
41
42
43
|
}
//save a file
void save(std::string filename){
|
7b3948ab
David Mayerich
added support for...
|
44
|
img.save(filename.c_str());
|
d32a1854
David Mayerich
added framework f...
|
45
46
|
}
|
8b7be670
David Mayerich
implemented savin...
|
47
|
//create an image from an interleaved buffer
|
fbf0ab02
David Mayerich
added support for...
|
48
49
50
51
52
53
54
55
56
57
|
void set_interleaved(T* buffer, unsigned int width, unsigned int height, unsigned int channels = 1){
unsigned char* non_interleaved = (unsigned char*)malloc(width * height * 3);
unsigned int S = width * height;
for(unsigned int i = 0; i < S; i++){
for(unsigned int c = 0; c < channels; c++){
non_interleaved[i + c * S] = buffer[i * channels + c];
}
}
|
d32a1854
David Mayerich
added framework f...
|
58
|
|
7b3948ab
David Mayerich
added support for...
|
59
60
61
|
img = cimg_library::CImg<unsigned char>(non_interleaved, width, height, 1, channels);
}
|
32504c2c
David Mayerich
fixed some functi...
|
62
|
//fills an allocated region of memory with non-interleaved data
|
8b7be670
David Mayerich
implemented savin...
|
63
64
65
66
67
68
69
|
void data_noninterleaved(T* data){
memcpy(data, img.data(), sizeof(T) * size());
}
void data_interleaved(T* data){
unsigned int C = channels();
|
faef7718
David Mayerich
updates to stim::...
|
70
|
unsigned int X = width() * height();
|
8b7be670
David Mayerich
implemented savin...
|
71
72
73
74
75
76
77
78
79
80
|
T* ptr = img.data();
//for each channel
for(unsigned int c = 0; c < C; c++)
//convert each pixel
for(unsigned int x = 0; x < X; x++)
data[x * C + c] = ptr[c * X + x];
}
|
41acaf5d
David Mayerich
added a CUDA Gaus...
|
81
82
83
84
85
86
87
88
89
90
91
|
image<T> channel(unsigned int c){
//create a new image
image<T> single;
single.img = img.channel(c);
return single;
}
|
8b7be670
David Mayerich
implemented savin...
|
92
93
94
95
96
97
98
99
100
101
|
unsigned int channels(){
return (unsigned int)img.spectrum();
}
unsigned int width(){
return img.width();
}
unsigned int height(){
return img.height();
|
7b3948ab
David Mayerich
added support for...
|
102
|
}
|
d32a1854
David Mayerich
added framework f...
|
103
|
|
7b3948ab
David Mayerich
added support for...
|
104
105
106
|
//returns the size (number of values) of the image
unsigned long size(){
return img.size();
|
d32a1854
David Mayerich
added framework f...
|
107
108
|
}
|
faef7718
David Mayerich
updates to stim::...
|
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
|
/// Returns the number of nonzero values
unsigned int nnz(){
unsigned long P = width() * height();
unsigned long C = channels();
T* ptr = img.data();
unsigned long n = 0;
for(unsigned long p = 0; p < P; p++){
for(unsigned long c = 0; c < C; c++){
if(ptr[c * P + p] > 0){
n++;
break;
}
}
}
return n; //return the number of nonzero pixels
}
//this function returns indices of pixels that have nonzero values
std::vector<unsigned long> sparse_idx(){
std::vector<unsigned long> s; //allocate an array
s.resize(nnz()); //allocate space in the array
unsigned long P = width() * height();
unsigned long C = channels();
T* ptr = img.data(); //get a pointer to the image data
unsigned long i = 0;
for(unsigned long p = 0; p < P; p++){
for(unsigned long c = 0; c < C; c++){
if(ptr[c * P + p] > 0){
s[i] = p;
i++;
break;
}
}
}
return s; //return the index list
}
|
d32a1854
David Mayerich
added framework f...
|
158
159
160
161
162
163
|
};
|
7b3948ab
David Mayerich
added support for...
|
164
165
|
}; //end namespace stim
|
d32a1854
David Mayerich
added framework f...
|
166
167
|
#endif
|