Note that there are some explanatory texts on larger screens.

plurals
  1. POiPhone SDK - Optimize a for loop
    primarykey
    data
    text
    <p>I'm developing an image processing application and I'm looking for an advise to tune my code.</p> <p>My need is to split the image into blocs (80x80), and for each blocs, calculate the average color.</p> <p>My first method contains the main loops where the second method is called : </p> <pre><code>- (NSArray*)getRGBAsFromImage:(UIImage *)image { int width = image.size.width; int height = image.size.height; int blocPerRow = 80; int blocPerCol = 80; int pixelPerRowBloc = width / blocPerRow; int pixelPerColBloc = height / blocPerCol; int xx,yy; // Row loop for (int i=0; i&lt;blocPerRow; i++) { xx = (i * pixelPerRowBloc) + 1; // Colon loop for (int j=0; j&lt;blocPerCol; j++) { yy = (j * pixelPerColBloc) +1; [self getRGBAsFromImageBloc:image atX:xx andY:yy withPixelPerRow:pixelPerRowBloc AndPixelPerCol:pixelPerColBloc]; } } // return my NSArray not done yet ! } </code></pre> <p>My second method browses the pixel bloc and returns a ColorStruct : </p> <pre><code>- (ColorStruct*)getRGBAsFromImageBloc:(UIImage*)image atX:(int)xx andY:(int)yy withPixelPerRow:(int)pixelPerRow AndPixelPerCol:(int)pixelPerCol { // First get the image into your data buffer CGImageRef imageRef = [image CGImage]; NSUInteger width = CGImageGetWidth(imageRef); NSUInteger height = CGImageGetHeight(imageRef); CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); unsigned char *rawData = malloc(height * width * 4); NSUInteger bytesPerPixel = 4; NSUInteger bytesPerRow = bytesPerPixel * width; NSUInteger bitsPerComponent = 8; CGContextRef context = CGBitmapContextCreate(rawData, width, height, bitsPerComponent, bytesPerRow, colorSpace, kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big); CGColorSpaceRelease(colorSpace); CGContextDrawImage(context, CGRectMake(0, 0, width, height), imageRef); CGContextRelease(context); // Now your rawData contains the image data in the RGBA8888 pixel format. int byteIndex = (bytesPerRow * yy) + xx * bytesPerPixel; int red = 0; int green = 0; int blue = 0; int alpha = 0; int currentAlpha; // bloc loop for (int i = 0 ; i &lt; (pixelPerRow*pixelPerCol) ; ++i) { currentAlpha = rawData[byteIndex + 3]; red += (rawData[byteIndex] ) * currentAlpha; green += (rawData[byteIndex + 1]) * currentAlpha; blue += (rawData[byteIndex + 2]) * currentAlpha; alpha += currentAlpha; byteIndex += 4; if ( i == pixelPerRow ) { byteIndex += (width-pixelPerRow) * 4; } } red /= alpha; green /= alpha; blue /= alpha; ColorStruct *bColorStruct = newColorStruct(red, blue, green); free(rawData); return bColorStruct; } </code></pre> <p>ColorStruct :</p> <pre><code>typedef struct { int red; int blue; int green; } ColorStruct; </code></pre> <p>with constructor : </p> <pre><code>ColorStruct *newColorStruct(int red, int blue, int green) { ColorStruct *ret = malloc(sizeof(ColorStruct)); ret-&gt;red = red; ret-&gt;blue = blue; ret-&gt;green = green; return ret; } </code></pre> <p>As you can see, I have three level of loop : the row loop, the colon loop, and the bloc loop.</p> <p>I have tested my code and it takes about 5 to 6 seconds for an 320x480 pictures.</p> <p>Any help is welcomed.</p> <p>Thanks, Bahaaldine</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload