1// Copyright (c) 2011 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 "chrome/browser/importer/importer.h"
6
7#include "chrome/browser/importer/importer_bridge.h"
8#include "chrome/browser/importer/importer_data_types.h"
9#include "skia/ext/image_operations.h"
10#include "third_party/skia/include/core/SkBitmap.h"
11#include "ui/gfx/codec/png_codec.h"
12#include "ui/gfx/favicon_size.h"
13#include "webkit/glue/image_decoder.h"
14
15void Importer::Cancel() {
16  cancelled_ = true;
17}
18
19Importer::Importer()
20    : import_to_bookmark_bar_(false),
21      bookmark_bar_disabled_(false),
22      cancelled_(false) {
23}
24
25Importer::~Importer() {
26}
27
28// static
29bool Importer::ReencodeFavicon(const unsigned char* src_data,
30                               size_t src_len,
31                               std::vector<unsigned char>* png_data) {
32  // Decode the favicon using WebKit's image decoder.
33  webkit_glue::ImageDecoder decoder(gfx::Size(kFaviconSize, kFaviconSize));
34  SkBitmap decoded = decoder.Decode(src_data, src_len);
35  if (decoded.empty())
36    return false;  // Unable to decode.
37
38  if (decoded.width() != kFaviconSize || decoded.height() != kFaviconSize) {
39    // The bitmap is not the correct size, re-sample.
40    int new_width = decoded.width();
41    int new_height = decoded.height();
42    calc_favicon_target_size(&new_width, &new_height);
43    decoded = skia::ImageOperations::Resize(
44        decoded, skia::ImageOperations::RESIZE_LANCZOS3, new_width, new_height);
45  }
46
47  // Encode our bitmap as a PNG.
48  gfx::PNGCodec::EncodeBGRASkBitmap(decoded, false, png_data);
49  return true;
50}
51