blob: 5eea2d1d8d3b9f727873914e9e71726771716e22 [file] [log] [blame]
Pierre Ossman403ac272017-01-02 17:00:41 +01001/* Copyright 2016 Pierre Ossman 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 <FL/Fl_RGB_Image.H>
22#include <FL/x.H>
23
24#include <rdr/Exception.h>
25
26#include "Surface.h"
27
28void Surface::clear(unsigned char r, unsigned char g, unsigned char b, unsigned char a)
29{
30 RGBQUAD* out;
31 int x, y;
32
33 r = (unsigned)r * a / 255;
34 g = (unsigned)g * a / 255;
35 b = (unsigned)b * a / 255;
36
37 out = data;
38 for (y = 0;y < width();y++) {
39 for (x = 0;x < height();x++) {
40 out->rgbRed = r;
41 out->rgbGreen = g;
42 out->rgbBlue = b;
43 out->rgbReserved = a;
44 out++;
45 }
46 }
47}
48
49void Surface::draw(int src_x, int src_y, int x, int y, int w, int h)
50{
51 HDC dc;
52
53 dc = CreateCompatibleDC(fl_gc);
54 if (!dc)
55 throw rdr::SystemException("CreateCompatibleDC", GetLastError());
56
57 if (!SelectObject(dc, bitmap))
58 throw rdr::SystemException("SelectObject", GetLastError());
59
60 if (!BitBlt(fl_gc, x, y, w, h, dc, src_x, src_y, SRCCOPY)) {
61 // If the desktop we're rendering to is inactive (like when the screen
62 // is locked or the UAC is active), then GDI calls will randomly fail.
63 // This is completely undocumented so we have no idea how best to deal
64 // with it. For now, we've only seen this error and for this function
65 // so only ignore this combination.
66 if (GetLastError() != ERROR_INVALID_HANDLE)
67 throw rdr::SystemException("BitBlt", GetLastError());
68 }
69
70 DeleteDC(dc);
71}
72
Pierre Ossman3d74d882017-01-02 19:49:52 +010073void Surface::draw(Surface* dst, int src_x, int src_y, int x, int y, int w, int h)
74{
75 HDC origdc, dstdc;
76
77 dstdc = CreateCompatibleDC(NULL);
78 if (!dstdc)
79 throw rdr::SystemException("CreateCompatibleDC", GetLastError());
80
81 if (!SelectObject(dstdc, dst->bitmap))
82 throw rdr::SystemException("SelectObject", GetLastError());
83
84 origdc = fl_gc;
85 fl_gc = dstdc;
86 draw(src_x, src_y, x, y, w, h);
87 fl_gc = origdc;
88
89 DeleteDC(dstdc);
90}
91
Pierre Ossman403ac272017-01-02 17:00:41 +010092void Surface::alloc()
93{
94 BITMAPINFOHEADER bih;
95
96 data = new RGBQUAD[width() * height()];
97
98 memset(&bih, 0, sizeof(bih));
99
100 bih.biSize = sizeof(BITMAPINFOHEADER);
101 bih.biBitCount = 32;
102 bih.biPlanes = 1;
103 bih.biWidth = width();
104 bih.biHeight = -height(); // Negative to get top-down
105 bih.biCompression = BI_RGB;
106
107 bitmap = CreateDIBSection(NULL, (BITMAPINFO*)&bih,
108 DIB_RGB_COLORS, (void**)&data, NULL, 0);
109 if (!bitmap)
110 throw rdr::SystemException("CreateDIBSection", GetLastError());
111}
112
113void Surface::dealloc()
114{
115 DeleteObject(bitmap);
116}
117
118void Surface::update(const Fl_RGB_Image* image)
119{
120 const unsigned char* in;
121 RGBQUAD* out;
122 int x, y;
123
124 assert(image->w() == width());
125 assert(image->h() == height());
126
127 // Convert data and pre-multiply alpha
128 in = (const unsigned char*)image->data()[0];
129 out = data;
130 for (y = 0;y < image->w();y++) {
131 for (x = 0;x < image->h();x++) {
132 switch (image->d()) {
133 case 1:
134 out->rgbBlue = in[0];
135 out->rgbGreen = in[0];
136 out->rgbRed = in[0];
137 out->rgbReserved = 0xff;
138 break;
139 case 2:
140 out->rgbBlue = (unsigned)in[0] * in[1] / 255;
141 out->rgbGreen = (unsigned)in[0] * in[1] / 255;
142 out->rgbRed = (unsigned)in[0] * in[1] / 255;
143 out->rgbReserved = in[1];
144 break;
145 case 3:
146 out->rgbBlue = in[2];
147 out->rgbGreen = in[1];
148 out->rgbRed = in[0];
149 out->rgbReserved = 0xff;
150 break;
151 case 4:
152 out->rgbBlue = (unsigned)in[2] * in[3] / 255;
153 out->rgbGreen = (unsigned)in[1] * in[3] / 255;
154 out->rgbRed = (unsigned)in[0] * in[3] / 255;
155 out->rgbReserved = in[3];
156 break;
157 }
158 in += image->d();
159 out++;
160 }
161 if (image->ld() != 0)
162 in += image->ld() - image->w() * image->d();
163 }
164}
165