blob: b5882d7abbdfa498a4b9b19d83df1764964860cc [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
19#include <assert.h>
20
21#include <ApplicationServices/ApplicationServices.h>
22
23#include <FL/Fl_Window.H>
24#include <FL/x.H>
25
26#include <rfb/LogWriter.h>
27#include <rfb/Exception.h>
28
29#include "OSXPixelBuffer.h"
30
31using namespace rfb;
32
33static rfb::LogWriter vlog("PlatformPixelBuffer");
34
35PlatformPixelBuffer::PlatformPixelBuffer(int width, int height) :
36 ManagedPixelBuffer(rfb::PixelFormat(32, 24, false, true,
37 255, 255, 255, 16, 8, 0),
38 width, height),
39 image(NULL)
40{
41 CGColorSpaceRef lut;
42 CGDataProviderRef provider;
43
44 lut = CGColorSpaceCreateDeviceRGB();
45 assert(lut);
46 provider = CGDataProviderCreateWithData(NULL, data, datasize, NULL);
47 assert(provider);
48
49 image = CGImageCreate(width, height, 8, 32, width*4, lut,
50 kCGImageAlphaNoneSkipFirst | kCGBitmapByteOrder32Little,
51 provider, NULL, false, kCGRenderingIntentDefault);
52 assert(image);
53
54 CGDataProviderRelease(provider);
55 CGColorSpaceRelease(lut);
56}
57
58
59PlatformPixelBuffer::~PlatformPixelBuffer()
60{
61 CGImageRelease((CGImageRef)image);
62}
63
64
65void PlatformPixelBuffer::draw(int src_x, int src_y, int x, int y, int w, int h)
66{
67 CGRect rect;
68 CGContextRef gc;
69 CGAffineTransform at;
70
71 gc = (CGContextRef)fl_gc;
72
73 CGContextSaveGState(gc);
74
75 // We have to use clipping to partially display an image
76
77 rect.origin.x = x - 0.5;
78 rect.origin.y = y - 0.5;
79 rect.size.width = w;
80 rect.size.height = h;
81
82 CGContextClipToRect(gc, rect);
83
84 // Oh the hackiness that is OS X image handling...
85 // The CGContextDrawImage() routine magically flips the images and offsets
86 // them by 0.5,0.5 in order to compensate for OS X unfamiliar coordinate
87 // system. But this breaks horribly when you set up the CTM to get the
88 // more familiar top-down system (which FLTK does), meaning we have to
89 // reset the CTM back to the identity matrix and calculate new origin
90 // coordinates.
91
92 at = CGContextGetCTM(gc);
93 CGContextScaleCTM(gc, 1, -1);
94 CGContextTranslateCTM(gc, -at.tx, -at.ty);
95
96 rect.origin.x = x - src_x;
97 rect.origin.y = Fl_Window::current()->h() - (y - src_y);
98 rect.size.width = width();
99 rect.size.height = -height(); // Negative height does _not_ flip the image
100
101 CGContextDrawImage(gc, rect, (CGImageRef)image);
102
103 CGContextRestoreGState(gc);
104}