source: abuse/trunk/src/imlib/image24.cpp @ 2

Last change on this file since 2 was 2, checked in by Sam Hocevar, 14 years ago
  • imported original 0.7.0 tarball
File size: 2.1 KB
Line 
1#include "image24.hpp"
2#include "image.hpp"
3
4image24::image24(unsigned short width, unsigned short height,
5                 unsigned char *buffer)
6{
7  w=width;
8  h=height;
9 
10  data=(unsigned char *)jmalloc(width*height*3,"image24");
11  CONDITION(data,"unable to alloc enough memory for 24 bit image");   
12}
13
14void image24::clear(unsigned char r, unsigned char g,
15                    unsigned char b)
16{
17  int x,y;
18  unsigned char *p=data; 
19  for (y=0;y<h;y++)
20  {   
21    for (x=0;x<w;x++)
22    {
23      *(p++)=r;
24      *(p++)=g;
25      *(p++)=b;
26    }   
27  } 
28}
29
30
31void image24::add_error(int x, int y, int r_error, int g_error, int b_error, int error_mult)
32{
33  if (x>=w || x<0 || y>=h || y<0)           // check to make sure this pixel is in the image
34    return ;
35  unsigned char *pix=data+(y*w*3+x*3);   
36  int result;
37 
38  result=(int)(*pix)+r_error*error_mult/32;
39  if (result>255)
40    *pix=255;
41  else if (result<0) *pix=0;
42  else *pix=result; 
43  pix++;
44
45  result=(int)(*pix)+g_error*error_mult/32;
46  if (result>255)
47    *pix=255;
48  else if (result<0) *pix=0;
49  else *pix=result;
50  pix++;
51 
52  result=(int)(*pix)+b_error*error_mult/32;
53  if (result>255)
54    *pix=255;
55  else if (result<0) *pix=0;
56  else *pix=result;
57}
58
59
60image *image24::dither(palette *pal)
61
62  int i,j,closest;
63  unsigned char r,g,b,*cur_pixel=data,ar,ag,ab; 
64  image *dest=new image(w,h); 
65  for (j=0;j<h;j++)
66  {   
67    for (i=0;i<w;i++)
68    {
69      r=(*cur_pixel);  cur_pixel++;
70      g=(*cur_pixel);  cur_pixel++;
71      b=(*cur_pixel);  cur_pixel++;     
72      closest=pal->find_closest(r,g,b);           // find the closest match in palette
73      dest->putpixel(i,j,closest);               // set the pixel to this color
74
75      pal->get(closest,ar,ag,ab);                   // see what the actual color we used was
76 
77      add_error(i+1,j,ar-r,ag-g,ab-b,8); 
78      add_error(i+2,j,ar-r,ag-g,ab-b,4); 
79
80      add_error(i-2,j+1,ar-r,ag-g,ab-b,2);
81      add_error(i-1,j+1,ar-r,ag-g,ab-b,4);
82      add_error(i,j+1,ar-r,ag-g,ab-b,8);
83      add_error(i+1,j+1,ar-r,ag-g,ab-b,4);
84      add_error(i+2,j+1,ar-r,ag-g,ab-b,2);
85    }
86  }   
87  return dest;
88}
89
90
91
92
93
Note: See TracBrowser for help on using the repository browser.