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 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
|
#include "image24.hpp"
#include "image.hpp"
image24::image24(unsigned short width, unsigned short height,
unsigned char *buffer)
{
w=width;
h=height;
data=(unsigned char *)jmalloc(width*height*3,"image24");
CONDITION(data,"unable to alloc enough memory for 24 bit image");
}
void image24::clear(unsigned char r, unsigned char g,
unsigned char b)
{
int x,y;
unsigned char *p=data;
for (y=0;y<h;y++)
{
for (x=0;x<w;x++)
{
*(p++)=r;
*(p++)=g;
*(p++)=b;
}
}
}
void image24::add_error(int x, int y, int r_error, int g_error, int b_error, int error_mult)
{
if (x>=w || x<0 || y>=h || y<0) // check to make sure this pixel is in the image
return ;
unsigned char *pix=data+(y*w*3+x*3);
int result;
result=(int)(*pix)+r_error*error_mult/32;
if (result>255)
*pix=255;
else if (result<0) *pix=0;
else *pix=result;
pix++;
result=(int)(*pix)+g_error*error_mult/32;
if (result>255)
*pix=255;
else if (result<0) *pix=0;
else *pix=result;
pix++;
result=(int)(*pix)+b_error*error_mult/32;
if (result>255)
*pix=255;
else if (result<0) *pix=0;
else *pix=result;
}
image *image24::dither(palette *pal)
{
int i,j,closest;
unsigned char r,g,b,*cur_pixel=data,ar,ag,ab;
image *dest=new image(w,h);
for (j=0;j<h;j++)
{
for (i=0;i<w;i++)
{
r=(*cur_pixel); cur_pixel++;
g=(*cur_pixel); cur_pixel++;
b=(*cur_pixel); cur_pixel++;
closest=pal->find_closest(r,g,b); // find the closest match in palette
dest->putpixel(i,j,closest); // set the pixel to this color
pal->get(closest,ar,ag,ab); // see what the actual color we used was
add_error(i+1,j,ar-r,ag-g,ab-b,8);
add_error(i+2,j,ar-r,ag-g,ab-b,4);
add_error(i-2,j+1,ar-r,ag-g,ab-b,2);
add_error(i-1,j+1,ar-r,ag-g,ab-b,4);
add_error(i,j+1,ar-r,ag-g,ab-b,8);
add_error(i+1,j+1,ar-r,ag-g,ab-b,4);
add_error(i+2,j+1,ar-r,ag-g,ab-b,2);
}
}
return dest;
}
|