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

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

Issue 1748423005: Crop the user-specified profile image for WebUI (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 9 months 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
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/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 UserImage::Bytes UserImage::Encode(const SkBitmap& bitmap) {
22 TRACE_EVENT2("oobe", "Encode",
hashimoto 2016/03/02 07:33:58 nit: s/Encode/UserImage::Encode/ might be more hel
satorux1 2016/03/02 08:23:51 Done.
23 "width", bitmap.width(), "height", bitmap.height());
24 SkAutoLockPixels lock_bitmap(bitmap);
25 Bytes output;
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)) {
33 return output;
hashimoto 2016/03/02 07:33:58 This line may enjoy NRVO or implicit move so there
satorux1 2016/03/02 08:23:51 Good point. Changed it to scoped_ptr<Bytes>
34 } else {
35 return UserImage::Bytes();
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(image);
hashimoto 2016/03/02 07:33:58 Just UserImage()?
satorux1 2016/03/02 08:23:51 Done.
43
44 Bytes image_bytes = Encode(*image.bitmap());
45 if (!image_bytes.empty()) {
41 UserImage result(image, 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) {
(...skipping 14 matching lines...) Expand all
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
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698