Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(211)

Side by Side Diff: components/user_manager/user_image/user_image.cc

Issue 2537713002: Add support for transparent/translucent pixels in the user image (Closed)
Patch Set: rebased Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « components/user_manager/user_image/user_image.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 auto* bitmap_data = reinterpret_cast<unsigned char*>(bitmap.getAddr32(0, 0));
29 reinterpret_cast<unsigned char*>(bitmap.getAddr32(0, 0)), 32 if (image_format == FORMAT_JPEG) {
30 gfx::JPEGCodec::FORMAT_SkBitmap, 33 if (gfx::JPEGCodec::Encode(
31 bitmap.width(), 34 bitmap_data,
32 bitmap.height(), 35 gfx::JPEGCodec::FORMAT_SkBitmap,
33 bitmap.width() * bitmap.bytesPerPixel(), 36 bitmap.width(),
34 kDefaultEncodingQuality, &output)) { 37 bitmap.height(),
35 return base::RefCountedBytes::TakeVector(&output); 38 bitmap.width() * bitmap.bytesPerPixel(),
39 kDefaultEncodingQuality, &output)) {
40 return base::RefCountedBytes::TakeVector(&output);
41 }
42 } else if (image_format == FORMAT_PNG) {
43 if (gfx::PNGCodec::Encode(
44 bitmap_data,
45 gfx::PNGCodec::FORMAT_SkBitmap,
46 gfx::Size(bitmap.width(), bitmap.height()),
47 bitmap.width() * bitmap.bytesPerPixel(),
48 false, // discard_transparency
49 std::vector<gfx::PNGCodec::Comment>(), &output)) {
50 return base::RefCountedBytes::TakeVector(&output);
51 }
36 } else { 52 } else {
37 return nullptr; 53 LOG(FATAL) << "Invalid image format: " << image_format;
38 } 54 }
55 return nullptr;
39 } 56 }
40 57
41 // static 58 // static
42 std::unique_ptr<UserImage> UserImage::CreateAndEncode( 59 std::unique_ptr<UserImage> UserImage::CreateAndEncode(
43 const gfx::ImageSkia& image) { 60 const gfx::ImageSkia& image,
61 ImageFormat image_format) {
44 if (image.isNull()) 62 if (image.isNull())
45 return base::WrapUnique(new UserImage); 63 return base::WrapUnique(new UserImage);
46 64
47 scoped_refptr<base::RefCountedBytes> image_bytes = Encode(*image.bitmap()); 65 scoped_refptr<base::RefCountedBytes> image_bytes = Encode(*image.bitmap(),
66 image_format);
48 if (image_bytes) { 67 if (image_bytes) {
49 std::unique_ptr<UserImage> result(new UserImage(image, image_bytes)); 68 std::unique_ptr<UserImage> result(
69 new UserImage(image, image_bytes, image_format));
50 result->MarkAsSafe(); 70 result->MarkAsSafe();
51 return result; 71 return result;
52 } 72 }
53 return base::WrapUnique(new UserImage(image)); 73 return base::WrapUnique(new UserImage(image));
54 } 74 }
55 75
76 // static
77 UserImage::ImageFormat UserImage::ChooseImageFormat(const SkBitmap& bitmap) {
78 return SkBitmap::ComputeIsOpaque(bitmap) ? FORMAT_JPEG : FORMAT_PNG;
79 }
80
56 UserImage::UserImage() { 81 UserImage::UserImage() {
57 } 82 }
58 83
59 UserImage::UserImage(const gfx::ImageSkia& image) 84 UserImage::UserImage(const gfx::ImageSkia& image)
60 : image_(image) { 85 : image_(image) {
61 } 86 }
62 87
63 UserImage::UserImage(const gfx::ImageSkia& image, 88 UserImage::UserImage(const gfx::ImageSkia& image,
64 scoped_refptr<base::RefCountedBytes> image_bytes) 89 scoped_refptr<base::RefCountedBytes> image_bytes,
90 ImageFormat image_format)
65 : image_(image), 91 : image_(image),
66 image_bytes_(image_bytes) { 92 image_bytes_(image_bytes),
93 image_format_(image_format) {
67 } 94 }
68 95
69 UserImage::~UserImage() {} 96 UserImage::~UserImage() {}
70 97
71 void UserImage::MarkAsSafe() { 98 void UserImage::MarkAsSafe() {
72 is_safe_format_ = true; 99 is_safe_format_ = true;
73 } 100 }
74 101
75 } // namespace user_manager 102 } // namespace user_manager
OLDNEW
« no previous file with comments | « components/user_manager/user_image/user_image.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698