OLD | NEW |
| (Empty) |
1 // Copyright (c) 2017 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 #import <Cocoa/Cocoa.h> | |
8 | |
9 #include "base/mac/scoped_nsobject.h" | |
10 #include "ui/gfx/image/image.h" | |
11 | |
12 namespace gfx { | |
13 | |
14 bool JPEG1xEncodedDataFromImage(const Image& image, | |
15 int quality, | |
16 std::vector<unsigned char>* dst) { | |
17 if (!image.HasRepresentation(gfx::Image::kImageRepCocoa)) | |
18 return JPEG1xEncodedDataFromSkiaRepresentation(image, quality, dst); | |
19 | |
20 NSImage* nsImage = image.ToNSImage(); | |
21 | |
22 CGImageRef cgImage = | |
23 [nsImage CGImageForProposedRect:nil context:nil hints:nil]; | |
24 base::scoped_nsobject<NSBitmapImageRep> rep( | |
25 [[NSBitmapImageRep alloc] initWithCGImage:cgImage]); | |
26 | |
27 float compressionFactor = quality / 100.0; | |
28 NSDictionary* options = @{ NSImageCompressionFactor : @(compressionFactor)}; | |
29 NSData* data = | |
30 [rep representationUsingType:NSJPEGFileType properties:options]; | |
31 | |
32 if ([data length] == 0) | |
33 return false; | |
34 | |
35 dst->resize([data length]); | |
36 [data getBytes:&dst->at(0) length:[data length]]; | |
37 return true; | |
38 } | |
39 | |
40 } // end namespace gfx | |
OLD | NEW |