histogram.cc 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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 "quantize/histogram.hh"
  9. #include "error/error.hh"
  10. #include "image/image.hh"
  11. #include <memory.h>
  12. #include "palette/pal.hh"
  13. #include "file/file.hh"
  14. i4_histogram_class::i4_histogram_class()
  15. {
  16. memset(reference,0,sizeof(reference));
  17. memset(counts,0,sizeof(counts));
  18. tcolors=0;
  19. total_pixels=0;
  20. }
  21. void i4_histogram_class::add_image_colors(i4_image_class *im, int counts_per_pixel)
  22. {
  23. if (im->get_pal()->source.pixel_depth!=I4_32BIT)
  24. {
  25. i4_warning("add_image_colors : image type not supported");
  26. return ;
  27. }
  28. w32 loop_count=im->width()*im->height();
  29. // this will iterate through all the pixels
  30. w32 *pixel=(w32 *)im->data;
  31. for (; loop_count; loop_count--)
  32. {
  33. i4_color c=*pixel;
  34. // convert the color to 16 bit
  35. c=
  36. (((c&0xff0000) >> (16+3)) << (6+5)) |
  37. (((c&0x00ff00) >> (8+2)) << (5+0)) |
  38. (((c&0x0000ff) >> (0+3)) << (0+0)) ;
  39. increment_color(c,counts_per_pixel);
  40. // move to the next pixel in the image
  41. ++pixel;
  42. }
  43. }
  44. void i4_histogram_class::save(i4_file_class *fp)
  45. {
  46. w32 i;
  47. fp->write_32(tcolors);
  48. fp->write_32(total_pixels);
  49. for (i=0; i<HIST_SIZE; i++)
  50. {
  51. reference[i]=s_to_lsb(reference[i]);
  52. counts[i]=l_to_lsb(counts[i]);
  53. }
  54. fp->write(reference, sizeof(reference));
  55. fp->write(counts, sizeof(counts));
  56. for (i=0; i<HIST_SIZE; i++)
  57. {
  58. reference[i]=s_to_lsb(reference[i]);
  59. counts[i]=l_to_lsb(counts[i]);
  60. }
  61. }
  62. void i4_histogram_class::load(i4_file_class *fp)
  63. {
  64. w32 i;
  65. tcolors=fp->read_32();
  66. total_pixels=fp->read_32();
  67. fp->read(reference, sizeof(reference));
  68. fp->read(counts, sizeof(counts));
  69. for (i=0; i<HIST_SIZE; i++)
  70. {
  71. reference[i]=s_to_lsb(reference[i]);
  72. counts[i]=l_to_lsb(counts[i]);
  73. }
  74. }