goat3dgfx
diff 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 diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/image.cc Thu Nov 14 05:27:09 2013 +0200 1.3 @@ -0,0 +1,192 @@ 1.4 +#include <string.h> 1.5 +#include "imago2.h" 1.6 +#include "image.h" 1.7 +#include "logger.h" 1.8 + 1.9 + 1.10 +static int pixel_elements(Image::Format fmt); 1.11 +static int elem_size(Image::Format fmt); 1.12 +static int pixel_size(Image::Format fmt); 1.13 + 1.14 +Image::Image() 1.15 +{ 1.16 + fmt = FMT_RGBA; 1.17 + width = height = 0; 1.18 + pixels = 0; 1.19 +} 1.20 + 1.21 +Image::~Image() 1.22 +{ 1.23 + delete [] (char*)pixels; 1.24 +} 1.25 + 1.26 +int Image::get_width() const 1.27 +{ 1.28 + return width; 1.29 +} 1.30 + 1.31 +int Image::get_height() const 1.32 +{ 1.33 + return height; 1.34 +} 1.35 + 1.36 +Image::Format Image::get_format() const 1.37 +{ 1.38 + return fmt; 1.39 +} 1.40 + 1.41 +bool Image::create(int x, int y, Format fmt) 1.42 +{ 1.43 + width = x; 1.44 + height = y; 1.45 + this->fmt = fmt; 1.46 + 1.47 + try { 1.48 + pixels = new char[x * y * pixel_size(fmt)]; 1.49 + } 1.50 + catch(...) { 1.51 + return false; 1.52 + } 1.53 + return true; 1.54 +} 1.55 + 1.56 +bool Image::set_pixels(int x, int y, void *pixels, Format fmt) 1.57 +{ 1.58 + if(!create(x, y, fmt)) { 1.59 + return false; 1.60 + } 1.61 + memcpy(this->pixels, pixels, x * y * pixel_size(fmt)); 1.62 + return true; 1.63 +} 1.64 + 1.65 +void *Image::get_pixels() const 1.66 +{ 1.67 + return pixels; 1.68 +} 1.69 + 1.70 +bool Image::load(const char *fname) 1.71 +{ 1.72 + struct img_pixmap pixmap; 1.73 + 1.74 + img_init(&pixmap); 1.75 + if(img_load(&pixmap, fname) == -1) { 1.76 + return false; 1.77 + } 1.78 + 1.79 + Format fmt; 1.80 + switch(pixmap.fmt) { 1.81 + case IMG_FMT_GREY8: 1.82 + fmt = FMT_GREY; 1.83 + break; 1.84 + case IMG_FMT_RGB24: 1.85 + fmt = FMT_RGB; 1.86 + break; 1.87 + case IMG_FMT_RGBA32: 1.88 + fmt = FMT_RGBA; 1.89 + break; 1.90 + case IMG_FMT_GREYF: 1.91 + fmt = FMT_GREY_FLOAT; 1.92 + break; 1.93 + case IMG_FMT_RGBF: 1.94 + fmt = FMT_RGB_FLOAT; 1.95 + break; 1.96 + case IMG_FMT_RGBAF: 1.97 + fmt = FMT_RGBA_FLOAT; 1.98 + break; 1.99 + default: 1.100 + img_destroy(&pixmap); 1.101 + return false; 1.102 + } 1.103 + 1.104 + if(!set_pixels(pixmap.width, pixmap.height, pixmap.pixels, fmt)) { 1.105 + img_destroy(&pixmap); 1.106 + return false; 1.107 + } 1.108 + img_destroy(&pixmap); 1.109 + return true; 1.110 +} 1.111 + 1.112 +bool Image::save(const char *fname) const 1.113 +{ 1.114 + struct img_pixmap pixmap; 1.115 + 1.116 + img_init(&pixmap); 1.117 + 1.118 + switch(fmt) { 1.119 + case FMT_GREY: 1.120 + pixmap.fmt = IMG_FMT_GREY8; 1.121 + break; 1.122 + case FMT_GREY_FLOAT: 1.123 + pixmap.fmt = IMG_FMT_GREYF; 1.124 + break; 1.125 + case FMT_RGB: 1.126 + pixmap.fmt = IMG_FMT_RGB24; 1.127 + break; 1.128 + case FMT_RGB_FLOAT: 1.129 + pixmap.fmt = IMG_FMT_RGBF; 1.130 + break; 1.131 + case FMT_RGBA: 1.132 + pixmap.fmt = IMG_FMT_RGBA32; 1.133 + break; 1.134 + case FMT_RGBA_FLOAT: 1.135 + pixmap.fmt = IMG_FMT_RGBAF; 1.136 + break; 1.137 + default: 1.138 + return false; 1.139 + } 1.140 + 1.141 + pixmap.width = width; 1.142 + pixmap.height = height; 1.143 + pixmap.pixels = pixels; 1.144 + pixmap.pixelsz = pixel_size(fmt); 1.145 + 1.146 + if(img_save(&pixmap, fname) == -1) { 1.147 + return false; 1.148 + } 1.149 + return true; 1.150 +} 1.151 + 1.152 +static int pixel_elements(Image::Format fmt) 1.153 +{ 1.154 + switch(fmt) { 1.155 + case Image::FMT_GREY: 1.156 + case Image::FMT_GREY_FLOAT: 1.157 + return 1; 1.158 + 1.159 + case Image::FMT_RGB: 1.160 + case Image::FMT_RGB_FLOAT: 1.161 + return 3; 1.162 + 1.163 + case Image::FMT_RGBA: 1.164 + case Image::FMT_RGBA_FLOAT: 1.165 + return 4; 1.166 + 1.167 + default: 1.168 + break; 1.169 + } 1.170 + return 0; 1.171 +} 1.172 + 1.173 +static int elem_size(Image::Format fmt) 1.174 +{ 1.175 + switch(fmt) { 1.176 + case Image::FMT_GREY: 1.177 + case Image::FMT_RGB: 1.178 + case Image::FMT_RGBA: 1.179 + return 1; 1.180 + 1.181 + case Image::FMT_GREY_FLOAT: 1.182 + case Image::FMT_RGB_FLOAT: 1.183 + case Image::FMT_RGBA_FLOAT: 1.184 + return sizeof(float); 1.185 + 1.186 + default: 1.187 + break; 1.188 + } 1.189 + return 0; 1.190 +} 1.191 + 1.192 +static int pixel_size(Image::Format fmt) 1.193 +{ 1.194 + return elem_size(fmt) * pixel_elements(fmt); 1.195 +}