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::lookup_nearest(float u, float v) const
126 int x = (int)(u * w) % w;
127 int y = (int)(v * h) % h;
130 return ((Vec4*)pixels)[y * w + x];
134 unsigned char *pptr = ((unsigned char*)pixels) + (y * w + x) * 4;
135 color.x = pptr[0] / 255.0;
136 color.y = pptr[1] / 255.0;
137 color.z = pptr[2] / 255.0;
138 color.w = pptr[3] / 255.0;