| 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 <algorithm> | 7 #include <algorithm> |
| 8 #include <math.h> | 8 #include <math.h> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 44 return 255; | 44 return 255; |
| 45 return static_cast<uint8>(new_channel + 0.5); | 45 return static_cast<uint8>(new_channel + 0.5); |
| 46 } | 46 } |
| 47 | 47 |
| 48 inline uint8 ProcessColor(uint8 src_color, uint8 dest_color, uint8 alpha) { | 48 inline uint8 ProcessColor(uint8 src_color, uint8 dest_color, uint8 alpha) { |
| 49 uint32 processed = static_cast<uint32>(src_color) * alpha + | 49 uint32 processed = static_cast<uint32>(src_color) * alpha + |
| 50 static_cast<uint32>(dest_color) * (0xFF - alpha); | 50 static_cast<uint32>(dest_color) * (0xFF - alpha); |
| 51 return static_cast<uint8>((processed / 0xFF) & 0xFF); | 51 return static_cast<uint8>((processed / 0xFF) & 0xFF); |
| 52 } | 52 } |
| 53 | 53 |
| 54 inline bool ImageDataContainsRect(const pp::ImageData& image_data, |
| 55 const pp::Rect& rect) { |
| 56 return rect.width() >= 0 && rect.height() >= 0 && |
| 57 pp::Rect(image_data.size()).Contains(rect); |
| 58 } |
| 59 |
| 54 bool AlphaBlend(const pp::ImageData& src, const pp::Rect& src_rc, | 60 bool AlphaBlend(const pp::ImageData& src, const pp::Rect& src_rc, |
| 55 pp::ImageData* dest, const pp::Point& dest_origin, | 61 pp::ImageData* dest, const pp::Point& dest_origin, |
| 56 uint8 alpha_adjustment) { | 62 uint8 alpha_adjustment) { |
| 57 const uint32_t* src_origin_pixel = src.GetAddr32(src_rc.point()); | 63 const uint32_t* src_origin_pixel = src.GetAddr32(src_rc.point()); |
| 58 uint32_t* dest_origin_pixel = dest->GetAddr32(dest_origin); | 64 uint32_t* dest_origin_pixel = dest->GetAddr32(dest_origin); |
| 59 | 65 |
| 60 int height = src_rc.height(); | 66 int height = src_rc.height(); |
| 61 int width = src_rc.width(); | 67 int width = src_rc.width(); |
| 62 for (int y = 0; y < height; y++) { | 68 for (int y = 0; y < height; y++) { |
| 63 const uint32_t* src_pixel = src_origin_pixel; | 69 const uint32_t* src_pixel = src_origin_pixel; |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 138 start_color, end_color, horizontal); | 144 start_color, end_color, horizontal); |
| 139 | 145 |
| 140 pp::Rect copy_rc(draw_rc); | 146 pp::Rect copy_rc(draw_rc); |
| 141 copy_rc.Offset(-gradient_rc.x(), -gradient_rc.y()); | 147 copy_rc.Offset(-gradient_rc.x(), -gradient_rc.y()); |
| 142 AlphaBlend(gradient, copy_rc, image, draw_rc.point(), transparency); | 148 AlphaBlend(gradient, copy_rc, image, draw_rc.point(), transparency); |
| 143 } | 149 } |
| 144 | 150 |
| 145 void CopyImage(const pp::ImageData& src, const pp::Rect& src_rc, | 151 void CopyImage(const pp::ImageData& src, const pp::Rect& src_rc, |
| 146 pp::ImageData* dest, const pp::Rect& dest_rc, | 152 pp::ImageData* dest, const pp::Rect& dest_rc, |
| 147 bool stretch) { | 153 bool stretch) { |
| 148 DCHECK(src_rc.width() <= dest_rc.width() && | 154 if (src_rc.IsEmpty() || !ImageDataContainsRect(src, src_rc)) |
| 149 src_rc.height() <= dest_rc.height()); | 155 return; |
| 150 if (src_rc.IsEmpty()) | 156 |
| 157 pp::Rect stretched_rc(dest_rc.point(), |
| 158 stretch ? dest_rc.size() : src_rc.size()); |
| 159 if (stretched_rc.IsEmpty() || !ImageDataContainsRect(*dest, stretched_rc)) |
| 151 return; | 160 return; |
| 152 | 161 |
| 153 const uint32_t* src_origin_pixel = src.GetAddr32(src_rc.point()); | 162 const uint32_t* src_origin_pixel = src.GetAddr32(src_rc.point()); |
| 154 uint32_t* dest_origin_pixel = dest->GetAddr32(dest_rc.point()); | 163 uint32_t* dest_origin_pixel = dest->GetAddr32(dest_rc.point()); |
| 155 if (stretch) { | 164 if (stretch) { |
| 156 double x_ratio = static_cast<double>(src_rc.width()) / dest_rc.width(); | 165 double x_ratio = static_cast<double>(src_rc.width()) / dest_rc.width(); |
| 157 double y_ratio = static_cast<double>(src_rc.height()) / dest_rc.height(); | 166 double y_ratio = static_cast<double>(src_rc.height()) / dest_rc.height(); |
| 158 int32_t height = dest_rc.height(); | 167 int32_t height = dest_rc.height(); |
| 159 int32_t width = dest_rc.width(); | 168 int32_t width = dest_rc.width(); |
| 160 for (int32_t y = 0; y < height; ++y) { | 169 for (int32_t y = 0; y < height; ++y) { |
| (...skipping 158 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 319 PaintShadow(image, rc.Intersect(clip_rc), shadow_rc, matrix); | 328 PaintShadow(image, rc.Intersect(clip_rc), shadow_rc, matrix); |
| 320 | 329 |
| 321 // Fill right part. | 330 // Fill right part. |
| 322 rc = pp::Rect(object_rc.right(), object_rc.y(), | 331 rc = pp::Rect(object_rc.right(), object_rc.y(), |
| 323 shadow_rc.right() - object_rc.right(), object_rc.height()); | 332 shadow_rc.right() - object_rc.right(), object_rc.height()); |
| 324 PaintShadow(image, rc.Intersect(clip_rc), shadow_rc, matrix); | 333 PaintShadow(image, rc.Intersect(clip_rc), shadow_rc, matrix); |
| 325 } | 334 } |
| 326 | 335 |
| 327 } // namespace chrome_pdf | 336 } // namespace chrome_pdf |
| 328 | 337 |
| OLD | NEW |