blob: 77b435dd63a4ad564d05c5f0c556d55fd9670772 [file] [log] [blame]
Pierre Ossmanc18753c2011-06-17 07:35:56 +00001/* Copyright 2011 Pierre Ossman <ossman@cendio.se> for Cendio AB
2 *
3 * This is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This software is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this software; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
16 * USA.
17 */
18
Peter Åstrandc359f362011-08-23 12:04:46 +000019#ifdef HAVE_CONFIG_H
20#include <config.h>
21#endif
22
Pierre Ossmanc18753c2011-06-17 07:35:56 +000023#include <assert.h>
24
25#include <ApplicationServices/ApplicationServices.h>
26
27#include <FL/Fl_Window.H>
28#include <FL/x.H>
29
30#include <rfb/LogWriter.h>
31#include <rfb/Exception.h>
32
33#include "OSXPixelBuffer.h"
34
35using namespace rfb;
36
37static rfb::LogWriter vlog("PlatformPixelBuffer");
38
39PlatformPixelBuffer::PlatformPixelBuffer(int width, int height) :
40 ManagedPixelBuffer(rfb::PixelFormat(32, 24, false, true,
41 255, 255, 255, 16, 8, 0),
42 width, height),
43 image(NULL)
44{
45 CGColorSpaceRef lut;
46 CGDataProviderRef provider;
47
48 lut = CGColorSpaceCreateDeviceRGB();
49 assert(lut);
50 provider = CGDataProviderCreateWithData(NULL, data, datasize, NULL);
51 assert(provider);
52
53 image = CGImageCreate(width, height, 8, 32, width*4, lut,
54 kCGImageAlphaNoneSkipFirst | kCGBitmapByteOrder32Little,
55 provider, NULL, false, kCGRenderingIntentDefault);
56 assert(image);
57
58 CGDataProviderRelease(provider);
59 CGColorSpaceRelease(lut);
60}
61
62
63PlatformPixelBuffer::~PlatformPixelBuffer()
64{
65 CGImageRelease((CGImageRef)image);
66}
67
68
69void PlatformPixelBuffer::draw(int src_x, int src_y, int x, int y, int w, int h)
70{
71 CGRect rect;
72 CGContextRef gc;
73 CGAffineTransform at;
74
75 gc = (CGContextRef)fl_gc;
76
77 CGContextSaveGState(gc);
78
79 // We have to use clipping to partially display an image
80
81 rect.origin.x = x - 0.5;
82 rect.origin.y = y - 0.5;
83 rect.size.width = w;
84 rect.size.height = h;
85
86 CGContextClipToRect(gc, rect);
87
88 // Oh the hackiness that is OS X image handling...
89 // The CGContextDrawImage() routine magically flips the images and offsets
90 // them by 0.5,0.5 in order to compensate for OS X unfamiliar coordinate
91 // system. But this breaks horribly when you set up the CTM to get the
92 // more familiar top-down system (which FLTK does), meaning we have to
93 // reset the CTM back to the identity matrix and calculate new origin
94 // coordinates.
95
96 at = CGContextGetCTM(gc);
97 CGContextScaleCTM(gc, 1, -1);
98 CGContextTranslateCTM(gc, -at.tx, -at.ty);
99
100 rect.origin.x = x - src_x;
101 rect.origin.y = Fl_Window::current()->h() - (y - src_y);
102 rect.size.width = width();
103 rect.size.height = -height(); // Negative height does _not_ flip the image
104
105 CGContextDrawImage(gc, rect, (CGImageRef)image);
106
107 CGContextRestoreGState(gc);
108}