1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/browser/chromeos/login/user_image.h"
7 #include "third_party/skia/include/core/SkBitmap.h"
8 #include "ui/gfx/codec/jpeg_codec.h"
10 #include "base/debug/trace_event.h"
16 // Default quality for encoding user images.
17 const int kDefaultEncodingQuality
= 90;
19 bool IsAnimatedImage(const UserImage::RawImage
& data
) {
20 const char kGIFStamp
[] = "GIF";
21 const size_t kGIFStampLength
= sizeof(kGIFStamp
) - 1;
23 if (data
.size() >= kGIFStampLength
&&
24 memcmp(&data
[0], kGIFStamp
, kGIFStampLength
) == 0) {
30 bool EncodeImageSkia(const gfx::ImageSkia
& image
,
31 std::vector
<unsigned char>* output
) {
32 TRACE_EVENT2("oobe", "EncodeImageSkia",
33 "width", image
.width(), "height", image
.height());
36 const SkBitmap
& bitmap
= *image
.bitmap();
37 SkAutoLockPixels
lock_image(bitmap
);
38 return gfx::JPEGCodec::Encode(
39 reinterpret_cast<unsigned char*>(bitmap
.getAddr32(0, 0)),
40 gfx::JPEGCodec::FORMAT_SkBitmap
,
43 bitmap
.width() * bitmap
.bytesPerPixel(),
44 kDefaultEncodingQuality
, output
);
50 UserImage
UserImage::CreateAndEncode(const gfx::ImageSkia
& image
) {
52 if (EncodeImageSkia(image
, &raw_image
)) {
53 UserImage
result(image
, raw_image
);
57 return UserImage(image
);
60 UserImage::UserImage()
61 : has_raw_image_(false),
62 has_animated_image_(false),
63 is_safe_format_(false) {
66 UserImage::UserImage(const gfx::ImageSkia
& image
)
68 has_raw_image_(false),
69 has_animated_image_(false),
70 is_safe_format_(false) {
73 UserImage::UserImage(const gfx::ImageSkia
& image
,
74 const RawImage
& raw_image
)
76 has_raw_image_(false),
77 has_animated_image_(false),
78 is_safe_format_(false) {
79 if (IsAnimatedImage(raw_image
)) {
80 has_animated_image_
= true;
81 animated_image_
= raw_image
;
82 if (EncodeImageSkia(image_
, &raw_image_
)) {
83 has_raw_image_
= true;
87 has_raw_image_
= true;
88 raw_image_
= raw_image
;
92 UserImage::~UserImage() {}
94 void UserImage::DiscardRawImage() {
95 RawImage().swap(raw_image_
); // Clear |raw_image_|.
98 void UserImage::MarkAsSafe() {
99 is_safe_format_
= true;
102 } // namespace chromeos