| 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 199 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 223 | 225 |
| 224 gfx::Size ImageSkia::size() const { | 226 gfx::Size ImageSkia::size() const { |
| 225 return gfx::Size(width(), height()); | 227 return gfx::Size(width(), height()); |
| 226 } | 228 } |
| 227 | 229 |
| 228 int ImageSkia::height() const { | 230 int ImageSkia::height() const { |
| 229 return isNull() ? 0 : storage_->size().height(); | 231 return isNull() ? 0 : storage_->size().height(); |
| 230 } | 232 } |
| 231 | 233 |
| 232 bool ImageSkia::extractSubset(ImageSkia* dst, const SkIRect& subset) const { | 234 bool ImageSkia::extractSubset(ImageSkia* dst, const SkIRect& subset) const { |
| 233 if (isNull()) | 235 gfx::Rect rect(subset.x(), subset.y(), subset.width(), subset.height()); |
| 234 return false; | 236 *dst = ImageSkiaOperations::ExtractSubset(*this, rect); |
| 235 ImageSkia image; | 237 return (!dst->isNull()); |
| 236 ImageSkiaReps& image_reps = storage_->image_reps(); | |
| 237 for (ImageSkiaReps::iterator it = image_reps.begin(); | |
| 238 it != image_reps.end(); ++it) { | |
| 239 const ImageSkiaRep& image_rep = *it; | |
| 240 float dip_scale = image_rep.GetScale(); | |
| 241 // Rounding boundary in case of a non-integer scale factor. | |
| 242 int x = static_cast<int>(subset.left() * dip_scale + 0.5); | |
| 243 int y = static_cast<int>(subset.top() * dip_scale + 0.5); | |
| 244 int w = static_cast<int>(subset.width() * dip_scale + 0.5); | |
| 245 int h = static_cast<int>(subset.height() * dip_scale + 0.5); | |
| 246 SkBitmap dst_bitmap; | |
| 247 SkIRect scaled_subset = SkIRect::MakeXYWH(x, y, w, h); | |
| 248 if (image_rep.sk_bitmap().extractSubset(&dst_bitmap, scaled_subset)) | |
| 249 image.AddRepresentation(ImageSkiaRep(dst_bitmap, | |
| 250 image_rep.scale_factor())); | |
| 251 } | |
| 252 if (image.empty()) | |
| 253 return false; | |
| 254 | |
| 255 *dst = image; | |
| 256 return true; | |
| 257 } | 238 } |
| 258 | 239 |
| 259 std::vector<ImageSkiaRep> ImageSkia::image_reps() const { | 240 std::vector<ImageSkiaRep> ImageSkia::image_reps() const { |
| 260 if (isNull()) | 241 if (isNull()) |
| 261 return std::vector<ImageSkiaRep>(); | 242 return std::vector<ImageSkiaRep>(); |
| 262 | 243 |
| 263 return storage_->image_reps(); | 244 return storage_->image_reps(); |
| 264 } | 245 } |
| 265 | 246 |
| 266 const SkBitmap* ImageSkia::bitmap() const { | 247 const SkBitmap* ImageSkia::bitmap() const { |
| (...skipping 11 matching lines...) Expand all Loading... |
| 278 if (image_rep.sk_bitmap().empty()) { | 259 if (image_rep.sk_bitmap().empty()) { |
| 279 storage_ = NULL; | 260 storage_ = NULL; |
| 280 return; | 261 return; |
| 281 } | 262 } |
| 282 storage_ = new internal::ImageSkiaStorage( | 263 storage_ = new internal::ImageSkiaStorage( |
| 283 NULL, gfx::Size(image_rep.GetWidth(), image_rep.GetHeight())); | 264 NULL, gfx::Size(image_rep.GetWidth(), image_rep.GetHeight())); |
| 284 storage_->image_reps().push_back(image_rep); | 265 storage_->image_reps().push_back(image_rep); |
| 285 } | 266 } |
| 286 | 267 |
| 287 } // namespace gfx | 268 } // namespace gfx |
| OLD | NEW |