| 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, | |
| 19 UserImage::Bytes* output) { | |
| 20 TRACE_EVENT2("oobe", "EncodeImageSkia", | |
| 21 "width", image.width(), "height", image.height()); | |
| 22 if (image.isNull()) | |
| 23 return false; | |
| 24 const SkBitmap& bitmap = *image.bitmap(); | |
| 25 SkAutoLockPixels lock_image(bitmap); | |
| 26 return gfx::JPEGCodec::Encode( | |
| 27 reinterpret_cast<unsigned char*>(bitmap.getAddr32(0, 0)), | |
| 28 gfx::JPEGCodec::FORMAT_SkBitmap, | |
| 29 bitmap.width(), | |
| 30 bitmap.height(), | |
| 31 bitmap.width() * bitmap.bytesPerPixel(), | |
| 32 kDefaultEncodingQuality, output); | |
| 33 } | |
| 34 | |
| 35 } // namespace | 18 } // namespace |
| 36 | 19 |
| 37 // static | 20 // static |
| 21 scoped_ptr<UserImage::Bytes> UserImage::Encode(const SkBitmap& bitmap) { |
| 22 TRACE_EVENT2("oobe", "UserImage::Encode", |
| 23 "width", bitmap.width(), "height", bitmap.height()); |
| 24 SkAutoLockPixels lock_bitmap(bitmap); |
| 25 scoped_ptr<Bytes> output(new Bytes); |
| 26 if (gfx::JPEGCodec::Encode( |
| 27 reinterpret_cast<unsigned char*>(bitmap.getAddr32(0, 0)), |
| 28 gfx::JPEGCodec::FORMAT_SkBitmap, |
| 29 bitmap.width(), |
| 30 bitmap.height(), |
| 31 bitmap.width() * bitmap.bytesPerPixel(), |
| 32 kDefaultEncodingQuality, output.get())) { |
| 33 return output; |
| 34 } else { |
| 35 return nullptr; |
| 36 } |
| 37 } |
| 38 |
| 39 // static |
| 38 UserImage UserImage::CreateAndEncode(const gfx::ImageSkia& image) { | 40 UserImage UserImage::CreateAndEncode(const gfx::ImageSkia& image) { |
| 39 Bytes image_bytes; | 41 if (image.isNull()) |
| 40 if (EncodeImageSkia(image, &image_bytes)) { | 42 return UserImage(); |
| 41 UserImage result(image, image_bytes); | 43 |
| 44 scoped_ptr<Bytes> image_bytes = Encode(*image.bitmap()); |
| 45 if (image_bytes) { |
| 46 UserImage result(image, *image_bytes); |
| 42 result.MarkAsSafe(); | 47 result.MarkAsSafe(); |
| 43 return result; | 48 return result; |
| 44 } | 49 } |
| 45 return UserImage(image); | 50 return UserImage(image); |
| 46 } | 51 } |
| 47 | 52 |
| 48 UserImage::UserImage() | 53 UserImage::UserImage() |
| 49 : has_image_bytes_(false), | 54 : has_image_bytes_(false), |
| 50 is_safe_format_(false) { | 55 is_safe_format_(false) { |
| 51 } | 56 } |
| (...skipping 13 matching lines...) Expand all Loading... |
| 65 image_bytes_ = image_bytes; | 70 image_bytes_ = image_bytes; |
| 66 } | 71 } |
| 67 | 72 |
| 68 UserImage::~UserImage() {} | 73 UserImage::~UserImage() {} |
| 69 | 74 |
| 70 void UserImage::MarkAsSafe() { | 75 void UserImage::MarkAsSafe() { |
| 71 is_safe_format_ = true; | 76 is_safe_format_ = true; |
| 72 } | 77 } |
| 73 | 78 |
| 74 } // namespace user_manager | 79 } // namespace user_manager |
| OLD | NEW |