discuss-gnustep
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Extracting from NSImage (NSImageView)


From: Andreas Hoeschler
Subject: Re: Extracting from NSImage (NSImageView)
Date: Fri, 4 Jun 2004 15:20:27 +0200

Hi Tima,

thanks a lot for the snippet.

Is there any other approach to accomplish - compositing images
and extracting a subregion - on GNUstep?

Yes. Here is an adapted piece of code from PhotoClip
(http://www.vaisburd.net/PhotoClip/index.html), development has been
interrupted, but I still hope to continue ;)

Cropping works if one draws NSImageRep instead of NSImage.


@implementation NSImage (PhotoClip)

- (NSImage *) imageFromRect: (NSRect) rect
{
    NSImage * canvas = [[NSImage alloc] initWithSize: rect.size];

    // we are going to draw self on canvas and return canvas
    [canvas lockFocus];

    // Prepare affine transformation to translate 'self'
    // so that rect.origin maps to (0,0)
    NSAffineTransform * xform = [NSAffineTransform transform];
    [xform translateXBy: -rect.origin.x yBy: -rect.origin.y];

    // Apply transform
    [xform concat];

    // Get NSImageRep of image
    NSImageRep * rep = [self bestRepresentationForDevice: nil];

    [rep drawAtPoint: NSZeroPoint];

    [canvas unlockFocus];
    return [canvas autorelease];
}

@end

I tried the following to composite an image of a number of tiles.

   NSImage *compositeImage = [[NSImage alloc] initWithSize:pixelSize];
   NSEnumerator *enumerator = [tiles objectEnumerator];

   [compositeImage lockFocus];
   while (tile = [enumerator nextObject])
     {
      NSRect tileRect = [tile visibleMapSection];
      NSImage *tileImage = [tile image];
      NSPoint dst;

dst.x = (tileRect.origin.x - unionRect.origin.x) / unionRect.size.width * pixelSize.width; dst.y = (tileRect.origin.y - unionRect.origin.y) / unionRect.size.height * pixelSize.height;

      NSAffineTransform * xform = [NSAffineTransform transform];
      [xform translateXBy: -dst.x yBy: -dst.y];
      // Apply transform
      [xform concat];

NSImageRep *tileRep = [tileImage bestRepresentationForDevice: nil];
      NSSize tileSize = [tileRep size];
      [tileRep setSize:tileSize];
      if ([tileRep drawAtPoint:NSZeroPoint])
        {
         NSLog(@"Rep successfully drawn on compositeImage!");
        }
      else
        {
         NSLog(@"rep could not be drawn!");
        }
     }
   [compositeImage unlockFocus];

   NSData *returnData = [compositeImage TIFFRepresentation];
   NSLog(@"representations %@", [compositeImage representations]);
   NSLog(@"returnData %@", returnData);
   return returnData;

The tileSize is 1024 * 0124. It says "Rep successfully drawn on compositeImage" and compositeImage has an NSCachedImageRepresentation, but TIFFRepresentation at the end returns nil, so no returnData is available. Any idea why and what to check next?

Regards,

   Andreas






reply via email to

[Prev in Thread] Current Thread [Next in Thread]