+#include <stdlib.h>
#include <string.h>
#include "imago2.h"
Image::Image()
{
- w = h = 0;
+ w = h = psz = 0;
pixels = 0;
+
+ is_float = false;
}
Image::~Image()
{
- delete [] pixels;
+ free(pixels);
}
Image::Image(const Image &image)
{
w = image.w;
h = image.h;
+ is_float = image.is_float;
+ psz = image.psz;
- pixels = new unsigned char[w * h * 4];
- memcpy(pixels, image.pixels, w * h * 4);
+ if(!(pixels = malloc(w * h * psz))) {
+ fprintf(stderr, "Failed to allocate pixels.\n");
+ abort();
+ }
+ memcpy(pixels, image.pixels, w * h * psz);
}
Image &Image::operator =(const Image &image)
if(&image == this)
return *this;
- delete [] pixels;
+ free(pixels);
w = image.w;
h = image.h;
+ psz = image.psz;
+ is_float = image.is_float;
- pixels = new unsigned char[w * h * 4];
- memcpy(pixels, image.pixels, w * h * 4);
+ if(!(pixels = malloc(w * h * psz))) {
+ fprintf(stderr, "Failed to allocate pixels.\n");
+ abort();
+ }
+ memcpy(pixels, image.pixels, w * h * psz);
return *this;
}
{
w = image.w;
h = image.h;
+ psz = image.psz;
+ is_float = image.is_float;
pixels = image.pixels;
image.pixels = 0;
if(&image == this)
return *this;
- delete [] pixels;
+ free(pixels);
w = image.w;
h = image.h;
+ psz = image.psz;
+ is_float = image.is_float;
pixels = image.pixels;
image.pixels = 0;
bool Image::load(const char *fname)
{
- unsigned char *imago_pixels;
- if(!(imago_pixels = (unsigned char *)img_load_pixels(fname, &w, &h))) {
- fprintf(stderr, "Failed to load pixels from file: %s.\n", fname);
+ free(pixels);
+
+ img_pixmap ipm;
+ img_init(&ipm);
+
+ if(img_load(&ipm, fname) == -1) {
+ fprintf(stderr, "Failed to load image: %s.\n", fname);
+
+ img_destroy(&ipm);
+ return false;
+ }
+
+ printf("Successfully loaded image: %s\n", fname);
+
+ img_fmt format = img_is_float(&ipm) ? IMG_FMT_RGBAF : IMG_FMT_RGBA32;
+ if(img_convert(&ipm, format) == -1) {
+ fprintf(stderr, "Failed to convert image %s.\n", fname);
+
+ img_destroy(&ipm);
return false;
}
- delete [] pixels;
- pixels = new unsigned char[w * h * 4];
- memcpy(pixels, imago_pixels, w * h * 4);
+ w = ipm.width;
+ h = ipm.height;
+ psz = ipm.pixelsz;
+ is_float = img_is_float(&ipm) ? true : false;
+
+ if(!(pixels = malloc(w * h * psz))) {
+ fprintf(stderr, "Failed to allocate pixels for image: %s.\n", fname);
+ return false;
+ }
- img_free_pixels(imago_pixels);
+ memcpy(pixels, ipm.pixels, w * h * psz);
+ img_destroy(&ipm);
return true;
}
\ No newline at end of file