Skip to content

Commit 35cde60

Browse files
author
psiroki
committed
Fixed some potential crashes/leaks, added scan enable/disable code
1 parent b758c0f commit 35cde60

File tree

4 files changed

+43
-9
lines changed

4 files changed

+43
-9
lines changed

English.lproj/VertexDocument.xib

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
</object>
2222
<object class="NSMutableArray" key="IBDocument.EditedObjectIDs">
2323
<bool key="EncodedWithXMLCoder">YES</bool>
24-
<integer value="100023"/>
24+
<integer value="5"/>
2525
</object>
2626
<object class="NSArray" key="IBDocument.PluginDependencies">
2727
<bool key="EncodedWithXMLCoder">YES</bool>
@@ -174,7 +174,7 @@
174174
<string key="NSFrameSize">{599, 300}</string>
175175
<reference key="NSSuperview" ref="808025513"/>
176176
<bool key="NSViewIsLayerTreeHost">YES</bool>
177-
<float key="zoomFactor">0.18125000596046448</float>
177+
<float key="zoomFactor">1</float>
178178
<float key="rotationAngle">0.0</float>
179179
<string key="currentToolMode">IKToolModeNone</string>
180180
<bool key="autoresizes">NO</bool>
@@ -202,7 +202,7 @@
202202
<object class="NSButton" id="209450221">
203203
<reference key="NSNextResponder" ref="190226495"/>
204204
<int key="NSvFlags">268</int>
205-
<string key="NSFrame">{{109, 4}, {118, 25}}</string>
205+
<string key="NSFrame">{{90, 4}, {137, 25}}</string>
206206
<reference key="NSSuperview" ref="190226495"/>
207207
<bool key="NSEnabled">YES</bool>
208208
<object class="NSButtonCell" key="NSCell" id="902948351">
@@ -463,7 +463,7 @@
463463
<string>public.url</string>
464464
</object>
465465
</object>
466-
<string key="NSFrameSize">{326, 28}</string>
466+
<string key="NSFrame">{{0, 14}, {326, 14}}</string>
467467
<reference key="NSSuperview" ref="246688413"/>
468468
<object class="NSTextContainer" key="NSTextContainer" id="728819784">
469469
<object class="NSLayoutManager" key="NSLayoutManager">
@@ -859,6 +859,14 @@
859859
</object>
860860
<int key="connectionID">100093</int>
861861
</object>
862+
<object class="IBConnectionRecord">
863+
<object class="IBOutletConnection" key="connection">
864+
<string key="label">scanButton</string>
865+
<reference key="source" ref="512844837"/>
866+
<reference key="destination" ref="209450221"/>
867+
</object>
868+
<int key="connectionID">100094</int>
869+
</object>
862870
</object>
863871
<object class="IBMutableOrderedSet" key="objectRecords">
864872
<object class="NSArray" key="orderedObjects">
@@ -1385,7 +1393,7 @@
13851393
</object>
13861394
</object>
13871395
<nil key="sourceID"/>
1388-
<int key="maxID">100093</int>
1396+
<int key="maxID">100094</int>
13891397
</object>
13901398
<object class="IBClassDescriber" key="IBDocument.Classes">
13911399
<object class="NSMutableArray" key="referencedPartialClassDescriptions">
@@ -1418,6 +1426,7 @@
14181426
<string>imageView</string>
14191427
<string>resultTextView</string>
14201428
<string>rowsTextField</string>
1429+
<string>scanButton</string>
14211430
<string>stylePopUpButton</string>
14221431
<string>typePopUpButton</string>
14231432
<string>variableTextField</string>
@@ -1428,6 +1437,7 @@
14281437
<string>IKImageView</string>
14291438
<string>NSTextView</string>
14301439
<string>NSTextField</string>
1440+
<string>NSButton</string>
14311441
<string>NSPopUpButton</string>
14321442
<string>NSPopUpButton</string>
14331443
<string>NSTextField</string>

VertexDocument.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
IBOutlet NSPopUpButton *typePopUpButton;
2323
IBOutlet NSPopUpButton *stylePopUpButton;
2424

25+
IBOutlet NSButton *scanButton;
26+
2527
ImageViewGridLayer *gridLayer;
2628
// each row has columns, each column has points
2729
NSMutableArray *pointMatrix;

