palette.hpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. #ifndef _PALETTE_H_
  2. #define _PALETTE_H_
  3. #include "linked.hpp"
  4. #include "specs.hpp"
  5. #define COLOR_BITS 6 // On PC-6, most others -8
  6. #define COLOR_SHIFT (8-COLOR_BITS)
  7. #define MAX_COLOR ((1<<COLOR_BITS)-1)
  8. #define RED3(x) (unsigned char) ((((int)x&(7<<5))>>5)*(int)255/(int)7)
  9. #define GREEN3(x) (unsigned char) (((x&(7<<2))>>2)*(int)255/(int)7)
  10. #define BLUE2(x) (unsigned char) ((x&3)*(int)255/(int)3)
  11. struct color
  12. {
  13. unsigned char red,green,blue;
  14. } ;
  15. class palette : public linked_node
  16. {
  17. color *pal;
  18. unsigned char *usd; // bit array
  19. short ncolors;
  20. int bg;
  21. public :
  22. palette(int number_colors=256);
  23. palette(spec_entry *e, bFILE *fp);
  24. palette(bFILE *fp);
  25. void set(int x, unsigned char red, unsigned char green, unsigned char blue);
  26. void get(int x, unsigned char &red, unsigned char &green, unsigned char &blue);
  27. long getquad(int x);
  28. unsigned int red(int x) { return pal[x].red; }
  29. unsigned int green(int x) { return pal[x].green; }
  30. unsigned int blue(int x) { return pal[x].blue; }
  31. void *addr() { return (void *) pal; }
  32. void shift(int amount);
  33. void load();
  34. void load_nice();
  35. void fade_to(int total_fades, int fade_on, int dest_r, int dest_g, int dest_b);
  36. void defaults();
  37. void set_rgbs();
  38. void make_black_white();
  39. void black_white();
  40. int pal_size() { return ncolors; }
  41. void set_used(int color_num);
  42. void set_unused(int color_num);
  43. int used(int color_num);
  44. void set_all_used();
  45. void set_all_unused();
  46. void set_background(unsigned char b) { bg=b; }
  47. int background(unsigned char b) { return bg; }
  48. int add_color(unsigned int r, unsigned int g, unsigned int b,
  49. int closest_only=0);
  50. int find_color(unsigned char r, unsigned char g, unsigned char b);
  51. int find_closest(unsigned char r, unsigned char g, unsigned char b);
  52. int find_closest_non0(unsigned char r, unsigned char g, unsigned char b);
  53. palette *copy();
  54. unsigned char brightest(int all=0);
  55. unsigned char darkest(int all=0, int noblack=1);
  56. int write(bFILE *fp);
  57. int size();
  58. ~palette();
  59. } ;
  60. class quant_node : public linked_node
  61. {
  62. quant_node *padre;
  63. public :
  64. unsigned tot;
  65. quant_node *children[8];
  66. int is_leaf() { return children[0]==this; }
  67. void be_childish() { children[0]=this; }
  68. quant_node *father() { return padre; }
  69. quant_node(int level, quant_node *dad,
  70. unsigned char r=0, unsigned char g=0, unsigned char b=0);
  71. void total(int &tnodes, int &tr, int &tg, int &tb);
  72. // void prune();
  73. void set(int r,int g,int b) {red=r; green=g; blue=b; }
  74. unsigned char red,green,blue;
  75. ~quant_node();
  76. } ;
  77. class quant_palette
  78. {
  79. linked_list level[8];
  80. quant_node *root;
  81. int nc,mx;
  82. void prune();
  83. void re_delete(quant_node *who, int lev);
  84. public :
  85. void add_color(unsigned char r, unsigned char g, unsigned char b);
  86. quant_palette(int max_colors=256);
  87. palette *create_pal();
  88. ~quant_palette();
  89. } ;
  90. palette *last_loaded();
  91. #endif