discuss-gnustep
[Top][All Lists]
Advanced

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

Cropping rectangle from an image


From: Vaisburd, Haim
Subject: Cropping rectangle from an image
Date: Fri, 22 Jul 2005 17:52:44 -0700

Hi everybody,

After a long while I'm ready to return to this topic.
I would like to crop a reclangle from an existing image
and save it to a file.

This is the code I use, it works only partially.

@implementation NSImage (PhotoClip) // category

- (NSImage *) imageFromRect: (NSRect) rect
                   scaledBy: (float) scale
                  rotatedBy: (float) degrees
{
    NSAffineTransform * xform = [NSAffineTransform transform];

    // translate reference frame to map rectangle 'rect' into first
quadrant
    [xform translateXBy: -rect.origin.x
                    yBy: -rect.origin.y];

    // in real life the affine transformation is more interesting
    // and includes scaling and rotation, but right now it's not
    // important
    
    NSImage * canvas = [[NSImage alloc] initWithSize: rect.size];
    [canvas lockFocus];

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

    [rep drawAtPoint: NSZeroPoint];
    
    [canvas unlockFocus];
    return AUTORELEASE(canvas);
}

@end

This method gives me the image I want so I can display in in the view
[NSImage -compositeToPoint: operation:].
However, I can't write it to the file, because [NSImage
-TIFFRepresentation]
on the newly created image returns 0.

I've learned how to crop the rectangle by direct bit manupulation with
the NSBitmapRep data, but this was rather complex (scaling) with too
many
coding schemes (planes or meshed, different number of bits per sample
etc).

I believe this functionality is provided by AppKit. Is gnustep
implementation
just lacks that piece or do I miss some ways to do that?

Thank you,
Tima.




reply via email to

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