| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "ui/gfx/image/image_skia.h" | 5 #include "ui/gfx/image/image_skia.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <cmath> | 8 #include <cmath> |
| 9 #include <limits> | 9 #include <limits> |
| 10 | 10 |
| 11 #include "base/logging.h" | 11 #include "base/logging.h" |
| 12 #include "base/memory/scoped_ptr.h" | 12 #include "base/memory/scoped_ptr.h" |
| 13 #include "ui/gfx/image/image_skia_operations.h" |
| 13 #include "ui/gfx/image/image_skia_source.h" | 14 #include "ui/gfx/image/image_skia_source.h" |
| 15 #include "ui/gfx/rect.h" |
| 14 #include "ui/gfx/size.h" | 16 #include "ui/gfx/size.h" |
| 15 #include "ui/gfx/skia_util.h" | 17 #include "ui/gfx/skia_util.h" |
| 16 | 18 |
| 17 namespace gfx { | 19 namespace gfx { |
| 18 namespace { | 20 namespace { |
| 19 | 21 |
| 20 // static | 22 // static |
| 21 gfx::ImageSkiaRep& NullImageRep() { | 23 gfx::ImageSkiaRep& NullImageRep() { |
| 22 CR_DEFINE_STATIC_LOCAL(ImageSkiaRep, null_image_rep, ()); | 24 CR_DEFINE_STATIC_LOCAL(ImageSkiaRep, null_image_rep, ()); |
| 23 return null_image_rep; | 25 return null_image_rep; |
| (...skipping 231 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 255 | 257 |
| 256 gfx::Size ImageSkia::size() const { | 258 gfx::Size ImageSkia::size() const { |
| 257 return gfx::Size(width(), height()); | 259 return gfx::Size(width(), height()); |
| 258 } | 260 } |
| 259 | 261 |
| 260 int ImageSkia::height() const { | 262 int ImageSkia::height() const { |
| 261 return isNull() ? 0 : storage_->size().height(); | 263 return isNull() ? 0 : storage_->size().height(); |
| 262 } | 264 } |
| 263 | 265 |
| 264 bool ImageSkia::extractSubset(ImageSkia* dst, const SkIRect& subset) const { | 266 bool ImageSkia::extractSubset(ImageSkia* dst, const SkIRect& subset) const { |
| 265 if (isNull()) | 267 gfx::Rect rect(subset.x(), subset.y(), subset.width(), subset.height()); |
| 266 return false; | 268 *dst = ImageSkiaOperations::ExtractSubset(*this, rect); |
| 267 ImageSkia image; | 269 return (!dst->isNull()); |
| 268 ImageSkiaReps& image_reps = storage_->image_reps(); | |
| 269 for (ImageSkiaReps::iterator it = image_reps.begin(); | |
| 270 it != image_reps.end(); ++it) { | |
| 271 const ImageSkiaRep& image_rep = *it; | |
| 272 float dip_scale = image_rep.GetScale(); | |
| 273 // Rounding boundary in case of a non-integer scale factor. | |
| 274 int x = static_cast<int>(subset.left() * dip_scale + 0.5); | |
| 275 int y = static_cast<int>(subset.top() * dip_scale + 0.5); | |
| 276 int w = static_cast<int>(subset.width() * dip_scale + 0.5); | |
| 277 int h = static_cast<int>(subset.height() * dip_scale + 0.5); | |
| 278 SkBitmap dst_bitmap; | |
| 279 SkIRect scaled_subset = SkIRect::MakeXYWH(x, y, w, h); | |
| 280 if (image_rep.sk_bitmap().extractSubset(&dst_bitmap, scaled_subset)) | |
| 281 image.AddRepresentation(ImageSkiaRep(dst_bitmap, | |
| 282 image_rep.scale_factor())); | |
| 283 } | |
| 284 if (image.empty()) | |
| 285 return false; | |
| 286 | |
| 287 *dst = image; | |
| 288 return true; | |
| 289 } | 270 } |
| 290 | 271 |
| 291 std::vector<ImageSkiaRep> ImageSkia::image_reps() const { | 272 std::vector<ImageSkiaRep> ImageSkia::image_reps() const { |
| 292 if (isNull()) | 273 if (isNull()) |
| 293 return std::vector<ImageSkiaRep>(); | 274 return std::vector<ImageSkiaRep>(); |
| 294 | 275 |
| 295 return storage_->image_reps(); | 276 return storage_->image_reps(); |
| 296 } | 277 } |
| 297 | 278 |
| 298 const SkBitmap* ImageSkia::bitmap() const { | 279 const SkBitmap* ImageSkia::bitmap() const { |
| (...skipping 13 matching lines...) Expand all Loading... |
| 312 if (image_rep.sk_bitmap().empty()) { | 293 if (image_rep.sk_bitmap().empty()) { |
| 313 storage_ = NULL; | 294 storage_ = NULL; |
| 314 return; | 295 return; |
| 315 } | 296 } |
| 316 storage_ = new internal::ImageSkiaStorage( | 297 storage_ = new internal::ImageSkiaStorage( |
| 317 NULL, gfx::Size(image_rep.GetWidth(), image_rep.GetHeight())); | 298 NULL, gfx::Size(image_rep.GetWidth(), image_rep.GetHeight())); |
| 318 storage_->image_reps().push_back(image_rep); | 299 storage_->image_reps().push_back(image_rep); |
| 319 } | 300 } |
| 320 | 301 |
| 321 } // namespace gfx | 302 } // namespace gfx |
| OLD | NEW |