]> git.cworth.org Git - apitrace/blob - gui/apisurface.cpp
d6bc72cc6b4861e1f470f56e850ca643929e5311
[apitrace] / gui / apisurface.cpp
1 #include "apisurface.h"
2 #include "thumbnail.h"
3
4 #include <sstream>
5
6 #include <QDebug>
7 #include <QSysInfo>
8
9 #include "image/image.hpp"
10
11
12 ApiSurface::ApiSurface()
13 {
14 }
15
16 QSize ApiSurface::size() const
17 {
18     return m_size;
19 }
20
21 void ApiSurface::setSize(const QSize &size)
22 {
23     m_size = size;
24 }
25
26 struct ByteArrayBuf : public std::streambuf
27 {
28     ByteArrayBuf(QByteArray & a)
29     {
30         setg(a.data(), a.data(), a.data() + a.size());
31     }
32 };
33
34 void ApiSurface::contentsFromBase64(const QByteArray &base64)
35 {
36     QByteArray dataArray = QByteArray::fromBase64(base64);
37
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
107     m_thumb = thumbnail(m_image);
108 }
109
110 QImage ApiSurface::image() const
111 {
112     return m_image;
113 }
114
115 QImage ApiSurface::thumb() const
116 {
117     return m_thumb;
118 }
119
120 int ApiSurface::depth() const
121 {
122     return m_depth;
123 }
124
125 void ApiSurface::setDepth(int depth)
126 {
127     m_depth = depth;
128 }
129
130 QString ApiSurface::formatName() const
131 {
132     return m_formatName;
133 }
134
135 void ApiSurface::setFormatName(const QString &str)
136 {
137     m_formatName = str;
138 }
139
140
141 ApiTexture::ApiTexture()
142     : ApiSurface()
143 {
144 }
145
146 QString ApiTexture::label() const
147 {
148     return m_label;
149 }
150
151 void ApiTexture::setLabel(const QString &str)
152 {
153     m_label = str;
154 }
155
156 ApiFramebuffer::ApiFramebuffer()
157     : ApiSurface()
158 {
159 }
160
161 QString ApiFramebuffer::type() const
162 {
163     return m_type;
164 }
165
166 void ApiFramebuffer::setType(const QString &str)
167 {
168     m_type = str;
169 }
170