Chromium Code Reviews| 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/memory/ptr_util.h" | 7 #include "base/memory/ptr_util.h" |
| 8 #include "base/trace_event/trace_event.h" | 8 #include "base/trace_event/trace_event.h" |
| 9 #include "third_party/skia/include/core/SkBitmap.h" | 9 #include "third_party/skia/include/core/SkBitmap.h" |
| 10 #include "ui/gfx/codec/jpeg_codec.h" | 10 #include "ui/gfx/codec/jpeg_codec.h" |
| 11 #include "ui/gfx/codec/png_codec.h" | |
| 12 #include "ui/gfx/geometry/size.h" | |
| 11 | 13 |
| 12 namespace user_manager { | 14 namespace user_manager { |
| 13 | 15 |
| 14 namespace { | 16 namespace { |
| 15 | 17 |
| 16 // Default quality for encoding user images. | 18 // Default quality for encoding user images. |
| 17 const int kDefaultEncodingQuality = 90; | 19 const int kDefaultEncodingQuality = 90; |
| 18 | 20 |
| 19 } // namespace | 21 } // namespace |
| 20 | 22 |
| 21 // static | 23 // static |
| 22 scoped_refptr<base::RefCountedBytes> UserImage::Encode( | 24 scoped_refptr<base::RefCountedBytes> UserImage::Encode( |
| 23 const SkBitmap& bitmap) { | 25 const SkBitmap& bitmap, |
| 26 ImageFormat image_format) { | |
| 24 TRACE_EVENT2("oobe", "UserImage::Encode", | 27 TRACE_EVENT2("oobe", "UserImage::Encode", |
| 25 "width", bitmap.width(), "height", bitmap.height()); | 28 "width", bitmap.width(), "height", bitmap.height()); |
| 26 SkAutoLockPixels lock_bitmap(bitmap); | 29 SkAutoLockPixels lock_bitmap(bitmap); |
| 27 std::vector<unsigned char> output; | 30 std::vector<unsigned char> output; |
| 28 if (gfx::JPEGCodec::Encode( | 31 if (image_format == FORMAT_JPEG) { |
| 29 reinterpret_cast<unsigned char*>(bitmap.getAddr32(0, 0)), | 32 if (gfx::JPEGCodec::Encode( |
| 30 gfx::JPEGCodec::FORMAT_SkBitmap, | 33 reinterpret_cast<unsigned char*>(bitmap.getAddr32(0, 0)), |
|
achuithb
2016/11/30 00:26:48
Does it make sense to have a temporary for this po
satorux1
2016/12/01 07:43:36
good idea. done
| |
| 31 bitmap.width(), | 34 gfx::JPEGCodec::FORMAT_SkBitmap, |
| 32 bitmap.height(), | 35 bitmap.width(), |
| 33 bitmap.width() * bitmap.bytesPerPixel(), | 36 bitmap.height(), |
| 34 kDefaultEncodingQuality, &output)) { | 37 bitmap.width() * bitmap.bytesPerPixel(), |
| 35 return base::RefCountedBytes::TakeVector(&output); | 38 kDefaultEncodingQuality, &output)) { |
| 39 return base::RefCountedBytes::TakeVector(&output); | |
| 40 } | |
| 41 } else if (image_format == FORMAT_PNG) { | |
| 42 if (gfx::PNGCodec::Encode( | |
| 43 reinterpret_cast<unsigned char*>(bitmap.getAddr32(0, 0)), | |
| 44 gfx::PNGCodec::FORMAT_SkBitmap, | |
| 45 gfx::Size(bitmap.width(), bitmap.height()), | |
| 46 bitmap.width() * bitmap.bytesPerPixel(), | |
| 47 false, // discard_transparency | |
| 48 std::vector<gfx::PNGCodec::Comment>(), &output)) { | |
| 49 return base::RefCountedBytes::TakeVector(&output); | |
| 50 } | |
| 36 } else { | 51 } else { |
| 37 return nullptr; | 52 LOG(FATAL) << "Invalid image format: " << image_format; |
| 38 } | 53 } |
| 54 return nullptr; | |
| 39 } | 55 } |
| 40 | 56 |
| 41 // static | 57 // static |
| 42 std::unique_ptr<UserImage> UserImage::CreateAndEncode( | 58 std::unique_ptr<UserImage> UserImage::CreateAndEncode( |
| 43 const gfx::ImageSkia& image) { | 59 const gfx::ImageSkia& image, |
| 60 ImageFormat image_format) { | |
| 44 if (image.isNull()) | 61 if (image.isNull()) |
| 45 return base::WrapUnique(new UserImage); | 62 return base::WrapUnique(new UserImage); |
| 46 | 63 |
| 47 scoped_refptr<base::RefCountedBytes> image_bytes = Encode(*image.bitmap()); | 64 scoped_refptr<base::RefCountedBytes> image_bytes = Encode(*image.bitmap(), |
| 65 image_format); | |
| 48 if (image_bytes) { | 66 if (image_bytes) { |
| 49 std::unique_ptr<UserImage> result(new UserImage(image, image_bytes)); | 67 std::unique_ptr<UserImage> result( |
| 68 new UserImage(image, image_bytes, image_format)); | |
| 50 result->MarkAsSafe(); | 69 result->MarkAsSafe(); |
| 51 return result; | 70 return result; |
| 52 } | 71 } |
| 53 return base::WrapUnique(new UserImage(image)); | 72 return base::WrapUnique(new UserImage(image)); |
| 54 } | 73 } |
| 55 | 74 |
| 75 // static | |
| 76 UserImage::ImageFormat UserImage::ChooseImageFormat(const SkBitmap& bitmap) { | |
| 77 if (SkBitmap::ComputeIsOpaque(bitmap)) | |
|
achuithb
2016/11/30 00:26:48
Ternary operator is more compact here, but up to y
satorux1
2016/12/01 07:43:36
Done.
| |
| 78 return FORMAT_JPEG; | |
| 79 return FORMAT_PNG; | |
| 80 } | |
| 81 | |
| 56 UserImage::UserImage() { | 82 UserImage::UserImage() { |
| 57 } | 83 } |
| 58 | 84 |
| 59 UserImage::UserImage(const gfx::ImageSkia& image) | 85 UserImage::UserImage(const gfx::ImageSkia& image) |
| 60 : image_(image) { | 86 : image_(image) { |
| 61 } | 87 } |
| 62 | 88 |
| 63 UserImage::UserImage(const gfx::ImageSkia& image, | 89 UserImage::UserImage(const gfx::ImageSkia& image, |
| 64 scoped_refptr<base::RefCountedBytes> image_bytes) | 90 scoped_refptr<base::RefCountedBytes> image_bytes, |
| 91 ImageFormat image_format) | |
| 65 : image_(image), | 92 : image_(image), |
| 66 image_bytes_(image_bytes) { | 93 image_bytes_(image_bytes), |
| 94 image_format_(image_format) { | |
| 67 } | 95 } |
| 68 | 96 |
| 69 UserImage::~UserImage() {} | 97 UserImage::~UserImage() {} |
| 70 | 98 |
| 71 void UserImage::MarkAsSafe() { | 99 void UserImage::MarkAsSafe() { |
| 72 is_safe_format_ = true; | 100 is_safe_format_ = true; |
| 73 } | 101 } |
| 74 | 102 |
| 75 } // namespace user_manager | 103 } // namespace user_manager |
| OLD | NEW |