Subversion Repositories gelsvn

Rev

Rev 657 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 657 Rev 659
Line 62... Line 62...
62
        [self setNeedsDisplay: YES];
62
        [self setNeedsDisplay: YES];
63
}
63
}
64
 
64
 
65
-(IBAction)save_window_to_pasteboard:(id)sender
65
-(IBAction)save_window_to_pasteboard:(id)sender
66
{
66
{
67
    
67
    // Get the pasteboard.
68
    NSPasteboard *pb = [NSPasteboard generalPasteboard];
68
    NSPasteboard *pb = [NSPasteboard generalPasteboard];
69
    
69
    
70
    // Telling the pasteboard what type of data we're going to send in
70
    // Telling the pasteboard what type of data we're going to send in
71
    [pb declareTypes:[NSArray arrayWithObjects:NSPasteboardTypePNG,nil] owner:self];
71
    [pb declareTypes:[NSArray arrayWithObjects:NSPasteboardTypePNG,nil] owner:self];
72
    
72
    
-
 
73
    // Get the size of the image in a retina safe way
73
    NSRect backRect = [self convertRectToBacking: [self bounds]];
74
    NSRect backRect = [self convertRectToBacking: [self bounds]];
74
    NSSize sz;
-
 
75
    sz.width = NSWidth(backRect);
75
    int W = NSWidth(backRect);
76
    sz.height = NSHeight(backRect);
76
    int H = NSHeight(backRect);
77
    
77
 
-
 
78
    // Create image. Note no alpha channel. I don't copy that.
78
    NSBitmapImageRep *rep = [[NSBitmapImageRep alloc] initWithBitmapDataPlanes: NULL
79
    NSBitmapImageRep *rep = [[NSBitmapImageRep alloc] initWithBitmapDataPlanes: NULL
79
                                                                    pixelsWide: sz.width
80
                                                                    pixelsWide: W
80
                                                                    pixelsHigh: sz.height
81
                                                                    pixelsHigh: H
81
                                                                 bitsPerSample: 8
82
                                                                 bitsPerSample: 8
82
                                                               samplesPerPixel: 3
83
                                                               samplesPerPixel: 3
83
                                                                      hasAlpha: NO
84
                                                                      hasAlpha: NO
84
                                                                      isPlanar: NO
85
                                                                      isPlanar: NO
85
                                                                colorSpaceName: NSCalibratedRGBColorSpace
86
                                                                colorSpaceName: NSCalibratedRGBColorSpace
86
                                                                   bytesPerRow: 0				// indicates no empty bytes at row end
87
                                                                   bytesPerRow: 3*W
87
                                                                  bitsPerPixel: 0];
88
                                                                  bitsPerPixel: 0];
88
    
89
    
-
 
90
    // The following block does the actual reading of the image
-
 
91
    glPushAttrib(GL_PIXEL_MODE_BIT); // Save state about reading buffers
89
    glReadBuffer(GL_FRONT);
92
    glReadBuffer(GL_FRONT);
90
    int bytesPerRow = [rep bytesPerRow];
-
 
91
	glPixelStorei(GL_PACK_ROW_LENGTH, 8*bytesPerRow/[rep bitsPerPixel]);
93
    glPixelStorei(GL_PACK_ALIGNMENT, 1); // Dense packing
92
    glReadPixels(0, 0, NSWidth(backRect), NSHeight(backRect), GL_RGB, GL_UNSIGNED_BYTE,  [rep bitmapData]);
94
    glReadPixels(0, 0, W, H, GL_RGB, GL_UNSIGNED_BYTE, [rep bitmapData]);
-
 
95
    glPopAttrib();
93
    
96
    
-
 
97
    // So we need one more image, since we must flip its orientation.
94
    NSBitmapImageRep* flipped =  [[NSBitmapImageRep alloc] initWithBitmapDataPlanes: NULL
98
    NSBitmapImageRep* flipped =  [[NSBitmapImageRep alloc] initWithBitmapDataPlanes: NULL
95
                                                                         pixelsWide: sz.width
99
                                                                         pixelsWide: W
96
                                                                         pixelsHigh: sz.height
100
                                                                         pixelsHigh: H
97
                                                                      bitsPerSample: 8
101
                                                                      bitsPerSample: 8
98
                                                                    samplesPerPixel: 3
102
                                                                    samplesPerPixel: 3
99
                                                                           hasAlpha: NO
103
                                                                           hasAlpha: NO
100
                                                                           isPlanar: NO
104
                                                                           isPlanar: NO
101
                                                                     colorSpaceName: NSCalibratedRGBColorSpace
105
                                                                     colorSpaceName: NSCalibratedRGBColorSpace
102
                                                                        bytesPerRow: 0				// indicates no empty bytes at row end
106
                                                                        bytesPerRow: 3*W
103
                                                                       bitsPerPixel: 0];
107
                                                                       bitsPerPixel: 0];
104
    
108
    
-
 
109
    // Primitive double for loop flipping the row order. Should be a better way. Can't find it.
105
    for(int j=1; j< sz.height+1; ++j)
110
    for(int j=1; j< H+1; ++j)
106
        for(int i=0;i<sz.width;++i)
111
        for(int i=0;i<W;++i)
107
        {
112
        {
108
            NSUInteger pixels[4];
113
            NSUInteger pixels[4];
109
            [rep getPixel: pixels atX:i y:j];
114
            [rep getPixel: pixels atX:i y:j];
110
            [flipped setPixel: pixels atX:i y:sz.height-j];
115
            [flipped setPixel: pixels atX:i y:H-j];
111
        }
116
        }
112
    
117
    
113
    // Converting the representation to PNG and sending it to the pasteboard (with type indicated)
118
    // Converting the representation to PNG and sending it to the pasteboard (with type indicated)
114
    [pb setData:[flipped representationUsingType:NSPNGFileType properties:nil] forType:NSPasteboardTypePNG];
119
    [pb setData:[flipped representationUsingType:NSPNGFileType properties:nil] forType:NSPasteboardTypePNG];
115
    
-
 
116
    
-
 
117
    
-
 
118
}
120
}
119
 
121
 
120
 
122
 
121
 
123
 
122
-(void)keyDown:(NSEvent *)theEvent
124
-(void)keyDown:(NSEvent *)theEvent