Chromium Code Reviews| 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 <limits> | 7 #include <limits> |
| 8 #include <cmath> | 8 #include <cmath> |
| 9 | 9 |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| 11 #include "ui/gfx/size.h" | 11 #include "ui/gfx/size.h" |
| 12 #include "ui/gfx/skia_util.h" | |
| 12 | 13 |
| 13 namespace gfx { | 14 namespace gfx { |
| 14 | 15 |
| 15 namespace internal { | 16 namespace internal { |
| 16 | 17 |
| 17 // A helper class such that ImageSkia can be cheaply copied. ImageSkia holds a | 18 // A helper class such that ImageSkia can be cheaply copied. ImageSkia holds a |
| 18 // refptr instance of ImageSkiaStorage, which in turn holds all of ImageSkia's | 19 // refptr instance of ImageSkiaStorage, which in turn holds all of ImageSkia's |
| 19 // information. | 20 // information. |
| 20 class ImageSkiaStorage : public base::RefCounted<ImageSkiaStorage> { | 21 class ImageSkiaStorage : public base::RefCounted<ImageSkiaStorage> { |
| 21 public: | 22 public: |
| (...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 152 return isNull() ? 0 : storage_->size().width(); | 153 return isNull() ? 0 : storage_->size().width(); |
| 153 } | 154 } |
| 154 | 155 |
| 155 int ImageSkia::height() const { | 156 int ImageSkia::height() const { |
| 156 return isNull() ? 0 : storage_->size().height(); | 157 return isNull() ? 0 : storage_->size().height(); |
| 157 } | 158 } |
| 158 | 159 |
| 159 bool ImageSkia::extractSubset(ImageSkia* dst, const SkIRect& subset) const { | 160 bool ImageSkia::extractSubset(ImageSkia* dst, const SkIRect& subset) const { |
| 160 if (isNull()) | 161 if (isNull()) |
| 161 return false; | 162 return false; |
| 162 SkBitmap dst_bitmap; | 163 gfx::ImageSkia image; |
| 163 bool return_value = storage_->bitmaps()[0].extractSubset(&dst_bitmap, | 164 int dip_width = width(); |
| 164 subset); | 165 std::vector<SkBitmap> bitmaps = storage_->bitmaps(); |
| 165 *dst = ImageSkia(dst_bitmap); | 166 for (std::vector<SkBitmap> ::const_iterator it = bitmaps.begin(); |
|
Robert Sesek
2012/06/06 19:44:39
nit: no space before :
kevers
2012/06/07 12:58:03
Done.
| |
| 166 return return_value; | 167 it != bitmaps.end(); ++it) { |
| 168 SkBitmap bitmap = *it; | |
| 169 int px_width = it->width(); | |
| 170 float dip_scale = (float) px_width / (float) dip_width; | |
|
Robert Sesek
2012/06/06 19:44:39
C-style cast
kevers
2012/06/07 12:58:03
Fixed.
| |
| 171 int x = static_cast<int>(subset.left() * dip_scale + 0.5); | |
| 172 int y = static_cast<int>(subset.top() * dip_scale + 0.5); | |
| 173 int w = static_cast<int>(subset.width() * dip_scale + 0.5); | |
| 174 int h = static_cast<int>(subset.height() * dip_scale + 0.5); | |
| 175 SkBitmap dst_bitmap; | |
| 176 SkIRect scaled_subset = SkIRect::MakeXYWH(x, y, w, h); | |
| 177 if (bitmap.extractSubset(&dst_bitmap, scaled_subset)) | |
| 178 image.AddBitmapForScale(dst_bitmap, dip_scale); | |
| 179 } | |
| 180 if (image.empty()) | |
| 181 return false; | |
| 182 | |
| 183 *dst = image; | |
| 184 return true; | |
| 167 } | 185 } |
| 168 | 186 |
| 169 const std::vector<SkBitmap> ImageSkia::bitmaps() const { | 187 const std::vector<SkBitmap> ImageSkia::bitmaps() const { |
| 170 return storage_->bitmaps(); | 188 return storage_->bitmaps(); |
| 171 } | 189 } |
| 172 | 190 |
| 173 const SkBitmap* ImageSkia::bitmap() const { | 191 const SkBitmap* ImageSkia::bitmap() const { |
| 174 if (isNull()) { | 192 if (isNull()) { |
| 175 // Callers expect an SkBitmap even if it is |isNull()|. | 193 // Callers expect an SkBitmap even if it is |isNull()|. |
| 176 // TODO(pkotwicz): Fix this. | 194 // TODO(pkotwicz): Fix this. |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 223 | 241 |
| 224 if (closest_index >= 0) { | 242 if (closest_index >= 0) { |
| 225 *bitmap_scale_factor = static_cast<float>(bitmaps[closest_index].width()) / | 243 *bitmap_scale_factor = static_cast<float>(bitmaps[closest_index].width()) / |
| 226 width(); | 244 width(); |
| 227 } | 245 } |
| 228 | 246 |
| 229 return closest_index; | 247 return closest_index; |
| 230 } | 248 } |
| 231 | 249 |
| 232 } // namespace gfx | 250 } // namespace gfx |
| OLD | NEW |