SkColorMatrix.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /*
  2. * Copyright 2011 Google Inc.
  3. *
  4. * Use of this source code is governed by a BSD-style license that can be
  5. * found in the LICENSE file.
  6. */
  7. #include "include/effects/SkColorMatrix.h"
  8. #include "include/private/SkFloatingPoint.h"
  9. enum {
  10. kR_Scale = 0,
  11. kG_Scale = 6,
  12. kB_Scale = 12,
  13. kA_Scale = 18,
  14. kR_Trans = 4,
  15. kG_Trans = 9,
  16. kB_Trans = 14,
  17. kA_Trans = 19,
  18. };
  19. static void set_concat(float result[20], const float outer[20], const float inner[20]) {
  20. float tmp[20];
  21. float* target;
  22. if (outer == result || inner == result) {
  23. target = tmp; // will memcpy answer when we're done into result
  24. } else {
  25. target = result;
  26. }
  27. int index = 0;
  28. for (int j = 0; j < 20; j += 5) {
  29. for (int i = 0; i < 4; i++) {
  30. target[index++] = outer[j + 0] * inner[i + 0] +
  31. outer[j + 1] * inner[i + 5] +
  32. outer[j + 2] * inner[i + 10] +
  33. outer[j + 3] * inner[i + 15];
  34. }
  35. target[index++] = outer[j + 0] * inner[4] +
  36. outer[j + 1] * inner[9] +
  37. outer[j + 2] * inner[14] +
  38. outer[j + 3] * inner[19] +
  39. outer[j + 4];
  40. }
  41. if (target != result) {
  42. memcpy(result, target, 20 * sizeof(float));
  43. }
  44. }
  45. void SkColorMatrix::setIdentity() {
  46. memset(fMat, 0, sizeof(fMat));
  47. fMat[kR_Scale] = fMat[kG_Scale] = fMat[kB_Scale] = fMat[kA_Scale] = 1;
  48. }
  49. void SkColorMatrix::setScale(float rScale, float gScale, float bScale, float aScale) {
  50. memset(fMat, 0, sizeof(fMat));
  51. fMat[kR_Scale] = rScale;
  52. fMat[kG_Scale] = gScale;
  53. fMat[kB_Scale] = bScale;
  54. fMat[kA_Scale] = aScale;
  55. }
  56. void SkColorMatrix::postTranslate(float dr, float dg, float db, float da) {
  57. fMat[kR_Trans] += dr;
  58. fMat[kG_Trans] += dg;
  59. fMat[kB_Trans] += db;
  60. fMat[kA_Trans] += da;
  61. }
  62. ///////////////////////////////////////////////////////////////////////////////
  63. void SkColorMatrix::setRotate(Axis axis, float degrees) {
  64. float r = sk_float_degrees_to_radians(degrees);
  65. this->setSinCos(axis, sk_float_sin(r), sk_float_cos(r));
  66. }
  67. void SkColorMatrix::setSinCos(Axis axis, float sine, float cosine) {
  68. SkASSERT((unsigned)axis < 3);
  69. static const uint8_t gRotateIndex[] = {
  70. 6, 7, 11, 12,
  71. 0, 10, 2, 12,
  72. 0, 1, 5, 6,
  73. };
  74. const uint8_t* index = gRotateIndex + axis * 4;
  75. this->setIdentity();
  76. fMat[index[0]] = cosine;
  77. fMat[index[1]] = sine;
  78. fMat[index[2]] = -sine;
  79. fMat[index[3]] = cosine;
  80. }
  81. void SkColorMatrix::preRotate(Axis axis, float degrees) {
  82. SkColorMatrix tmp;
  83. tmp.setRotate(axis, degrees);
  84. this->preConcat(tmp);
  85. }
  86. void SkColorMatrix::postRotate(Axis axis, float degrees) {
  87. SkColorMatrix tmp;
  88. tmp.setRotate(axis, degrees);
  89. this->postConcat(tmp);
  90. }
  91. void SkColorMatrix::setConcat(const SkColorMatrix& matA, const SkColorMatrix& matB) {
  92. set_concat(fMat, matA.fMat, matB.fMat);
  93. }
  94. ///////////////////////////////////////////////////////////////////////////////
  95. static void setrow(float row[], float r, float g, float b) {
  96. row[0] = r;
  97. row[1] = g;
  98. row[2] = b;
  99. }
  100. static const float kHueR = 0.213f;
  101. static const float kHueG = 0.715f;
  102. static const float kHueB = 0.072f;
  103. void SkColorMatrix::setSaturation(float sat) {
  104. memset(fMat, 0, sizeof(fMat));
  105. const float R = kHueR * (1 - sat);
  106. const float G = kHueG * (1 - sat);
  107. const float B = kHueB * (1 - sat);
  108. setrow(fMat + 0, R + sat, G, B);
  109. setrow(fMat + 5, R, G + sat, B);
  110. setrow(fMat + 10, R, G, B + sat);
  111. fMat[kA_Scale] = 1;
  112. }
  113. static const float kR2Y = 0.299f;
  114. static const float kG2Y = 0.587f;
  115. static const float kB2Y = 0.114f;
  116. static const float kR2U = -0.16874f;
  117. static const float kG2U = -0.33126f;
  118. static const float kB2U = 0.5f;
  119. static const float kR2V = 0.5f;
  120. static const float kG2V = -0.41869f;
  121. static const float kB2V = -0.08131f;
  122. void SkColorMatrix::setRGB2YUV() {
  123. memset(fMat, 0, sizeof(fMat));
  124. setrow(fMat + 0, kR2Y, kG2Y, kB2Y);
  125. setrow(fMat + 5, kR2U, kG2U, kB2U);
  126. setrow(fMat + 10, kR2V, kG2V, kB2V);
  127. fMat[kA_Scale] = 1;
  128. }
  129. static const float kV2R = 1.402f;
  130. static const float kU2G = -0.34414f;
  131. static const float kV2G = -0.71414f;
  132. static const float kU2B = 1.772f;
  133. void SkColorMatrix::setYUV2RGB() {
  134. memset(fMat, 0, sizeof(fMat));
  135. setrow(fMat + 0, 1, 0, kV2R);
  136. setrow(fMat + 5, 1, kU2G, kV2G);
  137. setrow(fMat + 10, 1, kU2B, 0);
  138. fMat[kA_Scale] = 1;
  139. }