55d927ae0417891cf1415f2314138611d1e2b4b9
[zxing.git] / iphone / ZXingWidget / 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 "GrayBytesMonochromeBitmapSource.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
46 - (void)willDecodeImage {
47   if ([self.delegate respondsToSelector:@selector(decoder:willDecodeImage:usingSubset:)]) {
48     [self.delegate decoder:self willDecodeImage:self.image usingSubset:self.subsetImage];
49   }
50 }
51
52 - (void)progressDecodingImage:(NSString *)progress {
53   if ([self.delegate respondsToSelector:@selector(decoder:decodingImage:usingSubset:progress:)]) {
54     [self.delegate decoder:self decodingImage:self.image usingSubset:self.subsetImage progress:progress];
55   }
56 }
57
58 - (void)didDecodeImage:(TwoDDecoderResult *)result {
59   if ([self.delegate respondsToSelector:@selector(decoder:didDecodeImage:usingSubset:withResult:)]) {
60     [self.delegate decoder:self didDecodeImage:self.image usingSubset:self.subsetImage withResult:result];
61   }
62 }
63
64 - (void)failedToDecodeImage:(NSString *)reason {
65   if ([self.delegate respondsToSelector:@selector(decoder:failedToDecodeImage:usingSubset:reason:)]) {
66     [self.delegate decoder:self failedToDecodeImage:self.image usingSubset:self.subsetImage reason:reason];
67   }
68 }
69
70 #define SUBSET_SIZE 320.0
71 - (void) prepareSubset {
72   CGSize size = [image size];
73 #ifdef DEBUG
74   NSLog(@"decoding: image is (%.1f x %.1f), cropRect is (%.1f,%.1f)x(%.1f,%.1f)", size.width, size.height,
75       cropRect.origin.x, cropRect.origin.y, cropRect.size.width, cropRect.size.height);
76 #endif
77   float scale = fminf(1.0f, fmaxf(SUBSET_SIZE / cropRect.size.width, SUBSET_SIZE / cropRect.size.height));
78   CGPoint offset = CGPointMake(-cropRect.origin.x, -cropRect.origin.y);
79 #ifdef DEBUG
80   NSLog(@"  offset = (%.1f, %.1f), scale = %.3f", offset.x, offset.y, scale);
81 #endif
82   
83   subsetWidth = cropRect.size.width * scale;
84   subsetHeight = cropRect.size.height * scale;
85   
86   subsetBytesPerRow = ((subsetWidth + 0xf) >> 4) << 4;
87 #ifdef DEBUG
88   NSLog(@"decoding: image to decode is (%d x %d) (%d bytes/row)", subsetWidth, subsetHeight, subsetBytesPerRow);
89 #endif
90   
91   subsetData = (unsigned char *)malloc(subsetBytesPerRow * subsetHeight);
92 #ifdef DEBUG
93   NSLog(@"allocated %d bytes of memory", subsetBytesPerRow * subsetHeight);
94 #endif
95   
96   CGColorSpaceRef grayColorSpace = CGColorSpaceCreateDeviceGray();
97   
98   CGContextRef ctx = 
99   CGBitmapContextCreate(subsetData, subsetWidth, subsetHeight, 
100               8, subsetBytesPerRow, grayColorSpace, 
101               kCGImageAlphaNone);
102   CGColorSpaceRelease(grayColorSpace);
103   CGContextSetInterpolationQuality(ctx, kCGInterpolationNone);
104   CGContextSetAllowsAntialiasing(ctx, false);
105   // adjust the coordinate system
106   CGContextTranslateCTM(ctx, 0.0, subsetHeight);
107   CGContextScaleCTM(ctx, 1.0, -1.0);  
108   
109 #ifdef DEBUG
110   NSLog(@"created %dx%d bitmap context", subsetWidth, subsetHeight);
111 #endif
112   
113   UIGraphicsPushContext(ctx);
114   CGRect rect = CGRectMake(offset.x * scale, offset.y * scale, scale * size.width, scale * size.height);
115 #ifdef DEBUG
116   NSLog(@"rect for image = (%.1f,%.1f)x(%.1f,%.1f)", rect.origin.x, rect.origin.y, rect.size.width, rect.size.height);
117 #endif
118   [image drawInRect:rect];
119   UIGraphicsPopContext();
120   
121 #ifdef DEBUG
122   NSLog(@"drew image into %d(%d)x%d  bitmap context", subsetWidth, subsetBytesPerRow, subsetHeight);
123 #endif
124   CGContextFlush(ctx);
125 #ifdef DEBUG
126   NSLog(@"flushed context");
127 #endif
128     
129   CGImageRef subsetImageRef = CGBitmapContextCreateImage(ctx);
130 #ifdef DEBUG
131   NSLog(@"created CGImage from context");
132 #endif
133   
134   self.subsetImage = [UIImage imageWithCGImage:subsetImageRef];
135   // for debug purposes.
136 //  UIImageWriteToSavedPhotosAlbum(self.subsetImage, nil, nil, nil);
137
138   CGImageRelease(subsetImageRef);
139   
140   CGContextRelease(ctx);
141 #ifdef DEBUG
142   NSLog(@"released context");  
143 #endif
144 }  
145
146 - (void)decode:(id)arg {
147   NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
148   { 
149
150     NSSet *formatReaders = [FormatReader formatReaders];
151     
152     Ref<LuminanceSource> source (new GrayBytesMonochromeBitmapSource(subsetData, subsetWidth, subsetHeight, subsetBytesPerRow));
153     
154     Ref<Binarizer> binarizer (new GlobalHistogramBinarizer(source));
155     Ref<BinaryBitmap> grayImage (new BinaryBitmap(binarizer));
156 #ifdef DEBUG
157     NSLog(@"created GrayBytesMonochromeBitmapSource", subsetWidth, subsetHeight);
158     NSLog(@"grayImage count = %d", grayImage->count());
159 #endif
160     
161     TwoDDecoderResult *decoderResult = nil;
162     
163 #ifdef TRY_ROTATIONS
164     for (int i = 0; !decoderResult && i < 4; i++) {
165 #endif
166       for (FormatReader *reader in formatReaders) {
167         try {
168   #ifdef DEBUG
169           NSLog(@"decoding gray image");
170   #endif
171           Ref<Result> result([reader decode:grayImage]);
172   #ifdef DEBUG
173           NSLog(@"gray image decoded");
174   #endif
175           
176           Ref<String> resultText(result->getText());
177           const char *cString = resultText->getText().c_str();
178           const std::vector<Ref<ResultPoint> > &resultPoints = result->getResultPoints();
179           NSMutableArray *points = 
180             [NSMutableArray arrayWithCapacity:resultPoints.size()];
181           
182           for (size_t i = 0; i < resultPoints.size(); i++) {
183             const Ref<ResultPoint> &rp = resultPoints[i];
184             CGPoint p = CGPointMake(rp->getX(), rp->getY());
185             [points addObject:[NSValue valueWithCGPoint:p]];
186           }
187           
188           NSString *resultString = [NSString stringWithCString:cString
189                                 encoding:NSUTF8StringEncoding];
190           
191           decoderResult = [TwoDDecoderResult resultWithText:resultString
192                                                      points:points];
193         } catch (ReaderException &rex) {
194           NSLog(@"failed to decode, caught ReaderException '%s'",
195               rex.what());
196         } catch (IllegalArgumentException &iex) {
197           NSLog(@"failed to decode, caught IllegalArgumentException '%s'", 
198               iex.what());
199         } catch (...) {
200           NSLog(@"Caught unknown exception!");
201         }
202       }
203       
204 #ifdef TRY_ROTATIONS
205       if (!decoderResult) {
206 #ifdef DEBUG
207         NSLog(@"rotating gray image");
208 #endif
209         grayImage = grayImage->rotateCounterClockwise();
210 #ifdef DEBUG
211         NSLog(@"gray image rotated");
212 #endif
213       }
214     }
215 #endif
216     
217     if (decoderResult) {
218       [self performSelectorOnMainThread:@selector(didDecodeImage:)
219                    withObject:decoderResult
220                 waitUntilDone:NO];
221     } else {
222       [self performSelectorOnMainThread:@selector(failedToDecodeImage:)
223                    withObject:NSLocalizedString(@"Decoder BarcodeDetectionFailure", @"No barcode detected.")
224                 waitUntilDone:NO];
225     }
226     
227     free(subsetData);
228     self.subsetData = NULL;
229   }
230   [pool drain];
231 #ifdef DEBUG
232   NSLog(@"finished decoding.");
233 #endif
234   
235   // if this is not the main thread, then we end it
236   if (![NSThread isMainThread]) {
237     [NSThread exit];
238   }
239 }
240
241 - (void) decodeImage:(UIImage *)i {
242   [self decodeImage:i cropRect:CGRectMake(0.0f, 0.0f, image.size.width, image.size.height)];
243 }
244
245 - (void) decodeImage:(UIImage *)i cropRect:(CGRect)cr {
246   self.image = i;
247   self.cropRect = cr;
248   
249   [self prepareSubset];
250   [self willDecodeImage];
251   [self performSelectorOnMainThread:@selector(progressDecodingImage:)
252                withObject:NSLocalizedString(@"Decoder MessageWhileDecoding", @"Decoding ...")
253             waitUntilDone:NO];  
254   
255   [NSThread detachNewThreadSelector:@selector(decode:) 
256                toTarget:self 
257                withObject:nil];
258 }
259
260 - (void) dealloc {
261   [image release];
262   [subsetImage release];
263   if (subsetData) free(subsetData);
264   [super dealloc];
265 }
266
267 @end