image_quantize.cpp 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. /*************************************************************************/
  2. /* image_quantize.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2016 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #include "image.h"
  30. #include <stdio.h>
  31. #include "print_string.h"
  32. #ifdef TOOLS_ENABLED
  33. #include "set.h"
  34. #include "sort.h"
  35. #include "os/os.h"
  36. //#define QUANTIZE_SPEED_OVER_QUALITY
  37. Image::MCBlock::MCBlock() {
  38. }
  39. Image::MCBlock::MCBlock(BColorPos *p_colors,int p_color_count) {
  40. colors=p_colors;
  41. color_count=p_color_count;
  42. min_color.color=BColor(255,255,255,255);
  43. max_color.color=BColor(0,0,0,0);
  44. shrink();
  45. }
  46. int Image::MCBlock::get_longest_axis_index() const {
  47. int max_dist=-1;
  48. int max_index=0;
  49. for(int i=0;i<4;i++) {
  50. int d = max_color.color.col[i]-min_color.color.col[i];
  51. if (d>max_dist) {
  52. max_index=i;
  53. max_dist=d;
  54. }
  55. }
  56. return max_index;
  57. }
  58. int Image::MCBlock::get_longest_axis_length() const {
  59. int max_dist=-1;
  60. for(int i=0;i<4;i++) {
  61. int d = max_color.color.col[i]-min_color.color.col[i];
  62. if (d>max_dist) {
  63. max_dist=d;
  64. }
  65. }
  66. return max_dist;
  67. }
  68. bool Image::MCBlock::operator<(const MCBlock& p_block) const {
  69. int alen = get_longest_axis_length();
  70. int blen = p_block.get_longest_axis_length();
  71. if (alen==blen) {
  72. return colors < p_block.colors;
  73. } else
  74. return alen < blen;
  75. }
  76. void Image::MCBlock::shrink() {
  77. min_color=colors[0];
  78. max_color=colors[0];
  79. for(int i=1;i<color_count;i++) {
  80. for(int j=0;j<4;j++) {
  81. min_color.color.col[j]=MIN(min_color.color.col[j],colors[i].color.col[j]);
  82. max_color.color.col[j]=MAX(max_color.color.col[j],colors[i].color.col[j]);
  83. }
  84. }
  85. }
  86. void Image::quantize() {
  87. bool has_alpha = detect_alpha()!=ALPHA_NONE;
  88. bool quantize_fast=OS::get_singleton()->has_environment("QUANTIZE_FAST");
  89. convert(FORMAT_RGBA);
  90. ERR_FAIL_COND( format!=FORMAT_RGBA );
  91. DVector<uint8_t> indexed_data;
  92. {
  93. int color_count = data.size()/4;
  94. ERR_FAIL_COND(color_count==0);
  95. Set<MCBlock> block_queue;
  96. DVector<BColorPos> data_colors;
  97. data_colors.resize(color_count);
  98. DVector<BColorPos>::Write dcw=data_colors.write();
  99. DVector<uint8_t>::Read dr = data.read();
  100. const BColor * drptr=(const BColor*)&dr[0];
  101. BColorPos *bcptr=&dcw[0];
  102. {
  103. for(int i=0;i<color_count;i++) {
  104. //uint32_t data_ofs=i<<2;
  105. bcptr[i].color=drptr[i];//BColor(drptr[data_ofs+0],drptr[data_ofs+1],drptr[data_ofs+2],drptr[data_ofs+3]);
  106. bcptr[i].index=i;
  107. }
  108. }
  109. //printf("color count: %i\n",color_count);
  110. /*
  111. for(int i=0;i<color_count;i++) {
  112. BColor bc = ((BColor*)&wb[0])[i];
  113. printf("%i - %i,%i,%i,%i\n",i,bc.r,bc.g,bc.b,bc.a);
  114. }*/
  115. MCBlock initial_block((BColorPos*)&dcw[0],color_count);
  116. block_queue.insert(initial_block);
  117. while( block_queue.size() < 256 && block_queue.back()->get().color_count > 1 ) {
  118. MCBlock longest = block_queue.back()->get();
  119. //printf("longest: %i (%i)\n",longest.get_longest_axis_index(),longest.get_longest_axis_length());
  120. block_queue.erase(block_queue.back());
  121. BColorPos *first = longest.colors;
  122. BColorPos *median = longest.colors + (longest.color_count+1)/2;
  123. BColorPos *end = longest.colors + longest.color_count;
  124. #if 0
  125. int lai =longest.get_longest_axis_index();
  126. switch(lai) {
  127. #if 0
  128. case 0: { SortArray<BColorPos,BColorPos::SortR> sort; sort.sort(first,end-first); } break;
  129. case 1: { SortArray<BColorPos,BColorPos::SortG> sort; sort.sort(first,end-first); } break;
  130. case 2: { SortArray<BColorPos,BColorPos::SortB> sort; sort.sort(first,end-first); } break;
  131. case 3: { SortArray<BColorPos,BColorPos::SortA> sort; sort.sort(first,end-first); } break;
  132. #else
  133. case 0: { SortArray<BColorPos,BColorPos::SortR> sort; sort.nth_element(0,end-first,median-first,first); } break;
  134. case 1: { SortArray<BColorPos,BColorPos::SortG> sort; sort.nth_element(0,end-first,median-first,first); } break;
  135. case 2: { SortArray<BColorPos,BColorPos::SortB> sort; sort.nth_element(0,end-first,median-first,first); } break;
  136. case 3: { SortArray<BColorPos,BColorPos::SortA> sort; sort.nth_element(0,end-first,median-first,first); } break;
  137. #endif
  138. }
  139. //avoid same color from being split in 2
  140. //search forward and flip
  141. BColorPos *median_end=median;
  142. BColorPos *p=median_end+1;
  143. while(p!=end) {
  144. if (median_end->color==p->color) {
  145. SWAP(*(median_end+1),*p);
  146. median_end++;
  147. }
  148. p++;
  149. }
  150. //search backward and flip
  151. BColorPos *median_begin=median;
  152. p=median_begin-1;
  153. while(p!=(first-1)) {
  154. if (median_begin->color==p->color) {
  155. SWAP(*(median_begin-1),*p);
  156. median_begin--;
  157. }
  158. p--;
  159. }
  160. if (first < median_begin) {
  161. median=median_begin;
  162. } else if (median_end < end-1) {
  163. median=median_end+1;
  164. } else {
  165. break; //shouldn't have arrived here, since it means all pixels are equal, but wathever
  166. }
  167. MCBlock left(first,median-first);
  168. MCBlock right(median,end-median);
  169. block_queue.insert(left);
  170. block_queue.insert(right);
  171. #else
  172. switch(longest.get_longest_axis_index()) {
  173. case 0: { SortArray<BColorPos,BColorPos::SortR> sort; sort.nth_element(0,end-first,median-first,first); } break;
  174. case 1: { SortArray<BColorPos,BColorPos::SortG> sort; sort.nth_element(0,end-first,median-first,first); } break;
  175. case 2: { SortArray<BColorPos,BColorPos::SortB> sort; sort.nth_element(0,end-first,median-first,first); } break;
  176. case 3: { SortArray<BColorPos,BColorPos::SortA> sort; sort.nth_element(0,end-first,median-first,first); } break;
  177. }
  178. MCBlock left(first,median-first);
  179. MCBlock right(median,end-median);
  180. block_queue.insert(left);
  181. block_queue.insert(right);
  182. #endif
  183. }
  184. while(block_queue.size() > 256) {
  185. block_queue.erase(block_queue.front());// erase least significant
  186. }
  187. int res_colors=0;
  188. int comp_size = (has_alpha?4:3);
  189. indexed_data.resize(color_count + 256*comp_size);
  190. DVector<uint8_t>::Write iw = indexed_data.write();
  191. uint8_t *iwptr=&iw[0];
  192. BColor pallete[256];
  193. // print_line("applying quantization - res colors "+itos(block_queue.size()));
  194. while(block_queue.size()) {
  195. const MCBlock &b = block_queue.back()->get();
  196. uint64_t sum[4]={0,0,0,0};
  197. for(int i=0;i<b.color_count;i++) {
  198. sum[0]+=b.colors[i].color.col[0];
  199. sum[1]+=b.colors[i].color.col[1];
  200. sum[2]+=b.colors[i].color.col[2];
  201. sum[3]+=b.colors[i].color.col[3];
  202. }
  203. BColor c( sum[0]/b.color_count, sum[1]/b.color_count, sum[2]/b.color_count, sum[3]/b.color_count );
  204. //printf(" %i: %i,%i,%i,%i out of %i\n",res_colors,c.r,c.g,c.b,c.a,b.color_count);
  205. for(int i=0;i<comp_size;i++) {
  206. iwptr[ color_count + res_colors * comp_size + i ] = c.col[i];
  207. }
  208. if (quantize_fast) {
  209. for(int i=0;i<b.color_count;i++) {
  210. iwptr[b.colors[i].index]=res_colors;
  211. }
  212. } else {
  213. pallete[res_colors]=c;
  214. }
  215. res_colors++;
  216. block_queue.erase(block_queue.back());
  217. }
  218. if (!quantize_fast) {
  219. for(int i=0;i<color_count;i++) {
  220. const BColor &c=drptr[i];
  221. uint8_t best_dist_idx=0;
  222. uint32_t dist=0xFFFFFFFF;
  223. for(int j=0;j<res_colors;j++) {
  224. const BColor &pc=pallete[j];
  225. uint32_t d = 0;
  226. { int16_t v = (int16_t)c.r-(int16_t)pc.r; d+=v*v; }
  227. { int16_t v = (int16_t)c.g-(int16_t)pc.g; d+=v*v; }
  228. { int16_t v = (int16_t)c.b-(int16_t)pc.b; d+=v*v; }
  229. { int16_t v = (int16_t)c.a-(int16_t)pc.a; d+=v*v; }
  230. if (d<=dist) {
  231. best_dist_idx=j;
  232. dist=d;
  233. }
  234. }
  235. iwptr[ i ] = best_dist_idx;
  236. }
  237. }
  238. //iw = DVector<uint8_t>::Write();
  239. //dr = DVector<uint8_t>::Read();
  240. //wb = DVector<uint8_t>::Write();
  241. }
  242. print_line(itos(indexed_data.size()));
  243. data=indexed_data;
  244. format=has_alpha?FORMAT_INDEXED_ALPHA:FORMAT_INDEXED;
  245. } //do none
  246. #else
  247. void Image::quantize() {} //do none
  248. #endif