image_quantize.cpp 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  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-2015 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. //printf(" ai:%i - %i\n",i,d);
  52. if (d>max_dist) {
  53. max_index=i;
  54. max_dist=d;
  55. }
  56. }
  57. return max_index;
  58. }
  59. int Image::MCBlock::get_longest_axis_length() const {
  60. int max_dist=-1;
  61. int max_index=0;
  62. for(int i=0;i<4;i++) {
  63. int d = max_color.color.col[i]-min_color.color.col[i];
  64. if (d>max_dist) {
  65. max_index=i;
  66. max_dist=d;
  67. }
  68. }
  69. return max_dist;
  70. }
  71. bool Image::MCBlock::operator<(const MCBlock& p_block) const {
  72. int alen = get_longest_axis_length();
  73. int blen = p_block.get_longest_axis_length();
  74. if (alen==blen) {
  75. return colors < p_block.colors;
  76. } else
  77. return alen < blen;
  78. }
  79. void Image::MCBlock::shrink() {
  80. min_color=colors[0];
  81. max_color=colors[0];
  82. for(int i=1;i<color_count;i++) {
  83. for(int j=0;j<4;j++) {
  84. min_color.color.col[j]=MIN(min_color.color.col[j],colors[i].color.col[j]);
  85. max_color.color.col[j]=MAX(max_color.color.col[j],colors[i].color.col[j]);
  86. }
  87. }
  88. }
  89. void Image::quantize() {
  90. Image::Format orig_format=format;
  91. bool has_alpha = detect_alpha()!=ALPHA_NONE;
  92. bool quantize_fast=OS::get_singleton()->has_environment("QUANTIZE_FAST");
  93. convert(FORMAT_RGBA);
  94. ERR_FAIL_COND( format!=FORMAT_RGBA );
  95. DVector<uint8_t> indexed_data;
  96. {
  97. int color_count = data.size()/4;
  98. ERR_FAIL_COND(color_count==0);
  99. Set<MCBlock> block_queue;
  100. DVector<BColorPos> data_colors;
  101. data_colors.resize(color_count);
  102. DVector<BColorPos>::Write dcw=data_colors.write();
  103. DVector<uint8_t>::Read dr = data.read();
  104. const BColor * drptr=(const BColor*)&dr[0];
  105. BColorPos *bcptr=&dcw[0];
  106. {
  107. for(int i=0;i<color_count;i++) {
  108. //uint32_t data_ofs=i<<2;
  109. bcptr[i].color=drptr[i];//BColor(drptr[data_ofs+0],drptr[data_ofs+1],drptr[data_ofs+2],drptr[data_ofs+3]);
  110. bcptr[i].index=i;
  111. }
  112. }
  113. //printf("color count: %i\n",color_count);
  114. /*
  115. for(int i=0;i<color_count;i++) {
  116. BColor bc = ((BColor*)&wb[0])[i];
  117. printf("%i - %i,%i,%i,%i\n",i,bc.r,bc.g,bc.b,bc.a);
  118. }*/
  119. MCBlock initial_block((BColorPos*)&dcw[0],color_count);
  120. block_queue.insert(initial_block);
  121. while( block_queue.size() < 256 && block_queue.back()->get().color_count > 1 ) {
  122. MCBlock longest = block_queue.back()->get();
  123. //printf("longest: %i (%i)\n",longest.get_longest_axis_index(),longest.get_longest_axis_length());
  124. block_queue.erase(block_queue.back());
  125. BColorPos *first = longest.colors;
  126. BColorPos *median = longest.colors + (longest.color_count+1)/2;
  127. BColorPos *end = longest.colors + longest.color_count;
  128. #if 0
  129. int lai =longest.get_longest_axis_index();
  130. switch(lai) {
  131. #if 0
  132. case 0: { SortArray<BColorPos,BColorPos::SortR> sort; sort.sort(first,end-first); } break;
  133. case 1: { SortArray<BColorPos,BColorPos::SortG> sort; sort.sort(first,end-first); } break;
  134. case 2: { SortArray<BColorPos,BColorPos::SortB> sort; sort.sort(first,end-first); } break;
  135. case 3: { SortArray<BColorPos,BColorPos::SortA> sort; sort.sort(first,end-first); } break;
  136. #else
  137. case 0: { SortArray<BColorPos,BColorPos::SortR> sort; sort.nth_element(0,end-first,median-first,first); } break;
  138. case 1: { SortArray<BColorPos,BColorPos::SortG> sort; sort.nth_element(0,end-first,median-first,first); } break;
  139. case 2: { SortArray<BColorPos,BColorPos::SortB> sort; sort.nth_element(0,end-first,median-first,first); } break;
  140. case 3: { SortArray<BColorPos,BColorPos::SortA> sort; sort.nth_element(0,end-first,median-first,first); } break;
  141. #endif
  142. }
  143. //avoid same color from being split in 2
  144. //search forward and flip
  145. BColorPos *median_end=median;
  146. BColorPos *p=median_end+1;
  147. while(p!=end) {
  148. if (median_end->color==p->color) {
  149. SWAP(*(median_end+1),*p);
  150. median_end++;
  151. }
  152. p++;
  153. }
  154. //search backward and flip
  155. BColorPos *median_begin=median;
  156. p=median_begin-1;
  157. while(p!=(first-1)) {
  158. if (median_begin->color==p->color) {
  159. SWAP(*(median_begin-1),*p);
  160. median_begin--;
  161. }
  162. p--;
  163. }
  164. if (first < median_begin) {
  165. median=median_begin;
  166. } else if (median_end < end-1) {
  167. median=median_end+1;
  168. } else {
  169. break; //shouldn't have arrived here, since it means all pixels are equal, but wathever
  170. }
  171. MCBlock left(first,median-first);
  172. MCBlock right(median,end-median);
  173. block_queue.insert(left);
  174. block_queue.insert(right);
  175. #else
  176. switch(longest.get_longest_axis_index()) {
  177. case 0: { SortArray<BColorPos,BColorPos::SortR> sort; sort.nth_element(0,end-first,median-first,first); } break;
  178. case 1: { SortArray<BColorPos,BColorPos::SortG> sort; sort.nth_element(0,end-first,median-first,first); } break;
  179. case 2: { SortArray<BColorPos,BColorPos::SortB> sort; sort.nth_element(0,end-first,median-first,first); } break;
  180. case 3: { SortArray<BColorPos,BColorPos::SortA> sort; sort.nth_element(0,end-first,median-first,first); } break;
  181. }
  182. MCBlock left(first,median-first);
  183. MCBlock right(median,end-median);
  184. block_queue.insert(left);
  185. block_queue.insert(right);
  186. #endif
  187. }
  188. while(block_queue.size() > 256) {
  189. block_queue.erase(block_queue.front());// erase least significant
  190. }
  191. int res_colors=0;
  192. int comp_size = (has_alpha?4:3);
  193. indexed_data.resize(color_count + 256*comp_size);
  194. DVector<uint8_t>::Write iw = indexed_data.write();
  195. uint8_t *iwptr=&iw[0];
  196. BColor pallete[256];
  197. // print_line("applying quantization - res colors "+itos(block_queue.size()));
  198. while(block_queue.size()) {
  199. const MCBlock &b = block_queue.back()->get();
  200. uint64_t sum[4]={0,0,0,0};
  201. for(int i=0;i<b.color_count;i++) {
  202. sum[0]+=b.colors[i].color.col[0];
  203. sum[1]+=b.colors[i].color.col[1];
  204. sum[2]+=b.colors[i].color.col[2];
  205. sum[3]+=b.colors[i].color.col[3];
  206. }
  207. BColor c( sum[0]/b.color_count, sum[1]/b.color_count, sum[2]/b.color_count, sum[3]/b.color_count );
  208. //printf(" %i: %i,%i,%i,%i out of %i\n",res_colors,c.r,c.g,c.b,c.a,b.color_count);
  209. for(int i=0;i<comp_size;i++) {
  210. iwptr[ color_count + res_colors * comp_size + i ] = c.col[i];
  211. }
  212. if (quantize_fast) {
  213. for(int i=0;i<b.color_count;i++) {
  214. iwptr[b.colors[i].index]=res_colors;
  215. }
  216. } else {
  217. pallete[res_colors]=c;
  218. }
  219. res_colors++;
  220. block_queue.erase(block_queue.back());
  221. }
  222. if (!quantize_fast) {
  223. for(int i=0;i<color_count;i++) {
  224. const BColor &c=drptr[i];
  225. uint8_t best_dist_idx=0;
  226. uint32_t dist=0xFFFFFFFF;
  227. for(int j=0;j<res_colors;j++) {
  228. const BColor &pc=pallete[j];
  229. uint32_t d = 0;
  230. { int16_t v = (int16_t)c.r-(int16_t)pc.r; d+=v*v; }
  231. { int16_t v = (int16_t)c.g-(int16_t)pc.g; d+=v*v; }
  232. { int16_t v = (int16_t)c.b-(int16_t)pc.b; d+=v*v; }
  233. { int16_t v = (int16_t)c.a-(int16_t)pc.a; d+=v*v; }
  234. if (d<=dist) {
  235. best_dist_idx=j;
  236. dist=d;
  237. }
  238. }
  239. iwptr[ i ] = best_dist_idx;
  240. }
  241. }
  242. //iw = DVector<uint8_t>::Write();
  243. //dr = DVector<uint8_t>::Read();
  244. //wb = DVector<uint8_t>::Write();
  245. }
  246. print_line(itos(indexed_data.size()));
  247. data=indexed_data;
  248. format=has_alpha?FORMAT_INDEXED_ALPHA:FORMAT_INDEXED;
  249. } //do none
  250. #else
  251. void Image::quantize() {} //do none
  252. #endif