blob: e81fb305948c4ef488cae600950f03917d6e6e30 [file] [log] [blame]
// Copyright 2014 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 "components/suggestions/image_encoder.h"
#import <UIKit/UIKit.h>
#include "base/mac/scoped_cftyperef.h"
#include "skia/ext/skia_utils_ios.h"
namespace suggestions {
SkBitmap* DecodeJPEGToSkBitmap(const std::vector<unsigned char>& encoded_data) {
NSData* data =
[NSData dataWithBytes:encoded_data.data() length:encoded_data.size()];
UIImage* image =
[UIImage imageWithData:data scale:1.0];
return new SkBitmap(gfx::CGImageToSkBitmap(image.CGImage, [image size], YES));
}
bool EncodeSkBitmapToJPEG(const SkBitmap& bitmap,
std::vector<unsigned char>* dest) {
base::ScopedCFTypeRef<CGColorSpaceRef> color_space(
CGColorSpaceCreateDeviceRGB());
UIImage* image =
gfx::SkBitmapToUIImageWithColorSpace(bitmap, 1 /* scale */, color_space);
NSData* data = UIImageJPEGRepresentation(image, 1.0);
const char* bytes = reinterpret_cast<const char*>([data bytes]);
dest->assign(bytes, bytes + [data length]);
return true;
}
} // namespace suggestions