try to make build portable: remove SDL_mixer dependency, remove -f from cp command...
[openc2e.git] / qtgui / imagepreview.cpp
blob3c5c3e96f9d5baeddb11db1e26a102b7dc531c0c
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 (engine.version == 1) {
37 QString filename = current->toolTip();
38 std::ifstream cobstream(filename.toAscii(), std::ios::binary);
39 if (cobstream.fail()) {
40 return; // TODO: throw some kind of warning or something
43 c1cobfile cobfile(cobstream);
45 if (cobfile.imagewidth > 0 && cobfile.imageheight > 0) {
46 previewimg = QImage((uchar*)cobfile.imagedata.get(), cobfile.imagewidth, cobfile.imageheight, QImage::Format_Indexed8).mirrored();
47 previewimg.setNumColors(256);
48 unsigned char *palette = engine.getPalette();
49 if (palette) {
50 for (unsigned int i = 0; i < 256; i++) {
51 previewimg.setColor(i, QColor(palette[i*3],palette[i*3+1],palette[i*3+2]).rgb());
55 } else if (engine.version == 2) {
56 cobAgentBlock *b = (cobAgentBlock *)current->data(Qt::UserRole).value<void *>();
57 assert(b);
58 if (b->thumbnailwidth > 0 && b->thumbnailheight > 0) {
59 unsigned short *oldimgdata = b->thumbnail;
60 assert(b->thumbnail);
61 if (imgdata) delete imgdata;
62 imgdata = new unsigned int[b->thumbnailwidth * b->thumbnailheight];
64 for (unsigned int i = 0; i < b->thumbnailwidth * b->thumbnailheight; i++) {
65 unsigned int v = oldimgdata[i];
66 unsigned int red = ((v & 0xf800) >> 8) & 0xFF;
67 unsigned int green = ((v & 0x07e0) >> 3) & 0xFF;
68 unsigned int blue = ((v & 0x001f) << 3) & 0xFF;
69 imgdata[i] = (red << 16) + (green << 8) + blue;
72 previewimg = QImage((uchar*)imgdata, b->thumbnailwidth, b->thumbnailheight, QImage::Format_RGB32);
76 update();
79 void ImagePreview::paintEvent(QPaintEvent *event) {
80 if (previewimg.width() > 0) {
81 QPainter painter(this);
82 painter.drawImage(QPoint(width()/2 - previewimg.width()/2,height()/2 - previewimg.height()/2), previewimg);