-
Notifications
You must be signed in to change notification settings - Fork 11
/
ZBarImage.m
306 lines (264 loc) · 7.71 KB
/
ZBarImage.m
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
//------------------------------------------------------------------------
// Copyright 2009-2010 (c) Jeff Brown <[email protected]>
//
// This file is part of the ZBar Bar Code Reader.
//
// The ZBar Bar Code Reader is free software; you can redistribute it
// and/or modify it under the terms of the GNU Lesser Public License as
// published by the Free Software Foundation; either version 2.1 of
// the License, or (at your option) any later version.
//
// The ZBar Bar Code Reader is distributed in the hope that it will be
// useful, but WITHOUT ANY WARRANTY; without even the implied warranty
// of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser Public License for more details.
//
// You should have received a copy of the GNU Lesser Public License
// along with the ZBar Bar Code Reader; if not, write to the Free
// Software Foundation, Inc., 51 Franklin St, Fifth Floor,
// Boston, MA 02110-1301 USA
//
// http://sourceforge.net/projects/zbar
//------------------------------------------------------------------------
#import <UIKit/UIKit.h>
#import <ZBarSDK/ZBarImage.h>
#import "debug.h"
static void image_cleanup(zbar_image_t *zimg)
{
ZBarImage *image = zbar_image_get_userdata(zimg);
[image cleanup];
}
@implementation ZBarImage
@dynamic format, sequence, size, crop, data, dataLength, symbols, zbarImage,
UIImage;
+ (unsigned long) fourcc: (NSString*) format
{
return(zbar_fourcc_parse([format UTF8String]));
}
- (id) initWithImage: (zbar_image_t*) image
{
if(!image) {
[self release];
return(nil);
}
if(self = [super init]) {
zimg = image;
zbar_image_ref(image, 1);
zbar_image_set_userdata(zimg, self);
}
return(self);
}
- (id) init
{
zbar_image_t *image = zbar_image_create();
self = [self initWithImage: image];
zbar_image_ref(image, -1);
return(self);
}
- (void) dealloc
{
if(zimg) {
zbar_image_ref(zimg, -1);
zimg = NULL;
}
[super dealloc];
}
- (id) initWithCGImage: (CGImageRef) image
crop: (CGRect) crop
size: (CGSize) size
{
if(!(self = [self init]))
return(nil);
uint64_t t_start = timer_now();
unsigned int w = size.width + 0.5;
unsigned int h = size.height + 0.5;
unsigned long datalen = w * h;
uint8_t *raw = malloc(datalen);
if(!raw) {
[self release];
return(nil);
}
zbar_image_set_data(zimg, raw, datalen, zbar_image_free_data);
zbar_image_set_format(zimg, zbar_fourcc('Y','8','0','0'));
zbar_image_set_size(zimg, w, h);
// scale and crop simultaneously
CGFloat scale = size.width / crop.size.width;
crop.origin.x *= -scale;
crop.size.width = scale * (CGFloat)CGImageGetWidth(image);
scale = size.height / crop.size.height;
CGFloat height = CGImageGetHeight(image);
// compensate for wacky origin
crop.origin.y = height - crop.origin.y - crop.size.height;
crop.origin.y *= -scale;
crop.size.height = scale * height;
// generate grayscale image data
CGColorSpaceRef cs = CGColorSpaceCreateDeviceGray();
CGContextRef ctx =
CGBitmapContextCreate(raw, w, h, 8, w, cs, kCGImageAlphaNone);
CGColorSpaceRelease(cs);
CGContextSetAllowsAntialiasing(ctx, 0);
CGContextDrawImage(ctx, crop, image);
#if 0
zlog(@"convert image %dx%d: crop %g,%g %gx%g size %gx%g (%dx%d)",
CGImageGetWidth(image), CGImageGetHeight(image),
crop.origin.x, crop.origin.y, crop.size.width, crop.size.height,
size.width, size.height, w, h);
CGImageRef cgdump = CGBitmapContextCreateImage(ctx);
UIImage *uidump = [[UIImage alloc]
initWithCGImage: cgdump];
CGImageRelease(cgdump);
UIImageWriteToSavedPhotosAlbum(uidump, nil, nil, NULL);
[uidump release];
#endif
CGContextRelease(ctx);
t_convert = timer_elapsed(t_start, timer_now());
return(self);
}
- (id) initWithCGImage: (CGImageRef) image
size: (CGSize) size
{
CGRect crop = CGRectMake(0, 0,
CGImageGetWidth(image),
CGImageGetHeight(image));
return([self initWithCGImage: image
crop: crop
size: size]);
}
- (id) initWithCGImage: (CGImageRef) image
{
CGRect crop = CGRectMake(0, 0,
CGImageGetWidth(image),
CGImageGetHeight(image));
return([self initWithCGImage: image
crop: crop
size: crop.size]);
}
- (zbar_image_t*) image
{
return(zimg);
}
- (unsigned long) format
{
return(zbar_image_get_format(zimg));
}
- (void) setFormat: (unsigned long) format
{
zbar_image_set_format(zimg, format);
}
- (unsigned) sequence
{
return(zbar_image_get_sequence(zimg));
}
- (void) setSequence: (unsigned) seq
{
zbar_image_set_sequence(zimg, seq);
}
- (CGSize) size
{
unsigned w, h;
zbar_image_get_size(zimg, &w, &h);
return(CGSizeMake(w, h));
}
- (void) setSize: (CGSize) size
{
zbar_image_set_size(zimg, size.width + .5, size.height + .5);
}
- (CGRect) crop
{
unsigned x, y, w, h;
zbar_image_get_crop(zimg, &x, &y, &w, &h);
return(CGRectMake(x, y, w, h));
}
- (void) setCrop: (CGRect) crop
{
zbar_image_set_crop(zimg, crop.origin.x + .5, crop.origin.y + .5,
crop.size.width + .5, crop.size.height + .5);
}
- (ZBarSymbolSet*) symbols
{
return([[[ZBarSymbolSet alloc]
initWithSymbolSet: zbar_image_get_symbols(zimg)]
autorelease]);
}
- (void) setSymbols: (ZBarSymbolSet*) symbols
{
zbar_image_set_symbols(zimg, [symbols zbarSymbolSet]);
}
- (const void*) data
{
return(zbar_image_get_data(zimg));
}
- (unsigned long) dataLength
{
return(zbar_image_get_data_length(zimg));
}
- (void) setData: (const void*) data
withLength: (unsigned long) length
{
zbar_image_set_data(zimg, data, length, image_cleanup);
}
- (zbar_image_t*) zbarImage
{
return(zimg);
}
- (UIImage*) UIImageWithOrientation: (UIImageOrientation) orient
{
unsigned long format = self.format;
size_t bpc, bpp;
switch(format)
{
case zbar_fourcc('R','G','B','3'):
bpc = 8;
bpp = 24;
break;
case zbar_fourcc('R','G','B','4'):
bpc = 8;
bpp = 32;
break;
case zbar_fourcc('R','G','B','Q'):
bpc = 5;
bpp = 16;
break;
default:
NSLog(@"ERROR: format %.4s(%08lx) is unsupported",
(char*)&format, format);
assert(0);
return(nil);
};
unsigned w = zbar_image_get_width(zimg);
unsigned h = zbar_image_get_height(zimg);
const void *data = zbar_image_get_data(zimg);
size_t datalen = zbar_image_get_data_length(zimg);
CGDataProviderRef datasrc =
CGDataProviderCreateWithData(self, data, datalen, (void*)CFRelease);
CGColorSpaceRef cs = CGColorSpaceCreateDeviceRGB();
CGImageRef cgimg =
CGImageCreate(w, h, bpc, bpp, ((bpp + 7) >> 3) * w, cs,
kCGBitmapByteOrderDefault |
kCGImageAlphaNoneSkipFirst,
datasrc, NULL, YES, kCGRenderingIntentDefault);
CGColorSpaceRelease(cs);
CGDataProviderRelease(datasrc);
UIImage *uiimg =
[UIImage imageWithCGImage: cgimg
scale: 1
orientation: orient];
CGImageRelease(cgimg);
return(uiimg);
}
- (UIImage*) UIImage
{
return([self UIImageWithOrientation: UIImageOrientationUp]);
}
- (void) cleanup
{
}
#if 0
- (ZBarImage*) convertToFormat: (unsigned long) format
{
zbar_image_t *zdst = zbar_image_convert(zimg, format);
ZBarImage *image = ;
return([[[ZBarImage alloc] initWithImage: zdst] autorelease]);
}
#endif
@end