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