C1: deny drops into black holes
[openc2e.git] / qtgui / imagepreview.cpp
blob06d4fe59308078415be0aeb3ef605ba546aea52e
1 /*
2 This program is free software; you can redistribute it and/or modify
3 it under the terms of the GNU General Public License as published by
4 the Free Software Foundation; either version 2 of the License, or
5 (at your option) any later version.
7 This program is distributed in the hope that it will be useful,
8 but WITHOUT ANY WARRANTY; without even the implied warranty of
9 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 GNU General Public License for more details.
12 You should have received a copy of the GNU General Public License along
13 with this program; if not, write to the Free Software Foundation, Inc.,
14 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17 #include "imagepreview.h"
18 #include "c1cobfile.h"
20 #include "Engine.h"
21 #include "cobFile.h"
23 #include <QPainter>
25 ImagePreview::ImagePreview(QWidget *parent): QWidget(parent) {
26 imgdata = 0;
29 ImagePreview::~ImagePreview() {
30 if (imgdata) delete imgdata;
33 void ImagePreview::onSelect(QListWidgetItem *current, QListWidgetItem *prev) {
34 previewimg = QImage();
36 if (!current) {
37 update();
38 return;
41 if (engine.version == 1) {
42 QString filename = current->toolTip();
43 std::ifstream cobstream(filename.toAscii(), std::ios::binary);
44 if (cobstream.fail()) {
45 return; // TODO: throw some kind of warning or something
48 c1cobfile cobfile(cobstream);
50 if (cobfile.imagewidth > 0 && cobfile.imageheight > 0) {
51 previewimg = QImage((uchar*)cobfile.imagedata.get(), cobfile.imagewidth, cobfile.imageheight, QImage::Format_Indexed8).mirrored();
52 previewimg.setNumColors(256);
53 unsigned char *palette = engine.getPalette();
54 if (palette) {
55 for (unsigned int i = 0; i < 256; i++) {
56 previewimg.setColor(i, QColor(palette[i*3],palette[i*3+1],palette[i*3+2]).rgb());
60 } else if (engine.version == 2) {
61 cobAgentBlock *b = (cobAgentBlock *)current->data(Qt::UserRole).value<void *>();
62 assert(b);
63 if (b->thumbnailwidth > 0 && b->thumbnailheight > 0) {
64 unsigned short *oldimgdata = b->thumbnail;
65 assert(b->thumbnail);
66 if (imgdata) delete imgdata;
67 imgdata = new unsigned int[b->thumbnailwidth * b->thumbnailheight];
69 for (unsigned int i = 0; i < (unsigned int)b->thumbnailwidth * (unsigned int)b->thumbnailheight; i++) {
70 unsigned int v = oldimgdata[i];
71 v = swapEndianShort(v);
72 unsigned int red = ((v & 0xf800) >> 8) & 0xFF;
73 unsigned int green = ((v & 0x07e0) >> 3) & 0xFF;
74 unsigned int blue = ((v & 0x001f) << 3) & 0xFF;
75 imgdata[i] = (red << 16) + (green << 8) + blue;
78 previewimg = QImage((uchar*)imgdata, b->thumbnailwidth, b->thumbnailheight, QImage::Format_RGB32);
82 update();
85 void ImagePreview::paintEvent(QPaintEvent *event) {
86 if (previewimg.width() > 0) {
87 QPainter painter(this);
88 painter.drawImage(QPoint(width()/2 - previewimg.width()/2,height()/2 - previewimg.height()/2), previewimg);