X-Git-Url: https://git.cworth.org/git?a=blobdiff_plain;f=gui%2Fapisurface.cpp;h=a872c13b6b2270a27eb68685a289b4827d77057c;hb=d36c6ee24ec7d642b5bcf39f3b6a265467c8b0f3;hp=d6bc72cc6b4861e1f470f56e850ca643929e5311;hpb=52398312caaba3e1bc0613016d5bf3507d1e242a;p=apitrace diff --git a/gui/apisurface.cpp b/gui/apisurface.cpp index d6bc72c..a872c13 100644 --- a/gui/apisurface.cpp +++ b/gui/apisurface.cpp @@ -33,83 +33,21 @@ struct ByteArrayBuf : public std::streambuf void ApiSurface::contentsFromBase64(const QByteArray &base64) { - QByteArray dataArray = QByteArray::fromBase64(base64); - - /* - * FIXME: Detect the float PFM images here. - */ - ByteArrayBuf buf(dataArray); - std::istream istr(&buf); - image::Image *image = image::readPNG(istr); + m_base64Data = base64; /* - * FIXME: Instead of converting to QImage here, we should be deferring the conversion - * to imageviewer.cpp. - * - * XXX: We still need the thumbnail though. + * We need to do the conversion to create the thumbnail */ - + image::Image *image = imageFromBase64(base64); 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(); - } - + QImage img = qimageFromRawImage(image); + m_thumb = thumbnail(img); delete image; - - m_thumb = thumbnail(m_image); } -QImage ApiSurface::image() const +QByteArray ApiSurface::base64Data() const { - return m_image; + return m_base64Data; } QImage ApiSurface::thumb() const @@ -168,3 +106,82 @@ 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; +}