]> git.cworth.org Git - apitrace/blobdiff - gui/apisurface.cpp
gui: make sure that the alpha channel is initialized
[apitrace] / gui / apisurface.cpp
index 8c985ae282a0b2321484705c8c054bdfa40ef5f6..a872c13b6b2270a27eb68685a289b4827d77057c 100644 (file)
@@ -1,8 +1,14 @@
 #include "apisurface.h"
+#include "thumbnail.h"
+
+#include <sstream>
 
 #include <QDebug>
 #include <QSysInfo>
 
+#include "image/image.hpp"
+
+
 ApiSurface::ApiSurface()
 {
 }
@@ -17,60 +23,31 @@ void ApiSurface::setSize(const QSize &size)
     m_size = size;
 }
 
-static inline int
-rgba8_to_argb(quint8 r, quint8 g, quint8 b, quint8 a)
-{
-    return (a << 24 | r << 16 | g << 8 | b);
-}
-
-static inline int
-rgbaf2argb(float r, float g, float b, float a)
+struct ByteArrayBuf : public std::streambuf
 {
-    quint8 rb = r * 255;
-    quint8 gb = g * 255;
-    quint8 bb = b * 255;
-    quint8 ab = a * 255;
-
-    return (ab << 24 | rb << 16 | gb << 8 | bb);
-}
+    ByteArrayBuf(QByteArray & a)
+    {
+        setg(a.data(), a.data(), a.data() + a.size());
+    }
+};
 
 void ApiSurface::contentsFromBase64(const QByteArray &base64)
 {
-    QByteArray dataArray = QByteArray::fromBase64(base64);
-    const quint8 *data = (const quint8*)dataArray.data();
-    int width = m_size.width();
-    int height = m_size.height();
+    m_base64Data = base64;
 
-    if (width <= 0 || height <= 0)
-        return;
-
-    int *pixelData = (int*)malloc(sizeof(int) * width * height);
-
-    //XXX not sure if this will work when
-    //    QSysInfo::ByteOrder == QSysInfo::BigEndian
-
-    for (int y = 0; y < height; ++y) {
-        for (int x = 0; x < width; ++x) {
-            int pixel = rgba8_to_argb(data[(y * width + x) * 4 + 0],
-                                      data[(y * width + x) * 4 + 1],
-                                      data[(y * width + x) * 4 + 2],
-                                      data[(y * width + x) * 4 + 3]);
-            pixelData[y * width + x] = pixel;
-        }
-    }
-
-    m_image = QImage((uchar*)pixelData,
-                     width, height,
-                     QImage::Format_ARGB32).mirrored();
-    m_thumb = m_image.scaled(64, 64, Qt::KeepAspectRatio);
-    //m_image.save("testoutput.png");
-
-    free(pixelData);
+    /*
+     * We need to do the conversion to create the thumbnail
+     */
+    image::Image *image = imageFromBase64(base64);
+    Q_ASSERT(image);
+    QImage img = qimageFromRawImage(image);
+    m_thumb = thumbnail(img);
+    delete image;
 }
 
-QImage ApiSurface::image() const
+QByteArray ApiSurface::base64Data() const
 {
-    return m_image;
+    return m_base64Data;
 }
 
 QImage ApiSurface::thumb() const
@@ -78,41 +55,40 @@ QImage ApiSurface::thumb() const
     return m_thumb;
 }
 
-ApiTexture::ApiTexture()
-    : ApiSurface(),
-      m_unit(0),
-      m_level(0)
+int ApiSurface::depth() const
 {
+    return m_depth;
 }
 
-int ApiTexture::unit() const
+void ApiSurface::setDepth(int depth)
 {
-    return m_unit;
+    m_depth = depth;
 }
 
-void ApiTexture::setUnit(int un)
+QString ApiSurface::formatName() const
 {
-    m_unit = un;
+    return m_formatName;
 }
 
-QString ApiTexture::target() const
+void ApiSurface::setFormatName(const QString &str)
 {
-    return m_target;
+    m_formatName = str;
 }
 
-void ApiTexture::setTarget(const QString &str)
+
+ApiTexture::ApiTexture()
+    : ApiSurface()
 {
-    m_target = str;
 }
 
-int ApiTexture::level() const
+QString ApiTexture::label() const
 {
-    return m_level;
+    return m_label;
 }
 
-void ApiTexture::setLevel(int l)
+void ApiTexture::setLabel(const QString &str)
 {
-    m_level = l;
+    m_label = str;
 }
 
 ApiFramebuffer::ApiFramebuffer()
@@ -129,3 +105,83 @@ void ApiFramebuffer::setType(const QString &str)
 {
     m_type = str;
 }
+
+image::Image *
+ApiSurface::imageFromBase64(const QByteArray &base64)
+{
+    QByteArray dataArray = QByteArray::fromBase64(base64);
+    image::Image *image;
+
+    /*
+     * Detect the PNG vs PFM images.
+     */
+    const char pngSignature[] = {(char)0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A, 0};
+    if (dataArray.startsWith(pngSignature)) {
+        ByteArrayBuf buf(dataArray);
+        std::istream istr(&buf);
+        image = image::readPNG(istr);
+    } else {
+        image = image::readPNM(dataArray.data(), dataArray.size());
+    }
+
+    return image;
+}
+
+
+QImage
+ApiSurface::qimageFromRawImage(const image::Image *image)
+{
+    QImage img;
+    int width = image->width;
+    int height = image->height;
+
+    img = QImage(width, height, QImage::Format_ARGB32);
+
+    const unsigned char *srcRow = image->start();
+    for (int y = 0; y < height; ++y) {
+        QRgb *dst = (QRgb *)img.scanLine(y);
+
+        if (image->channelType == image::TYPE_UNORM8) {
+            const unsigned char *src = srcRow;
+            for (int x = 0; x < width; ++x) {
+                unsigned char rgba[4] = {0, 0, 0, 0xff};
+                for (int c = 0; c < image->channels; ++c) {
+                    rgba[c] = *src++;
+                }
+                if (image->channels == 1) {
+                    // Use gray-scale instead of red
+                    rgba[1] = rgba[0];
+                    rgba[2] = rgba[0];
+                }
+                dst[x] = qRgba(rgba[0], rgba[1], rgba[2], rgba[3]);
+            }
+        } else {
+            const float *src = (const float *)srcRow;
+            for (int x = 0; x < width; ++x) {
+                unsigned char rgba[4] = {0, 0, 0, 0xff};
+                for (int c = 0; c < image->channels; ++c) {
+                    float f = *src++;
+                    unsigned char u;
+                    if (f >= 1.0f) {
+                        u = 255;
+                    } else if (f <= 0.0f) {
+                        u = 0;
+                    } else {
+                        u = f * 255 + 0.5;
+                    }
+                    rgba[c] = u;
+                }
+                if (image->channels == 1) {
+                    // Use gray-scale instead of red
+                    rgba[1] = rgba[0];
+                    rgba[2] = rgba[0];
+                }
+                dst[x] = qRgba(rgba[0], rgba[1], rgba[2], rgba[3]);
+            }
+        }
+
+        srcRow += image->stride();
+    }
+
+    return img;
+}