A recodeImage() method is added to the JavaScript ArrayBuffer object.
Showing the changes to libraries/script-engine/src/ArrayBufferPrototype.cpp (1 of 3 files). Show all
Unified · Split
| @@ -11,6 +11,9 @@ · 10 unmodified lines | ||
| 11 | 11 | |
| 12 | 12 | #include <glm/glm.hpp> |
| 13 | 13 | |
| 14 | +#include <QBuffer> | |
| 15 | +#include <QImage> | |
| 16 | + | |
| 14 | 17 | #include "ArrayBufferClass.h" |
| 15 | 18 | #include "ArrayBufferPrototype.h" |
| 16 | 19 | |
| @@ -47,17 +50,40 @@ QByteArray ArrayBufferPrototype::slice(qint32 begin) const { · 30 unmodified lines | ||
| 47 | 50 | } |
| 48 | 51 | |
| 49 | 52 | QByteArray ArrayBufferPrototype::compress() const { |
| 53 | + // Compresses the ArrayBuffer data in Zlib format. | |
| 50 | 54 | QByteArray* ba = thisArrayBuffer(); |
| 51 | 55 | |
| 52 | 56 | QByteArray buffer = qCompress(*ba); |
| 53 | − | |
| 54 | − // Qt's qCompress() default compression level (-1) is the standard zLib compression. | |
| 55 | − // Here remove Qt's custom header that prevents the data server from uncompressing the files with zLib. | |
| 56 | − buffer.remove(QCOMPRESS_HEADER_POSITION, QCOMPRESS_HEADER_SIZE); | |
| 57 | + buffer.remove(QCOMPRESS_HEADER_POSITION, QCOMPRESS_HEADER_SIZE); // Remove Qt's custom header to make it proper Zlib. | |
| 57 | 58 | |
| 58 | 59 | return buffer; |
| 59 | 60 | } |
| 60 | 61 | |
| 62 | +QByteArray ArrayBufferPrototype::recodeImage(const QString& sourceFormat, const QString& targetFormat, qint32 maxSize) const { | |
| 63 | + // Recodes image data if sourceFormat and targetFormat are different. | |
| 64 | + // Rescales image data if either dimension is greater than the specified maximum. | |
| 65 | + QByteArray* ba = thisArrayBuffer(); | |
| 66 | + | |
| 67 | + bool mustRecode = sourceFormat.toLower() != targetFormat.toLower(); | |
| 68 | + | |
| 69 | + QImage image = QImage::fromData(*ba); | |
| 70 | + if (image.width() > maxSize || image.height() > maxSize) { | |
| 71 | + image = image.scaled(maxSize, maxSize, Qt::KeepAspectRatio); | |
| 72 | + mustRecode = true; | |
| 73 | + } | |
| 74 | + | |
| 75 | + if (mustRecode) { | |
| 76 | + QBuffer buffer; | |
| 77 | + buffer.open(QIODevice::WriteOnly); | |
| 78 | + std::string str = targetFormat.toUpper().toStdString(); | |
| 79 | + const char* format = str.c_str(); | |
| 80 | + image.save(&buffer, format); | |
| 81 | + return buffer.data(); | |
| 82 | + } | |
| 83 | + | |
| 84 | + return *ba; | |
| 85 | +} | |
| 86 | + | |
| 61 | 87 | QByteArray* ArrayBufferPrototype::thisArrayBuffer() const { |
| 62 | 88 | return qscriptvalue_cast<QByteArray*>(thisObject().data()); |
| 63 | 89 | } |