[Top][All Lists]

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

Scaling and rotating a porttion of image

From: Tima Vaisburd
Subject: Scaling and rotating a porttion of image
Date: Sat, 13 Sep 2003 23:11:17 -0700
User-agent: KMail/1.4.3

Hello everybody,

I'm trying to clip a fragment out of a larger image, and then scale and rotate
it on the screen. I got problems with both backends, I'm mostly interested in 
making it work with art backend now.

I thought I'd understood how to make clips and scalings separately:

@implementation NSImage (PhotoClip)

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

    [self compositeToPoint: NSZeroPoint
          fromRect: rect
          operation: NSCompositeCopy];

    [canvas unlockFocus];
    return [canvas autorelease];

- (NSImage *) imageTransformedBy: (NSAffineTransform *) transform
    NSSize new_size = [transform transformSize: [self size]];
    NSImage * canvas = [[NSImage alloc] initWithSize: new_size];
    [canvas lockFocus];

    [transform concat];

    NSImageRep * rep = [self bestRepresentationForDevice: nil];
    [rep drawAtPoint: NSZeroPoint];
    [canvas unlockFocus];
    return [canvas autorelease];

@end // NSImage (PhotoClip)

But if I combine them clipping first, and then scaling, affine transformation 
is not applied:

- (NSImage *) imageFromRect: (NSRect) rect
              transformedBy: (NSAffineTransform *) transform
        NSImage * fragment = [self imageFromRect: rect];
        return [fragment imageTransformedBy: transform];

This was confusing: I thought the 'fragment' was an independent bitmap
as good as the original one. What really happens? How can I make such
a bitmap?

The other way  around, i.e. first scale, and then clip, works:

    NSImage * transformed = [self imageTransformedBy: xform];

    // transform the rect

    return [transformed imageFromRect: transformed_rect];

In this case, though, I sometimes get distorted image or even complete
garbage at certain values of parameters when image is clipped
and scale is between 0.5 and 1.0.

I suspect this is a bug in back-art since xlib never produced garbage
in the similar situations (but I need art since xlib  does not support
rotations and transparency).

Can anybody help?

Thank you,

reply via email to

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