Created
November 24, 2012 09:39
-
-
Save dryman/4139025 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| NSImage* zh_road = [NSImage imageNamed:@"zh_road.png"]; | |
| NSLog(@"%@", [zh_road representations]); | |
| NSBitmapImageRep *rep = [[zh_road representations] objectAtIndex:0]; | |
| NSLog(@"bytes per row: %ld, bits per pixel: %ld", [rep bytesPerRow], [rep bitsPerPixel]); | |
| unsigned long img_height = [rep pixelsHigh]; | |
| unsigned long img_width = [rep pixelsWide]; | |
| unsigned char * const bmpData = [rep bitmapData]; | |
| unsigned int * const histogram_rgba = calloc(img_width, sizeof(int)); | |
| { | |
| unsigned char * restrict src = bmpData; | |
| unsigned int * restrict dst; | |
| int idx; | |
| for (int h = 0; h < img_height; h++) { | |
| idx = 0; | |
| dst = histogram_rgba; | |
| while (idx++ < img_width) { | |
| *dst += (src[0] + src[1] + src[2])*src[3]/255; | |
| dst++; | |
| src+=4; | |
| } | |
| } | |
| } | |
| int left_idx_arr[100] = {}; // 100 indexes should be enough | |
| int left_iter = 0; | |
| { | |
| __label__ outside, inside, barrier; | |
| int idx = 0; | |
| outside: | |
| while (idx < img_width-1) { | |
| if (histogram_rgba[idx] > PIXEL_THRESHOLD && histogram_rgba[idx+1] > PIXEL_THRESHOLD) { | |
| left_idx_arr[left_iter++] = idx; | |
| idx++; | |
| goto inside; | |
| } | |
| idx++; | |
| } | |
| goto barrier; | |
| inside: | |
| while (idx < img_width) { | |
| if (histogram_rgba[idx] < PIXEL_THRESHOLD) { | |
| idx++; | |
| goto outside; | |
| } | |
| idx++; | |
| } | |
| barrier: | |
| for (int i = 0; i < left_iter; ++i) { | |
| printf("%d, ", left_idx_arr[i]); | |
| } | |
| printf("\n"); | |
| } | |
| NSMutableArray* segments = [NSMutableArray arrayWithCapacity:left_iter]; | |
| for (int i = 0; i < left_iter; i++) { | |
| int j = 0, idx = left_idx_arr[i]; | |
| NSMutableArray* elements = [NSMutableArray arrayWithObject:[NSNumber numberWithInt:idx]]; | |
| { | |
| __label__ inside, outside, barrier; | |
| inside: | |
| while (j < WORD_THRESHOLD && idx < img_width) { | |
| if (histogram_rgba[idx] < PIXEL_THRESHOLD) { | |
| [elements addObject:[NSNumber numberWithInt:idx-1]]; | |
| j++; | |
| idx++; | |
| goto outside; | |
| } | |
| j++; | |
| idx++; | |
| } | |
| // may handle img == img_width case here | |
| goto barrier; | |
| outside: | |
| while (j < WORD_THRESHOLD && idx < img_width) { | |
| if (histogram_rgba[idx] > PIXEL_THRESHOLD) { | |
| j++; | |
| idx++; | |
| goto inside; | |
| } | |
| j++; | |
| idx++; | |
| } | |
| barrier: | |
| [segments addObject:elements]; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment