]> git.cworth.org Git - apitrace/blobdiff - gui/apisurface.cpp
ui: Handle float images in dumps.
[apitrace] / gui / apisurface.cpp
index bfc6cfdf128038d6e6b18a6afb057ebeb53e50e2..3732ed5f717482ad976fbf6f9f0d9e447113b91c 100644 (file)
@@ -1,9 +1,14 @@
 #include "apisurface.h"
 #include "thumbnail.h"
 
+#include <sstream>
+
 #include <QDebug>
 #include <QSysInfo>
 
+#include "image/image.hpp"
+
+
 ApiSurface::ApiSurface()
 {
 }
@@ -18,20 +23,94 @@ void ApiSurface::setSize(const QSize &size)
     m_size = size;
 }
 
-int ApiSurface::numChannels() const
+struct ByteArrayBuf : public std::streambuf
 {
-    return m_numChannels;
-}
-
-void ApiSurface::setNumChannels(int numChannels)
-{
-    m_numChannels = numChannels;
-}
+    ByteArrayBuf(QByteArray & a)
+    {
+        setg(a.data(), a.data(), a.data() + a.size());
+    }
+};
 
 void ApiSurface::contentsFromBase64(const QByteArray &base64)
 {
     QByteArray dataArray = QByteArray::fromBase64(base64);
-    m_image.loadFromData(dataArray, "png");
+
+    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());
+    }
+
+    /*
+     * FIXME: Instead of converting to QImage here, we should be deferring the conversion
+     * to imageviewer.cpp.
+     *
+     * XXX: We still need the thumbnail though.
+     */
+
+    Q_ASSERT(image);
+
+    int width = image->width;
+    int height = image->height;
+
+    m_image = QImage(width, height, QImage::Format_ARGB32);
+
+    const unsigned char *srcRow = image->start();
+    for (int y = 0; y < height; ++y) {
+        QRgb *dst = (QRgb *)m_image.scanLine(y);
+
+        if (image->channelType == image::TYPE_UNORM8) {
+            const unsigned char *src = srcRow;
+            for (int x = 0; x < width; ++x) {
+                unsigned char rgba[4];
+                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();
+    }
+
+    delete image;
+
     m_thumb = thumbnail(m_image);
 }