GammaFilter.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. #include "GammaFilter.h"
  2. #include "api.h"
  3. int GammaFilter::filterBitmap(uint8_t *bits, int w, int h, int bpp, const wchar_t *element_id, const wchar_t *forcegroup)
  4. {
  5. int r = 0, g = 0, b = 0;
  6. int gray = 0;
  7. int boost = 0;
  8. const wchar_t *gammagroup;
  9. if (forcegroup && *forcegroup)
  10. gammagroup = forcegroup;
  11. else
  12. gammagroup = WASABI_API_PALETTE->getGammaGroupFromId(element_id);
  13. WASABI_API_COLORTHEMES->getGammaForGroup(gammagroup, &r, &g, &b, &gray, &boost);
  14. if (!r && !g && !b && !gray && !boost) return 1;
  15. if (bpp != 32) return 0;
  16. int *p;
  17. int l = w * h;
  18. int c;
  19. if (gray == 1)
  20. {
  21. int r, g, b;
  22. p = (int *)bits;
  23. while (l--)
  24. {
  25. r = (*p & 0xff0000) >> 16;
  26. g = (*p & 0xff00) >> 8;
  27. b = (*p & 0xff);
  28. c = MAX(MAX(r, g), b);
  29. c = (c << 16) | (c << 8) | c;
  30. *p = (*p & 0xff000000) | c;
  31. p++;
  32. }
  33. }
  34. if (gray == 2)
  35. {
  36. int r, g, b;
  37. p = (int *)bits;
  38. while (l--)
  39. {
  40. r = (*p & 0xff0000) >> 16;
  41. g = (*p & 0xff00) >> 8;
  42. b = (*p & 0xff);
  43. c = (r + g + b) / 3;
  44. c = (c << 16) | (c << 8) | c;
  45. *p = (*p & 0xff000000) | c;
  46. p++;
  47. }
  48. }
  49. if (boost)
  50. {
  51. l = w * h;
  52. int r, g, b, a;
  53. p = (int *)bits;
  54. while (l--)
  55. {
  56. a = (*p & 0xff000000) >> 25;
  57. r = ((*p & 0xff0000) >> 17) + a;
  58. g = ((*p & 0xff00) >> 9) + a;
  59. b = ((*p & 0xff) >> 1) + a;
  60. *p = (*p & 0xff000000) | (r << 16) | (g << 8) | b;
  61. p++;
  62. }
  63. }
  64. int rm = 65535 + (r << 4);
  65. int gm = 65535 + (g << 4);
  66. int bm = 65535 + (b << 4);
  67. l = w * h;
  68. p = (int *)bits;
  69. while (l--)
  70. {
  71. r = ((((*p & 0xff0000) >> 16) * rm)) & 0xffff0000;
  72. c = MAX(0, MIN(r, 0xFF0000));
  73. r = ((((*p & 0xff00) >> 8) * gm) >> 8) & 0xffff00;
  74. c |= MAX(0, MIN(r, 0xFF00));
  75. r = (((*p & 0xff) * bm) >> 16) & 0xffff;
  76. c |= MAX(0, MIN(r, 0xFF));
  77. c = (c & 0xFFFFFF) | (*p & 0xFF000000);
  78. *p = c;
  79. p++;
  80. }
  81. return 1;
  82. }
  83. ARGB32 GammaFilter::filterColor(ARGB32 color, const wchar_t *element_id, const wchar_t *forcegroup)
  84. {
  85. int r = 0, g = 0, b = 0, gray = 0, boost = 0;
  86. const wchar_t *gammagroup;
  87. if (forcegroup && *forcegroup)
  88. gammagroup = forcegroup;
  89. else
  90. gammagroup = WASABI_API_PALETTE->getGammaGroupFromId(element_id);
  91. WASABI_API_COLORTHEMES->getGammaForGroup(gammagroup, &r, &g, &b, &gray, &boost);
  92. if (!r && !g && !b && !gray && !boost) return color;
  93. ARGB32 c;
  94. if (gray == 1)
  95. {
  96. int r, g, b;
  97. r = (color & 0xff0000) >> 16;
  98. g = (color & 0xff00) >> 8;
  99. b = (color & 0xff);
  100. c = MAX(MAX(r, g), b);
  101. c = (c << 16) | (c << 8) | c;
  102. color = (color & 0xff000000) | c;
  103. }
  104. if (gray == 2)
  105. {
  106. int r, g, b;
  107. r = (color & 0xff0000) >> 16;
  108. g = (color & 0xff00) >> 8;
  109. b = (color & 0xff);
  110. c = (r + g + b) / 3;
  111. c = (c << 16) | (c << 8) | c;
  112. color = (color & 0xff000000) | c;
  113. }
  114. if (boost)
  115. {
  116. int r, g, b;
  117. r = ((color & 0xff0000) >> 17) + 0x80;
  118. g = ((color & 0xff00) >> 9) + 0x80;
  119. b = ((color & 0xff) >> 1) + 0x80;
  120. color = (color & 0xff000000) | (r << 16) | (g << 8) | b;
  121. }
  122. int bm = 65536 + (r << 4);
  123. int gm = 65536 + (g << 4);
  124. int rm = 65536 + (b << 4);
  125. r = ((((color & 0xff0000) >> 16) * rm)) & 0xffff0000;
  126. c = MAX(0, MIN(r, 0xFF0000));
  127. r = ((((color & 0xff00) >> 8) * gm) >> 8) & 0xffff00;
  128. c |= MAX(0, MIN(r, 0xFF00));
  129. r = (((color & 0xff) * bm) >> 16) & 0xffff;
  130. c |= MAX(0, MIN(r, 0xFF));
  131. c = (c & 0xFFFFFF) | (color & 0xFF000000);
  132. return c;
  133. }
  134. #define CBCLASS GammaFilter
  135. START_DISPATCH;
  136. CB(FILTERBITMAP, filterBitmap);
  137. CB(FILTERCOLOR, filterColor);
  138. END_DISPATCH;
  139. #undef CBCLASS
  140. static GammaFilter gammaFilterService;
  141. // {B092B4BD-32E1-4c35-B4AE-90B34565D9D6}
  142. static const GUID gammaFilterGUID =
  143. { 0xb092b4bd, 0x32e1, 0x4c35, { 0xb4, 0xae, 0x90, 0xb3, 0x45, 0x65, 0xd9, 0xd6 } };
  144. FOURCC GammaFilterFactory::GetServiceType()
  145. {
  146. return gammaFilterService.getServiceType();
  147. }
  148. const char *GammaFilterFactory::GetServiceName()
  149. {
  150. return gammaFilterService.getServiceName();
  151. }
  152. GUID GammaFilterFactory::GetGUID()
  153. {
  154. return gammaFilterGUID;
  155. }
  156. void *GammaFilterFactory::GetInterface(int global_lock)
  157. {
  158. // if (global_lock)
  159. // WASABI_API_SVC->service_lock(this, (void *)ifc);
  160. return &gammaFilterService;
  161. }
  162. int GammaFilterFactory::SupportNonLockingInterface()
  163. {
  164. return 1;
  165. }
  166. int GammaFilterFactory::ReleaseInterface(void *ifc)
  167. {
  168. //WASABI_API_SVC->service_unlock(ifc);
  169. return 1;
  170. }
  171. const char *GammaFilterFactory::GetTestString()
  172. {
  173. return 0;
  174. }
  175. int GammaFilterFactory::ServiceNotify(int msg, int param1, int param2)
  176. {
  177. return 1;
  178. }
  179. #define CBCLASS GammaFilterFactory
  180. START_DISPATCH;
  181. CB(WASERVICEFACTORY_GETSERVICETYPE, GetServiceType)
  182. CB(WASERVICEFACTORY_GETSERVICENAME, GetServiceName)
  183. CB(WASERVICEFACTORY_GETGUID, GetGUID)
  184. CB(WASERVICEFACTORY_GETINTERFACE, GetInterface)
  185. CB(WASERVICEFACTORY_SUPPORTNONLOCKINGGETINTERFACE, SupportNonLockingInterface)
  186. CB(WASERVICEFACTORY_RELEASEINTERFACE, ReleaseInterface)
  187. CB(WASERVICEFACTORY_GETTESTSTRING, GetTestString)
  188. CB(WASERVICEFACTORY_SERVICENOTIFY, ServiceNotify)
  189. END_DISPATCH;
  190. #undef CBCLASS