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