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_util.h" | 5 #include "ui/gfx/image/image_util.h" |
| 6 | 6 |
| 7 #include <stdint.h> | 7 #include <stdint.h> |
| 8 | 8 |
| 9 #include <algorithm> | 9 #include <algorithm> |
| 10 | 10 |
| 11 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
| 12 #include "build/build_config.h" | 12 #include "build/build_config.h" |
| 13 #include "third_party/skia/include/core/SkBitmap.h" | 13 #include "third_party/skia/include/core/SkBitmap.h" |
| 14 #include "ui/gfx/codec/jpeg_codec.h" | 14 #include "ui/gfx/codec/jpeg_codec.h" |
| 15 #include "ui/gfx/image/image.h" | 15 #include "ui/gfx/image/image.h" |
| 16 #include "ui/gfx/image/image_skia.h" | 16 #include "ui/gfx/image/image_skia.h" |
| 17 | 17 |
| 18 namespace { | |
| 19 | |
| 20 // Returns whether column |x| of |bitmap| has any pixels with opacity > min, | |
|
danakj
2016/03/25 23:35:28
min looks like it's hardcoded, maybe it was a para
Peter Kasting
2016/03/26 01:01:57
I actually wrote the comment to describe this hard
| |
| 21 // where min is a small number representing "pretty much trasparent". | |
| 22 bool ColumnHasVisiblePixels(const SkBitmap& bitmap, int x) { | |
| 23 const SkAlpha kMinimumVisibleOpacity = 12; | |
| 24 for (int y = 0; y < bitmap.height(); ++y) { | |
| 25 if (SkColorGetA(bitmap.getColor(x, y)) > kMinimumVisibleOpacity) | |
| 26 return true; | |
| 27 } | |
| 28 return false; | |
| 29 } | |
| 30 | |
| 31 } // namespace | |
| 32 | |
| 18 namespace gfx { | 33 namespace gfx { |
| 19 | 34 |
| 20 const uint32_t kMinimumVisibleOpacity = 12; | |
| 21 | |
| 22 // The iOS implementations of the JPEG functions are in image_util_ios.mm. | 35 // The iOS implementations of the JPEG functions are in image_util_ios.mm. |
| 23 #if !defined(OS_IOS) | 36 #if !defined(OS_IOS) |
| 24 Image ImageFrom1xJPEGEncodedData(const unsigned char* input, | 37 Image ImageFrom1xJPEGEncodedData(const unsigned char* input, |
| 25 size_t input_size) { | 38 size_t input_size) { |
| 26 scoped_ptr<SkBitmap> bitmap(gfx::JPEGCodec::Decode(input, input_size)); | 39 scoped_ptr<SkBitmap> bitmap(gfx::JPEGCodec::Decode(input, input_size)); |
| 27 if (bitmap.get()) | 40 if (bitmap.get()) |
| 28 return Image::CreateFrom1xBitmap(*bitmap); | 41 return Image::CreateFrom1xBitmap(*bitmap); |
| 29 | 42 |
| 30 return Image(); | 43 return Image(); |
| 31 } | 44 } |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 45 | 58 |
| 46 return gfx::JPEGCodec::Encode( | 59 return gfx::JPEGCodec::Encode( |
| 47 reinterpret_cast<unsigned char*>(bitmap.getAddr32(0, 0)), | 60 reinterpret_cast<unsigned char*>(bitmap.getAddr32(0, 0)), |
| 48 gfx::JPEGCodec::FORMAT_SkBitmap, bitmap.width(), | 61 gfx::JPEGCodec::FORMAT_SkBitmap, bitmap.width(), |
| 49 bitmap.height(), | 62 bitmap.height(), |
| 50 static_cast<int>(bitmap.rowBytes()), quality, | 63 static_cast<int>(bitmap.rowBytes()), quality, |
| 51 dst); | 64 dst); |
| 52 } | 65 } |
| 53 #endif // !defined(OS_IOS) | 66 #endif // !defined(OS_IOS) |
| 54 | 67 |
| 55 bool VisibleMargins(const ImageSkia& image, int* leading, int* trailing) { | 68 void GetVisibleMargins(const ImageSkia& image, int* left, int* right) { |
| 56 *leading = 0; | 69 *left = 0; |
| 57 *trailing = std::max(1, image.width()) - 1; | 70 *right = 0; |
| 58 if (!image.HasRepresentation(1.0)) | 71 const SkBitmap& bitmap = image.GetRepresentation(1.0).sk_bitmap(); |
| 59 return false; | 72 if (bitmap.drawsNothing() || bitmap.isOpaque()) |
| 73 return; | |
| 60 | 74 |
| 61 const ImageSkiaRep& rep = image.GetRepresentation(1.0); | 75 SkAutoLockPixels lock(bitmap); |
| 62 if (rep.is_null()) | 76 int x = 0; |
| 63 return false; | 77 for (; x < bitmap.width(); ++x) { |
| 78 if (ColumnHasVisiblePixels(bitmap, x)) { | |
| 79 *left = x; | |
| 80 break; | |
| 81 } | |
| 82 } | |
| 64 | 83 |
| 65 const SkBitmap& bitmap = rep.sk_bitmap(); | 84 if (x == bitmap.width()) { |
| 66 if (bitmap.isNull() || bitmap.width() == 0) | 85 // Image is fully transparent. Divide the width in half, giving the leading |
| 67 return false; | 86 // region the extra pixel for odd widths. |
| 87 *left = (bitmap.width() + 1) / 2; | |
| 88 *right = bitmap.width() - *left; | |
| 89 return; | |
| 90 } | |
| 68 | 91 |
| 69 if (bitmap.isOpaque()) | 92 // Since we already know column *leading is non-transparent, we can avoid |
|
danakj
2016/03/25 23:35:28
leading->left ?
Peter Kasting
2016/03/26 01:01:57
Yeah, oops.
| |
| 70 return true; | 93 // rechecking that column; hence the '>' here. |
| 71 | 94 for (x = bitmap.width() - 1; x > *left; --x) { |
| 72 SkAutoLockPixels l(bitmap); | 95 if (ColumnHasVisiblePixels(bitmap, x)) |
| 73 int inner_min = bitmap.width(); | |
| 74 for (int x = 0; x < bitmap.width(); ++x) { | |
| 75 for (int y = 0; y < bitmap.height(); ++y) { | |
| 76 if (SkColorGetA(bitmap.getColor(x, y)) > kMinimumVisibleOpacity) { | |
| 77 inner_min = x; | |
| 78 break; | |
| 79 } | |
| 80 } | |
| 81 if (inner_min < bitmap.width()) | |
| 82 break; | 96 break; |
| 83 } | 97 } |
| 84 | 98 *right = bitmap.width() - 1 - x; |
| 85 int inner_max = -1; | |
| 86 for (int x = bitmap.width() - 1; x > inner_min; --x) { | |
| 87 for (int y = 0; y < bitmap.height(); ++y) { | |
| 88 if (SkColorGetA(bitmap.getColor(x, y)) > kMinimumVisibleOpacity) { | |
| 89 inner_max = x; | |
| 90 break; | |
| 91 } | |
| 92 } | |
| 93 if (inner_max > -1) | |
| 94 break; | |
| 95 } | |
| 96 | |
| 97 if (inner_min == bitmap.width()) { | |
| 98 *leading = bitmap.width()/2; | |
| 99 *trailing = bitmap.width()/2 + 1; | |
| 100 return true; | |
| 101 } | |
| 102 | |
| 103 *leading = inner_min; | |
| 104 *trailing = inner_max; | |
| 105 return true; | |
| 106 } | 99 } |
| 107 | 100 |
| 108 } // namespace gfx | 101 } // namespace gfx |
| OLD | NEW |