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.
4
5#include "ui/gfx/image/image_util.h"
6
7#include "base/memory/scoped_ptr.h"
8#include "third_party/skia/include/core/SkBitmap.h"
9#include "ui/gfx/codec/jpeg_codec.h"
10#include "ui/gfx/image/image.h"
11#include "ui/gfx/image/image_skia.h"
12
13namespace gfx {
14
15// The iOS implementations of the JPEG functions are in image_util_ios.mm.
16#if !defined(OS_IOS)
17Image ImageFrom1xJPEGEncodedData(const unsigned char* input,
18                                 size_t input_size) {
19  scoped_ptr<SkBitmap> bitmap(gfx::JPEGCodec::Decode(input, input_size));
20  if (bitmap.get())
21    return Image::CreateFrom1xBitmap(*bitmap);
22
23  return Image();
24}
25
26bool JPEG1xEncodedDataFromImage(const Image& image, int quality,
27                              std::vector<unsigned char>* dst) {
28  const gfx::ImageSkiaRep& image_skia_rep =
29      image.AsImageSkia().GetRepresentation(1.0f);
30  if (image_skia_rep.scale() != 1.0f)
31    return false;
32
33  const SkBitmap& bitmap = image_skia_rep.sk_bitmap();
34  SkAutoLockPixels bitmap_lock(bitmap);
35
36  if (!bitmap.readyToDraw())
37    return false;
38
39  return gfx::JPEGCodec::Encode(
40          reinterpret_cast<unsigned char*>(bitmap.getAddr32(0, 0)),
41          gfx::JPEGCodec::FORMAT_SkBitmap, bitmap.width(),
42          bitmap.height(),
43          static_cast<int>(bitmap.rowBytes()), quality,
44          dst);
45}
46#endif  // !defined(OS_IOS)
47
48}
49