forked from CE-Programming/CEmu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
qtframebuffer.cpp
73 lines (60 loc) · 2.29 KB
/
qtframebuffer.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/* Copyright (C) 2015 Fabian Vogt
* Modified for the CE calculator by CEmu developers
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
#include <QtGui/QPainter>
#include "qtframebuffer.h"
#include "qtkeypadbridge.h"
#include "core/asic.h"
#define CLAMP(a) ( ((a) > 255) ? 255 : (((a) < 0) ? 0 : (int)(a)) )
QImage brighten(QImage &img, float factor) {
/* Scale each RGB value by the brightening factor */
for(int y = 0; y < img.height(); y++) {
for(int x = 0; x < img.width(); x++) {
QRgb p = img.pixel(x, y);
int r = qRed(p) * factor;
r = CLAMP(r);
int g = qGreen(p) * factor;
g = CLAMP(g);
int b = qBlue(p) * factor;
b = CLAMP(b);
img.setPixel(x, y, qRgb(r, g, b));
}
}
return img;
}
QImage renderFramebuffer() {
uint16_t *framebuffer = reinterpret_cast<uint16_t*>(malloc(320 * 240 * 2));
uint32_t bitfields[] = { 0x01F, 0x000, 0x000};
lcd_drawframe(framebuffer, bitfields);
QImage::Format format = bitfields[0] == 0x00F ? QImage::Format_RGB444 : QImage::Format_RGB16;
QImage image(reinterpret_cast<const uchar*>(framebuffer), 320, 240, 320 * 2, format, free, framebuffer);
float factor = (310-(float)backlight.brightness)/160.0;
factor = (factor > 1) ? 1 : factor;
return brighten(image, factor);
}
void paintFramebuffer(QPainter *p) {
if (!(lcd.control & 0x800)) {
p->fillRect(p->window(), Qt::black);
p->setPen(Qt::white);
p->drawText(p->window(), Qt::AlignCenter, QObject::tr("LCD OFF"));
} else {
QImage img = renderFramebuffer();
p->drawImage(p->window(), img);
}
}
QMLFramebuffer::QMLFramebuffer(QQuickItem *p) : QQuickPaintedItem(p) {
installEventFilter(&qt_keypad_bridge);
}
void QMLFramebuffer::paint(QPainter *p) {
paintFramebuffer(p);
}