Reinstate more optimization, but avoid disagreement with dex by properly disabling...
[zxing.git] / iphone / Classes / ScannedImageView.m
1 //
2 //  ScannedImageView.m
3 //  ZXing
4 //
5 //  Created by Christian Brunschen on 01/07/2008.
6 /*
7  * Copyright 2008 ZXing authors
8  *
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  */
21
22 #import "ScannedImageView.h"
23 #import <math.h>
24
25 @implementation ScannedImageView
26
27 - (id)initWithFrame:(CGRect)frame {
28         if ((self = [super initWithFrame:frame])) {
29     resultPoints = [[NSMutableArray alloc] initWithCapacity:10];
30         }
31         return self;
32 }
33
34 - (id)initWithCoder:(NSCoder *)decoder {
35   if ((self = [super initWithCoder:decoder]) != nil) {
36     resultPoints = [[NSMutableArray alloc] initWithCapacity:10];
37   }
38   return self;
39 }
40
41 - (void)drawRect:(CGRect)rect {
42   [super drawRect:rect];
43   
44   if (image) {
45     // draw the image, scaled to fit, top and center
46     CGSize imageSize = image.size;
47     CGRect bounds = [self bounds];
48     double imageScale = fminf(bounds.size.width / imageSize.width,
49                               bounds.size.height / imageSize.height);
50     double dx = (bounds.size.width - imageSize.width * imageScale) / 2.0;
51     double dy = 0.0;
52     
53     CGContextRef ctx = UIGraphicsGetCurrentContext();
54     CGContextSetInterpolationQuality(ctx, kCGInterpolationDefault);
55     CGRect imageRect = CGRectMake(dx, dy, 
56                                   imageSize.width * imageScale,
57                                   imageSize.height * imageScale);
58     [image drawInRect:imageRect];
59     
60     [[UIColor greenColor] set];
61
62     if (resultPoints && [resultPoints count]) {
63 #define R 4.0
64       if ([resultPoints count] == 2) {
65         CGPoint p0 = [[resultPoints objectAtIndex:0] CGPointValue];
66         CGPoint p1 = [[resultPoints objectAtIndex:1] CGPointValue];
67         CGContextMoveToPoint(ctx, dx + p0.x * imageScale, dy + p0.y * imageScale);
68         CGContextAddLineToPoint(ctx, dx + p1.x * imageScale, dy + p1.y * imageScale);
69         CGContextSetLineWidth(ctx, 4.0);
70         CGContextSetLineCap(ctx, kCGLineCapSquare);
71         CGContextStrokePath(ctx);
72       } else {
73         // for each resultPoint, draw it
74         for (NSValue *pointValue in resultPoints) {
75           CGPoint resultPoint = [pointValue CGPointValue];
76           float px = dx + resultPoint.x * imageScale;
77           float py = dy + resultPoint.y * imageScale;
78           CGContextAddRect(ctx,
79                            CGRectMake(px - R, py - R, 2 * R, 2 * R));
80         }
81         CGContextFillPath(ctx);
82       }
83       CGContextFlush(ctx);
84 #undef R
85     }
86   }
87 }
88
89 - (void) addResultPoint:(CGPoint)p {
90   [resultPoints addObject:[NSValue valueWithCGPoint:p]];
91   [self setNeedsDisplay];
92 }
93
94 - (void) clearResultPoints {
95   [resultPoints removeAllObjects];
96 }
97
98 - (void) setImage:(UIImage *)newImage {
99   [newImage retain];
100   [image release];
101   image = newImage;
102   [self clearResultPoints];
103   [self setNeedsDisplay];
104 }
105
106 - (UIImage *)image {
107   return image;
108 }
109
110
111 - (void)dealloc {
112   [image release];
113   [resultPoints release];
114         [super dealloc];
115 }
116
117
118 @end