History

Recode and rescale texture file data before uploading

A recodeImage() method is added to the JavaScript ArrayBuffer object.

David Rowe committed 12 years ago

Commit f39aed31 parent 287e3d63 files · +46 −4Browse files

Showing the changes to libraries/script-engine/src/ArrayBufferPrototype.cpp (1 of 3 files). Show all

Unified · Split

libraries/script-engine/src/ArrayBufferPrototype.cpp+30−4
@@ -11,6 +11,9 @@ · 10 unmodified lines
1111
1212 #include <glm/glm.hpp>
1313
14+#include <QBuffer>
15+#include <QImage>
16+
1417 #include "ArrayBufferClass.h"
1518 #include "ArrayBufferPrototype.h"
1619
@@ -47,17 +50,40 @@ QByteArray ArrayBufferPrototype::slice(qint32 begin) const { · 30 unmodified lines
4750 }
4851
4952 QByteArray ArrayBufferPrototype::compress() const {
53+ // Compresses the ArrayBuffer data in Zlib format.
5054 QByteArray* ba = thisArrayBuffer();
5155
5256 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.
5758
5859 return buffer;
5960 }
6061
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+
6187 QByteArray* ArrayBufferPrototype::thisArrayBuffer() const {
6288 return qscriptvalue_cast<QByteArray*>(thisObject().data());
6389 }