SkEmbossMask.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /*
  2. * Copyright 2006 The Android Open Source Project
  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 "src/effects/SkEmbossMask.h"
  8. #include "include/core/SkMath.h"
  9. #include "include/private/SkFixed.h"
  10. #include "include/private/SkTo.h"
  11. #include "src/core/SkMathPriv.h"
  12. static inline int nonzero_to_one(int x) {
  13. #if 0
  14. return x != 0;
  15. #else
  16. return ((unsigned)(x | -x)) >> 31;
  17. #endif
  18. }
  19. static inline int neq_to_one(int x, int max) {
  20. #if 0
  21. return x != max;
  22. #else
  23. SkASSERT(x >= 0 && x <= max);
  24. return ((unsigned)(x - max)) >> 31;
  25. #endif
  26. }
  27. static inline int neq_to_mask(int x, int max) {
  28. #if 0
  29. return -(x != max);
  30. #else
  31. SkASSERT(x >= 0 && x <= max);
  32. return (x - max) >> 31;
  33. #endif
  34. }
  35. static inline unsigned div255(unsigned x) {
  36. SkASSERT(x <= (255*255));
  37. return x * ((1 << 24) / 255) >> 24;
  38. }
  39. #define kDelta 32 // small enough to show off angle differences
  40. void SkEmbossMask::Emboss(SkMask* mask, const SkEmbossMaskFilter::Light& light) {
  41. SkASSERT(mask->fFormat == SkMask::k3D_Format);
  42. int specular = light.fSpecular;
  43. int ambient = light.fAmbient;
  44. SkFixed lx = SkScalarToFixed(light.fDirection[0]);
  45. SkFixed ly = SkScalarToFixed(light.fDirection[1]);
  46. SkFixed lz = SkScalarToFixed(light.fDirection[2]);
  47. SkFixed lz_dot_nz = lz * kDelta;
  48. int lz_dot8 = lz >> 8;
  49. size_t planeSize = mask->computeImageSize();
  50. uint8_t* alpha = mask->fImage;
  51. uint8_t* multiply = (uint8_t*)alpha + planeSize;
  52. uint8_t* additive = multiply + planeSize;
  53. int rowBytes = mask->fRowBytes;
  54. int maxy = mask->fBounds.height() - 1;
  55. int maxx = mask->fBounds.width() - 1;
  56. int prev_row = 0;
  57. for (int y = 0; y <= maxy; y++) {
  58. int next_row = neq_to_mask(y, maxy) & rowBytes;
  59. for (int x = 0; x <= maxx; x++) {
  60. int nx = alpha[x + neq_to_one(x, maxx)] - alpha[x - nonzero_to_one(x)];
  61. int ny = alpha[x + next_row] - alpha[x - prev_row];
  62. SkFixed numer = lx * nx + ly * ny + lz_dot_nz;
  63. int mul = ambient;
  64. int add = 0;
  65. if (numer > 0) { // preflight when numer/denom will be <= 0
  66. int denom = SkSqrt32(nx * nx + ny * ny + kDelta*kDelta);
  67. SkFixed dot = numer / denom;
  68. dot >>= 8; // now dot is 2^8 instead of 2^16
  69. mul = SkMin32(mul + dot, 255);
  70. // now for the reflection
  71. // R = 2 (Light * Normal) Normal - Light
  72. // hilite = R * Eye(0, 0, 1)
  73. int hilite = (2 * dot - lz_dot8) * lz_dot8 >> 8;
  74. if (hilite > 0) {
  75. // pin hilite to 255, since our fast math is also a little sloppy
  76. hilite = SkClampMax(hilite, 255);
  77. // specular is 4.4
  78. // would really like to compute the fractional part of this
  79. // and then possibly cache a 256 table for a given specular
  80. // value in the light, and just pass that in to this function.
  81. add = hilite;
  82. for (int i = specular >> 4; i > 0; --i) {
  83. add = div255(add * hilite);
  84. }
  85. }
  86. }
  87. multiply[x] = SkToU8(mul);
  88. additive[x] = SkToU8(add);
  89. }
  90. alpha += rowBytes;
  91. multiply += rowBytes;
  92. additive += rowBytes;
  93. prev_row = rowBytes;
  94. }
  95. }