VertexDocument.m

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ - (void)windowControllerDidLoadNib:(NSWindowController *) aController
4949
[gridLayer setNeedsDisplay];
5050

5151
[imageView setOverlay:gridLayer forType:IKOverlayTypeImage];
52+
[scanButton setEnabled:NO];
5253
}
5354

5455
- (NSData *)dataOfType:(NSString *)typeName error:(NSError **)outError
@@ -84,6 +85,10 @@ - (IBAction)updateGrid:(id)sender
8485
int rows = [[rowsTextField stringValue] intValue];
8586
int cols = [[colsTextField stringValue] intValue];
8687

88+
BOOL enabled = (rows > 0 && cols > 0);
89+
NSLog(@"Matrix: %d %d", rows, cols);
90+
[scanButton setEnabled:enabled];
91+
8792

8893
if (rows <= 50 && cols <= 50 && (rows != gridLayer.rows || cols != gridLayer.cols)) {
8994
//reset our array
@@ -104,13 +109,21 @@ - (IBAction)updateGrid:(id)sender
104109
}
105110
- (IBAction)scanImage:(id)sender
106111
{
112+
NSLog(@"Image properties: %@", [imageView imageProperties]);
107113
[self updateGrid:sender];
114+
108115
CGImageRef img = [imageView image];
109116
size_t width = CGImageGetWidth(img);
110117
size_t height = CGImageGetHeight(img);
111118
size_t pitch = CGImageGetBytesPerRow(img);
112-
CFDataRef dataRef = CGDataProviderCopyData(CGImageGetDataProvider(img));
113-
const UInt8 *data = CFDataGetBytePtr(dataRef);
119+
120+
UInt8 *bits = (UInt8*)malloc(width * height * 4);
121+
CGContextRef textureContext = CGBitmapContextCreate(bits, width, height, 8, width*4,
122+
CGImageGetColorSpace(img), kCGImageAlphaPremultipliedLast);
123+
CGContextDrawImage(textureContext, CGRectMake(0.0, 0.0, (CGFloat)width, (CGFloat)height), img);
124+
CGContextRelease(textureContext);
125+
126+
const UInt8 *data = bits;
114127
int cellWidth = (width / gridLayer.cols);
115128
int cellHeight = (height / gridLayer.rows);
116129

@@ -127,7 +140,7 @@ - (IBAction)scanImage:(id)sender
127140
// so data will point to the last row
128141
cell.data = data+(height-1)*pitch;
129142
// also offset it by the coordinates of the cell
130-
cell.data += (cx*cellWidth*4)+(cy*cellHeight*pitch);
143+
cell.data += (cx*cellWidth*4)+(cy*cellHeight*(-pitch));
131144
Vec2Array points;
132145
findPoints(&cell, &points);
133146
if(points.count > 0)
@@ -142,8 +155,11 @@ - (IBAction)scanImage:(id)sender
142155
[arr addObject:[NSValue valueWithPoint:NSMakePoint(p.x, p.y)]];
143156
}
144157
}
158+
if(points.points)
159+
free(points.points);
145160
}
146161
}
162+
free(bits);
147163
[gridLayer setNeedsDisplay];
148164
[self updateResultTextField];
149165
}

VertexScanner.mm

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
*
77
*/
88

9-
#define VERBOSE
9+
//#define VERBOSE
1010

1111
#ifdef VERBOSE
1212
#import <Foundation/Foundation.h>
@@ -66,6 +66,9 @@ static Vec2 normalize(const Vec2 &a)
6666

6767
static vector<Vec2> removeDuplicates(vector<Vec2> &points)
6868
{
69+
if(points.size() <= 0)
70+
return points;
71+
6972
vector<Vec2> result;
7073
Vec2 last = *(points.end()-1);
7174
for(int i=0; i<points.size(); i++)
@@ -90,6 +93,9 @@ inline bool isConvex(float sineOfAngle)
9093

9194
static vector<Vec2> makeConvex(vector<Vec2> &points)
9295
{
96+
if(points.size() <= 0)
97+
return points;
98+
9399
vector<Vec2> convex;
94100
convex.push_back(points[0]);
95101
convex.push_back(points[1]);

0 commit comments

Comments
 (0)