File indexing completed on 2024-12-22 04:10:30
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_tile_compressors_test.h" 0008 #include <simpletest.h> 0009 0010 #include "tiles3/kis_tiled_data_manager.h" 0011 #include "tiles3/swap/kis_legacy_tile_compressor.h" 0012 #include "tiles3/swap/kis_tile_compressor_2.h" 0013 0014 #include "tiles_test_utils.h" 0015 0016 void KisTileCompressorsTest::doRoundTrip(KisAbstractTileCompressor *compressor) 0017 { 0018 quint8 defaultPixel = 0; 0019 KisTiledDataManager dm(1, &defaultPixel); 0020 0021 quint8 oddPixel1 = 128; 0022 KisTileSP tile11; 0023 0024 dm.clear(64, 64, 64, 64, &oddPixel1); 0025 0026 tile11 = dm.getTile(1, 1, false); 0027 QVERIFY(memoryIsFilled(oddPixel1, tile11->data(), TILESIZE)); 0028 0029 KoStoreFake fakeStore; 0030 KisFakePaintDeviceWriter writer(&fakeStore); 0031 0032 bool retval = compressor->writeTile(tile11, writer); 0033 Q_ASSERT(retval); 0034 tile11 = 0; 0035 0036 fakeStore.startReading(); 0037 0038 dm.clear(); 0039 0040 tile11 = dm.getTile(1, 1, false); 0041 QVERIFY(memoryIsFilled(defaultPixel, tile11->data(), TILESIZE)); 0042 tile11 = 0; 0043 0044 bool res = compressor->readTile(fakeStore.device(), &dm); 0045 Q_ASSERT(res); 0046 Q_UNUSED(res); 0047 tile11 = dm.getTile(1, 1, false); 0048 QVERIFY(memoryIsFilled(oddPixel1, tile11->data(), TILESIZE)); 0049 tile11 = 0; 0050 } 0051 0052 void KisTileCompressorsTest::doLowLevelRoundTrip(KisAbstractTileCompressor *compressor) 0053 { 0054 const qint32 pixelSize = 1; 0055 quint8 oddPixel1 = 128; 0056 quint8 oddPixel2 = 129; 0057 0058 /** 0059 * A small hack to acquire a standalone tile data. 0060 * globalTileDataStore is not exported out of kritaimage.so, 0061 * so we get it from the data manager 0062 */ 0063 KisTiledDataManager dm(pixelSize, &oddPixel1); 0064 KisTileSP tile = dm.getTile(0, 0, true); 0065 tile->lockForWrite(); 0066 0067 0068 KisTileData *td = tile->tileData(); 0069 QVERIFY(memoryIsFilled(oddPixel1, td->data(), TILESIZE)); 0070 0071 qint32 bufferSize = compressor->tileDataBufferSize(td); 0072 quint8 *buffer = new quint8[bufferSize]; 0073 qint32 bytesWritten; 0074 compressor->compressTileData(td, buffer, bufferSize, bytesWritten); 0075 dbgKrita << ppVar(bytesWritten); 0076 0077 0078 memset(td->data(), oddPixel2, TILESIZE); 0079 QVERIFY(memoryIsFilled(oddPixel2, td->data(), TILESIZE)); 0080 0081 compressor->decompressTileData(buffer, bytesWritten, td); 0082 0083 QVERIFY(memoryIsFilled(oddPixel1, td->data(), TILESIZE)); 0084 0085 delete[] buffer; 0086 tile->unlock(); 0087 } 0088 0089 void KisTileCompressorsTest::doLowLevelRoundTripIncompressible(KisAbstractTileCompressor *compressor) 0090 { 0091 const qint32 pixelSize = 1; 0092 quint8 oddPixel1 = 128; 0093 quint8 oddPixel2 = 129; 0094 0095 QFile file(QString(FILES_DATA_DIR) + QDir::separator() + "tile.png"); 0096 if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) 0097 return; 0098 0099 QByteArray incompressibleArray = file.readAll(); 0100 0101 /** 0102 * A small hack to acquire a standalone tile data. 0103 * globalTileDataStore is not exported out of kritaimage.so, 0104 * so we get it from the data manager 0105 */ 0106 KisTiledDataManager dm(pixelSize, &oddPixel1); 0107 KisTileSP tile = dm.getTile(0, 0, true); 0108 tile->lockForWrite(); 0109 0110 0111 KisTileData *td = tile->tileData(); 0112 QVERIFY(memoryIsFilled(oddPixel1, td->data(), TILESIZE)); 0113 0114 memcpy(td->data(), incompressibleArray.data(), TILESIZE); 0115 QVERIFY(!memcmp(td->data(), incompressibleArray.data(), TILESIZE)); 0116 0117 qint32 bufferSize = compressor->tileDataBufferSize(td); 0118 quint8 *buffer = new quint8[bufferSize]; 0119 qint32 bytesWritten; 0120 compressor->compressTileData(td, buffer, bufferSize, bytesWritten); 0121 dbgKrita << ppVar(bytesWritten); 0122 0123 0124 memset(td->data(), oddPixel2, TILESIZE); 0125 QVERIFY(memoryIsFilled(oddPixel2, td->data(), TILESIZE)); 0126 0127 compressor->decompressTileData(buffer, bytesWritten, td); 0128 0129 QVERIFY(!memcmp(td->data(), incompressibleArray.data(), TILESIZE)); 0130 0131 delete[] buffer; 0132 tile->unlock(); 0133 } 0134 0135 void KisTileCompressorsTest::testRoundTripLegacy() 0136 { 0137 KisAbstractTileCompressor *compressor = new KisLegacyTileCompressor(); 0138 doRoundTrip(compressor); 0139 delete compressor; 0140 } 0141 0142 void KisTileCompressorsTest::testLowLevelRoundTripLegacy() 0143 { 0144 KisAbstractTileCompressor *compressor = new KisLegacyTileCompressor(); 0145 doLowLevelRoundTrip(compressor); 0146 delete compressor; 0147 } 0148 0149 void KisTileCompressorsTest::testRoundTrip2() 0150 { 0151 KisAbstractTileCompressor *compressor = new KisTileCompressor2(); 0152 doRoundTrip(compressor); 0153 delete compressor; 0154 } 0155 0156 void KisTileCompressorsTest::testLowLevelRoundTrip2() 0157 { 0158 KisAbstractTileCompressor *compressor = new KisTileCompressor2(); 0159 doLowLevelRoundTrip(compressor); 0160 delete compressor; 0161 } 0162 0163 void KisTileCompressorsTest::testLowLevelRoundTripIncompressible2() 0164 { 0165 KisAbstractTileCompressor *compressor = new KisTileCompressor2(); 0166 doLowLevelRoundTripIncompressible(compressor); 0167 delete compressor; 0168 } 0169 0170 0171 SIMPLE_TEST_MAIN(KisTileCompressorsTest) 0172