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> |
Martin Schulze | b44232b | 2016-04-16 19:06:16 +0200 | [diff] [blame] | 5 | #include <memory> |
José Fonseca | 5239831 | 2013-09-11 18:42:07 +0100 | [diff] [blame] | 6 | |
Zack Rusin | 952e9d4 | 2011-04-09 23:37:21 -0400 | [diff] [blame] | 7 | #include <QDebug> |
| 8 | #include <QSysInfo> |
| 9 | |
José Fonseca | 5239831 | 2013-09-11 18:42:07 +0100 | [diff] [blame] | 10 | #include "image/image.hpp" |
| 11 | |
| 12 | |
Zack Rusin | 952e9d4 | 2011-04-09 23:37:21 -0400 | [diff] [blame] | 13 | ApiSurface::ApiSurface() |
| 14 | { |
| 15 | } |
| 16 | |
| 17 | QSize ApiSurface::size() const |
| 18 | { |
| 19 | return m_size; |
| 20 | } |
| 21 | |
| 22 | void ApiSurface::setSize(const QSize &size) |
| 23 | { |
| 24 | m_size = size; |
| 25 | } |
| 26 | |
José Fonseca | 5239831 | 2013-09-11 18:42:07 +0100 | [diff] [blame] | 27 | struct ByteArrayBuf : public std::streambuf |
| 28 | { |
Jose Fonseca | c296a3e | 2015-05-01 17:43:54 +0100 | [diff] [blame] | 29 | ByteArrayBuf(const QByteArray & a) |
José Fonseca | 5239831 | 2013-09-11 18:42:07 +0100 | [diff] [blame] | 30 | { |
Jose Fonseca | c296a3e | 2015-05-01 17:43:54 +0100 | [diff] [blame] | 31 | setg((char *)a.data(), (char *)a.data(), (char *)a.data() + a.size()); |
José Fonseca | 5239831 | 2013-09-11 18:42:07 +0100 | [diff] [blame] | 32 | } |
| 33 | }; |
| 34 | |
Martin Schulze | b44232b | 2016-04-16 19:06:16 +0200 | [diff] [blame] | 35 | QImage ApiSurface::calculateThumbnail(bool opaque, bool alpha) const |
Zack Rusin | 952e9d4 | 2011-04-09 23:37:21 -0400 | [diff] [blame] | 36 | { |
Martin Schulze | b44232b | 2016-04-16 19:06:16 +0200 | [diff] [blame] | 37 | return m_data.isEmpty() ? QImage{} : calculateThumbnail(m_data, opaque, alpha); |
| 38 | } |
José Fonseca | 994535d | 2013-09-12 17:26:34 +0100 | [diff] [blame] | 39 | |
Martin Schulze | b44232b | 2016-04-16 19:06:16 +0200 | [diff] [blame] | 40 | QImage ApiSurface::calculateThumbnail(const QByteArray &data, bool opaque, |
| 41 | bool alpha) const |
| 42 | { |
José Fonseca | 5239831 | 2013-09-11 18:42:07 +0100 | [diff] [blame] | 43 | /* |
Zack Rusin | a69f0de | 2013-09-12 17:21:51 -0400 | [diff] [blame] | 44 | * We need to do the conversion to create the thumbnail |
José Fonseca | 5239831 | 2013-09-11 18:42:07 +0100 | [diff] [blame] | 45 | */ |
Martin Schulze | b44232b | 2016-04-16 19:06:16 +0200 | [diff] [blame] | 46 | std::unique_ptr<image::Image> image{imageFromData(data)}; |
José Fonseca | 5239831 | 2013-09-11 18:42:07 +0100 | [diff] [blame] | 47 | Q_ASSERT(image); |
Martin Schulze | b44232b | 2016-04-16 19:06:16 +0200 | [diff] [blame] | 48 | QImage img = qimageFromRawImage(image.get(), 0.0f, 1.0f, opaque, alpha); |
| 49 | return thumbnail(img); |
| 50 | } |
| 51 | |
| 52 | void ApiSurface::setData(const QByteArray &data) |
| 53 | { |
| 54 | m_data = data; |
Zack Rusin | 952e9d4 | 2011-04-09 23:37:21 -0400 | [diff] [blame] | 55 | } |
| 56 | |
Jose Fonseca | 93a7c0c | 2015-05-27 20:52:51 +0100 | [diff] [blame] | 57 | QByteArray ApiSurface::data() const |
Zack Rusin | 952e9d4 | 2011-04-09 23:37:21 -0400 | [diff] [blame] | 58 | { |
Jose Fonseca | 93a7c0c | 2015-05-27 20:52:51 +0100 | [diff] [blame] | 59 | return m_data; |
Zack Rusin | 952e9d4 | 2011-04-09 23:37:21 -0400 | [diff] [blame] | 60 | } |
| 61 | |
Zack Rusin | b25c4b9 | 2011-11-16 22:43:34 -0500 | [diff] [blame] | 62 | int ApiSurface::depth() const |
| 63 | { |
| 64 | return m_depth; |
| 65 | } |
| 66 | |
| 67 | void ApiSurface::setDepth(int depth) |
| 68 | { |
| 69 | m_depth = depth; |
| 70 | } |
| 71 | |
Zack Rusin | e181b99 | 2011-11-17 16:00:41 -0500 | [diff] [blame] | 72 | QString ApiSurface::formatName() const |
| 73 | { |
| 74 | return m_formatName; |
| 75 | } |
| 76 | |
| 77 | void ApiSurface::setFormatName(const QString &str) |
| 78 | { |
| 79 | m_formatName = str; |
| 80 | } |
| 81 | |
| 82 | |
Zack Rusin | 952e9d4 | 2011-04-09 23:37:21 -0400 | [diff] [blame] | 83 | ApiTexture::ApiTexture() |
José Fonseca | 18081d5 | 2011-05-07 00:10:25 +0100 | [diff] [blame] | 84 | : ApiSurface() |
Zack Rusin | 952e9d4 | 2011-04-09 23:37:21 -0400 | [diff] [blame] | 85 | { |
| 86 | } |
| 87 | |
José Fonseca | 18081d5 | 2011-05-07 00:10:25 +0100 | [diff] [blame] | 88 | QString ApiTexture::label() const |
Zack Rusin | 952e9d4 | 2011-04-09 23:37:21 -0400 | [diff] [blame] | 89 | { |
José Fonseca | 18081d5 | 2011-05-07 00:10:25 +0100 | [diff] [blame] | 90 | return m_label; |
Zack Rusin | 952e9d4 | 2011-04-09 23:37:21 -0400 | [diff] [blame] | 91 | } |
| 92 | |
José Fonseca | 18081d5 | 2011-05-07 00:10:25 +0100 | [diff] [blame] | 93 | void ApiTexture::setLabel(const QString &str) |
Zack Rusin | 952e9d4 | 2011-04-09 23:37:21 -0400 | [diff] [blame] | 94 | { |
José Fonseca | 18081d5 | 2011-05-07 00:10:25 +0100 | [diff] [blame] | 95 | m_label = str; |
Zack Rusin | 952e9d4 | 2011-04-09 23:37:21 -0400 | [diff] [blame] | 96 | } |
Zack Rusin | a684641 | 2011-04-10 19:51:44 -0400 | [diff] [blame] | 97 | |
| 98 | ApiFramebuffer::ApiFramebuffer() |
| 99 | : ApiSurface() |
| 100 | { |
| 101 | } |
| 102 | |
| 103 | QString ApiFramebuffer::type() const |
| 104 | { |
| 105 | return m_type; |
| 106 | } |
| 107 | |
| 108 | void ApiFramebuffer::setType(const QString &str) |
| 109 | { |
| 110 | m_type = str; |
| 111 | } |
Zack Rusin | b25c4b9 | 2011-11-16 22:43:34 -0500 | [diff] [blame] | 112 | |
Zack Rusin | a69f0de | 2013-09-12 17:21:51 -0400 | [diff] [blame] | 113 | image::Image * |
Jose Fonseca | 93a7c0c | 2015-05-27 20:52:51 +0100 | [diff] [blame] | 114 | ApiSurface::imageFromData(const QByteArray &dataArray) |
Zack Rusin | a69f0de | 2013-09-12 17:21:51 -0400 | [diff] [blame] | 115 | { |
Zack Rusin | a69f0de | 2013-09-12 17:21:51 -0400 | [diff] [blame] | 116 | image::Image *image; |
| 117 | |
| 118 | /* |
| 119 | * Detect the PNG vs PFM images. |
| 120 | */ |
| 121 | const char pngSignature[] = {(char)0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A, 0}; |
| 122 | if (dataArray.startsWith(pngSignature)) { |
| 123 | ByteArrayBuf buf(dataArray); |
| 124 | std::istream istr(&buf); |
| 125 | image = image::readPNG(istr); |
| 126 | } else { |
| 127 | image = image::readPNM(dataArray.data(), dataArray.size()); |
| 128 | } |
| 129 | |
| 130 | return image; |
| 131 | } |
| 132 | |
| 133 | |
José Fonseca | 36509be | 2013-09-17 15:22:50 +0100 | [diff] [blame] | 134 | static inline unsigned char clamp(int x) |
| 135 | { |
| 136 | if (x <= 0) { |
| 137 | return 0; |
| 138 | } |
| 139 | if (x > 255) { |
| 140 | return 255; |
| 141 | } |
| 142 | return (unsigned char) x; |
| 143 | } |
| 144 | |
| 145 | static inline unsigned char clamp(float x) |
| 146 | { |
| 147 | if (x <= 0.0f) { |
| 148 | return 0; |
| 149 | } |
| 150 | if (x > 255.0f) { |
| 151 | return 255; |
| 152 | } |
| 153 | return (unsigned char) (x + 0.5f); |
| 154 | } |
| 155 | |
| 156 | |
Zack Rusin | a69f0de | 2013-09-12 17:21:51 -0400 | [diff] [blame] | 157 | QImage |
José Fonseca | 36509be | 2013-09-17 15:22:50 +0100 | [diff] [blame] | 158 | ApiSurface::qimageFromRawImage(const image::Image *image, |
| 159 | float lowerValue, |
| 160 | float upperValue, |
| 161 | bool opaque, |
| 162 | bool alpha) |
Zack Rusin | a69f0de | 2013-09-12 17:21:51 -0400 | [diff] [blame] | 163 | { |
| 164 | QImage img; |
| 165 | int width = image->width; |
| 166 | int height = image->height; |
| 167 | |
| 168 | img = QImage(width, height, QImage::Format_ARGB32); |
| 169 | |
José Fonseca | 36509be | 2013-09-17 15:22:50 +0100 | [diff] [blame] | 170 | int offset = - lowerValue * 255; |
| 171 | int scale = 256 / (upperValue - lowerValue); |
| 172 | |
| 173 | float offset_f = - lowerValue; |
| 174 | float scale_f = 255.0f / (upperValue - lowerValue); |
| 175 | |
| 176 | int aMask = (opaque || alpha) ? 0xff : 0; |
| 177 | |
Zack Rusin | a69f0de | 2013-09-12 17:21:51 -0400 | [diff] [blame] | 178 | const unsigned char *srcRow = image->start(); |
| 179 | for (int y = 0; y < height; ++y) { |
| 180 | QRgb *dst = (QRgb *)img.scanLine(y); |
| 181 | |
| 182 | if (image->channelType == image::TYPE_UNORM8) { |
| 183 | const unsigned char *src = srcRow; |
| 184 | for (int x = 0; x < width; ++x) { |
Zack Rusin | d36c6ee | 2013-09-12 17:41:57 -0400 | [diff] [blame] | 185 | unsigned char rgba[4] = {0, 0, 0, 0xff}; |
Zack Rusin | a69f0de | 2013-09-12 17:21:51 -0400 | [diff] [blame] | 186 | for (int c = 0; c < image->channels; ++c) { |
José Fonseca | 36509be | 2013-09-17 15:22:50 +0100 | [diff] [blame] | 187 | rgba[c] = clamp(((*src++ + offset) * scale) >> 8); |
Zack Rusin | a69f0de | 2013-09-12 17:21:51 -0400 | [diff] [blame] | 188 | } |
| 189 | if (image->channels == 1) { |
| 190 | // Use gray-scale instead of red |
| 191 | rgba[1] = rgba[0]; |
| 192 | rgba[2] = rgba[0]; |
| 193 | } |
José Fonseca | 36509be | 2013-09-17 15:22:50 +0100 | [diff] [blame] | 194 | if (alpha) { |
| 195 | rgba[2] = rgba[1] = rgba[0] = rgba[3]; |
| 196 | } |
| 197 | rgba[3] |= aMask; |
Zack Rusin | a69f0de | 2013-09-12 17:21:51 -0400 | [diff] [blame] | 198 | dst[x] = qRgba(rgba[0], rgba[1], rgba[2], rgba[3]); |
| 199 | } |
| 200 | } else { |
| 201 | const float *src = (const float *)srcRow; |
| 202 | for (int x = 0; x < width; ++x) { |
| 203 | unsigned char rgba[4] = {0, 0, 0, 0xff}; |
| 204 | for (int c = 0; c < image->channels; ++c) { |
José Fonseca | 36509be | 2013-09-17 15:22:50 +0100 | [diff] [blame] | 205 | rgba[c] = clamp((*src++ + offset_f)*scale_f); |
Zack Rusin | a69f0de | 2013-09-12 17:21:51 -0400 | [diff] [blame] | 206 | } |
| 207 | if (image->channels == 1) { |
| 208 | // Use gray-scale instead of red |
| 209 | rgba[1] = rgba[0]; |
| 210 | rgba[2] = rgba[0]; |
| 211 | } |
José Fonseca | 36509be | 2013-09-17 15:22:50 +0100 | [diff] [blame] | 212 | if (alpha) { |
| 213 | rgba[2] = rgba[1] = rgba[0] = rgba[3]; |
| 214 | } |
| 215 | rgba[3] |= aMask; |
Zack Rusin | a69f0de | 2013-09-12 17:21:51 -0400 | [diff] [blame] | 216 | dst[x] = qRgba(rgba[0], rgba[1], rgba[2], rgba[3]); |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | srcRow += image->stride(); |
| 221 | } |
| 222 | |
| 223 | return img; |
| 224 | } |