gpuray_glsl
diff src/image.cc @ 0:f234630e38ff
initial commit
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Sun, 09 Nov 2014 13:03:36 +0200 |
parents | |
children |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/image.cc Sun Nov 09 13:03:36 2014 +0200 1.3 @@ -0,0 +1,69 @@ 1.4 +#include <stdio.h> 1.5 +#include <string.h> 1.6 +#include <errno.h> 1.7 +#include "image.h" 1.8 +#include "material.h" 1.9 +#include <imago2.h> 1.10 + 1.11 +Image::Image() 1.12 +{ 1.13 + pixels = 0; 1.14 + xsz = ysz = 0; 1.15 +} 1.16 + 1.17 +Image::Image(int xsz, int ysz) 1.18 +{ 1.19 + pixels = 0; 1.20 + set_pixels(xsz, ysz, 0); 1.21 +} 1.22 + 1.23 +Image::~Image() 1.24 +{ 1.25 + delete [] pixels; 1.26 +} 1.27 + 1.28 +void Image::set_pixels(int xsz, int ysz, const Color *pix) 1.29 +{ 1.30 + delete [] pixels; 1.31 + 1.32 + pixels = new Color[xsz * ysz]; 1.33 + this->xsz = xsz; 1.34 + this->ysz = ysz; 1.35 + 1.36 + if(pix) { 1.37 + memcpy(pixels, pix, xsz * ysz * sizeof *pixels); 1.38 + } 1.39 +} 1.40 + 1.41 +bool Image::load(const char *fname) 1.42 +{ 1.43 + int xsz, ysz; 1.44 + Color *img = (Color*)img_load_pixels(fname, &xsz, &ysz, IMG_FMT_RGBF); 1.45 + if(!img) { 1.46 + return false; 1.47 + } 1.48 + 1.49 + /* convert the pixels to linear color space */ 1.50 + for(int i=0; i<xsz*ysz; i++) { 1.51 + img[i].x = pow(img[i].x, 2.2); 1.52 + img[i].y = pow(img[i].y, 2.2); 1.53 + img[i].z = pow(img[i].z, 2.2); 1.54 + } 1.55 + 1.56 + set_pixels(xsz, ysz, img); 1.57 + img_free_pixels(img); 1.58 + return true; 1.59 +} 1.60 + 1.61 + 1.62 +bool Image::save(const char *fname) const 1.63 +{ 1.64 + if(!pixels) { 1.65 + return false; 1.66 + } 1.67 + 1.68 + if(img_save_pixels(fname, pixels, xsz, ysz, IMG_FMT_RGBAF) == -1) { 1.69 + return false; 1.70 + } 1.71 + return true; 1.72 +}