Zack Rusin | 952e9d4 | 2011-04-09 23:37:21 -0400 | [diff] [blame] | 1 | #include "apisurface.h" |
| 2 | |
| 3 | #include <QDebug> |
| 4 | #include <QSysInfo> |
| 5 | |
| 6 | ApiSurface::ApiSurface() |
| 7 | { |
| 8 | } |
| 9 | |
| 10 | QSize ApiSurface::size() const |
| 11 | { |
| 12 | return m_size; |
| 13 | } |
| 14 | |
| 15 | void ApiSurface::setSize(const QSize &size) |
| 16 | { |
| 17 | m_size = size; |
| 18 | } |
| 19 | |
José Fonseca | b794df1 | 2011-04-12 08:28:45 +0100 | [diff] [blame] | 20 | int ApiSurface::numChannels() const |
| 21 | { |
| 22 | return m_numChannels; |
| 23 | } |
| 24 | |
| 25 | void ApiSurface::setNumChannels(int numChannels) |
| 26 | { |
| 27 | m_numChannels = numChannels; |
| 28 | } |
| 29 | |
Zack Rusin | 952e9d4 | 2011-04-09 23:37:21 -0400 | [diff] [blame] | 30 | static inline int |
José Fonseca | 3b9fc08 | 2011-04-10 19:25:01 +0100 | [diff] [blame] | 31 | rgba8_to_argb(quint8 r, quint8 g, quint8 b, quint8 a) |
| 32 | { |
| 33 | return (a << 24 | r << 16 | g << 8 | b); |
| 34 | } |
| 35 | |
| 36 | static inline int |
Zack Rusin | 952e9d4 | 2011-04-09 23:37:21 -0400 | [diff] [blame] | 37 | rgbaf2argb(float r, float g, float b, float a) |
| 38 | { |
| 39 | quint8 rb = r * 255; |
| 40 | quint8 gb = g * 255; |
| 41 | quint8 bb = b * 255; |
| 42 | quint8 ab = a * 255; |
| 43 | |
| 44 | return (ab << 24 | rb << 16 | gb << 8 | bb); |
| 45 | } |
| 46 | |
| 47 | void ApiSurface::contentsFromBase64(const QByteArray &base64) |
| 48 | { |
| 49 | QByteArray dataArray = QByteArray::fromBase64(base64); |
José Fonseca | 3b9fc08 | 2011-04-10 19:25:01 +0100 | [diff] [blame] | 50 | const quint8 *data = (const quint8*)dataArray.data(); |
Zack Rusin | 952e9d4 | 2011-04-09 23:37:21 -0400 | [diff] [blame] | 51 | int width = m_size.width(); |
| 52 | int height = m_size.height(); |
| 53 | |
| 54 | if (width <= 0 || height <= 0) |
| 55 | return; |
| 56 | |
| 57 | int *pixelData = (int*)malloc(sizeof(int) * width * height); |
| 58 | |
| 59 | //XXX not sure if this will work when |
| 60 | // QSysInfo::ByteOrder == QSysInfo::BigEndian |
| 61 | |
José Fonseca | b794df1 | 2011-04-12 08:28:45 +0100 | [diff] [blame] | 62 | if (m_numChannels == 4) { |
| 63 | for (int y = 0; y < height; ++y) { |
| 64 | for (int x = 0; x < width; ++x) { |
| 65 | int pixel = rgba8_to_argb(data[(y * width + x) * 4 + 0], |
| 66 | data[(y * width + x) * 4 + 1], |
| 67 | data[(y * width + x) * 4 + 2], |
| 68 | data[(y * width + x) * 4 + 3]); |
| 69 | pixelData[y * width + x] = pixel; |
| 70 | } |
Zack Rusin | 952e9d4 | 2011-04-09 23:37:21 -0400 | [diff] [blame] | 71 | } |
José Fonseca | b794df1 | 2011-04-12 08:28:45 +0100 | [diff] [blame] | 72 | } else if (m_numChannels == 1) { |
| 73 | for (int y = 0; y < height; ++y) { |
| 74 | for (int x = 0; x < width; ++x) { |
| 75 | int pixel = rgba8_to_argb(data[y * width + x], |
| 76 | data[y * width + x], |
| 77 | data[y * width + x], |
| 78 | 255); |
| 79 | pixelData[y * width + x] = pixel; |
| 80 | } |
| 81 | } |
| 82 | } else { |
| 83 | Q_ASSERT(0); |
Zack Rusin | 952e9d4 | 2011-04-09 23:37:21 -0400 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | m_image = QImage((uchar*)pixelData, |
| 87 | width, height, |
| 88 | QImage::Format_ARGB32).mirrored(); |
| 89 | m_thumb = m_image.scaled(64, 64, Qt::KeepAspectRatio); |
| 90 | //m_image.save("testoutput.png"); |
| 91 | |
| 92 | free(pixelData); |
| 93 | } |
| 94 | |
| 95 | QImage ApiSurface::image() const |
| 96 | { |
| 97 | return m_image; |
| 98 | } |
| 99 | |
| 100 | QImage ApiSurface::thumb() const |
| 101 | { |
| 102 | return m_thumb; |
| 103 | } |
| 104 | |
| 105 | ApiTexture::ApiTexture() |
| 106 | : ApiSurface(), |
| 107 | m_unit(0), |
| 108 | m_level(0) |
| 109 | { |
| 110 | } |
| 111 | |
| 112 | int ApiTexture::unit() const |
| 113 | { |
| 114 | return m_unit; |
| 115 | } |
| 116 | |
| 117 | void ApiTexture::setUnit(int un) |
| 118 | { |
| 119 | m_unit = un; |
| 120 | } |
| 121 | |
| 122 | QString ApiTexture::target() const |
| 123 | { |
| 124 | return m_target; |
| 125 | } |
| 126 | |
| 127 | void ApiTexture::setTarget(const QString &str) |
| 128 | { |
| 129 | m_target = str; |
| 130 | } |
| 131 | |
| 132 | int ApiTexture::level() const |
| 133 | { |
| 134 | return m_level; |
| 135 | } |
| 136 | |
| 137 | void ApiTexture::setLevel(int l) |
| 138 | { |
| 139 | m_level = l; |
| 140 | } |
Zack Rusin | a684641 | 2011-04-10 19:51:44 -0400 | [diff] [blame] | 141 | |
| 142 | ApiFramebuffer::ApiFramebuffer() |
| 143 | : ApiSurface() |
| 144 | { |
| 145 | } |
| 146 | |
| 147 | QString ApiFramebuffer::type() const |
| 148 | { |
| 149 | return m_type; |
| 150 | } |
| 151 | |
| 152 | void ApiFramebuffer::setType(const QString &str) |
| 153 | { |
| 154 | m_type = str; |
| 155 | } |