20 Image::Image(const Image &image)
24 is_float = image.is_float;
27 if(!(pixels = malloc(w * h * psz))) {
28 fprintf(stderr, "Failed to allocate pixels.\n");
31 memcpy(pixels, image.pixels, w * h * psz);
34 Image &Image::operator =(const Image &image)
44 is_float = image.is_float;
46 if(!(pixels = malloc(w * h * psz))) {
47 fprintf(stderr, "Failed to allocate pixels.\n");
50 memcpy(pixels, image.pixels, w * h * psz);
55 Image::Image(Image &&image)
60 is_float = image.is_float;
62 pixels = image.pixels;
66 Image &Image::operator =(Image &&image)
76 is_float = image.is_float;
78 pixels = image.pixels;
84 bool Image::load(const char *fname)
91 if(img_load(&ipm, fname) == -1) {
92 fprintf(stderr, "Failed to load image: %s.\n", fname);
98 printf("Successfully loaded image: %s\n", fname);
100 img_fmt format = img_is_float(&ipm) ? IMG_FMT_RGBAF : IMG_FMT_RGBA32;
101 if(img_convert(&ipm, format) == -1) {
102 fprintf(stderr, "Failed to convert image %s.\n", fname);
111 is_float = img_is_float(&ipm) ? true : false;
113 if(!(pixels = malloc(w * h * psz))) {
114 fprintf(stderr, "Failed to allocate pixels for image: %s.\n", fname);
118 memcpy(pixels, ipm.pixels, w * h * psz);
124 Vec4 Image::get_pixel(int x, int y) const
127 return ((Vec4 *)pixels)[y * w + x];
131 unsigned char *pptr = ((unsigned char *)pixels) + (y * w + x) * 4;
132 color.x = pptr[0] / 255.0;
133 color.y = pptr[1] / 255.0;
134 color.z = pptr[2] / 255.0;
135 color.w = pptr[3] / 255.0;
140 Vec4 Image::lookup_nearest(float u, float v) const
142 int x = (int)(u * w) % w;
143 int y = (int)(v * h) % h;
145 return get_pixel(x, y);
148 Vec4 Image::lookup_linear(float u, float v, float du, float dv) const
151 int x0 = (int)(u * w) % w;
152 int y0 = (int)(v * h) % h;
153 int x1 = (x0 + 1) % w;
154 int y1 = (y0 + 1) % h;
156 /* uv coordinates at the img corners */
157 float u0 = (float)x0 / (float)w;
158 float v0 = (float)y0 / (float)h;
159 float u1 = (float)(x0 + 1) / (float)w;
160 float v1 = (float)(y0 + 1) / (float)h;
162 float tu = (u - u0) / (u1 - u0);
163 float tv = (v - v0) / (v1 - v0);
165 Vec4 p00 = get_pixel(x0, y0);
166 Vec4 p01 = get_pixel(x0, y1);
167 Vec4 p11 = get_pixel(x1, y1);
168 Vec4 p10 = get_pixel(x1, y0);
170 Vec4 ptop = lerp(p01, p11, tu);
171 Vec4 pbot = lerp(p00, p10, tu);
173 return lerp(pbot, ptop, tv);