summaryrefslogtreecommitdiffstats
path: root/ui/gfx/image/image_util.cc
blob: ea870c0530ea581a071b58150aeb5292e66ad717 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ui/gfx/image/image_util.h"

#include "base/memory/scoped_ptr.h"
#include "third_party/skia/include/core/SkBitmap.h"
#include "ui/gfx/codec/jpeg_codec.h"
#include "ui/gfx/codec/png_codec.h"
#include "ui/gfx/image/image.h"

namespace gfx {

Image* ImageFromPNGEncodedData(const unsigned char* input, size_t input_size) {
  SkBitmap favicon_bitmap;
  if (gfx::PNGCodec::Decode(input, input_size, &favicon_bitmap))
    return new Image(favicon_bitmap);

  return NULL;
}

bool PNGEncodedDataFromImage(const Image& image,
                             std::vector<unsigned char>* dst) {
  const SkBitmap& bitmap = *image.ToSkBitmap();
  return gfx::PNGCodec::EncodeBGRASkBitmap(bitmap, false, dst);
}

bool JPEGEncodedDataFromImage(const Image& image, int quality,
                              std::vector<unsigned char>* dst) {
  const SkBitmap& bitmap = *image.ToSkBitmap();
  SkAutoLockPixels bitmap_lock(bitmap);

  if (!bitmap.readyToDraw())
    return false;

  return gfx::JPEGCodec::Encode(
          reinterpret_cast<unsigned char*>(bitmap.getAddr32(0, 0)),
          gfx::JPEGCodec::FORMAT_BGRA, bitmap.width(),
          bitmap.height(),
          static_cast<int>(bitmap.rowBytes()), quality,
          dst);
}

}