• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*M///////////////////////////////////////////////////////////////////////////////////////
2//
3//  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
4//
5//  By downloading, copying, installing or using the software you agree to this license.
6//  If you do not agree to this license, do not download, install,
7//  copy or use the software.
8//
9//
10//                          License Agreement
11//                For Open Source Computer Vision Library
12//
13// Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
14// Copyright (C) 2009, Willow Garage Inc., all rights reserved.
15// Third party copyrights are property of their respective owners.
16//
17// Redistribution and use in source and binary forms, with or without modification,
18// are permitted provided that the following conditions are met:
19//
20//   * Redistribution's of source code must retain the above copyright notice,
21//     this list of conditions and the following disclaimer.
22//
23//   * Redistribution's in binary form must reproduce the above copyright notice,
24//     this list of conditions and the following disclaimer in the documentation
25//     and/or other materials provided with the distribution.
26//
27//   * The name of the copyright holders may not be used to endorse or promote products
28//     derived from this software without specific prior written permission.
29//
30// This software is provided by the copyright holders and contributors "as is" and
31// any express or implied warranties, including, but not limited to, the implied
32// warranties of merchantability and fitness for a particular purpose are disclaimed.
33// In no event shall the Intel Corporation or contributors be liable for any direct,
34// indirect, incidental, special, exemplary, or consequential damages
35// (including, but not limited to, procurement of substitute goods or services;
36// loss of use, data, or profits; or business interruption) however caused
37// and on any theory of liability, whether in contract, strict liability,
38// or tort (including negligence or otherwise) arising in any way out of
39// the use of this software, even if advised of the possibility of such damage.
40//
41//M*/
42
43#import <UIKit/UIKit.h>
44#import <Accelerate/Accelerate.h>
45#import <AVFoundation/AVFoundation.h>
46#import <ImageIO/ImageIO.h>
47#include "opencv2/core.hpp"
48#include "precomp.hpp"
49
50UIImage* MatToUIImage(const cv::Mat& image);
51void UIImageToMat(const UIImage* image, cv::Mat& m, bool alphaExist);
52
53UIImage* MatToUIImage(const cv::Mat& image) {
54
55    NSData *data = [NSData dataWithBytes:image.data
56                                  length:image.elemSize()*image.total()];
57
58    CGColorSpaceRef colorSpace;
59
60    if (image.elemSize() == 1) {
61        colorSpace = CGColorSpaceCreateDeviceGray();
62    } else {
63        colorSpace = CGColorSpaceCreateDeviceRGB();
64    }
65
66    CGDataProviderRef provider =
67            CGDataProviderCreateWithCFData((__bridge CFDataRef)data);
68
69    // Creating CGImage from cv::Mat
70    CGImageRef imageRef = CGImageCreate(image.cols,
71                                        image.rows,
72                                        8,
73                                        8 * image.elemSize(),
74                                        image.step.p[0],
75                                        colorSpace,
76                                        kCGImageAlphaNone|
77                                        kCGBitmapByteOrderDefault,
78                                        provider,
79                                        NULL,
80                                        false,
81                                        kCGRenderingIntentDefault
82                                        );
83
84
85    // Getting UIImage from CGImage
86    UIImage *finalImage = [UIImage imageWithCGImage:imageRef];
87    CGImageRelease(imageRef);
88    CGDataProviderRelease(provider);
89    CGColorSpaceRelease(colorSpace);
90
91    return finalImage;
92}
93
94void UIImageToMat(const UIImage* image,
95                         cv::Mat& m, bool alphaExist) {
96    CGColorSpaceRef colorSpace = CGImageGetColorSpace(image.CGImage);
97    CGFloat cols = image.size.width, rows = image.size.height;
98    CGContextRef contextRef;
99    CGBitmapInfo bitmapInfo = kCGImageAlphaPremultipliedLast;
100    if (CGColorSpaceGetModel(colorSpace) == 0)
101    {
102        m.create(rows, cols, CV_8UC1); // 8 bits per component, 1 channel
103        bitmapInfo = kCGImageAlphaNone;
104        if (!alphaExist)
105            bitmapInfo = kCGImageAlphaNone;
106        contextRef = CGBitmapContextCreate(m.data, m.cols, m.rows, 8,
107                                           m.step[0], colorSpace,
108                                           bitmapInfo);
109    }
110    else
111    {
112        m.create(rows, cols, CV_8UC4); // 8 bits per component, 4 channels
113        if (!alphaExist)
114            bitmapInfo = kCGImageAlphaNoneSkipLast |
115                                kCGBitmapByteOrderDefault;
116        contextRef = CGBitmapContextCreate(m.data, m.cols, m.rows, 8,
117                                           m.step[0], colorSpace,
118                                           bitmapInfo);
119    }
120    CGContextDrawImage(contextRef, CGRectMake(0, 0, cols, rows),
121                       image.CGImage);
122    CGContextRelease(contextRef);
123}
124