source: golgotha/src/i4/quantize/dither_quantize.cc

Last change on this file was 80, checked in by Sam Hocevar, 15 years ago
  • Adding the Golgotha source code. Not sure what's going to be interesting in there, but since it's all public domain, there's certainly stuff to pick up.
File size: 2.2 KB
Line 
1/********************************************************************** <BR>
2  This file is part of Crack dot Com's free source code release of
3  Golgotha. <a href="http://www.crack.com/golgotha_release"> <BR> for
4  information about compiling & licensing issues visit this URL</a>
5  <PRE> If that doesn't help, contact Jonathan Clark at
6  golgotha_source@usa.net (Subject should have "GOLG" in it)
7***********************************************************************/
8
9#include "image/image.hh"
10#include "error/error.hh"
11#include "memory/malloc.hh"
12#include "image/context.hh"
13#include <memory.h>
14
15inline void argb_split(w32 c, int &a, int &r, int &g, int &b)
16{
17  a=(c&0xff000000)>>24;
18  r=(c&0xff0000)>>16;
19  g=(c&0xff00)>>8;
20  b=(c&0xff)>>0;
21}
22
23inline w32 pack_argb(int a, int r, int g, int b)
24{
25  return (a<<24) | (r<<16) | (g<<8) | b;
26}
27
28void i4_dither_quantize(i4_image_class *source, i4_image_class *dest)
29{
30  int w=source->width(), h=source->height(), x,y;
31
32  I4_ASSERT(source && dest && w==dest->width() && h==dest->height(), "source size !=dest");
33 
34  int size=(w+1)*(h+1)*4;
35  w32 *er_im=(w32 *)i4_malloc(size,"");
36  memset(er_im, 0, size);
37  w32 *cur_er=er_im;
38
39  i4_draw_context_class context(0,0, w-1, h-1);
40  for (y=0; y<h; y++)
41  {
42    for (x=0; x<w; x++, cur_er++)
43    {
44      w32 c=source->get_pixel(x,y, context);
45      int r,g,b,a, ea,er,eg,eb;
46
47      argb_split(c, a,r,g,b);
48      argb_split(*cur_er, ea, er, eg,eb);
49
50      ea-=127; er-=127; eg-=127; eb-=127;
51
52      a+=ea; if (a<0) a=0; if (a>255) a=255;
53      r+=er; if (r<0) r=0; if (r>255) r=255;
54      g+=eg; if (g<0) g=0; if (g>255) g=255;
55      b+=eb; if (b<0) b=0; if (b>255) b=255;
56     
57
58      dest->put_pixel(x,y, pack_argb(a,r,g,b), context);
59      w32 d=dest->get_pixel(x,y, context);      // see what the differance is
60      argb_split(d, ea, er, eg, eb);
61
62      ea-=a;
63      er-=r;
64      eg-=g;
65      eb-=b;
66
67      cur_er[1]+=pack_argb( (ea/4)+127, (er/4)+127, (eg/4)+127, (eb/4)+127);
68      cur_er[w+1]+=pack_argb( (ea/2)+127, (er/2)+127, (eg/2)+127, (eb/2)+127);
69      cur_er[w+1+1]+=pack_argb( (ea/4)+127, (er/4)+127, (eg/4)+127, (eb/4)+127);   
70    }
71
72    cur_er++;
73  }
74
75  i4_free(er_im);
76}
77
Note: See TracBrowser for help on using the repository browser.