palet.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /*
  2. XCC Utilities and Library
  3. Copyright (C) 2000 Olaf van der Spek <[email protected]>
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <https://www.gnu.org/licenses/>.
  14. */
  15. #include "stdafx.h"
  16. #include <climits>
  17. #include "palet.h"
  18. void convert_image_8_to_24(const byte* s, byte* d, int cx, int cy, const t_palet palet)
  19. {
  20. const byte* r = s;
  21. byte* w = d;
  22. int c = cx * cy;
  23. while (c--)
  24. {
  25. const t_palet_entry* v = palet + *r++;
  26. *w++ = v->r;
  27. *w++ = v->g;
  28. *w++ = v->b;
  29. }
  30. }
  31. void create_downsample_table(const t_palet palet, byte* rp)
  32. {
  33. byte* rp_w = rp;
  34. for (int b = 0; b < 0x40; b++)
  35. {
  36. for (int g = 0; g < 0x40; g++)
  37. {
  38. for (int r = 0; r < 0x40; r++)
  39. *rp_w++ = find_color(r * 255 / 63, g * 255 / 63, b * 255 / 63, palet);
  40. }
  41. }
  42. }
  43. void convert_image_24_to_8(const byte* s, byte* d, int cx, int cy, const byte* rp)
  44. {
  45. const byte* r = s;
  46. byte* w = d;
  47. int c = cx * cy;
  48. while (c--)
  49. {
  50. int red = *r++ >> 2 & 0x3f;
  51. int green = *r++ >> 2 & 0x3f;
  52. int blue = *r++ >> 2 & 0x3f;
  53. *w++ = rp[red | green << 6 | blue << 12];
  54. }
  55. }
  56. void convert_image_24_to_8(const byte* s, byte* d, int cx, int cy, const t_palet palet)
  57. {
  58. const byte* r = s;
  59. byte* w = d;
  60. int c = cx * cy;
  61. while (c--)
  62. {
  63. int red = *r++;
  64. int green = *r++;
  65. int blue = *r++;
  66. *w++ = find_color(red, green, blue, palet);
  67. }
  68. }
  69. void downsample_image(const t_palet32entry* s, byte* d, int cx, int cy, const byte* rp)
  70. {
  71. const t_palet32entry* r = s;
  72. byte* w = d;
  73. int c = cx * cy;
  74. while (c--)
  75. {
  76. t_palet32entry e = *r++;
  77. *w++ = e.a < 0x80 ? 0 : rp[e.r >> 2 | (e.g & 0xfc) << 4 | (e.b & 0xfc) << 10];
  78. }
  79. }
  80. void downsample_image(const t_palet32entry* s, byte* d, int cx, int cy, const t_palet palet)
  81. {
  82. const t_palet32entry* r = s;
  83. byte* w = d;
  84. int c = cx * cy;
  85. while (c--)
  86. {
  87. t_palet32entry e = *r++;
  88. if (e.a < 0x80)
  89. *w++ = 0;
  90. else
  91. *w++ = find_color(e.r, e.g, e.b, palet);
  92. }
  93. }
  94. void upsample_image(const byte* s, t_palet32entry* d, int cx, int cy, const t_palet palet)
  95. {
  96. const byte* r = s;
  97. t_palet32entry* w = d;
  98. int c = cx * cy;
  99. while (c--)
  100. {
  101. t_palet32entry& e = *w++;
  102. int z = *r++;
  103. if (z)
  104. {
  105. const t_palet_entry* v = palet + z;
  106. e.r = v->r;
  107. e.g = v->g;
  108. e.b = v->b;
  109. e.a = 0xff;
  110. }
  111. else
  112. {
  113. e.r = 0x80;
  114. e.g = 0x80;
  115. e.b = 0x80;
  116. e.a = 0;
  117. }
  118. }
  119. }
  120. void convert_palet_18_to_24(const t_palet s, t_palet d)
  121. {
  122. for (int i = 0; i < 256; i++)
  123. {
  124. d[i].r = (s[i].r & 63) * 255 / 63;
  125. d[i].g = (s[i].g & 63) * 255 / 63;
  126. d[i].b = (s[i].b & 63) * 255 / 63;
  127. }
  128. }
  129. void convert_palet_24_to_18(const t_palet s, t_palet d)
  130. {
  131. for (int i = 0; i < 256; i++)
  132. {
  133. d[i].r = s[i].r >> 2;
  134. d[i].g = s[i].g >> 2;
  135. d[i].b = s[i].b >> 2;
  136. }
  137. }
  138. void convert_palet_18_to_24(t_palet palet)
  139. {
  140. convert_palet_18_to_24(palet, palet);
  141. }
  142. void convert_palet_24_to_18(t_palet palet)
  143. {
  144. convert_palet_24_to_18(palet, palet);
  145. }
  146. int find_color(int r, int g, int b, const t_palet p)
  147. {
  148. int best_i;
  149. int min_d = INT_MAX;
  150. for (int i = 0; i < 256; i++)
  151. {
  152. int d_r = p[i].r - r;
  153. int d_g = p[i].g - g;
  154. int d_b = p[i].b - b;
  155. int d = d_r * d_r + d_g * d_g + d_b * d_b;
  156. if (d < min_d)
  157. {
  158. min_d = d;
  159. best_i = i;
  160. }
  161. }
  162. return best_i;
  163. }
  164. int find_color_t(int r, int g, int b, const t_palet p)
  165. {
  166. int best_i;
  167. int min_d = INT_MAX;
  168. for (int i = 1; i < 256; i++)
  169. {
  170. int d_r = p[i].r - r;
  171. int d_g = p[i].g - g;
  172. int d_b = p[i].b - b;
  173. int d = d_r * d_r + d_g * d_g + d_b * d_b;
  174. if (d < min_d)
  175. {
  176. min_d = d;
  177. best_i = i;
  178. }
  179. }
  180. return best_i;
  181. }
  182. void create_rp(const t_palet s1, const t_palet s2, byte* d)
  183. {
  184. d[0] = 0;
  185. for (int i = 1; i < 256; i++)
  186. d[i] = find_color(s1[i].r, s1[i].g, s1[i].b, s2);
  187. }
  188. void apply_rp(byte* d, int cb_d, const byte* rp)
  189. {
  190. while (cb_d--)
  191. {
  192. *d = rp[*d];
  193. d++;
  194. }
  195. }