dither_quantize.cc 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. #include "image/image.hh"
  9. #include "error/error.hh"
  10. #include "memory/malloc.hh"
  11. #include "image/context.hh"
  12. #include <memory.h>
  13. inline void argb_split(w32 c, int &a, int &r, int &g, int &b)
  14. {
  15. a=(c&0xff000000)>>24;
  16. r=(c&0xff0000)>>16;
  17. g=(c&0xff00)>>8;
  18. b=(c&0xff)>>0;
  19. }
  20. inline w32 pack_argb(int a, int r, int g, int b)
  21. {
  22. return (a<<24) | (r<<16) | (g<<8) | b;
  23. }
  24. void i4_dither_quantize(i4_image_class *source, i4_image_class *dest)
  25. {
  26. int w=source->width(), h=source->height(), x,y;
  27. I4_ASSERT(source && dest && w==dest->width() && h==dest->height(), "source size !=dest");
  28. int size=(w+1)*(h+1)*4;
  29. w32 *er_im=(w32 *)i4_malloc(size,"");
  30. memset(er_im, 0, size);
  31. w32 *cur_er=er_im;
  32. i4_draw_context_class context(0,0, w-1, h-1);
  33. for (y=0; y<h; y++)
  34. {
  35. for (x=0; x<w; x++, cur_er++)
  36. {
  37. w32 c=source->get_pixel(x,y, context);
  38. int r,g,b,a, ea,er,eg,eb;
  39. argb_split(c, a,r,g,b);
  40. argb_split(*cur_er, ea, er, eg,eb);
  41. ea-=127; er-=127; eg-=127; eb-=127;
  42. a+=ea; if (a<0) a=0; if (a>255) a=255;
  43. r+=er; if (r<0) r=0; if (r>255) r=255;
  44. g+=eg; if (g<0) g=0; if (g>255) g=255;
  45. b+=eb; if (b<0) b=0; if (b>255) b=255;
  46. dest->put_pixel(x,y, pack_argb(a,r,g,b), context);
  47. w32 d=dest->get_pixel(x,y, context); // see what the differance is
  48. argb_split(d, ea, er, eg, eb);
  49. ea-=a;
  50. er-=r;
  51. eg-=g;
  52. eb-=b;
  53. cur_er[1]+=pack_argb( (ea/4)+127, (er/4)+127, (eg/4)+127, (eb/4)+127);
  54. cur_er[w+1]+=pack_argb( (ea/2)+127, (er/2)+127, (eg/2)+127, (eb/2)+127);
  55. cur_er[w+1+1]+=pack_argb( (ea/4)+127, (er/4)+127, (eg/4)+127, (eb/4)+127);
  56. }
  57. cur_er++;
  58. }
  59. i4_free(er_im);
  60. }