rev |
line source |
nuclear@0
|
1 #include <string.h>
|
nuclear@0
|
2 #include "imago2.h"
|
nuclear@0
|
3 #include "image.h"
|
nuclear@0
|
4 #include "logger.h"
|
nuclear@0
|
5
|
nuclear@0
|
6
|
nuclear@0
|
7 static int pixel_elements(Image::Format fmt);
|
nuclear@0
|
8 static int elem_size(Image::Format fmt);
|
nuclear@0
|
9 static int pixel_size(Image::Format fmt);
|
nuclear@0
|
10
|
nuclear@0
|
11 Image::Image()
|
nuclear@0
|
12 {
|
nuclear@0
|
13 fmt = FMT_RGBA;
|
nuclear@0
|
14 width = height = 0;
|
nuclear@0
|
15 pixels = 0;
|
nuclear@0
|
16 }
|
nuclear@0
|
17
|
nuclear@0
|
18 Image::~Image()
|
nuclear@0
|
19 {
|
nuclear@0
|
20 delete [] (char*)pixels;
|
nuclear@0
|
21 }
|
nuclear@0
|
22
|
nuclear@0
|
23 int Image::get_width() const
|
nuclear@0
|
24 {
|
nuclear@0
|
25 return width;
|
nuclear@0
|
26 }
|
nuclear@0
|
27
|
nuclear@0
|
28 int Image::get_height() const
|
nuclear@0
|
29 {
|
nuclear@0
|
30 return height;
|
nuclear@0
|
31 }
|
nuclear@0
|
32
|
nuclear@0
|
33 Image::Format Image::get_format() const
|
nuclear@0
|
34 {
|
nuclear@0
|
35 return fmt;
|
nuclear@0
|
36 }
|
nuclear@0
|
37
|
nuclear@0
|
38 bool Image::create(int x, int y, Format fmt)
|
nuclear@0
|
39 {
|
nuclear@0
|
40 width = x;
|
nuclear@0
|
41 height = y;
|
nuclear@0
|
42 this->fmt = fmt;
|
nuclear@0
|
43
|
nuclear@0
|
44 try {
|
nuclear@0
|
45 pixels = new char[x * y * pixel_size(fmt)];
|
nuclear@0
|
46 }
|
nuclear@0
|
47 catch(...) {
|
nuclear@0
|
48 return false;
|
nuclear@0
|
49 }
|
nuclear@0
|
50 return true;
|
nuclear@0
|
51 }
|
nuclear@0
|
52
|
nuclear@0
|
53 bool Image::set_pixels(int x, int y, void *pixels, Format fmt)
|
nuclear@0
|
54 {
|
nuclear@0
|
55 if(!create(x, y, fmt)) {
|
nuclear@0
|
56 return false;
|
nuclear@0
|
57 }
|
nuclear@0
|
58 memcpy(this->pixels, pixels, x * y * pixel_size(fmt));
|
nuclear@0
|
59 return true;
|
nuclear@0
|
60 }
|
nuclear@0
|
61
|
nuclear@0
|
62 void *Image::get_pixels() const
|
nuclear@0
|
63 {
|
nuclear@0
|
64 return pixels;
|
nuclear@0
|
65 }
|
nuclear@0
|
66
|
nuclear@0
|
67 bool Image::load(const char *fname)
|
nuclear@0
|
68 {
|
nuclear@0
|
69 struct img_pixmap pixmap;
|
nuclear@0
|
70
|
nuclear@0
|
71 img_init(&pixmap);
|
nuclear@0
|
72 if(img_load(&pixmap, fname) == -1) {
|
nuclear@0
|
73 return false;
|
nuclear@0
|
74 }
|
nuclear@0
|
75
|
nuclear@0
|
76 Format fmt;
|
nuclear@0
|
77 switch(pixmap.fmt) {
|
nuclear@0
|
78 case IMG_FMT_GREY8:
|
nuclear@0
|
79 fmt = FMT_GREY;
|
nuclear@0
|
80 break;
|
nuclear@0
|
81 case IMG_FMT_RGB24:
|
nuclear@0
|
82 fmt = FMT_RGB;
|
nuclear@0
|
83 break;
|
nuclear@0
|
84 case IMG_FMT_RGBA32:
|
nuclear@0
|
85 fmt = FMT_RGBA;
|
nuclear@0
|
86 break;
|
nuclear@0
|
87 case IMG_FMT_GREYF:
|
nuclear@0
|
88 fmt = FMT_GREY_FLOAT;
|
nuclear@0
|
89 break;
|
nuclear@0
|
90 case IMG_FMT_RGBF:
|
nuclear@0
|
91 fmt = FMT_RGB_FLOAT;
|
nuclear@0
|
92 break;
|
nuclear@0
|
93 case IMG_FMT_RGBAF:
|
nuclear@0
|
94 fmt = FMT_RGBA_FLOAT;
|
nuclear@0
|
95 break;
|
nuclear@0
|
96 default:
|
nuclear@0
|
97 img_destroy(&pixmap);
|
nuclear@0
|
98 return false;
|
nuclear@0
|
99 }
|
nuclear@0
|
100
|
nuclear@0
|
101 if(!set_pixels(pixmap.width, pixmap.height, pixmap.pixels, fmt)) {
|
nuclear@0
|
102 img_destroy(&pixmap);
|
nuclear@0
|
103 return false;
|
nuclear@0
|
104 }
|
nuclear@0
|
105 img_destroy(&pixmap);
|
nuclear@0
|
106 return true;
|
nuclear@0
|
107 }
|
nuclear@0
|
108
|
nuclear@0
|
109 bool Image::save(const char *fname) const
|
nuclear@0
|
110 {
|
nuclear@0
|
111 struct img_pixmap pixmap;
|
nuclear@0
|
112
|
nuclear@0
|
113 img_init(&pixmap);
|
nuclear@0
|
114
|
nuclear@0
|
115 switch(fmt) {
|
nuclear@0
|
116 case FMT_GREY:
|
nuclear@0
|
117 pixmap.fmt = IMG_FMT_GREY8;
|
nuclear@0
|
118 break;
|
nuclear@0
|
119 case FMT_GREY_FLOAT:
|
nuclear@0
|
120 pixmap.fmt = IMG_FMT_GREYF;
|
nuclear@0
|
121 break;
|
nuclear@0
|
122 case FMT_RGB:
|
nuclear@0
|
123 pixmap.fmt = IMG_FMT_RGB24;
|
nuclear@0
|
124 break;
|
nuclear@0
|
125 case FMT_RGB_FLOAT:
|
nuclear@0
|
126 pixmap.fmt = IMG_FMT_RGBF;
|
nuclear@0
|
127 break;
|
nuclear@0
|
128 case FMT_RGBA:
|
nuclear@0
|
129 pixmap.fmt = IMG_FMT_RGBA32;
|
nuclear@0
|
130 break;
|
nuclear@0
|
131 case FMT_RGBA_FLOAT:
|
nuclear@0
|
132 pixmap.fmt = IMG_FMT_RGBAF;
|
nuclear@0
|
133 break;
|
nuclear@0
|
134 default:
|
nuclear@0
|
135 return false;
|
nuclear@0
|
136 }
|
nuclear@0
|
137
|
nuclear@0
|
138 pixmap.width = width;
|
nuclear@0
|
139 pixmap.height = height;
|
nuclear@0
|
140 pixmap.pixels = pixels;
|
nuclear@0
|
141 pixmap.pixelsz = pixel_size(fmt);
|
nuclear@0
|
142
|
nuclear@0
|
143 if(img_save(&pixmap, fname) == -1) {
|
nuclear@0
|
144 return false;
|
nuclear@0
|
145 }
|
nuclear@0
|
146 return true;
|
nuclear@0
|
147 }
|
nuclear@0
|
148
|
nuclear@0
|
149 static int pixel_elements(Image::Format fmt)
|
nuclear@0
|
150 {
|
nuclear@0
|
151 switch(fmt) {
|
nuclear@0
|
152 case Image::FMT_GREY:
|
nuclear@0
|
153 case Image::FMT_GREY_FLOAT:
|
nuclear@0
|
154 return 1;
|
nuclear@0
|
155
|
nuclear@0
|
156 case Image::FMT_RGB:
|
nuclear@0
|
157 case Image::FMT_RGB_FLOAT:
|
nuclear@0
|
158 return 3;
|
nuclear@0
|
159
|
nuclear@0
|
160 case Image::FMT_RGBA:
|
nuclear@0
|
161 case Image::FMT_RGBA_FLOAT:
|
nuclear@0
|
162 return 4;
|
nuclear@0
|
163
|
nuclear@0
|
164 default:
|
nuclear@0
|
165 break;
|
nuclear@0
|
166 }
|
nuclear@0
|
167 return 0;
|
nuclear@0
|
168 }
|
nuclear@0
|
169
|
nuclear@0
|
170 static int elem_size(Image::Format fmt)
|
nuclear@0
|
171 {
|
nuclear@0
|
172 switch(fmt) {
|
nuclear@0
|
173 case Image::FMT_GREY:
|
nuclear@0
|
174 case Image::FMT_RGB:
|
nuclear@0
|
175 case Image::FMT_RGBA:
|
nuclear@0
|
176 return 1;
|
nuclear@0
|
177
|
nuclear@0
|
178 case Image::FMT_GREY_FLOAT:
|
nuclear@0
|
179 case Image::FMT_RGB_FLOAT:
|
nuclear@0
|
180 case Image::FMT_RGBA_FLOAT:
|
nuclear@0
|
181 return sizeof(float);
|
nuclear@0
|
182
|
nuclear@0
|
183 default:
|
nuclear@0
|
184 break;
|
nuclear@0
|
185 }
|
nuclear@0
|
186 return 0;
|
nuclear@0
|
187 }
|
nuclear@0
|
188
|
nuclear@0
|
189 static int pixel_size(Image::Format fmt)
|
nuclear@0
|
190 {
|
nuclear@0
|
191 return elem_size(fmt) * pixel_elements(fmt);
|
nuclear@0
|
192 }
|