| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "pdf/draw_utils.h" | 5 #include "pdf/draw_utils.h" |
| 6 | 6 |
| 7 #include <math.h> | 7 #include <math.h> |
| 8 #include <stddef.h> | 8 #include <stddef.h> |
| 9 #include <stdint.h> | 9 #include <stdint.h> |
| 10 #include <algorithm> | 10 #include <algorithm> |
| (...skipping 18 matching lines...) Expand all Loading... |
| 29 | 29 |
| 30 inline uint8_t GetAlpha(const uint32_t& pixel) { | 30 inline uint8_t GetAlpha(const uint32_t& pixel) { |
| 31 return static_cast<uint8_t>((pixel >> 24) & 0xFF); | 31 return static_cast<uint8_t>((pixel >> 24) & 0xFF); |
| 32 } | 32 } |
| 33 | 33 |
| 34 inline uint32_t MakePixel(uint8_t red, | 34 inline uint32_t MakePixel(uint8_t red, |
| 35 uint8_t green, | 35 uint8_t green, |
| 36 uint8_t blue, | 36 uint8_t blue, |
| 37 uint8_t alpha) { | 37 uint8_t alpha) { |
| 38 return (static_cast<uint32_t>(alpha) << 24) | | 38 return (static_cast<uint32_t>(alpha) << 24) | |
| 39 (static_cast<uint32_t>(red) << 16) | | 39 (static_cast<uint32_t>(red) << 16) | |
| 40 (static_cast<uint32_t>(green) << 8) | | 40 (static_cast<uint32_t>(green) << 8) | static_cast<uint32_t>(blue); |
| 41 static_cast<uint32_t>(blue); | |
| 42 } | 41 } |
| 43 | 42 |
| 44 inline uint8_t ProcessColor(uint8_t src_color, | 43 inline uint8_t ProcessColor(uint8_t src_color, |
| 45 uint8_t dest_color, | 44 uint8_t dest_color, |
| 46 uint8_t alpha) { | 45 uint8_t alpha) { |
| 47 uint32_t processed = static_cast<uint32_t>(src_color) * alpha + | 46 uint32_t processed = static_cast<uint32_t>(src_color) * alpha + |
| 48 static_cast<uint32_t>(dest_color) * (0xFF - alpha); | 47 static_cast<uint32_t>(dest_color) * (0xFF - alpha); |
| 49 return static_cast<uint8_t>((processed / 0xFF) & 0xFF); | 48 return static_cast<uint8_t>((processed / 0xFF) & 0xFF); |
| 50 } | 49 } |
| 51 | 50 |
| (...skipping 10 matching lines...) Expand all Loading... |
| 62 | 61 |
| 63 double r = static_cast<double>(depth_); | 62 double r = static_cast<double>(depth_); |
| 64 double coef = 256.0 / pow(r, factor); | 63 double coef = 256.0 / pow(r, factor); |
| 65 | 64 |
| 66 for (uint32_t y = 0; y < depth_; y++) { | 65 for (uint32_t y = 0; y < depth_; y++) { |
| 67 // Since matrix is symmetrical, we can reduce the number of calculations | 66 // Since matrix is symmetrical, we can reduce the number of calculations |
| 68 // by mirroring results. | 67 // by mirroring results. |
| 69 for (uint32_t x = 0; x <= y; x++) { | 68 for (uint32_t x = 0; x <= y; x++) { |
| 70 // Fill cache if needed. | 69 // Fill cache if needed. |
| 71 if (pow_pv[x] == 0.0) | 70 if (pow_pv[x] == 0.0) |
| 72 pow_pv[x] = pow(x, pv); | 71 pow_pv[x] = pow(x, pv); |
| 73 if (pow_pv[y] == 0.0) | 72 if (pow_pv[y] == 0.0) |
| 74 pow_pv[y] = pow(y, pv); | 73 pow_pv[y] = pow(y, pv); |
| 75 | 74 |
| 76 // v - is a value for the smoothing function. | 75 // v - is a value for the smoothing function. |
| 77 // If x == 0 simplify calculations. | 76 // If x == 0 simplify calculations. |
| 78 double v = (x == 0) ? y : pow(pow_pv[x] + pow_pv[y], 1 / pv); | 77 double v = (x == 0) ? y : pow(pow_pv[x] + pow_pv[y], 1 / pv); |
| 79 | 78 |
| 80 // Smoothing function. | 79 // Smoothing function. |
| 81 // If factor == 1, smoothing will be linear from 0 to the end, | 80 // If factor == 1, smoothing will be linear from 0 to the end, |
| 82 // if 0 < factor < 1, smoothing will drop faster near 0. | 81 // if 0 < factor < 1, smoothing will drop faster near 0. |
| 83 // if factor > 1, smoothing will drop faster near the end (depth). | 82 // if factor > 1, smoothing will drop faster near the end (depth). |
| 84 double f = 256.0 - coef * pow(v, factor); | 83 double f = 256.0 - coef * pow(v, factor); |
| (...skipping 11 matching lines...) Expand all Loading... |
| 96 uint8_t blue = ProcessColor(0, GetBlue(background), alpha); | 95 uint8_t blue = ProcessColor(0, GetBlue(background), alpha); |
| 97 uint32_t pixel = MakePixel(red, green, blue, GetAlpha(background)); | 96 uint32_t pixel = MakePixel(red, green, blue, GetAlpha(background)); |
| 98 | 97 |
| 99 // Mirror matrix. | 98 // Mirror matrix. |
| 100 matrix_[y * depth_ + x] = pixel; | 99 matrix_[y * depth_ + x] = pixel; |
| 101 matrix_[x * depth_ + y] = pixel; | 100 matrix_[x * depth_ + y] = pixel; |
| 102 } | 101 } |
| 103 } | 102 } |
| 104 } | 103 } |
| 105 | 104 |
| 106 ShadowMatrix::~ShadowMatrix() { | 105 ShadowMatrix::~ShadowMatrix() {} |
| 107 } | |
| 108 | 106 |
| 109 void PaintShadow(pp::ImageData* image, | 107 void PaintShadow(pp::ImageData* image, |
| 110 const pp::Rect& clip_rc, | 108 const pp::Rect& clip_rc, |
| 111 const pp::Rect& shadow_rc, | 109 const pp::Rect& shadow_rc, |
| 112 const ShadowMatrix& matrix) { | 110 const ShadowMatrix& matrix) { |
| 113 pp::Rect draw_rc = shadow_rc.Intersect(clip_rc); | 111 pp::Rect draw_rc = shadow_rc.Intersect(clip_rc); |
| 114 if (draw_rc.IsEmpty()) | 112 if (draw_rc.IsEmpty()) |
| 115 return; | 113 return; |
| 116 | 114 |
| 117 int32_t depth = static_cast<int32_t>(matrix.depth()); | 115 int32_t depth = static_cast<int32_t>(matrix.depth()); |
| (...skipping 27 matching lines...) Expand all Loading... |
| 145 const ShadowMatrix& matrix) { | 143 const ShadowMatrix& matrix) { |
| 146 if (shadow_rc == object_rc) | 144 if (shadow_rc == object_rc) |
| 147 return; // Nothing to paint. | 145 return; // Nothing to paint. |
| 148 | 146 |
| 149 // Fill top part. | 147 // Fill top part. |
| 150 pp::Rect rc(shadow_rc.point(), | 148 pp::Rect rc(shadow_rc.point(), |
| 151 pp::Size(shadow_rc.width(), object_rc.y() - shadow_rc.y())); | 149 pp::Size(shadow_rc.width(), object_rc.y() - shadow_rc.y())); |
| 152 PaintShadow(image, rc.Intersect(clip_rc), shadow_rc, matrix); | 150 PaintShadow(image, rc.Intersect(clip_rc), shadow_rc, matrix); |
| 153 | 151 |
| 154 // Fill bottom part. | 152 // Fill bottom part. |
| 155 rc = pp::Rect(shadow_rc.x(), object_rc.bottom(), | 153 rc = pp::Rect(shadow_rc.x(), object_rc.bottom(), shadow_rc.width(), |
| 156 shadow_rc.width(), shadow_rc.bottom() - object_rc.bottom()); | 154 shadow_rc.bottom() - object_rc.bottom()); |
| 157 PaintShadow(image, rc.Intersect(clip_rc), shadow_rc, matrix); | 155 PaintShadow(image, rc.Intersect(clip_rc), shadow_rc, matrix); |
| 158 | 156 |
| 159 // Fill left part. | 157 // Fill left part. |
| 160 rc = pp::Rect(shadow_rc.x(), object_rc.y(), | 158 rc = pp::Rect(shadow_rc.x(), object_rc.y(), object_rc.x() - shadow_rc.x(), |
| 161 object_rc.x() - shadow_rc.x(), object_rc.height()); | 159 object_rc.height()); |
| 162 PaintShadow(image, rc.Intersect(clip_rc), shadow_rc, matrix); | 160 PaintShadow(image, rc.Intersect(clip_rc), shadow_rc, matrix); |
| 163 | 161 |
| 164 // Fill right part. | 162 // Fill right part. |
| 165 rc = pp::Rect(object_rc.right(), object_rc.y(), | 163 rc = pp::Rect(object_rc.right(), object_rc.y(), |
| 166 shadow_rc.right() - object_rc.right(), object_rc.height()); | 164 shadow_rc.right() - object_rc.right(), object_rc.height()); |
| 167 PaintShadow(image, rc.Intersect(clip_rc), shadow_rc, matrix); | 165 PaintShadow(image, rc.Intersect(clip_rc), shadow_rc, matrix); |
| 168 } | 166 } |
| 169 | 167 |
| 170 } // namespace chrome_pdf | 168 } // namespace chrome_pdf |
| 171 | |
| OLD | NEW |