gpuray_glsl

view 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 source
1 #include <stdio.h>
2 #include <string.h>
3 #include <errno.h>
4 #include "image.h"
5 #include "material.h"
6 #include <imago2.h>
8 Image::Image()
9 {
10 pixels = 0;
11 xsz = ysz = 0;
12 }
14 Image::Image(int xsz, int ysz)
15 {
16 pixels = 0;
17 set_pixels(xsz, ysz, 0);
18 }
20 Image::~Image()
21 {
22 delete [] pixels;
23 }
25 void Image::set_pixels(int xsz, int ysz, const Color *pix)
26 {
27 delete [] pixels;
29 pixels = new Color[xsz * ysz];
30 this->xsz = xsz;
31 this->ysz = ysz;
33 if(pix) {
34 memcpy(pixels, pix, xsz * ysz * sizeof *pixels);
35 }
36 }
38 bool Image::load(const char *fname)
39 {
40 int xsz, ysz;
41 Color *img = (Color*)img_load_pixels(fname, &xsz, &ysz, IMG_FMT_RGBF);
42 if(!img) {
43 return false;
44 }
46 /* convert the pixels to linear color space */
47 for(int i=0; i<xsz*ysz; i++) {
48 img[i].x = pow(img[i].x, 2.2);
49 img[i].y = pow(img[i].y, 2.2);
50 img[i].z = pow(img[i].z, 2.2);
51 }
53 set_pixels(xsz, ysz, img);
54 img_free_pixels(img);
55 return true;
56 }
59 bool Image::save(const char *fname) const
60 {
61 if(!pixels) {
62 return false;
63 }
65 if(img_save_pixels(fname, pixels, xsz, ysz, IMG_FMT_RGBAF) == -1) {
66 return false;
67 }
68 return true;
69 }