image.h
2.32 KB
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
110
111
112
113
114
115
116
117
#ifndef STIM_IMAGE_H
#define STIM_IMAGE_H
#ifdef JPEG_FOUND
#define cimg_use_jpeg //necessary for JPG files
#endif
#include "CImg.h"
#include <iostream>
namespace stim{
//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{
cimg_library::CImg<T> img;
public:
//default constructor
image(){
}
//constructor (load an image file)
image(std::string filename){
img.load(filename.c_str());
}
/// 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);
}
//Load an image from a file
void load(std::string filename){
img.load(filename.c_str());
}
//save a file
void save(std::string filename){
img.save(filename.c_str());
}
//create an image from an interleaved buffer
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];
}
}
img = cimg_library::CImg<unsigned char>(non_interleaved, width, height, 1, channels);
}
//fills an allocated region of memory with non-interleaved data
void data_noninterleaved(T* data){
memcpy(data, img.data(), sizeof(T) * size());
}
void data_interleaved(T* data){
unsigned int C = channels();
unsigned int X = size();
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];
}
image<T> channel(unsigned int c){
//create a new image
image<T> single;
single.img = img.channel(c);
return single;
}
unsigned int channels(){
return (unsigned int)img.spectrum();
}
unsigned int width(){
return img.width();
}
unsigned int height(){
return img.height();
}
//returns the size (number of values) of the image
unsigned long size(){
return img.size();
}
};
}; //end namespace stim
#endif