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 "components/enhanced_bookmarks/image_store_util.h"
7 #include "third_party/skia/include/core/SkBitmap.h"
8 #include "ui/gfx/color_analysis.h"
9 #include "ui/gfx/image/image_skia.h"
10 #include "ui/gfx/image/image_util.h"
13 const int kJpegEncodingQuality
= 70;
16 namespace enhanced_bookmarks
{
18 scoped_refptr
<base::RefCountedMemory
> BytesForImage(const gfx::Image
& image
) {
19 DCHECK(image
.AsImageSkia().image_reps().size() == 1);
20 DCHECK(image
.AsImageSkia().image_reps().begin()->scale() == 1.0f
);
22 std::vector
<unsigned char> data
;
24 gfx::JPEG1xEncodedDataFromImage(image
, kJpegEncodingQuality
, &data
);
27 return scoped_refptr
<base::RefCountedMemory
>();
29 return scoped_refptr
<base::RefCountedMemory
>(new base::RefCountedBytes(data
));
32 gfx::Image
ImageForBytes(const scoped_refptr
<base::RefCountedMemory
>& bytes
) {
33 return gfx::ImageFrom1xJPEGEncodedData(bytes
->front(), bytes
->size());
36 SkColor
DominantColorForImage(const gfx::Image
& image
) {
37 return color_utils::CalculateKMeanColorOfBitmap(*image
.ToSkBitmap());