blob: 5a9a654632c9f67a614e692765e619cf487e5ba8 [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
73void Surface::alloc()
74{
75 BITMAPINFOHEADER bih;
76
77 data = new RGBQUAD[width() * height()];
78
79 memset(&bih, 0, sizeof(bih));
80
81 bih.biSize = sizeof(BITMAPINFOHEADER);
82 bih.biBitCount = 32;
83 bih.biPlanes = 1;
84 bih.biWidth = width();
85 bih.biHeight = -height(); // Negative to get top-down
86 bih.biCompression = BI_RGB;
87
88 bitmap = CreateDIBSection(NULL, (BITMAPINFO*)&bih,
89 DIB_RGB_COLORS, (void**)&data, NULL, 0);
90 if (!bitmap)
91 throw rdr::SystemException("CreateDIBSection", GetLastError());
92}
93
94void Surface::dealloc()
95{
96 DeleteObject(bitmap);
97}
98
99void Surface::update(const Fl_RGB_Image* image)
100{
101 const unsigned char* in;
102 RGBQUAD* out;
103 int x, y;
104
105 assert(image->w() == width());
106 assert(image->h() == height());
107
108 // Convert data and pre-multiply alpha
109 in = (const unsigned char*)image->data()[0];
110 out = data;
111 for (y = 0;y < image->w();y++) {
112 for (x = 0;x < image->h();x++) {
113 switch (image->d()) {
114 case 1:
115 out->rgbBlue = in[0];
116 out->rgbGreen = in[0];
117 out->rgbRed = in[0];
118 out->rgbReserved = 0xff;
119 break;
120 case 2:
121 out->rgbBlue = (unsigned)in[0] * in[1] / 255;
122 out->rgbGreen = (unsigned)in[0] * in[1] / 255;
123 out->rgbRed = (unsigned)in[0] * in[1] / 255;
124 out->rgbReserved = in[1];
125 break;
126 case 3:
127 out->rgbBlue = in[2];
128 out->rgbGreen = in[1];
129 out->rgbRed = in[0];
130 out->rgbReserved = 0xff;
131 break;
132 case 4:
133 out->rgbBlue = (unsigned)in[2] * in[3] / 255;
134 out->rgbGreen = (unsigned)in[1] * in[3] / 255;
135 out->rgbRed = (unsigned)in[0] * in[3] / 255;
136 out->rgbReserved = in[3];
137 break;
138 }
139 in += image->d();
140 out++;
141 }
142 if (image->ld() != 0)
143 in += image->ld() - image->w() * image->d();
144 }
145}
146