Skip to content

Instantly share code, notes, and snippets.

@ku
Created July 1, 2009 12:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ku/138764 to your computer and use it in GitHub Desktop.
Save ku/138764 to your computer and use it in GitHub Desktop.
teiten hacks in objc
+(NSData*)dumpCGImage:(CGImageRef)cgImage offset:(CGPoint)offset imageSize:(CGSize)imageSize {
CGDataProviderRef dataProvider = CGImageGetDataProvider(cgImage);
CFDataRef data = CGDataProviderCopyData(dataProvider);
// int length = CFDataGetLength(data);
const UInt8* buffer = CFDataGetBytePtr(data);
int width = imageSize.width;
// int height = imageSize.height;
char* bf = (char*)malloc((161*121*(3+6)) + 256);
char* p = bf;
int n = sprintf(bf, "colors=");
p += n;
// teiten expects x,y order and
// UIImage bitmap is serialized y,x order.
for ( int x = 0; x < 160; x++ ) {
for ( int y = 0; y < 121; y++ ) {
int t = (y + offset.y) * width + x + offset.x;
const ARGB* pixel = (const ARGB*)(buffer + t * sizeof(ARGB));
n = sprintf(p, "%02x%02x%02x%%2C",
pixel->r,
pixel->g,
pixel->b
);
p += n;
}
}
p-= 3; // length of "%2C"
sprintf(p, "&h=120&w=160");
NSString* d = [[[NSString alloc] initWithUTF8String:bf] autorelease];
id body = [d dataUsingEncoding:NSUTF8StringEncoding];
free(bf);
return body;
}
void post_to_teiten(UIImage* fullsizeimage) {
CGPoint offset;
float scale;
int x, y;
if ( isPortrait() ) {
scale = 0.5;
offset = CGPointMake(0, 60);
x = 320 * scale;
y = 480 * scale;
} else {
scale = 0.4;
offset = CGPointMake(16, 4);
x = 480 * scale;
y = 320 * scale;
}
if ( 1 ) {
UIGraphicsBeginImageContext(CGSizeMake(x, y));
[fullsizeimage drawInRect:CGRectMake(0, 0, x, y)];
UIImage* img = UIGraphicsGetImageFromCurrentImageContext();
NSData* body = [NTLNAppDelegate dumpCGImage:img.CGImage offset:offset imageSize:CGSizeMake(x,y)];
UIGraphicsEndImageContext();
// NSURL *u = [NSURL URLWithString:@"http://ido.nu/kuma/t.php"];
NSURL *u = [NSURL URLWithString:@"http://teiten.org/snapshot.php"];
NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:u];
[request setHTTPBody:body];
[request setHTTPMethod:@"POST"];
NSURLResponse* response;
NSError* error;
[NSURLConnection sendSynchronousRequest:request
returningResponse:&response error:&error];
}
}
-(id)loginAccount:(NSString*)account password:(NSString*)password {
NSURL* url = [NSURL URLWithString:@"http://teiten.org/signin"];
NSMutableURLRequest* req = [NSMutableURLRequest requestWithURL:url];
[req setHTTPMethod:@"POST"];
NSString* body = [NSString stringWithFormat:@"account=%@&password=%@&mode=submit", account, password];
[req setHTTPBody:[body dataUsingEncoding:NSUTF8StringEncoding]];
NSHTTPURLResponse* response;
NSError * error;
id data = [NSURLConnection sendSynchronousRequest:req returningResponse:&response error:&error];
if ( [response statusCode] == 200 ) {
return nil;
} else {
return data;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment