| 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 234 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 258 | 260 |
| 259 gfx::Size ImageSkia::size() const { | 261 gfx::Size ImageSkia::size() const { |
| 260 return gfx::Size(width(), height()); | 262 return gfx::Size(width(), height()); |
| 261 } | 263 } |
| 262 | 264 |
| 263 int ImageSkia::height() const { | 265 int ImageSkia::height() const { |
| 264 return isNull() ? 0 : storage_->size().height(); | 266 return isNull() ? 0 : storage_->size().height(); |
| 265 } | 267 } |
| 266 | 268 |
| 267 bool ImageSkia::extractSubset(ImageSkia* dst, const SkIRect& subset) const { | 269 bool ImageSkia::extractSubset(ImageSkia* dst, const SkIRect& subset) const { |
| 268 if (isNull()) | 270 gfx::Rect rect(subset.x(), subset.y(), subset.width(), subset.height()); |
| 269 return false; | 271 *dst = ImageSkiaOperations::ExtractSubset(*this, rect); |
| 270 ImageSkia image; | 272 return (!dst->isNull()); |
| 271 ImageSkiaReps& image_reps = storage_->image_reps(); | |
| 272 for (ImageSkiaReps::iterator it = image_reps.begin(); | |
| 273 it != image_reps.end(); ++it) { | |
| 274 const ImageSkiaRep& image_rep = *it; | |
| 275 float dip_scale = image_rep.GetScale(); | |
| 276 // Rounding boundary in case of a non-integer scale factor. | |
| 277 int x = static_cast<int>(subset.left() * dip_scale + 0.5); | |
| 278 int y = static_cast<int>(subset.top() * dip_scale + 0.5); | |
| 279 int w = static_cast<int>(subset.width() * dip_scale + 0.5); | |
| 280 int h = static_cast<int>(subset.height() * dip_scale + 0.5); | |
| 281 SkBitmap dst_bitmap; | |
| 282 SkIRect scaled_subset = SkIRect::MakeXYWH(x, y, w, h); | |
| 283 if (image_rep.sk_bitmap().extractSubset(&dst_bitmap, scaled_subset)) | |
| 284 image.AddRepresentation(ImageSkiaRep(dst_bitmap, | |
| 285 image_rep.scale_factor())); | |
| 286 } | |
| 287 if (image.empty()) | |
| 288 return false; | |
| 289 | |
| 290 *dst = image; | |
| 291 return true; | |
| 292 } | 273 } |
| 293 | 274 |
| 294 std::vector<ImageSkiaRep> ImageSkia::image_reps() const { | 275 std::vector<ImageSkiaRep> ImageSkia::image_reps() const { |
| 295 if (isNull()) | 276 if (isNull()) |
| 296 return std::vector<ImageSkiaRep>(); | 277 return std::vector<ImageSkiaRep>(); |
| 297 | 278 |
| 298 return storage_->image_reps(); | 279 return storage_->image_reps(); |
| 299 } | 280 } |
| 300 | 281 |
| 301 const SkBitmap* ImageSkia::bitmap() const { | 282 const SkBitmap* ImageSkia::bitmap() const { |
| (...skipping 13 matching lines...) Expand all Loading... |
| 315 if (image_rep.sk_bitmap().empty()) { | 296 if (image_rep.sk_bitmap().empty()) { |
| 316 storage_ = NULL; | 297 storage_ = NULL; |
| 317 return; | 298 return; |
| 318 } | 299 } |
| 319 storage_ = new internal::ImageSkiaStorage( | 300 storage_ = new internal::ImageSkiaStorage( |
| 320 NULL, gfx::Size(image_rep.GetWidth(), image_rep.GetHeight())); | 301 NULL, gfx::Size(image_rep.GetWidth(), image_rep.GetHeight())); |
| 321 storage_->image_reps().push_back(image_rep); | 302 storage_->image_reps().push_back(image_rep); |
| 322 } | 303 } |
| 323 | 304 |
| 324 } // namespace gfx | 305 } // namespace gfx |
| OLD | NEW |