1 #include "apisurface.h"
9 #include "image/image.hpp"
12 ApiSurface::ApiSurface()
16 QSize ApiSurface::size() const
21 void ApiSurface::setSize(const QSize &size)
26 struct ByteArrayBuf : public std::streambuf
28 ByteArrayBuf(QByteArray & a)
30 setg(a.data(), a.data(), a.data() + a.size());
34 void ApiSurface::contentsFromBase64(const QByteArray &base64)
36 m_base64Data = base64;
39 * We need to do the conversion to create the thumbnail
41 image::Image *image = imageFromBase64(base64);
43 QImage img = qimageFromRawImage(image);
44 m_thumb = thumbnail(img);
48 QByteArray ApiSurface::base64Data() const
53 QImage ApiSurface::thumb() const
58 int ApiSurface::depth() const
63 void ApiSurface::setDepth(int depth)
68 QString ApiSurface::formatName() const
73 void ApiSurface::setFormatName(const QString &str)
79 ApiTexture::ApiTexture()
84 QString ApiTexture::label() const
89 void ApiTexture::setLabel(const QString &str)
94 ApiFramebuffer::ApiFramebuffer()
99 QString ApiFramebuffer::type() const
104 void ApiFramebuffer::setType(const QString &str)
110 ApiSurface::imageFromBase64(const QByteArray &base64)
112 QByteArray dataArray = QByteArray::fromBase64(base64);
116 * Detect the PNG vs PFM images.
118 const char pngSignature[] = {(char)0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A, 0};
119 if (dataArray.startsWith(pngSignature)) {
120 ByteArrayBuf buf(dataArray);
121 std::istream istr(&buf);
122 image = image::readPNG(istr);
124 image = image::readPNM(dataArray.data(), dataArray.size());
132 ApiSurface::qimageFromRawImage(const image::Image *image)
135 int width = image->width;
136 int height = image->height;
138 img = QImage(width, height, QImage::Format_ARGB32);
140 const unsigned char *srcRow = image->start();
141 for (int y = 0; y < height; ++y) {
142 QRgb *dst = (QRgb *)img.scanLine(y);
144 if (image->channelType == image::TYPE_UNORM8) {
145 const unsigned char *src = srcRow;
146 for (int x = 0; x < width; ++x) {
147 unsigned char rgba[4];
148 for (int c = 0; c < image->channels; ++c) {
151 if (image->channels == 1) {
152 // Use gray-scale instead of red
156 dst[x] = qRgba(rgba[0], rgba[1], rgba[2], rgba[3]);
159 const float *src = (const float *)srcRow;
160 for (int x = 0; x < width; ++x) {
161 unsigned char rgba[4] = {0, 0, 0, 0xff};
162 for (int c = 0; c < image->channels; ++c) {
167 } else if (f <= 0.0f) {
174 if (image->channels == 1) {
175 // Use gray-scale instead of red
179 dst[x] = qRgba(rgba[0], rgba[1], rgba[2], rgba[3]);
183 srcRow += image->stride();