13d9439141f1e176cf234d2d4fe3c0827fb533aa
[zxing.git] / iphone / ZXingWidget / Classes / Decoder.mm
1 //
2 //  Decoder.m
3 //  ZXing
4 //
5 //  Created by Christian Brunschen on 31/03/2008.
6 //
7 /*
8  * Copyright 2008 ZXing authors
9  *
10  * Licensed under the Apache License, Version 2.0 (the "License");
11  * you may not use this file except in compliance with the License.
12  * You may obtain a copy of the License at
13  *
14  *      http://www.apache.org/licenses/LICENSE-2.0
15  *
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  */
22
23 #import "Decoder.h"
24 #import "TwoDDecoderResult.h"
25 #import "FormatReader.h"
26
27 #include <zxing/BinaryBitmap.h>
28 #include <zxing/ReaderException.h>
29 #include <zxing/common/IllegalArgumentException.h>
30 #include <zxing/common/GlobalHistogramBinarizer.h>
31 #include <zxing/common/GreyscaleLuminanceSource.h>
32
33 using namespace zxing;
34
35 @implementation Decoder
36
37 @synthesize image;
38 @synthesize cropRect;
39 @synthesize subsetImage;
40 @synthesize subsetData;
41 @synthesize subsetWidth;
42 @synthesize subsetHeight;
43 @synthesize subsetBytesPerRow;
44 @synthesize delegate;
45 @synthesize operationQueue;
46 @synthesize readers;
47
48
49 - (id)init {
50   if (self = [super init]) {
51     NSOperationQueue *opQueue = [[NSOperationQueue alloc] init];
52     self.operationQueue = opQueue;
53     [opQueue release];
54   }
55   return self;
56 }
57 - (void)willDecodeImage {
58   if ([self.delegate respondsToSelector:@selector(decoder:willDecodeImage:usingSubset:)]) {
59     [self.delegate decoder:self willDecodeImage:self.image usingSubset:self.subsetImage];
60   }
61 }
62
63 - (void)progressDecodingImage:(NSString *)progress {
64   if ([self.delegate respondsToSelector:@selector(decoder:decodingImage:usingSubset:progress:)]) {
65     [self.delegate decoder:self decodingImage:self.image usingSubset:self.subsetImage progress:progress];
66   }
67 }
68
69 - (void)didDecodeImage:(TwoDDecoderResult *)result {
70
71   if ([self.delegate respondsToSelector:@selector(decoder:didDecodeImage:usingSubset:withResult:)]) {
72     [self.delegate decoder:self didDecodeImage:self.image usingSubset:self.subsetImage withResult:result];
73   }
74         
75   [result release];
76 }
77
78 - (void)failedToDecodeImage:(NSString *)reason {
79
80   if ([self.delegate respondsToSelector:@selector(decoder:failedToDecodeImage:usingSubset:reason:)]) {
81     [self.delegate decoder:self failedToDecodeImage:self.image usingSubset:self.subsetImage reason:reason];
82   }
83 }
84
85 #define SUBSET_SIZE 320.0
86 - (void) prepareSubset {
87   CGSize size = [image size];
88 #ifdef DEBUG
89   NSLog(@"decoding: image is (%.1f x %.1f), cropRect is (%.1f,%.1f)x(%.1f,%.1f)", size.width, size.height,
90       cropRect.origin.x, cropRect.origin.y, cropRect.size.width, cropRect.size.height);
91 #endif
92   float scale = fminf(1.0f, fmaxf(SUBSET_SIZE / cropRect.size.width, SUBSET_SIZE / cropRect.size.height));
93   CGPoint offset = CGPointMake(-cropRect.origin.x, -cropRect.origin.y);
94 #ifdef DEBUG
95   NSLog(@"  offset = (%.1f, %.1f), scale = %.3f", offset.x, offset.y, scale);
96 #endif
97   
98   subsetWidth = cropRect.size.width * scale;
99   subsetHeight = cropRect.size.height * scale;
100   
101   subsetBytesPerRow = ((subsetWidth + 0xf) >> 4) << 4;
102 #ifdef DEBUG
103   NSLog(@"decoding: image to decode is (%d x %d) (%d bytes/row)", subsetWidth, subsetHeight, subsetBytesPerRow);
104 #endif
105   
106   subsetData = (unsigned char *)malloc(subsetBytesPerRow * subsetHeight);
107 #ifdef DEBUG
108   NSLog(@"allocated %d bytes of memory", subsetBytesPerRow * subsetHeight);
109 #endif
110   
111   CGColorSpaceRef grayColorSpace = CGColorSpaceCreateDeviceGray();
112   
113   CGContextRef ctx = 
114   CGBitmapContextCreate(subsetData, subsetWidth, subsetHeight, 
115               8, subsetBytesPerRow, grayColorSpace, 
116               kCGImageAlphaNone);
117   CGColorSpaceRelease(grayColorSpace);
118   CGContextSetInterpolationQuality(ctx, kCGInterpolationNone);
119   CGContextSetAllowsAntialiasing(ctx, false);
120   // adjust the coordinate system
121   CGContextTranslateCTM(ctx, 0.0, subsetHeight);
122   CGContextScaleCTM(ctx, 1.0, -1.0);  
123   
124 #ifdef DEBUG
125   NSLog(@"created %dx%d bitmap context", subsetWidth, subsetHeight);
126 #endif
127   
128   UIGraphicsPushContext(ctx);
129   CGRect rect = CGRectMake(offset.x * scale, offset.y * scale, scale * size.width, scale * size.height);
130 #ifdef DEBUG
131   NSLog(@"rect for image = (%.1f,%.1f)x(%.1f,%.1f)", rect.origin.x, rect.origin.y, rect.size.width, rect.size.height);
132 #endif
133   [image drawInRect:rect];
134   UIGraphicsPopContext();
135   
136 #ifdef DEBUG
137   NSLog(@"drew image into %d(%d)x%d  bitmap context", subsetWidth, subsetBytesPerRow, subsetHeight);
138 #endif
139   CGContextFlush(ctx);
140 #ifdef DEBUG
141   NSLog(@"flushed context");
142 #endif
143     
144   CGImageRef subsetImageRef = CGBitmapContextCreateImage(ctx);
145 #ifdef DEBUG
146   NSLog(@"created CGImage from context");
147 #endif
148   
149   self.subsetImage = [UIImage imageWithCGImage:subsetImageRef];
150   CGImageRelease(subsetImageRef);
151   
152   CGContextRelease(ctx);
153 #ifdef DEBUG
154   NSLog(@"released context");  
155 #endif
156 }  
157
158 - (void)decode:(id)arg {
159   NSAutoreleasePool* mainpool = [[NSAutoreleasePool alloc] init];
160   { 
161     //NSSet *formatReaders = [FormatReader formatReaders];
162     NSSet *formatReaders = self.readers;
163     Ref<LuminanceSource> source 
164            (new GreyscaleLuminanceSource(subsetData, subsetBytesPerRow, subsetHeight, 0, 0, subsetWidth, subsetHeight));
165     Ref<Binarizer> binarizer (new GlobalHistogramBinarizer(source));
166     Ref<BinaryBitmap> grayImage (new BinaryBitmap(binarizer));
167 #ifdef DEBUG
168     NSLog(@"created GreyscaleLuminanceSource", subsetWidth, subsetHeight);
169     NSLog(@"grayImage count = %d", grayImage->count());
170 #endif
171     
172     TwoDDecoderResult *decoderResult = nil;
173     
174 #ifdef TRY_ROTATIONS
175     for (int i = 0; !decoderResult && i < 4; i++) {
176 #endif
177       for (FormatReader *reader in formatReaders) {
178         NSAutoreleasePool *secondarypool = [[NSAutoreleasePool alloc] init];
179         try {
180   #ifdef DEBUG
181           NSLog(@"decoding gray image");
182   #endif  
183           Ref<Result> result([reader decode:grayImage]);
184   #ifdef DEBUG
185           NSLog(@"gray image decoded");
186   #endif
187           
188           Ref<String> resultText(result->getText());
189           const char *cString = resultText->getText().c_str();
190           const std::vector<Ref<ResultPoint> > &resultPoints = result->getResultPoints();
191           NSMutableArray *points = 
192             [[NSMutableArray alloc ] initWithCapacity:resultPoints.size()];
193           
194           for (size_t i = 0; i < resultPoints.size(); i++) {
195             const Ref<ResultPoint> &rp = resultPoints[i];
196             CGPoint p = CGPointMake(rp->getX(), rp->getY());
197             [points addObject:[NSValue valueWithCGPoint:p]];
198           }
199           
200           NSString *resultString = [NSString stringWithCString:cString
201                                 encoding:NSUTF8StringEncoding];
202           
203           decoderResult = [[TwoDDecoderResult resultWithText:resultString
204                                                      points:points] retain];
205           [points release];
206         } catch (ReaderException &rex) {
207           NSLog(@"failed to decode, caught ReaderException '%s'",
208               rex.what());
209         } catch (IllegalArgumentException &iex) {
210           NSLog(@"failed to decode, caught IllegalArgumentException '%s'", 
211               iex.what());
212         } catch (...) {
213           NSLog(@"Caught unknown exception!");
214         }
215         [secondarypool release];
216       }
217       
218 #ifdef TRY_ROTATIONS
219       if (!decoderResult) {
220 #ifdef DEBUG
221         NSLog(@"rotating gray image");
222 #endif
223         grayImage = grayImage->rotateCounterClockwise();
224 #ifdef DEBUG
225         NSLog(@"gray image rotated");
226 #endif
227       }
228     }
229 #endif
230           
231         free(subsetData);
232         self.subsetData = NULL;
233           
234     if (decoderResult) {
235       [self performSelectorOnMainThread:@selector(didDecodeImage:)
236                    withObject:decoderResult
237                 waitUntilDone:NO];
238     } else {
239       [self performSelectorOnMainThread:@selector(failedToDecodeImage:)
240                    withObject:NSLocalizedString(@"Decoder BarcodeDetectionFailure", @"No barcode detected.")
241                 waitUntilDone:NO];
242     }
243   }
244   
245   
246 #ifdef DEBUG
247   NSLog(@"finished decoding.");
248 #endif
249   [mainpool release];
250 }
251
252 - (void) decodeImage:(UIImage *)i {
253   [self decodeImage:i cropRect:CGRectMake(0.0f, 0.0f, i.size.width, i.size.height)];
254 }
255
256
257 - (void) asyncDecodeImage {
258   [self prepareSubset];
259   [self willDecodeImage];
260   [self performSelectorOnMainThread:@selector(willDecodeImage) 
261                          withObject:nil 
262                       waitUntilDone:NO];
263   [self performSelectorOnMainThread:@selector(progressDecodingImage:)
264                          withObject:NSLocalizedString(@"Decoder MessageWhileDecoding", @"Decoding ...")
265                       waitUntilDone:NO];
266 /*  NSInvocationOperation *op = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(decode:) object:nil];
267   [operationQueue addOperation:op];
268   [op release];*/
269   [self decode:nil];
270 }
271 - (void) decodeImage:(UIImage *)i cropRect:(CGRect)cr {
272   self.image = i;
273   self.cropRect = cr;
274   NSInvocationOperation *op = [[NSInvocationOperation  alloc] initWithTarget:self selector:@selector(asyncDecodeImage) object:nil];
275   [operationQueue addOperation:op];
276   [op release];
277 }
278
279 - (void) dealloc {
280
281   [image release];
282   [subsetImage release];
283   if (subsetData) free(subsetData);
284   [operationQueue release];
285   [readers release];
286   [super dealloc];
287 }
288
289 @end