Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Author: Wesley Stessens
- */
- #ifndef COMPRESSOR_H
- #define COMPRESSOR_H
- #include <QString>
- #include <QMatrix4x4>
- class Compressor
- {
- public:
- static bool compress(const QString &input, const QString &output);
- static QString getLastError() {return lastError;}
- private:
- static QMatrix4x4 transformDCT(const QMatrix4x4 &matrix);
- static QMatrix4x4 quantize(const QMatrix4x4 &matrix);
- static QList<int> zigZagScan(const QMatrix4x4 &matrix);
- static QString lastError;
- };
- #endif
- /*
- * Author: Wesley Stessens
- */
- #include "compressor.h"
- #include "BitStream.h"
- #include <QFile>
- QString Compressor::lastError = "Unknown";
- // Author: Wesley Stessens
- bool Compressor::compress(const QString &input, const QString &output)
- {
- QFile file(input);
- if (!file.open(QIODevice::ReadOnly))
- return NULL;
- int width = 356;
- int bufferSize = width * 4;
- QByteArray bytes;
- while (!file.atEnd()) {
- bytes = file.read(bufferSize);
- uint8_t *buffer = (uint8_t*)bytes.data();
- util::BitStreamReader b(buffer, bufferSize);
- QList<int> values;
- for (int i = 0; i < bufferSize; ++i)
- values.append(b.get(8));
- for (int count = 0; count < (width >> 2); ++count) {
- QList<int> block;
- for (int i = 0; i < 4; ++i) {
- for (int j = 0; j < 4; ++j) {
- // count * 4 = initial offset
- // i * width = vertical offset
- // j = horizontal offset
- // - 128 = centralize around zero
- int value = values.at(count * 4 + i * width + j) - 128;
- block.append(value);
- }
- }
- // Process block
- QMatrix4x4 matrix(block.at(0), block.at(1), block.at(2), block.at(3),
- block.at(4), block.at(5), block.at(6), block.at(7),
- block.at(8), block.at(9), block.at(10), block.at(11),
- block.at(12), block.at(13), block.at(14), block.at(15));
- QMatrix4x4 transformed = transformDCT(matrix);
- qDebug() << transformed;
- QMatrix4x4 quantized = quantize(transformed);
- qDebug() << quantized;
- QList<int> zigzagged = zigZagScan(quantized);
- qDebug() << zigzagged;
- }
- }
- return false;
- }
- QMatrix4x4 Compressor::transformDCT(const QMatrix4x4 &matrix)
- {
- QMatrix4x4 transformMatrix(0.500, 0.500, 0.500, 0.500,
- 0.653, 0.271, -0.271, -0.653,
- 0.500, -0.500, -0.500, 0.500,
- 0.271, -0.653, 0.653, -0.271);
- return transformMatrix * matrix * transformMatrix.transposed();
- }
- QMatrix4x4 Compressor::quantize(const QMatrix4x4 &matrix)
- {
- QList<int> values;
- QMatrix4x4 quantizeMatrix( 2, 4, 8, 16,
- 4, 4, 8, 16,
- 8, 8, 32, 64,
- 16, 32, 64, 128);
- for (int i = 0; i < 4; ++i)
- for (int j = 0; j < 4; ++j)
- values.append(qRound(matrix(i, j) / quantizeMatrix(i, j)));
- QMatrix4x4 result(values.at(0), values.at(1), values.at(2), values.at(3),
- values.at(4), values.at(5), values.at(6), values.at(7),
- values.at(8), values.at(9), values.at(10), values.at(11),
- values.at(12), values.at(13), values.at(14), values.at(15));
- return result;
- }
- QList<int> Compressor::zigZagScan(const QMatrix4x4 &matrix)
- {
- QList<int> result;
- result.append(matrix(0,0));
- result.append(matrix(0,1));
- result.append(matrix(1,0));
- result.append(matrix(2,0));
- result.append(matrix(1,1));
- result.append(matrix(0,2));
- result.append(matrix(0,3));
- result.append(matrix(1,2));
- result.append(matrix(2,1));
- result.append(matrix(3,0));
- result.append(matrix(3,1));
- result.append(matrix(2,2));
- result.append(matrix(1,3));
- result.append(matrix(2,3));
- result.append(matrix(3,2));
- result.append(matrix(3,3));
- return result;
- }
- /*QByteArray Compressor::runLengthEncode(QList<int> values)
- {
- //
- }*/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement