summaryrefslogtreecommitdiff
path: root/chromium/components/suggestions/image_encoder_ios.mm
blob: 1fe64d056eac753738b884a21898e5b594ec4d9a (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
// 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"

#include <stddef.h>
#import <UIKit/UIKit.h>

#include "base/mac/scoped_cftyperef.h"
#include "base/memory/ptr_util.h"
#include "skia/ext/skia_utils_ios.h"

namespace suggestions {

std::unique_ptr<SkBitmap> DecodeJPEGToSkBitmap(const void* encoded_data,
                                               size_t size) {
  NSData* data = [NSData dataWithBytes:encoded_data length:size];
  UIImage* image = [UIImage imageWithData:data scale:1.0];
  return base::WrapUnique(
      new SkBitmap(skia::CGImageToSkBitmap(image.CGImage, [image size], YES)));
}

bool EncodeSkBitmapToJPEG(const SkBitmap& bitmap,
                          std::vector<unsigned char>* dest) {
  base::ScopedCFTypeRef<CGColorSpaceRef> color_space(
      CGColorSpaceCreateDeviceRGB());
  UIImage* image =
      skia::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