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