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
3 // found in the LICENSE file.
5 #include "chrome/browser/chromeos/login/users/avatar/user_image.h"
7 #include "base/debug/trace_event.h"
8 #include "third_party/skia/include/core/SkBitmap.h"
9 #include "ui/gfx/codec/jpeg_codec.h"
15 // Default quality for encoding user images.
16 const int kDefaultEncodingQuality = 90;
18 bool IsAnimatedImage(const UserImage::RawImage& data) {
19 const char kGIFStamp[] = "GIF";
20 const size_t kGIFStampLength = sizeof(kGIFStamp) - 1;
22 if (data.size() >= kGIFStampLength &&
23 memcmp(&data[0], kGIFStamp, kGIFStampLength) == 0) {
29 bool EncodeImageSkia(const gfx::ImageSkia& image,
30 std::vector<unsigned char>* output) {
31 TRACE_EVENT2("oobe", "EncodeImageSkia",
32 "width", image.width(), "height", image.height());
35 const SkBitmap& bitmap = *image.bitmap();
36 SkAutoLockPixels lock_image(bitmap);
37 return gfx::JPEGCodec::Encode(
38 reinterpret_cast<unsigned char*>(bitmap.getAddr32(0, 0)),
39 gfx::JPEGCodec::FORMAT_SkBitmap,
42 bitmap.width() * bitmap.bytesPerPixel(),
43 kDefaultEncodingQuality, output);
49 UserImage UserImage::CreateAndEncode(const gfx::ImageSkia& image) {
51 if (EncodeImageSkia(image, &raw_image)) {
52 UserImage result(image, raw_image);
56 return UserImage(image);
59 UserImage::UserImage()
60 : has_raw_image_(false),
61 has_animated_image_(false),
62 is_safe_format_(false) {
65 UserImage::UserImage(const gfx::ImageSkia& image)
67 has_raw_image_(false),
68 has_animated_image_(false),
69 is_safe_format_(false) {
72 UserImage::UserImage(const gfx::ImageSkia& image,
73 const RawImage& raw_image)
75 has_raw_image_(false),
76 has_animated_image_(false),
77 is_safe_format_(false) {
78 if (IsAnimatedImage(raw_image)) {
79 has_animated_image_ = true;
80 animated_image_ = raw_image;
81 if (EncodeImageSkia(image_, &raw_image_)) {
82 has_raw_image_ = true;
86 has_raw_image_ = true;
87 raw_image_ = raw_image;
91 UserImage::~UserImage() {}
93 void UserImage::DiscardRawImage() {
94 RawImage().swap(raw_image_); // Clear |raw_image_|.
97 void UserImage::MarkAsSafe() {
98 is_safe_format_ = true;
101 } // namespace chromeos