Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / chromeos / login / user_image.cc
blob347c1fbd758c32fc4dc70b08abaad945c5bda65c
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"
12 namespace chromeos {
14 namespace {
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) {
25 return true;
27 return false;
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());
34 if (image.isNull())
35 return false;
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,
41 bitmap.width(),
42 bitmap.height(),
43 bitmap.width() * bitmap.bytesPerPixel(),
44 kDefaultEncodingQuality, output);
47 } // namespace
49 // static
50 UserImage UserImage::CreateAndEncode(const gfx::ImageSkia& image) {
51 RawImage raw_image;
52 if (EncodeImageSkia(image, &raw_image)) {
53 UserImage result(image, raw_image);
54 result.MarkAsSafe();
55 return result;
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)
67 : image_(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)
75 : image_(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;
84 MarkAsSafe();
86 } else {
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