Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 12-16-2010, 02:32 AM   #1 (permalink)
Registered Member
 
Join Date: Dec 2010
Posts: 1
KolNedra is on a distinguished road
Post Image distortion

I've been googling and looking on other threads, but I can't seem to find a solid solution.

I want to accomplish the following:


So I have this UIImage(View) and I want to distort it according to 4 points (topleft, topright, bottomright, bottomleft)

I came across a Flash AS3 equivalant which I try to port to an objective-c one:
Code:
/*!
 * Use like: [imageView setAffineTransform:compute_transform_matrix(320, 480, 200, 200, 500, 220, 600, 800, 200, 680)];

- (CGAffineTransform) compute_transform_matrix:(float)W
                                     H:(float)H 
                                   tlx:(float)tlx
                                   tly:(float)tly
                                   trx:(float)trx 
                                   trY:(float)trY
                                   brx:(float)brx 
                                   bry:(float)bry
                                   blx:(float)blx
                                   bly:(float)bly {

    NSMutableArray *_p = [[NSMutableArray alloc] init];
    NSMutableArray *_tri = [[NSMutableArray alloc] init];
    float ix;
    float iy;
    float _xMin = 0;
    float _yMin = 0;
    float _hsLen = W / (SEGMENTS + 1);
    float _vsLen = H / (SEGMENTS + 1);
    
    float x;
    float y;
    for (ix = 0; ix < SEGMENTS + 2; ix++) {
        for (iy = 0; iy < SEGMENTS + 2; iy++) {
            x = ix * _hsLen;
            y = iy * _vsLen;
            [_p addObject:[NSMutableDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithFloat:x], @"x", [NSNumber numberWithFloat:y], @"y",[NSNumber numberWithFloat:x], @"sx", [NSNumber numberWithFloat:y], @"sy", nil]];
        }
    }
    
    for (ix = 0; ix < SEGMENTS + 1; ix++) {
        for (iy = 0; iy < SEGMENTS + 1; iy++) {
            [_tri addObject:[NSMutableArray arrayWithObjects:[_p objectAtIndex:(iy + ix * (SEGMENTS + 2))], [_p objectAtIndex:(iy + ix * (SEGMENTS + 2) + 1)], [_p objectAtIndex:(iy + (ix + 1) * (SEGMENTS + 2))], nil]];
            [_tri addObject:[NSMutableArray arrayWithObjects:[_p objectAtIndex:(iy + (ix + 1) * (SEGMENTS + 2) + 1)], [_p objectAtIndex:(iy + (ix + 1) * (SEGMENTS + 2))], [_p objectAtIndex:(iy + ix * (SEGMENTS + 2) + 1)], nil]];
        }
    }
    
    float dx30 = blx - tlx;
    float dy30 = bly - tly;
    float dx21 = brx - trx;
    float dy21 = bry - trY;
    int l = [_p count];
    NSMutableDictionary *point;
    float _x;
    float _y;
    float gx;
    float gy;
    float bx;
    float by;
    float _sx;
    float _sy;
    while(--l > -1) {
        point = [_p objectAtIndex:l];
        _x = [[point objectForKey:@"x"] floatValue];
        _y = [[point objectForKey:@"y"] floatValue];
        gx = (_x - _xMin) / W;
        gy = (_y - _yMin) / H;
        bx = (tlx + gy * (dx30));
        by = (tly + gy * (dy30));
        _sx = bx + gx * ((trx + gy * (dx21)) - bx);
        _sy = by + gx * ((trY + gy * (dy21)) - by);
        [point setObject:[NSNumber numberWithFloat:_sx] forKey:@"sx"];
        [point setObject:[NSNumber numberWithFloat:_sy] forKey:@"sy"];
        [_p replaceObjectAtIndex:l withObject:point];        
    }
    
    l = [_tri count];
    NSMutableDictionary *p0;
    NSMutableDictionary *p1;
    NSMutableDictionary *p2;
    
    float x0;
    float y0;
    float x1;
    float y1;
    float x2;
    float y2;
    float u0;
    float v0;
    float u1;
    float v1;
    float u2;
    float v2;
    CGAffineTransform _tMat = CGAffineTransformIdentity;
    CGAffineTransform _sMat = CGAffineTransformIdentity;
    NSMutableArray *a;    
    while (--l > -1) {
        a = [_tri objectAtIndex:l];
        p0 = [a objectAtIndex:0];
        p1 = [a objectAtIndex:1];
        p2 = [a objectAtIndex:2];
        x0 = [[p0 objectForKey:@"sx"] floatValue];
        y0 = [[p0 objectForKey:@"sy"] floatValue];
        x1 = [[p1 objectForKey:@"sx"] floatValue];
        y1 = [[p1 objectForKey:@"sy"] floatValue];
        x2 = [[p2 objectForKey:@"sx"] floatValue];
        y2 = [[p2 objectForKey:@"sy"] floatValue];
        u0 = [[p0 objectForKey:@"x"] floatValue];
        v0 = [[p0 objectForKey:@"y"] floatValue];
        u1 = [[p1 objectForKey:@"x"] floatValue];
        v1 = [[p1 objectForKey:@"y"] floatValue];
        u2 = [[p2 objectForKey:@"x"] floatValue];
        v2 = [[p2 objectForKey:@"y"] floatValue];
        
        _tMat.tx = u0;
        _tMat.ty = v0;
        _tMat.a = (u1 - u0) / W;
        _tMat.b = (v1 - v0) / W;
        _tMat.c = (u2 - u0) / H;
        _tMat.d = (v2 - v0) / H;
        
        _sMat.a = (x1 - x0) / W;
        _sMat.b = (y1 - y0) / W;
        _sMat.c = (x2 - x0) / H;
        _sMat.d = (y2 - y0) / H;
        _sMat.tx = x0;
        _sMat.ty = y0;        
        CGAffineTransformInvert(_tMat);
        CGAffineTransformConcat(_tMat, _sMat);
    }
    
    return _tMat;
}
But nothing exciting happens.
I came across a thread on stackoverflow with the same 'problem', only the solution didn't do it for me.

Anyone who can give me a pointer or even maybe a working example?

Last edited by KolNedra; 12-16-2010 at 06:22 AM.
KolNedra is offline   Reply With Quote
Reply

Bookmarks

Tags
distort, image, transform

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 396
9 members and 387 guests
chemistry, daudrizek, HemiMG, jeroenkeij, Kirkout, PavelMik, whitey99
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,665
Threads: 94,120
Posts: 402,898
Top Poster: BrianSlick (7,990)
Welcome to our newest member, daudrizek
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 02:59 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0