| 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 "components/user_manager/user_image/user_image.h" | 5 #include "components/user_manager/user_image/user_image.h" |
| 6 | 6 |
| 7 #include "base/trace_event/trace_event.h" | 7 #include "base/trace_event/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 user_manager { | 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 EncodeImageSkia(const gfx::ImageSkia& image, | 18 bool EncodeImageSkia(const gfx::ImageSkia& image, |
| 19 std::vector<unsigned char>* output) { | 19 UserImage::Bytes* output) { |
| 20 TRACE_EVENT2("oobe", "EncodeImageSkia", | 20 TRACE_EVENT2("oobe", "EncodeImageSkia", |
| 21 "width", image.width(), "height", image.height()); | 21 "width", image.width(), "height", image.height()); |
| 22 if (image.isNull()) | 22 if (image.isNull()) |
| 23 return false; | 23 return false; |
| 24 const SkBitmap& bitmap = *image.bitmap(); | 24 const SkBitmap& bitmap = *image.bitmap(); |
| 25 SkAutoLockPixels lock_image(bitmap); | 25 SkAutoLockPixels lock_image(bitmap); |
| 26 return gfx::JPEGCodec::Encode( | 26 return gfx::JPEGCodec::Encode( |
| 27 reinterpret_cast<unsigned char*>(bitmap.getAddr32(0, 0)), | 27 reinterpret_cast<unsigned char*>(bitmap.getAddr32(0, 0)), |
| 28 gfx::JPEGCodec::FORMAT_SkBitmap, | 28 gfx::JPEGCodec::FORMAT_SkBitmap, |
| 29 bitmap.width(), | 29 bitmap.width(), |
| 30 bitmap.height(), | 30 bitmap.height(), |
| 31 bitmap.width() * bitmap.bytesPerPixel(), | 31 bitmap.width() * bitmap.bytesPerPixel(), |
| 32 kDefaultEncodingQuality, output); | 32 kDefaultEncodingQuality, output); |
| 33 } | 33 } |
| 34 | 34 |
| 35 } // namespace | 35 } // namespace |
| 36 | 36 |
| 37 // static | 37 // static |
| 38 UserImage UserImage::CreateAndEncode(const gfx::ImageSkia& image) { | 38 UserImage UserImage::CreateAndEncode(const gfx::ImageSkia& image) { |
| 39 RawImage raw_image; | 39 Bytes image_bytes; |
| 40 if (EncodeImageSkia(image, &raw_image)) { | 40 if (EncodeImageSkia(image, &image_bytes)) { |
| 41 UserImage result(image, raw_image); | 41 UserImage result(image, image_bytes); |
| 42 result.MarkAsSafe(); | 42 result.MarkAsSafe(); |
| 43 return result; | 43 return result; |
| 44 } | 44 } |
| 45 return UserImage(image); | 45 return UserImage(image); |
| 46 } | 46 } |
| 47 | 47 |
| 48 UserImage::UserImage() | 48 UserImage::UserImage() |
| 49 : has_raw_image_(false), | 49 : has_image_bytes_(false), |
| 50 is_safe_format_(false) { | 50 is_safe_format_(false) { |
| 51 } | 51 } |
| 52 | 52 |
| 53 UserImage::UserImage(const gfx::ImageSkia& image) | 53 UserImage::UserImage(const gfx::ImageSkia& image) |
| 54 : image_(image), | 54 : image_(image), |
| 55 has_raw_image_(false), | 55 has_image_bytes_(false), |
| 56 is_safe_format_(false) { | 56 is_safe_format_(false) { |
| 57 } | 57 } |
| 58 | 58 |
| 59 UserImage::UserImage(const gfx::ImageSkia& image, | 59 UserImage::UserImage(const gfx::ImageSkia& image, |
| 60 const RawImage& raw_image) | 60 const Bytes& image_bytes) |
| 61 : image_(image), | 61 : image_(image), |
| 62 has_raw_image_(false), | 62 has_image_bytes_(false), |
| 63 is_safe_format_(false) { | 63 is_safe_format_(false) { |
| 64 has_raw_image_ = true; | 64 has_image_bytes_ = true; |
| 65 raw_image_ = raw_image; | 65 image_bytes_ = image_bytes; |
| 66 } | 66 } |
| 67 | 67 |
| 68 UserImage::~UserImage() {} | 68 UserImage::~UserImage() {} |
| 69 | 69 |
| 70 void UserImage::DiscardRawImage() { | |
| 71 RawImage().swap(raw_image_); // Clear |raw_image_|. | |
| 72 } | |
| 73 | |
| 74 void UserImage::MarkAsSafe() { | 70 void UserImage::MarkAsSafe() { |
| 75 is_safe_format_ = true; | 71 is_safe_format_ = true; |
| 76 } | 72 } |
| 77 | 73 |
| 78 } // namespace user_manager | 74 } // namespace user_manager |
| OLD | NEW |