Zack Rusin | 952e9d4 | 2011-04-09 23:37:21 -0400 | [diff] [blame] | 1 | #include "apisurface.h" |
José Fonseca | 3f45640 | 2012-03-25 20:59:24 +0100 | [diff] [blame] | 2 | #include "thumbnail.h" |
Zack Rusin | 952e9d4 | 2011-04-09 23:37:21 -0400 | [diff] [blame] | 3 | |
José Fonseca | 5239831 | 2013-09-11 18:42:07 +0100 | [diff] [blame] | 4 | #include <sstream> |
| 5 | |
Zack Rusin | 952e9d4 | 2011-04-09 23:37:21 -0400 | [diff] [blame] | 6 | #include <QDebug> |
| 7 | #include <QSysInfo> |
| 8 | |
José Fonseca | 5239831 | 2013-09-11 18:42:07 +0100 | [diff] [blame] | 9 | #include "image/image.hpp" |
| 10 | |
| 11 | |
Zack Rusin | 952e9d4 | 2011-04-09 23:37:21 -0400 | [diff] [blame] | 12 | ApiSurface::ApiSurface() |
| 13 | { |
| 14 | } |
| 15 | |
| 16 | QSize ApiSurface::size() const |
| 17 | { |
| 18 | return m_size; |
| 19 | } |
| 20 | |
| 21 | void ApiSurface::setSize(const QSize &size) |
| 22 | { |
| 23 | m_size = size; |
| 24 | } |
| 25 | |
José Fonseca | 5239831 | 2013-09-11 18:42:07 +0100 | [diff] [blame] | 26 | struct ByteArrayBuf : public std::streambuf |
| 27 | { |
| 28 | ByteArrayBuf(QByteArray & a) |
| 29 | { |
| 30 | setg(a.data(), a.data(), a.data() + a.size()); |
| 31 | } |
| 32 | }; |
| 33 | |
Zack Rusin | 952e9d4 | 2011-04-09 23:37:21 -0400 | [diff] [blame] | 34 | void ApiSurface::contentsFromBase64(const QByteArray &base64) |
| 35 | { |
| 36 | QByteArray dataArray = QByteArray::fromBase64(base64); |
José Fonseca | 5239831 | 2013-09-11 18:42:07 +0100 | [diff] [blame] | 37 | |
| 38 | /* |
| 39 | * FIXME: Detect the float PFM images here. |
| 40 | */ |
| 41 | ByteArrayBuf buf(dataArray); |
| 42 | std::istream istr(&buf); |
| 43 | image::Image *image = image::readPNG(istr); |
| 44 | |
| 45 | /* |
| 46 | * FIXME: Instead of converting to QImage here, we should be deferring the conversion |
| 47 | * to imageviewer.cpp. |
| 48 | * |
| 49 | * XXX: We still need the thumbnail though. |
| 50 | */ |
| 51 | |
| 52 | Q_ASSERT(image); |
| 53 | |
| 54 | int width = image->width; |
| 55 | int height = image->height; |
| 56 | |
| 57 | m_image = QImage(width, height, QImage::Format_ARGB32); |
| 58 | |
| 59 | const unsigned char *srcRow = image->start(); |
| 60 | for (int y = 0; y < height; ++y) { |
| 61 | QRgb *dst = (QRgb *)m_image.scanLine(y); |
| 62 | |
| 63 | if (image->channelType == image::TYPE_UNORM8) { |
| 64 | const unsigned char *src = srcRow; |
| 65 | for (int x = 0; x < width; ++x) { |
| 66 | unsigned char rgba[4]; |
| 67 | for (int c = 0; c < image->channels; ++c) { |
| 68 | rgba[c] = *src++; |
| 69 | } |
| 70 | if (image->channels == 1) { |
| 71 | // Use gray-scale instead of red |
| 72 | rgba[1] = rgba[0]; |
| 73 | rgba[2] = rgba[0]; |
| 74 | } |
| 75 | dst[x] = qRgba(rgba[0], rgba[1], rgba[2], rgba[3]); |
| 76 | } |
| 77 | } else { |
| 78 | const float *src = (const float *)srcRow; |
| 79 | for (int x = 0; x < width; ++x) { |
| 80 | unsigned char rgba[4] = {0, 0, 0, 0xff}; |
| 81 | for (int c = 0; c < image->channels; ++c) { |
| 82 | float f = *src++; |
| 83 | unsigned char u; |
| 84 | if (f >= 255.0f) { |
| 85 | u = 255; |
| 86 | } else if (f <= 0.0f) { |
| 87 | u = 0; |
| 88 | } else { |
| 89 | u = f * 255 + 0.5; |
| 90 | } |
| 91 | rgba[c] = u; |
| 92 | } |
| 93 | if (image->channels == 1) { |
| 94 | // Use gray-scale instead of red |
| 95 | rgba[1] = rgba[0]; |
| 96 | rgba[2] = rgba[0]; |
| 97 | } |
| 98 | dst[x] = qRgba(rgba[0], rgba[1], rgba[2], rgba[3]); |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | srcRow += image->stride(); |
| 103 | } |
| 104 | |
| 105 | delete image; |
| 106 | |
José Fonseca | 3f45640 | 2012-03-25 20:59:24 +0100 | [diff] [blame] | 107 | m_thumb = thumbnail(m_image); |
Zack Rusin | 952e9d4 | 2011-04-09 23:37:21 -0400 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | QImage ApiSurface::image() const |
| 111 | { |
| 112 | return m_image; |
| 113 | } |
| 114 | |
| 115 | QImage ApiSurface::thumb() const |
| 116 | { |
| 117 | return m_thumb; |
| 118 | } |
| 119 | |
Zack Rusin | b25c4b9 | 2011-11-16 22:43:34 -0500 | [diff] [blame] | 120 | int ApiSurface::depth() const |
| 121 | { |
| 122 | return m_depth; |
| 123 | } |
| 124 | |
| 125 | void ApiSurface::setDepth(int depth) |
| 126 | { |
| 127 | m_depth = depth; |
| 128 | } |
| 129 | |
Zack Rusin | e181b99 | 2011-11-17 16:00:41 -0500 | [diff] [blame] | 130 | QString ApiSurface::formatName() const |
| 131 | { |
| 132 | return m_formatName; |
| 133 | } |
| 134 | |
| 135 | void ApiSurface::setFormatName(const QString &str) |
| 136 | { |
| 137 | m_formatName = str; |
| 138 | } |
| 139 | |
| 140 | |
Zack Rusin | 952e9d4 | 2011-04-09 23:37:21 -0400 | [diff] [blame] | 141 | ApiTexture::ApiTexture() |
José Fonseca | 18081d5 | 2011-05-07 00:10:25 +0100 | [diff] [blame] | 142 | : ApiSurface() |
Zack Rusin | 952e9d4 | 2011-04-09 23:37:21 -0400 | [diff] [blame] | 143 | { |
| 144 | } |
| 145 | |
José Fonseca | 18081d5 | 2011-05-07 00:10:25 +0100 | [diff] [blame] | 146 | QString ApiTexture::label() const |
Zack Rusin | 952e9d4 | 2011-04-09 23:37:21 -0400 | [diff] [blame] | 147 | { |
José Fonseca | 18081d5 | 2011-05-07 00:10:25 +0100 | [diff] [blame] | 148 | return m_label; |
Zack Rusin | 952e9d4 | 2011-04-09 23:37:21 -0400 | [diff] [blame] | 149 | } |
| 150 | |
José Fonseca | 18081d5 | 2011-05-07 00:10:25 +0100 | [diff] [blame] | 151 | void ApiTexture::setLabel(const QString &str) |
Zack Rusin | 952e9d4 | 2011-04-09 23:37:21 -0400 | [diff] [blame] | 152 | { |
José Fonseca | 18081d5 | 2011-05-07 00:10:25 +0100 | [diff] [blame] | 153 | m_label = str; |
Zack Rusin | 952e9d4 | 2011-04-09 23:37:21 -0400 | [diff] [blame] | 154 | } |
Zack Rusin | a684641 | 2011-04-10 19:51:44 -0400 | [diff] [blame] | 155 | |
| 156 | ApiFramebuffer::ApiFramebuffer() |
| 157 | : ApiSurface() |
| 158 | { |
| 159 | } |
| 160 | |
| 161 | QString ApiFramebuffer::type() const |
| 162 | { |
| 163 | return m_type; |
| 164 | } |
| 165 | |
| 166 | void ApiFramebuffer::setType(const QString &str) |
| 167 | { |
| 168 | m_type = str; |
| 169 | } |
Zack Rusin | b25c4b9 | 2011-11-16 22:43:34 -0500 | [diff] [blame] | 170 | |