X-Git-Url: https://git.cworth.org/git?a=blobdiff_plain;f=gui%2Fapisurface.cpp;h=d6bc72cc6b4861e1f470f56e850ca643929e5311;hb=16bfd14f69f131ae53b1f3d1b5af87a54d95bc38;hp=64aa1532bc7083a89de2ea130489c3f7b146d8bb;hpb=18081d51fb0a0a5b1b7cb9fa2923339fae58de7b;p=apitrace diff --git a/gui/apisurface.cpp b/gui/apisurface.cpp index 64aa153..d6bc72c 100644 --- a/gui/apisurface.cpp +++ b/gui/apisurface.cpp @@ -1,8 +1,14 @@ #include "apisurface.h" +#include "thumbnail.h" + +#include #include #include +#include "image/image.hpp" + + ApiSurface::ApiSurface() { } @@ -17,22 +23,88 @@ void ApiSurface::setSize(const QSize &size) m_size = size; } -int ApiSurface::numChannels() const -{ - return m_numChannels; -} - -void ApiSurface::setNumChannels(int numChannels) +struct ByteArrayBuf : public std::streambuf { - 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"); - m_image = m_image.mirrored(); - m_thumb = m_image.scaled(64, 64, Qt::KeepAspectRatio); + + /* + * FIXME: Detect the float PFM images here. + */ + ByteArrayBuf buf(dataArray); + std::istream istr(&buf); + image::Image *image = image::readPNG(istr); + + /* + * 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 >= 255.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); } QImage ApiSurface::image() const @@ -45,6 +117,27 @@ QImage ApiSurface::thumb() const return m_thumb; } +int ApiSurface::depth() const +{ + return m_depth; +} + +void ApiSurface::setDepth(int depth) +{ + m_depth = depth; +} + +QString ApiSurface::formatName() const +{ + return m_formatName; +} + +void ApiSurface::setFormatName(const QString &str) +{ + m_formatName = str; +} + + ApiTexture::ApiTexture() : ApiSurface() { @@ -74,3 +167,4 @@ void ApiFramebuffer::setType(const QString &str) { m_type = str; } +