Many changes! Snapshots now working properly. Programmatic environment interface...
[RExecServer.git] / RCGImageDevice.m
blob9c888935eeb9e93e71a04b40171cf3a7072e3d3f
1 #import "RCGImageDevice.h"
3 @implementation RCGImageDevice
4 + (void)load {
5         [RDevice registerDevice:self forDisplay:@"jpeg"];
6         [RDevice registerDevice:self forDisplay:@"png"];
7         [RDevice registerDevice:self forDisplay:@"tiff"];
8         [RDevice registerDevice:self forDisplay:@""];
11 - (void)didResize {
12         //Dispose of old context if needed
13         if(context != NULL && bitmap != NULL) {
14                 CGContextRelease(context);
15                 free(bitmap);
16                 bitmapSize = 0;
17         }
18         
19         NSSize size = [self size];
20         size_t width    = 1*size.width;
21         size_t height   = 1*size.height;
22         size_t rowBytes = (width*8*4+7)/8;
23         
24         bitmapSize = rowBytes*height;
25         bitmap = malloc(bitmapSize);
26         memset(bitmap,0,bitmapSize);
27         context = CGBitmapContextCreate(bitmap,width,height,8,rowBytes,CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB),kCGImageAlphaPremultipliedLast);
28         CGContextTranslateCTM(context,0,1*size.height);
29         CGContextScaleCTM(context,1.0,-1.0);
30         CGContextSetShouldAntialias(context,RDevice_GetAntialias(desc) ? true : false);
31         RDevice_ResetGDepth(desc);
34 - (void)finishOpening {
35         RDevice_SetScale(desc,1.0);
36         [self didResize];
39 - (void)drawInRect:(NSRect)rect {
40         if(NULL != context) {
41                 CGImageRef image = CGBitmapContextCreateImage(context);
42                 CGContextDrawImage([[NSGraphicsContext currentContext] graphicsPort],CGRectMake(rect.origin.x,rect.origin.y,rect.size.width,rect.size.height),image);
43                 CGImageRelease(image);
44         }
47 - (CGContextRef)context {
48         return context;
51 - (BOOL)canDrawInView { return YES; }
52 - (BOOL)canResize { return YES; }
54 - (void)close {
55         if(nil != [self display] && nil != [self target]) {
56                 NSData *data = [self deviceRepresentationOfType:[self display]];
57                 if(nil != data) {
58                         [data retain];
59                         [data writeToURL:[NSURL URLWithString:[self target]] atomically:YES];
60                         [data release];
61                 }
62         }
63         [super close];
66 - (NSData*)deviceRepresentationOfType:(NSString*)aType {
67         if((nil == aType || [aType isEqual:@""])) {
68                 NSString *aDisplay = [self display];
69                 //If we don't have a display just return the raw bitmap
70                 if(nil == aDisplay || [aDisplay isEqual:@""]) 
71                         return [[[NSData alloc] initWithBytes:bitmap length:bitmapSize] autorelease];
72                 else aType = aDisplay;
73         }
74         if([aType isEqual:@"tiff"] || [aType isEqual:NSTIFFPboardType]) {
75         }
76         return nil;
79 //For easier access
80 - (NSData*)deviceRepresentation { return [self deviceRepresentationOfType:nil]; }
83 @end