fork(1) download
  1. /*
  2. Copyright (c) 2012 Cem Kalyoncu
  3.  
  4. Permission is hereby granted, free of charge, to any person obtaining a copy of
  5. this software and associated documentation files (the "Software"), to deal in
  6. the Software without restriction, including without limitation the rights to
  7. use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  8. the Software, and to permit persons to whom the Software is furnished to do so,
  9. subject to the following conditions:
  10.  
  11. The above copyright notice and this permission notice shall be included in all
  12. copies or substantial portions of the Software.
  13.  
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  16. FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  17. COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  18. IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  19. CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  20. */
  21.  
  22.  
  23. //color lists, they are not required by the functions, just for reference
  24. static unsigned int bodycolors[]={
  25. 0xfff3c3a3,
  26. 0xffffd3b3,
  27. 0xffffe3c3,
  28. 0xfffee3d3,
  29. 0xfff0e8d8,
  30. 0xfff0b393,
  31. 0xffeece84,
  32. 0xffeede84,
  33. 0xff806030,
  34. 0xff604224,
  35. //0xff306030, //fantasy stuff
  36. //0xff50e080,
  37. //0xff804080,
  38. };
  39. static unsigned int eyecolors[]={
  40. 0xff493929,
  41. 0xff50ecd4,
  42. 0xff50d4ec,
  43. 0xffb0c084,
  44. };
  45. static unsigned int haircolors[]={
  46. 0xff221009,
  47. 0xffa77423,
  48. 0xffb7a423,
  49. 0xffd7a443,
  50. 0xffa74423,
  51. 0xffffeeb0,
  52. };
  53.  
  54. //clips a value between 0-255 to prevent overflowing
  55. unsigned char clip(double v) {
  56. return (unsigned char)std::min(std::max(v+0.5,0.),255.);
  57. }
  58.  
  59. //These are class neutral functions which require the following members of canvas to exist, you may modify the code
  60. //as necessary
  61. // * int GetWidth() / int GetHeight()
  62. // * T_ operator ()(int x, int y)
  63. // T_ should support lower case ARGB members
  64.  
  65. //These functions can be implemented using HLS or better yet LCh color modes, however, they will be slower and
  66. //will require additional library. If you do require it, I have the necessary equipment and I will be happy to help.
  67.  
  68. //Switches body color, both skin and eye. If your model is different than base, you may need to change base colors.
  69. //Notice that this will work for blue or green eyes, otherwise it would be harder to detect eyes
  70. template<class C_>
  71. void BodyColorSwitch(C_ &canvas, unsigned int bodytarget, unsigned int eyetarget, unsigned int bodybase=0xfff3c3a3, unsigned int eyebase=0xff50d4ec) {
  72. //extract base color components
  73. double cr=(bodybase/0x10000)%0x100,cg=(bodybase/0x100)%0x100,cb=(bodybase/0x1)%0x100;
  74. //calculate old-to-new ratio
  75. cr=(bodytarget/0x10000)%0x100 / cr;
  76. cg=(bodytarget/0x100)%0x100 / cg;
  77. cb=(bodytarget/0x1)%0x100 / cb;
  78.  
  79. //same for the eyes
  80. double ecr=(eyebase/0x10000)%0x100,ecg=(eyebase/0x100)%0x100,ecb=(eyebase/0x1)%0x100;
  81. ecr=(eyetarget/0x10000)%0x100 / ecr;
  82. ecg=(eyetarget/0x100)%0x100 / ecg;
  83. ecb=(eyetarget/0x1)%0x100 / ecb;
  84.  
  85. //for all pixels, determined by GetHeight and GetWidth functions, change them as you need
  86. for(int y=0;y<canvas.GetHeight();y++) {
  87. for(int x=0;x<canvas.GetWidth();x++) {
  88. //In skin color r>>g and r>>b
  89. if(canvas(x,y).r>canvas(x,y).g+4 && canvas(x,y).r>canvas(x,y).b+5) {
  90. canvas(x,y).r=clip(cr*canvas(x,y).r);
  91. canvas(x,y).g=clip(cg*canvas(x,y).g);
  92. canvas(x,y).b=clip(cb*canvas(x,y).b);
  93. }
  94. //if g>>>r or b>>>r we probably have an eye color
  95. else if(canvas(x,y).r+10<canvas(x,y).g || canvas(x,y).r+10<canvas(x,y).b) {
  96. canvas(x,y).r=clip(ecr*canvas(x,y).r);
  97. canvas(x,y).g=clip(ecg*canvas(x,y).g);
  98. canvas(x,y).b=clip(ecb*canvas(x,y).b);
  99. }
  100. //rest is the white parts of the eyes
  101. }
  102. }
  103. }
  104.  
  105. //This function switches the hair color (can be another part as which will totally be recolored), as before base color is important
  106. //it is designed for the blonde hair wulax had provided
  107. template<class C_>
  108. void HairColorSwitch(C_ &canvas, unsigned int target, unsigned int base=0xffa77423) {
  109. //same as body color switch except without restrictions
  110. double cr=(base/0x10000)%0x100,cg=(base/0x100)%0x100,cb=(base/0x1)%0x100;
  111. cr=(target/0x10000)%0x100 / cr;
  112. cg=(target/0x100)%0x100 / cg;
  113. cb=(target/0x1)%0x100 / cb;
  114. for(int y=0;y<canvas.GetHeight();y++) {
  115. for(int x=0;x<canvas.GetWidth();x++) {
  116. canvas(x,y).r=clip(cr*canvas(x,y).r);
  117. canvas(x,y).g=clip(cg*canvas(x,y).g);
  118. canvas(x,y).b=clip(cb*canvas(x,y).b);
  119. }
  120. }
  121. }
  122.  
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
Standard output is empty