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