| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "chrome/browser/chromeos/login/users/avatar/user_image.h" | 5 #include "components/user_manager/avatar/user_image.h" |
| 6 | 6 |
| 7 #include "base/debug/trace_event.h" | 7 #include "base/debug/trace_event.h" |
| 8 #include "third_party/skia/include/core/SkBitmap.h" | 8 #include "third_party/skia/include/core/SkBitmap.h" |
| 9 #include "ui/gfx/codec/jpeg_codec.h" | 9 #include "ui/gfx/codec/jpeg_codec.h" |
| 10 | 10 |
| 11 namespace chromeos { | 11 namespace user_manager { |
| 12 | 12 |
| 13 namespace { | 13 namespace { |
| 14 | 14 |
| 15 // Default quality for encoding user images. | 15 // Default quality for encoding user images. |
| 16 const int kDefaultEncodingQuality = 90; | 16 const int kDefaultEncodingQuality = 90; |
| 17 | 17 |
| 18 bool IsAnimatedImage(const UserImage::RawImage& data) { | 18 bool IsAnimatedImage(const UserImage::RawImage& data) { |
| 19 const char kGIFStamp[] = "GIF"; | 19 const char kGIFStamp[] = "GIF"; |
| 20 const size_t kGIFStampLength = sizeof(kGIFStamp) - 1; | 20 const size_t kGIFStampLength = sizeof(kGIFStamp) - 1; |
| 21 | 21 |
| 22 if (data.size() >= kGIFStampLength && | 22 if (data.size() >= kGIFStampLength && |
| 23 memcmp(&data[0], kGIFStamp, kGIFStampLength) == 0) { | 23 memcmp(&data[0], kGIFStamp, kGIFStampLength) == 0) { |
| 24 return true; | 24 return true; |
| 25 } | 25 } |
| 26 return false; | 26 return false; |
| 27 } | 27 } |
| 28 | 28 |
| 29 bool EncodeImageSkia(const gfx::ImageSkia& image, | 29 bool EncodeImageSkia(const gfx::ImageSkia& image, |
| 30 std::vector<unsigned char>* output) { | 30 std::vector<unsigned char>* output) { |
| 31 TRACE_EVENT2("oobe", "EncodeImageSkia", | 31 TRACE_EVENT2("oobe", |
| 32 "width", image.width(), "height", image.height()); | 32 "EncodeImageSkia", |
| 33 "width", |
| 34 image.width(), |
| 35 "height", |
| 36 image.height()); |
| 33 if (image.isNull()) | 37 if (image.isNull()) |
| 34 return false; | 38 return false; |
| 35 const SkBitmap& bitmap = *image.bitmap(); | 39 const SkBitmap& bitmap = *image.bitmap(); |
| 36 SkAutoLockPixels lock_image(bitmap); | 40 SkAutoLockPixels lock_image(bitmap); |
| 37 return gfx::JPEGCodec::Encode( | 41 return gfx::JPEGCodec::Encode( |
| 38 reinterpret_cast<unsigned char*>(bitmap.getAddr32(0, 0)), | 42 reinterpret_cast<unsigned char*>(bitmap.getAddr32(0, 0)), |
| 39 gfx::JPEGCodec::FORMAT_SkBitmap, | 43 gfx::JPEGCodec::FORMAT_SkBitmap, |
| 40 bitmap.width(), | 44 bitmap.width(), |
| 41 bitmap.height(), | 45 bitmap.height(), |
| 42 bitmap.width() * bitmap.bytesPerPixel(), | 46 bitmap.width() * bitmap.bytesPerPixel(), |
| 43 kDefaultEncodingQuality, output); | 47 kDefaultEncodingQuality, |
| 48 output); |
| 44 } | 49 } |
| 45 | 50 |
| 46 } // namespace | 51 } // namespace |
| 47 | 52 |
| 48 // static | 53 // static |
| 49 UserImage UserImage::CreateAndEncode(const gfx::ImageSkia& image) { | 54 UserImage UserImage::CreateAndEncode(const gfx::ImageSkia& image) { |
| 50 RawImage raw_image; | 55 RawImage raw_image; |
| 51 if (EncodeImageSkia(image, &raw_image)) { | 56 if (EncodeImageSkia(image, &raw_image)) { |
| 52 UserImage result(image, raw_image); | 57 UserImage result(image, raw_image); |
| 53 result.MarkAsSafe(); | 58 result.MarkAsSafe(); |
| 54 return result; | 59 return result; |
| 55 } | 60 } |
| 56 return UserImage(image); | 61 return UserImage(image); |
| 57 } | 62 } |
| 58 | 63 |
| 59 UserImage::UserImage() | 64 UserImage::UserImage() |
| 60 : has_raw_image_(false), | 65 : has_raw_image_(false), |
| 61 has_animated_image_(false), | 66 has_animated_image_(false), |
| 62 is_safe_format_(false) { | 67 is_safe_format_(false) { |
| 63 } | 68 } |
| 64 | 69 |
| 65 UserImage::UserImage(const gfx::ImageSkia& image) | 70 UserImage::UserImage(const gfx::ImageSkia& image) |
| 66 : image_(image), | 71 : image_(image), |
| 67 has_raw_image_(false), | 72 has_raw_image_(false), |
| 68 has_animated_image_(false), | 73 has_animated_image_(false), |
| 69 is_safe_format_(false) { | 74 is_safe_format_(false) { |
| 70 } | 75 } |
| 71 | 76 |
| 72 UserImage::UserImage(const gfx::ImageSkia& image, | 77 UserImage::UserImage(const gfx::ImageSkia& image, const RawImage& raw_image) |
| 73 const RawImage& raw_image) | |
| 74 : image_(image), | 78 : image_(image), |
| 75 has_raw_image_(false), | 79 has_raw_image_(false), |
| 76 has_animated_image_(false), | 80 has_animated_image_(false), |
| 77 is_safe_format_(false) { | 81 is_safe_format_(false) { |
| 78 if (IsAnimatedImage(raw_image)) { | 82 if (IsAnimatedImage(raw_image)) { |
| 79 has_animated_image_ = true; | 83 has_animated_image_ = true; |
| 80 animated_image_ = raw_image; | 84 animated_image_ = raw_image; |
| 81 if (EncodeImageSkia(image_, &raw_image_)) { | 85 if (EncodeImageSkia(image_, &raw_image_)) { |
| 82 has_raw_image_ = true; | 86 has_raw_image_ = true; |
| 83 MarkAsSafe(); | 87 MarkAsSafe(); |
| 84 } | 88 } |
| 85 } else { | 89 } else { |
| 86 has_raw_image_ = true; | 90 has_raw_image_ = true; |
| 87 raw_image_ = raw_image; | 91 raw_image_ = raw_image; |
| 88 } | 92 } |
| 89 } | 93 } |
| 90 | 94 |
| 91 UserImage::~UserImage() {} | 95 UserImage::~UserImage() { |
| 96 } |
| 92 | 97 |
| 93 void UserImage::DiscardRawImage() { | 98 void UserImage::DiscardRawImage() { |
| 94 RawImage().swap(raw_image_); // Clear |raw_image_|. | 99 RawImage().swap(raw_image_); // Clear |raw_image_|. |
| 95 } | 100 } |
| 96 | 101 |
| 97 void UserImage::MarkAsSafe() { | 102 void UserImage::MarkAsSafe() { |
| 98 is_safe_format_ = true; | 103 is_safe_format_ = true; |
| 99 } | 104 } |
| 100 | 105 |
| 101 } // namespace chromeos | 106 } // namespace user_manager |
| OLD | NEW |