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 const std::vector<SkBitmap>& bitmaps = storage_->bitmaps(); |
165 *dst = ImageSkia(dst_bitmap); | 166 for (std::vector<SkBitmap>::const_iterator it = bitmaps.begin(); |
166 return return_value; | 167 it != bitmaps.end(); ++it) { |
| 168 const SkBitmap& bitmap = *it; |
| 169 int px_width = it->width(); |
| 170 float dip_scale = static_cast<float>(px_width) / |
| 171 static_cast<float>(dip_width); |
| 172 // Rounding boundary in case of a non-integer scale factor. |
| 173 int x = static_cast<int>(subset.left() * dip_scale + 0.5); |
| 174 int y = static_cast<int>(subset.top() * dip_scale + 0.5); |
| 175 int w = static_cast<int>(subset.width() * dip_scale + 0.5); |
| 176 int h = static_cast<int>(subset.height() * dip_scale + 0.5); |
| 177 SkBitmap dst_bitmap; |
| 178 SkIRect scaled_subset = SkIRect::MakeXYWH(x, y, w, h); |
| 179 if (bitmap.extractSubset(&dst_bitmap, scaled_subset)) |
| 180 image.AddBitmapForScale(dst_bitmap, dip_scale); |
| 181 } |
| 182 if (image.empty()) |
| 183 return false; |
| 184 |
| 185 *dst = image; |
| 186 return true; |
167 } | 187 } |
168 | 188 |
169 const std::vector<SkBitmap> ImageSkia::bitmaps() const { | 189 const std::vector<SkBitmap> ImageSkia::bitmaps() const { |
170 return storage_->bitmaps(); | 190 return storage_->bitmaps(); |
171 } | 191 } |
172 | 192 |
173 const SkBitmap* ImageSkia::bitmap() const { | 193 const SkBitmap* ImageSkia::bitmap() const { |
174 if (isNull()) { | 194 if (isNull()) { |
175 // Callers expect an SkBitmap even if it is |isNull()|. | 195 // Callers expect an SkBitmap even if it is |isNull()|. |
176 // TODO(pkotwicz): Fix this. | 196 // TODO(pkotwicz): Fix this. |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
223 | 243 |
224 if (closest_index >= 0) { | 244 if (closest_index >= 0) { |
225 *bitmap_scale_factor = static_cast<float>(bitmaps[closest_index].width()) / | 245 *bitmap_scale_factor = static_cast<float>(bitmaps[closest_index].width()) / |
226 width(); | 246 width(); |
227 } | 247 } |
228 | 248 |
229 return closest_index; | 249 return closest_index; |
230 } | 250 } |
231 | 251 |
232 } // namespace gfx | 252 } // namespace gfx |
OLD | NEW |