blob: d6bc72cc6b4861e1f470f56e850ca643929e5311 [file] [log] [blame]
Zack Rusin952e9d42011-04-09 23:37:21 -04001#include "apisurface.h"
José Fonseca3f456402012-03-25 20:59:24 +01002#include "thumbnail.h"
Zack Rusin952e9d42011-04-09 23:37:21 -04003
José Fonseca52398312013-09-11 18:42:07 +01004#include <sstream>
5
Zack Rusin952e9d42011-04-09 23:37:21 -04006#include <QDebug>
7#include <QSysInfo>
8
José Fonseca52398312013-09-11 18:42:07 +01009#include "image/image.hpp"
10
11
Zack Rusin952e9d42011-04-09 23:37:21 -040012ApiSurface::ApiSurface()
13{
14}
15
16QSize ApiSurface::size() const
17{
18 return m_size;
19}
20
21void ApiSurface::setSize(const QSize &size)
22{
23 m_size = size;
24}
25
José Fonseca52398312013-09-11 18:42:07 +010026struct ByteArrayBuf : public std::streambuf
27{
28 ByteArrayBuf(QByteArray & a)
29 {
30 setg(a.data(), a.data(), a.data() + a.size());
31 }
32};
33
Zack Rusin952e9d42011-04-09 23:37:21 -040034void ApiSurface::contentsFromBase64(const QByteArray &base64)
35{
36 QByteArray dataArray = QByteArray::fromBase64(base64);
José Fonseca52398312013-09-11 18:42:07 +010037
38 /*
39 * FIXME: Detect the float PFM images here.
40 */
41 ByteArrayBuf buf(dataArray);
42 std::istream istr(&buf);
43 image::Image *image = image::readPNG(istr);
44
45 /*
46 * FIXME: Instead of converting to QImage here, we should be deferring the conversion
47 * to imageviewer.cpp.
48 *
49 * XXX: We still need the thumbnail though.
50 */
51
52 Q_ASSERT(image);
53
54 int width = image->width;
55 int height = image->height;
56
57 m_image = QImage(width, height, QImage::Format_ARGB32);
58
59 const unsigned char *srcRow = image->start();
60 for (int y = 0; y < height; ++y) {
61 QRgb *dst = (QRgb *)m_image.scanLine(y);
62
63 if (image->channelType == image::TYPE_UNORM8) {
64 const unsigned char *src = srcRow;
65 for (int x = 0; x < width; ++x) {
66 unsigned char rgba[4];
67 for (int c = 0; c < image->channels; ++c) {
68 rgba[c] = *src++;
69 }
70 if (image->channels == 1) {
71 // Use gray-scale instead of red
72 rgba[1] = rgba[0];
73 rgba[2] = rgba[0];
74 }
75 dst[x] = qRgba(rgba[0], rgba[1], rgba[2], rgba[3]);
76 }
77 } else {
78 const float *src = (const float *)srcRow;
79 for (int x = 0; x < width; ++x) {
80 unsigned char rgba[4] = {0, 0, 0, 0xff};
81 for (int c = 0; c < image->channels; ++c) {
82 float f = *src++;
83 unsigned char u;
84 if (f >= 255.0f) {
85 u = 255;
86 } else if (f <= 0.0f) {
87 u = 0;
88 } else {
89 u = f * 255 + 0.5;
90 }
91 rgba[c] = u;
92 }
93 if (image->channels == 1) {
94 // Use gray-scale instead of red
95 rgba[1] = rgba[0];
96 rgba[2] = rgba[0];
97 }
98 dst[x] = qRgba(rgba[0], rgba[1], rgba[2], rgba[3]);
99 }
100 }
101
102 srcRow += image->stride();
103 }
104
105 delete image;
106
José Fonseca3f456402012-03-25 20:59:24 +0100107 m_thumb = thumbnail(m_image);
Zack Rusin952e9d42011-04-09 23:37:21 -0400108}
109
110QImage ApiSurface::image() const
111{
112 return m_image;
113}
114
115QImage ApiSurface::thumb() const
116{
117 return m_thumb;
118}
119
Zack Rusinb25c4b92011-11-16 22:43:34 -0500120int ApiSurface::depth() const
121{
122 return m_depth;
123}
124
125void ApiSurface::setDepth(int depth)
126{
127 m_depth = depth;
128}
129
Zack Rusine181b992011-11-17 16:00:41 -0500130QString ApiSurface::formatName() const
131{
132 return m_formatName;
133}
134
135void ApiSurface::setFormatName(const QString &str)
136{
137 m_formatName = str;
138}
139
140
Zack Rusin952e9d42011-04-09 23:37:21 -0400141ApiTexture::ApiTexture()
José Fonseca18081d52011-05-07 00:10:25 +0100142 : ApiSurface()
Zack Rusin952e9d42011-04-09 23:37:21 -0400143{
144}
145
José Fonseca18081d52011-05-07 00:10:25 +0100146QString ApiTexture::label() const
Zack Rusin952e9d42011-04-09 23:37:21 -0400147{
José Fonseca18081d52011-05-07 00:10:25 +0100148 return m_label;
Zack Rusin952e9d42011-04-09 23:37:21 -0400149}
150
José Fonseca18081d52011-05-07 00:10:25 +0100151void ApiTexture::setLabel(const QString &str)
Zack Rusin952e9d42011-04-09 23:37:21 -0400152{
José Fonseca18081d52011-05-07 00:10:25 +0100153 m_label = str;
Zack Rusin952e9d42011-04-09 23:37:21 -0400154}
Zack Rusina6846412011-04-10 19:51:44 -0400155
156ApiFramebuffer::ApiFramebuffer()
157 : ApiSurface()
158{
159}
160
161QString ApiFramebuffer::type() const
162{
163 return m_type;
164}
165
166void ApiFramebuffer::setType(const QString &str)
167{
168 m_type = str;
169}
Zack Rusinb25c4b92011-11-16 22:43:34 -0500170