Visual Servoing Platform version 3.7.0
Loading...
Searching...
No Matches
ImageConversion.mm
1/*
2 * ViSP, open source Visual Servoing Platform software.
3 * Copyright (C) 2005 - 2025 by Inria. All rights reserved.
4 *
5 * This software is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 * See the file LICENSE.txt at the root directory of this source
10 * distribution for additional information about the GNU GPL.
11 *
12 * For using ViSP with software that can not be combined with the GNU
13 * GPL, please contact Inria about acquiring a ViSP Professional
14 * Edition License.
15 *
16 * See https://visp.inria.fr for more information.
17 *
18 * This software was developed at:
19 * Inria Rennes - Bretagne Atlantique
20 * Campus Universitaire de Beaulieu
21 * 35042 Rennes Cedex
22 * France
23 *
24 * If you have questions regarding the use of this file, please contact
25 * Inria at visp@inria.fr
26 *
27 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
28 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
29 */
30
31#ifndef DOXYGEN_SHOULD_SKIP_THIS
32
33#import "ImageConversion.h"
34
35@implementation ImageConversion
36
37
39// Converts an UIImage that could be in gray or color into a ViSP color image
40+ (vpImage<vpRGBa>)vpImageColorFromUIImage:(UIImage *)image
41{
42 CGColorSpaceRef colorSpace = CGImageGetColorSpace(image.CGImage);
43
44 if (CGColorSpaceGetModel(colorSpace) == kCGColorSpaceModelMonochrome) {
45 NSLog(@"Input UIImage is grayscale");
46 vpImage<unsigned char> gray(image.size.height, image.size.width); // 8 bits per component, 1 channel
47
48 CGContextRef contextRef = CGBitmapContextCreate(gray.bitmap, // pointer to data
49 image.size.width, // width of bitmap
50 image.size.height, // height of bitmap
51 8, // bits per component
52 image.size.width, // bytes per row
53 colorSpace, // colorspace
54 kCGImageAlphaNone |
55 kCGBitmapByteOrderDefault); // bitmap info flags
56
57 CGContextDrawImage(contextRef, CGRectMake(0, 0, image.size.width, image.size.height), image.CGImage);
58 CGContextRelease(contextRef);
59
60 vpImage<vpRGBa> color;
61 vpImageConvert::convert(gray, color);
62
63 return color;
64 }
65 else {
66 NSLog(@"Input UIImage is color");
67 vpImage<vpRGBa> color(image.size.height, image.size.width); // 8 bits per component, 4 channels
68
69 colorSpace = CGColorSpaceCreateDeviceRGB();
70
71 CGContextRef contextRef = CGBitmapContextCreate(color.bitmap, // pointer to data
72 image.size.width, // width of bitmap
73 image.size.height, // height of bitmap
74 8, // bits per component
75 4 * image.size.width, // bytes per row
76 colorSpace, // colorspace
77 kCGImageAlphaNoneSkipLast |
78 kCGBitmapByteOrderDefault); // bitmap info flags
79
80 CGContextDrawImage(contextRef, CGRectMake(0, 0, image.size.width, image.size.height), image.CGImage);
81 CGContextRelease(contextRef);
82
83 return color;
84 }
85}
87
89// Converts an UIImage that could be in gray or color into a ViSP gray image
90+ (vpImage<unsigned char>)vpImageGrayFromUIImage:(UIImage *)image
91{
92 CGColorSpaceRef colorSpace = CGImageGetColorSpace(image.CGImage);
93
94 if (CGColorSpaceGetModel(colorSpace) == kCGColorSpaceModelMonochrome) {
95 NSLog(@"Input UIImage is grayscale");
96 vpImage<unsigned char> gray(image.size.height, image.size.width); // 8 bits per component, 1 channel
97
98 CGContextRef contextRef = CGBitmapContextCreate(gray.bitmap, // pointer to data
99 image.size.width, // width of bitmap
100 image.size.height, // height of bitmap
101 8, // bits per component
102 image.size.width, // bytes per row
103 colorSpace, // colorspace
104 kCGImageAlphaNone |
105 kCGBitmapByteOrderDefault); // bitmap info flags
106
107 CGContextDrawImage(contextRef, CGRectMake(0, 0, image.size.width, image.size.height), image.CGImage);
108 CGContextRelease(contextRef);
109
110 return gray;
111 } else {
112 NSLog(@"Input UIImage is color");
113 vpImage<vpRGBa> color(image.size.height, image.size.width); // 8 bits per component, 4 channels (color channels + alpha)
114
115 colorSpace = CGColorSpaceCreateDeviceRGB();
116
117 CGContextRef contextRef = CGBitmapContextCreate(color.bitmap, // pointer to data
118 image.size.width, // width of bitmap
119 image.size.height, // height of bitmap
120 8, // bits per component
121 4 * image.size.width, // bytes per row
122 colorSpace, // colorspace
123 kCGImageAlphaNoneSkipLast |
124 kCGBitmapByteOrderDefault); // bitmap info flags
125
126 CGContextDrawImage(contextRef, CGRectMake(0, 0, image.size.width, image.size.height), image.CGImage);
127 CGContextRelease(contextRef);
128
129 vpImage<unsigned char> gray;
130 vpImageConvert::convert(color, gray);
131
132 return gray;
133 }
134}
136
138// Converts a color ViSP image into a color UIImage
139+ (UIImage *)UIImageFromVpImageColor:(const vpImage<vpRGBa> &)I
140{
141 NSData *data = [NSData dataWithBytes:I.bitmap length:I.getSize()*4];
142 CGColorSpaceRef colorSpace;
143
144 colorSpace = CGColorSpaceCreateDeviceRGB();
145
146 CGDataProviderRef provider = CGDataProviderCreateWithCFData((__bridge CFDataRef)data);
147
148 // Creating CGImage from vpImage
149 CGImageRef imageRef = CGImageCreate(I.getWidth(), // width
150 I.getHeight(), // height
151 8, // bits per component
152 8 * 4, // bits per pixel
153 4 * I.getWidth(), // bytesPerRow
154 colorSpace, // colorspace
155 kCGImageAlphaNone|kCGBitmapByteOrderDefault,// bitmap info
156 provider, // CGDataProviderRef
157 nullptr, // decode
158 false, // should interpolate
159 kCGRenderingIntentDefault // intent
160 );
161
162
163 // Getting UIImage from CGImage
164 UIImage *finalImage = [UIImage imageWithCGImage:imageRef];
165 CGImageRelease(imageRef);
166 CGDataProviderRelease(provider);
167 CGColorSpaceRelease(colorSpace);
168
169 return finalImage;
170}
172
174// Converts a gray level ViSP image into a gray level UIImage
175+ (UIImage *)UIImageFromVpImageGray:(const vpImage<unsigned char> &)I
176{
177 NSData *data = [NSData dataWithBytes:I.bitmap length:I.getSize()];
178 CGColorSpaceRef colorSpace;
179
180 colorSpace = CGColorSpaceCreateDeviceGray();
181
182 CGDataProviderRef provider = CGDataProviderCreateWithCFData((__bridge CFDataRef)data);
183
184 // Creating CGImage from vpImage
185 CGImageRef imageRef = CGImageCreate(I.getWidth(), // width
186 I.getHeight(), // height
187 8, // bits per component
188 8, // bits per pixel
189 I.getWidth(), // bytesPerRow
190 colorSpace, // colorspace
191 kCGImageAlphaNone|kCGBitmapByteOrderDefault,// bitmap info
192 provider, // CGDataProviderRef
193 nullptr, // decode
194 false, // should interpolate
195 kCGRenderingIntentDefault // intent
196 );
197
198
199 // Getting UIImage from CGImage
200 UIImage *finalImage = [UIImage imageWithCGImage:imageRef];
201 CGImageRelease(imageRef);
202 CGDataProviderRelease(provider);
203 CGColorSpaceRelease(colorSpace);
204
205 return finalImage;
206}
208
209@end
210
211#endif
static void convert(const vpImage< unsigned char > &src, vpImage< vpRGBa > &dest)
Definition of the vpImage class member functions.
Definition vpImage.h:131
unsigned int getWidth() const
Definition vpImage.h:242
unsigned int getHeight() const
Definition vpImage.h:181