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 bool return_value = true; |
| 163 bool return_value = storage_->bitmaps()[0].extractSubset(&dst_bitmap, | 164 gfx::ImageSkia image; |
| 164 subset); | 165 int dip_width = width(); |
| 165 *dst = ImageSkia(dst_bitmap); | 166 std::vector<SkBitmap> bitmaps = storage_->bitmaps(); |
| 167 for (std::vector<SkBitmap> ::const_iterator it = bitmaps.begin(); | |
| 168 it != bitmaps.end(); ++it) { | |
|
Robert Sesek
2012/06/05 17:52:09
nit: align under s
kevers
2012/06/05 20:29:34
Done.
| |
| 169 SkBitmap bitmap = *it; | |
| 170 int px_width = it->width(); | |
| 171 float dip_scale = (float) px_width / (float) dip_width; | |
|
Robert Sesek
2012/06/05 17:52:09
C-style casts aren't allowed by the style guide.
kevers
2012/06/05 20:29:34
Changed to static_cast.
| |
| 172 int x = (int) (subset.left() * dip_scale + 0.5); | |
| 173 int y = (int) (subset.top() * dip_scale + 0.5); | |
| 174 int w = (int) (subset.width() * dip_scale + 0.5); | |
| 175 int h = (int) (subset.height() * dip_scale + 0.5); | |
| 176 SkBitmap dst_bitmap; | |
| 177 SkIRect scaled_subset = SkIRect::MakeXYWH(x, y, w, h); | |
| 178 return_value &= bitmap.extractSubset(&dst_bitmap, scaled_subset); | |
| 179 image.AddBitmapForScale(dst_bitmap, dip_scale); | |
| 180 } | |
| 181 *dst = image; | |
|
Robert Sesek
2012/06/05 17:52:09
Should you overwrite dst ven if return_value==fals
kevers
2012/06/05 20:29:34
Changed to store only successfully extracted subse
| |
| 166 return return_value; | 182 return return_value; |
| 167 } | 183 } |
| 168 | 184 |
| 169 const std::vector<SkBitmap> ImageSkia::bitmaps() const { | 185 const std::vector<SkBitmap> ImageSkia::bitmaps() const { |
| 170 return storage_->bitmaps(); | 186 return storage_->bitmaps(); |
| 171 } | 187 } |
| 172 | 188 |
| 173 const SkBitmap* ImageSkia::bitmap() const { | 189 const SkBitmap* ImageSkia::bitmap() const { |
| 174 if (isNull()) { | 190 if (isNull()) { |
| 175 // Callers expect an SkBitmap even if it is |isNull()|. | 191 // Callers expect an SkBitmap even if it is |isNull()|. |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 223 | 239 |
| 224 if (closest_index >= 0) { | 240 if (closest_index >= 0) { |
| 225 *bitmap_scale_factor = static_cast<float>(bitmaps[closest_index].width()) / | 241 *bitmap_scale_factor = static_cast<float>(bitmaps[closest_index].width()) / |
| 226 width(); | 242 width(); |
| 227 } | 243 } |
| 228 | 244 |
| 229 return closest_index; | 245 return closest_index; |
| 230 } | 246 } |
| 231 | 247 |
| 232 } // namespace gfx | 248 } // namespace gfx |
| OLD | NEW |