discuss-gnustep
[Top][All Lists]
Advanced

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

Obscure rotation problem in base-1.14/gui-0.12


From: Vaisburd, Haim
Subject: Obscure rotation problem in base-1.14/gui-0.12
Date: Tue, 5 Jun 2007 23:19:03 -0700

Hi,

I installed newest packages at last, after that tested PhotoClip application.
Apparently, the rotation is broken in the latest libraries (I tested only art 
backend). I attach the application that rotate an image +90 degrees (that is,
counterclockwise) and displays it.

The program is complete, but you'd need a testing.jpg file.

It workes in gui-0.11 but displays image incorrectly in gui-0.12.

I noticed that [NSAffineTransform -transformPoint:] transforms coordinates as 
expected (and identical in the old and new version), so the problem is
somewhere else.

Thanks in advence for your help.

--Tima

-------------- begin ---------------------

#include <Foundation/Foundation.h>
#include <AppKit/AppKit.h>

// Method -imageRotatedBy90 that is being tested

@interface NSImage (MyTest)
- (NSImage *) imageRotatedBy90;
@end

@implementation NSImage (MyTest)

- (NSImage *) imageRotatedBy90
{
    NSSize canvas_size;
    NSSize sz = [self size];
    canvas_size.width  = [self size].height;
    canvas_size.height = [self size].width;

    NSAffineTransform * xform = [NSAffineTransform transform];

    [xform rotateByDegrees: 90];
    [xform translateXBy: 0 yBy: -sz.height];

    // equivalent transformation
    //[xform translateXBy: sz.height yBy: 0];
    //[xform rotateByDegrees: 90];

    NSPoint pt[4];
    pt[0] = NSMakePoint( 1, 1 );
    pt[1] = NSMakePoint( sz.width - 1, 1 );
    pt[2] = NSMakePoint( sz.width - 1, sz.height -1 );
    pt[3] = NSMakePoint( 1, sz.height - 1 );
    int i;
    for ( i=0; i < 4; ++i )
        NSLog( @"%@ -> %@",
               NSStringFromPoint(pt[i]),
               NSStringFromPoint([xform transformPoint: pt[i]]) );

    // Create canvas to draw upon
    NSImage * canvas = [[NSImage alloc] initWithSize: canvas_size];
    [canvas lockFocus];

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

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

@end


// Call this method in [NSView -drawRect]

@interface MyView : NSView
- (void) drawRect: (NSRect) rect;
@end

@implementation MyView

- (void) drawRect: (NSRect) rect
{
    NSString * fileName = @"testimg.jpg";

    NSImage * image = [[NSImage alloc] initWithContentsOfFile: fileName];
    if ( image == nil )
    {
        NSLog(@"can't load file '%@'\n", fileName);
        return;
    }

    NSImage * rotatedImage = [image imageRotatedBy90];

    NSPoint origin = NSMakePoint(20, 20);
    [rotatedImage compositeToPoint: origin operation: NSCompositeCopy];

    RELEASE(image);
}

@end

// --------------- All the rest is supporting frame ----------------------

@interface MyDelegate : NSObject
{
    NSWindow * my_window;
}

- (void) dealloc;
- (void) createWindow;
- (void) applicationWillFinishLaunching: (NSNotification *)not;
- (void) applicationDidFinishLaunching: (NSNotification *)not;
- (BOOL) applicationShouldTerminateAfterLastWindowClosed: (id)sender;
@end


@implementation MyDelegate : NSObject 

- (void) dealloc
{
    RELEASE(my_window);
    [super dealloc];
}

- (void) createWindow
{
    // Determine good window size.
    NSSize screenSize = [[NSScreen mainScreen] frame].size;
    NSRect rect = NSMakeRect(0,0,
                             screenSize.width * 4./5.,
                             screenSize.height * 4./5.);

    unsigned int styleMask = NSTitledWindowMask
        | NSClosableWindowMask
        | NSMiniaturizableWindowMask
        | NSResizableWindowMask;


    my_window = [[NSWindow alloc] initWithContentRect: rect
                                  styleMask: styleMask
                                  backing: NSBackingStoreRetained
                                  defer: YES];
    [my_window setTitle: @"Test imageRotatedBy90"];
    [my_window setContentView: [[MyView alloc] initWithFrame: rect]];
}


- (void) applicationWillFinishLaunching: (NSNotification *)not
{
    [self createWindow];
}

- (void) applicationDidFinishLaunching: (NSNotification *)not
{
    [my_window makeKeyAndOrderFront: nil];

}

- (BOOL) applicationShouldTerminateAfterLastWindowClosed: (id)sender
{
    return YES;
}

@end

int main (int argc, const char **argv)
{
    [NSApplication sharedApplication];
    [NSApp setDelegate: [MyDelegate new]];

    return NSApplicationMain (argc, argv);
}

-----------  end ----------------





reply via email to

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