1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
|
#include <stdlib.h>
#include "image/imagecombiner.h"
#include "exception.h"
ImageCombiner::ImageCombiner(Image *rgb, Image *alpha)
{
if (rgb->get_width() != alpha->get_width() ||
rgb->get_height() != alpha->get_height()) {
throw new FatalException("Alpha image is not the same dimensions as the base image!");
}
if (rgb->get_bpp() != 8 && rgb->get_bpp() != 24) {
throw new FatalException("RGB image must be 8bpp or 24bpp!");
}
if (alpha->get_bpp() != 8) {
throw new FatalException("Alpha map but isn't 8bpp!");
}
/* slow but works well. :-) */
this->width = rgb->get_width();
this->height = rgb->get_height();
if (rgb->get_bpp() == 24) {
this->bpp = 32;
this->image_buf = (unsigned char *)malloc(width * height * 4);
if (this->image_buf == NULL)
throw new FatalException("Out of memory!");
unsigned char *ptr_rgb = rgb->get_pixel_data();
unsigned char *ptr_alpha = alpha->get_pixel_data();
unsigned char *out = this->image_buf;
for (unsigned int i = 0; i < width * height; i++) {
*out++ = *ptr_rgb++;
*out++ = *ptr_rgb++;
*out++ = *ptr_rgb++;
*out++ = *ptr_alpha++;
}
} else {
this->bpp = 16;
this->image_buf = (unsigned char *)malloc(width * height * 2);
if (this->image_buf == NULL)
throw new FatalException("Out of memory!");
unsigned char *ptr_rgb = rgb->get_pixel_data();
unsigned char *ptr_alpha = alpha->get_pixel_data();
unsigned char *out = this->image_buf;
for (unsigned int i = 0; i < width * height; i++) {
*out++ = *ptr_rgb++;
*out++ = *ptr_alpha++;
}
}
}
ImageCombiner::~ImageCombiner()
{
free(this->image_buf);
this->image_buf = NULL;
}
unsigned char *ImageCombiner::get_pixel_data()
{
return this->image_buf;
}
|