Warning, file /graphics/krita/libs/image/tiles3/tests/kis_compression_tests.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 /*
0002  *  SPDX-FileCopyrightText: 2010 Dmitry Kazakov <dimula73@gmail.com>
0003  *
0004  *  SPDX-License-Identifier: GPL-2.0-or-later
0005  */
0006 
0007 #include "kis_compression_tests.h"
0008 #include <simpletest.h>
0009 
0010 #include <QImage>
0011 
0012 #include "../../../sdk/tests/testutil.h"
0013 #include "tiles3/swap/kis_lzf_compression.h"
0014 #include <kis_debug.h>
0015 
0016 #define TEST_FILE "tile.png"
0017 //#define TEST_FILE "hakonepa.png"
0018 
0019 
0020 void PRINT_COMPRESSION(const QString &title, quint32 src, quint32 dst) {
0021 
0022     dbgKrita << title << dst << "/" << src << "\t|" << double(dst)/src;
0023 }
0024 
0025 
0026 void KisCompressionTests::roundTrip(KisAbstractCompression *compression)
0027 {
0028     QImage referenceImage(QString(FILES_DATA_DIR) + QDir::separator() + TEST_FILE);
0029 
0030     QImage image(referenceImage);
0031     qint32 srcSize = image.byteCount();
0032 
0033     qint32 outputSize = compression->outputBufferSize(srcSize);
0034     quint8 *output = new quint8[outputSize];
0035 
0036     qint32 compressedBytes;
0037     qint32 uncompressedBytes;
0038 
0039     compressedBytes = compression->compress(image.bits(), srcSize,
0040                                             output, outputSize);
0041     uncompressedBytes = compression->decompress(output, compressedBytes,
0042                                                 image.bits(), srcSize);
0043 
0044     PRINT_COMPRESSION("Single-pass:\t", uncompressedBytes, compressedBytes);
0045 
0046     QCOMPARE(uncompressedBytes, srcSize);
0047     QVERIFY(referenceImage == image);
0048 }
0049 
0050 void KisCompressionTests::roundTripTwoPass(KisAbstractCompression *compression)
0051 {
0052     QImage image(QString(FILES_DATA_DIR) + QDir::separator() + TEST_FILE);
0053 
0054     qint32 srcSize = image.byteCount();
0055     qint32 outputSize = compression->outputBufferSize(srcSize);
0056 
0057     quint8 *output = new quint8[outputSize];
0058 
0059     qint32 compressedBytes;
0060     qint32 uncompressedBytes;
0061 
0062     quint8 *tempBuffer = new quint8[srcSize];
0063     KisAbstractCompression::linearizeColors(image.bits(), tempBuffer,
0064                                             srcSize, 4);
0065 
0066     compressedBytes = compression->compress(tempBuffer, srcSize,
0067                                             output, outputSize);
0068 
0069     uncompressedBytes = compression->decompress(output, compressedBytes,
0070                                                 tempBuffer, srcSize);
0071 
0072     KisAbstractCompression::delinearizeColors(tempBuffer, image.bits(),
0073                                               srcSize, 4);
0074 
0075     PRINT_COMPRESSION("Two-pass:\t", uncompressedBytes, compressedBytes);
0076 
0077     QCOMPARE(uncompressedBytes, srcSize);
0078 
0079     QImage referenceImage(QString(FILES_DATA_DIR) + QDir::separator() + TEST_FILE);
0080     QVERIFY(referenceImage == image);
0081 }
0082 
0083 void KisCompressionTests::benchmarkCompression(KisAbstractCompression *compression)
0084 {
0085     QImage image(QString(FILES_DATA_DIR) + QDir::separator() + TEST_FILE);
0086 
0087     qint32 srcSize = image.byteCount();
0088     qint32 outputSize = compression->outputBufferSize(srcSize);
0089 
0090     quint8 *output = new quint8[outputSize];
0091 
0092     qint32 compressedBytes;
0093 
0094     QBENCHMARK {
0095         compressedBytes = compression->compress(image.bits(), srcSize,
0096                                                 output, outputSize);
0097     }
0098     Q_UNUSED(compressedBytes);
0099 }
0100 
0101 void KisCompressionTests::benchmarkCompressionTwoPass(KisAbstractCompression *compression)
0102 {
0103     QImage image(QString(FILES_DATA_DIR) + QDir::separator() + TEST_FILE);
0104 
0105     qint32 srcSize = image.byteCount();
0106     qint32 outputSize = compression->outputBufferSize(srcSize);
0107 
0108     quint8 *output = new quint8[outputSize];
0109 
0110     qint32 compressedBytes;
0111 
0112     quint8 *tempBuffer = new quint8[srcSize];
0113 
0114     QBENCHMARK {
0115         KisAbstractCompression::linearizeColors(image.bits(), tempBuffer,
0116                                                 srcSize, 4);
0117         compressedBytes = compression->compress(tempBuffer, srcSize,
0118                                                 output, outputSize);
0119     }
0120     Q_UNUSED(compressedBytes);
0121 }
0122 
0123 void KisCompressionTests::benchmarkDecompression(KisAbstractCompression *compression)
0124 {
0125     QImage image(QString(FILES_DATA_DIR) + QDir::separator() + TEST_FILE);
0126 
0127     qint32 srcSize = image.byteCount();
0128     qint32 outputSize = compression->outputBufferSize(srcSize);
0129 
0130     quint8 *output = new quint8[outputSize];
0131 
0132     qint32 compressedBytes;
0133     qint32 uncompressedBytes;
0134 
0135     compressedBytes = compression->compress(image.bits(), srcSize,
0136                                             output, outputSize);
0137 
0138     QBENCHMARK {
0139         uncompressedBytes = compression->decompress(output, compressedBytes,
0140                                                     image.bits(), srcSize);
0141     }
0142     Q_UNUSED(uncompressedBytes);
0143 }
0144 
0145 void KisCompressionTests::benchmarkDecompressionTwoPass(KisAbstractCompression *compression)
0146 {
0147     QImage image(QString(FILES_DATA_DIR) + QDir::separator() + TEST_FILE);
0148 
0149     qint32 srcSize = image.byteCount();
0150     qint32 outputSize = compression->outputBufferSize(srcSize);
0151 
0152     quint8 *output = new quint8[outputSize];
0153 
0154     qint32 compressedBytes;
0155     qint32 uncompressedBytes;
0156 
0157     quint8 *tempBuffer = new quint8[srcSize];
0158     KisAbstractCompression::linearizeColors(image.bits(), tempBuffer, srcSize, 4);
0159 
0160     compressedBytes = compression->compress(tempBuffer, srcSize,
0161                                             output, outputSize);
0162 
0163     QBENCHMARK {
0164         uncompressedBytes = compression->decompress(output, compressedBytes,
0165                                                     tempBuffer, srcSize);
0166 
0167         KisAbstractCompression::delinearizeColors(tempBuffer, image.bits(),
0168                                                   srcSize, 4);
0169     }
0170     Q_UNUSED(uncompressedBytes);
0171 }
0172 
0173 void KisCompressionTests::testOverflow(KisAbstractCompression *compression)
0174 {
0175     QFile file(QString(FILES_DATA_DIR) + QDir::separator() + TEST_FILE);
0176     if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
0177         return;
0178 
0179     QByteArray array = file.readAll();
0180 
0181     qint32 srcSize = array.count();
0182     qint32 outputSize = compression->outputBufferSize(srcSize);
0183 
0184     quint8 *output = new quint8[outputSize];
0185 
0186     qint32 compressedBytes;
0187     compressedBytes = compression->compress((quint8*)array.data(), srcSize,
0188                                             output, outputSize);
0189 
0190     PRINT_COMPRESSION("Uncompressable:\t", srcSize, compressedBytes);
0191     dbgKrita << "Max buffer size:" << outputSize;
0192     QVERIFY(compressedBytes <= outputSize);
0193 }
0194 
0195 void KisCompressionTests::testLzfRoundTrip()
0196 {
0197     KisAbstractCompression *compression = new KisLzfCompression();
0198 
0199     roundTrip(compression);
0200     roundTripTwoPass(compression);
0201 
0202     delete compression;
0203 }
0204 
0205 void KisCompressionTests::testLzfOverflow()
0206 {
0207     KisAbstractCompression *compression = new KisLzfCompression();
0208     testOverflow(compression);
0209     delete compression;
0210 }
0211 
0212 void KisCompressionTests::benchmarkMemCpy()
0213 {
0214     QImage image(QString(FILES_DATA_DIR) + QDir::separator() + TEST_FILE);
0215     qint32 srcSize = image.byteCount();
0216     quint8 *output = new quint8[srcSize];
0217 
0218     QBENCHMARK {
0219         memcpy(output, image.bits(), srcSize);
0220     }
0221 
0222     delete[] output;
0223 }
0224 
0225 void KisCompressionTests::benchmarkCompressionLzf()
0226 {
0227     KisAbstractCompression *compression = new KisLzfCompression();
0228     benchmarkCompression(compression);
0229     delete compression;
0230 }
0231 
0232 void KisCompressionTests::benchmarkCompressionLzfTwoPass()
0233 {
0234     KisAbstractCompression *compression = new KisLzfCompression();
0235     benchmarkCompressionTwoPass(compression);
0236     delete compression;
0237 }
0238 
0239 void KisCompressionTests::benchmarkDecompressionLzf()
0240 {
0241     KisAbstractCompression *compression = new KisLzfCompression();
0242     benchmarkDecompression(compression);
0243     delete compression;
0244 }
0245 
0246 void KisCompressionTests::benchmarkDecompressionLzfTwoPass()
0247 {
0248     KisAbstractCompression *compression = new KisLzfCompression();
0249     benchmarkDecompressionTwoPass(compression);
0250     delete compression;
0251 }
0252 
0253 SIMPLE_TEST_MAIN(KisCompressionTests)
0254