NSImage lock/unlock slow

  • Hi

    I am trying to get an idea of what is fast efficient and so on.

    In my case the origin is a NSBitmapImage that I want cutout a portion
    from and scale that portion into a new NSBitmapImage.

    for (int i = 0; i < 10000; i++) {
    // Create a NSImage from NSBitmapImageRep
    NSImage *sourceImage = [NSImage alloc];
    [sourceImage addRepresentation:image];
    [sourceImage setFlipped:true];

    // Create a new NSImage
          NSRect targetRect = NSMakeRect(0, 0, 20, 20);
    NSImage *targetImage = [[NSImage alloc] initWithSize: targetRect.size];
    [targetImage setFlipped:true];

    [targetImage lockFocus];
    // Draw a part from source into target and stretch/shrink
    [sourceImage drawInRect:targetRect fromRect:NSMakeRect(371, 179, 10,
    10) operation:NSCompositeCopy fraction:1.0];

    // Create a NSBitmapImageRep from locked NSImage
    NSBitmapImageRep *targetRep = [[NSBitmapImageRep alloc]
    initWithFocusedViewRect: NSMakeRect(0, 0, 20, 20)];
    [targetImage unlockFocus];
    [targetImage release];
    [sourceImage release];
    }

    When running this code over and over again. approx 50% of the time is
    spent in lock/unlock and the other 50% in the drawInRect command.

    I am a bit demotivated from the fact that the focus command is so
    expensive.
    To my knowledge there is no way to get around this?????

    THX
  • > To my knowledge there is no way to get around this?????

    Aside from other work you aren't interested in, this is making an
    extra copy of the image data.  -lockFocus is going to end up drawing
    your original bitmap into a buffer used to hold your new drawing, and
    -[NSBitmapImageRep  initWithFocusedViewRect:] is going to create a
    third buffer.

    Since you're after specifically a bitmap, it's better to draw directly
    to a bitmap.  Here's a related snippet from the 10.4 AppKit release
    notes:

        NSGraphicsContext *bitmapGraphicsContext = [NSGraphicsContext
    graphicsContextWithBitmapImageRep:cacheBitmapImageRep];
        [NSGraphicsContext saveGraphicsState];
        [NSGraphicsContext setCurrentContext:bitmapGraphicsContext];
        [[NSColor clearColor] set];
        NSRectFill(NSMakeRect(0, 0, [cacheBitmapImageRep size].width,
    [cacheBitmapImageRep size].height));
        [NSGraphicsContext restoreGraphicsState];

    Also, be careful!  You never init'd your source image object.  It's a
    little bit surprising if an un-init'd image works on 10.5 at all, but
    there's no guarantee it will in future OSes.

    -Ken

    On Dec 1, 2007 5:16 PM,  <s5804...> wrote:
    > Hi
    >
    > I am trying to get an idea of what is fast efficient and so on.
    >
    > In my case the origin is a NSBitmapImage that I want cutout a portion
    > from and scale that portion into a new NSBitmapImage.
    >
    >
    > for (int i = 0; i < 10000; i++) {
    > // Create a NSImage from NSBitmapImageRep
    > NSImage *sourceImage = [NSImage alloc];
    > [sourceImage addRepresentation:image];
    > [sourceImage setFlipped:true];
    >
    > // Create a new NSImage
    > NSRect targetRect = NSMakeRect(0, 0, 20, 20);
    > NSImage *targetImage = [[NSImage alloc] initWithSize: targetRect.size];
    > [targetImage setFlipped:true];
    >
    > [targetImage lockFocus];
    > // Draw a part from source into target and stretch/shrink
    > [sourceImage drawInRect:targetRect fromRect:NSMakeRect(371, 179, 10,
    > 10) operation:NSCompositeCopy fraction:1.0];
    >
    > // Create a NSBitmapImageRep from locked NSImage
    > NSBitmapImageRep *targetRep = [[NSBitmapImageRep alloc]
    > initWithFocusedViewRect: NSMakeRect(0, 0, 20, 20)];
    > [targetImage unlockFocus];
    > [targetImage release];
    > [sourceImage release];
    > }
    >
    > When running this code over and over again. approx 50% of the time is
    > spent in lock/unlock and the other 50% in the drawInRect command.
    >
    > I am a bit demotivated from the fact that the focus command is so
    > expensive.
    > To my knowledge there is no way to get around this?????
    >
    > THX
    >
previous month december 2007 next month
MTWTFSS
          1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31            
Go to today