From: <man...@us...> - 2014-09-24 14:25:59
|
Revision: 4309 http://sourceforge.net/p/modplug/code/4309 Author: manxorist Date: 2014-09-24 14:25:52 +0000 (Wed, 24 Sep 2014) Log Message: ----------- [Ref] IT Compression: Convert from FILE* to std::ostream. [Ref] test: Avoid superfluous file i/o in IT compression tests. Modified Paths: -------------- trunk/OpenMPT/soundlib/ITCompression.cpp trunk/OpenMPT/soundlib/ITCompression.h trunk/OpenMPT/soundlib/SampleIO.cpp trunk/OpenMPT/test/test.cpp Modified: trunk/OpenMPT/soundlib/ITCompression.cpp =================================================================== --- trunk/OpenMPT/soundlib/ITCompression.cpp 2014-09-24 14:25:02 UTC (rev 4308) +++ trunk/OpenMPT/soundlib/ITCompression.cpp 2014-09-24 14:25:52 UTC (rev 4309) @@ -12,6 +12,8 @@ #include <stdafx.h> #include "ITCompression.h" #include "../common/misc_util.h" +#include <ostream> +#include "../common/mptIO.h" OPENMPT_NAMESPACE_BEGIN @@ -49,8 +51,8 @@ // IT 2.14 compression -ITCompression::ITCompression(const ModSample &sample, bool it215, FILE *f) : file(f), mptSample(sample), is215(it215) -//------------------------------------------------------------------------------------------------------------------- +ITCompression::ITCompression(const ModSample &sample, bool it215, std::ostream *f) : file(f), mptSample(sample), is215(it215) +//--------------------------------------------------------------------------------------------------------------------------- { packedData = new (std::nothrow) uint8[bufferSize]; sampleData = new (std::nothrow) uint8[blockSize]; @@ -77,7 +79,7 @@ else Compress<IT8BitParams>(static_cast<const int8 *>(sample.pSample) + chn, offset, remain); - if(file) fwrite(&packedData[0], packedLength, 1, file); + if(file) mpt::IO::WriteRaw(*file, &packedData[0], packedLength); packedTotalLength += packedLength; offset += baseLength; Modified: trunk/OpenMPT/soundlib/ITCompression.h =================================================================== --- trunk/OpenMPT/soundlib/ITCompression.h 2014-09-24 14:25:02 UTC (rev 4308) +++ trunk/OpenMPT/soundlib/ITCompression.h 2014-09-24 14:25:52 UTC (rev 4309) @@ -14,6 +14,7 @@ #include <vector> #include "ModSample.h" #include "FileReader.h" +#include <iosfwd> OPENMPT_NAMESPACE_BEGIN @@ -24,7 +25,7 @@ //================= { public: - ITCompression(const ModSample &sample, bool it215, FILE *f); + ITCompression(const ModSample &sample, bool it215, std::ostream *f); size_t GetCompressedSize() const { return packedTotalLength; } static const size_t bufferSize = 2 + 0xFFFF; // Our output buffer can't be longer than this. @@ -33,7 +34,7 @@ protected: std::vector<int> bwt; // Bit width table uint8 *packedData; // Compressed data for current sample block - FILE *file; // File to which compressed data will be written (can be nullptr if you only want to find out the sample size) + std::ostream *file; // File to which compressed data will be written (can be nullptr if you only want to find out the sample size) void *sampleData; // Pre-processed sample data for currently compressed sample block const ModSample &mptSample; // Sample that is being processed size_t packedLength; // Size of currently compressed sample block Modified: trunk/OpenMPT/soundlib/SampleIO.cpp =================================================================== --- trunk/OpenMPT/soundlib/SampleIO.cpp 2014-09-24 14:25:02 UTC (rev 4308) +++ trunk/OpenMPT/soundlib/SampleIO.cpp 2014-09-24 14:25:52 UTC (rev 4309) @@ -15,6 +15,7 @@ #include "SampleIO.h" #include "SampleFormatConverters.h" #include "ITCompression.h" +#include "../common/mptIO.h" OPENMPT_NAMESPACE_BEGIN @@ -736,7 +737,8 @@ else if(GetEncoding() == IT214 || GetEncoding() == IT215) { // IT2.14-encoded samples - ITCompression its(sample, GetEncoding() == IT215, f); + mpt::FILE_ostream s(f); + ITCompression its(sample, GetEncoding() == IT215, &s); len = its.GetCompressedSize(); } Modified: trunk/OpenMPT/test/test.cpp =================================================================== --- trunk/OpenMPT/test/test.cpp 2014-09-24 14:25:02 UTC (rev 4308) +++ trunk/OpenMPT/test/test.cpp 2014-09-24 14:25:52 UTC (rev 4309) @@ -1599,28 +1599,25 @@ } -static void RunITCompressionTest(const std::vector<int8> &sampleData, ChannelFlags smpFormat, bool it215, int testcount) -//---------------------------------------------------------------------------------------------------------------------- +static void RunITCompressionTest(const std::vector<int8> &sampleData, ChannelFlags smpFormat, bool it215) +//------------------------------------------------------------------------------------------------------- { - mpt::PathString filename = GetTempFilenameBase() + MPT_PATHSTRING("itcomp") + mpt::PathString::FromWide(StringifyW(testcount)) + MPT_PATHSTRING(".raw"); ModSample smp; smp.uFlags = smpFormat; smp.pSample = const_cast<int8 *>(&sampleData[0]); smp.nLength = sampleData.size() / smp.GetBytesPerSample(); + std::string data; + { - FILE *f = mpt_fopen(filename, "wb"); - ITCompression compression(smp, it215, f); - fclose(f); + std::ostringstream f; + ITCompression compression(smp, it215, &f); + data = f.str(); } { - FILE *f = mpt_fopen(filename, "rb"); - fseek(f, 0, SEEK_END); - std::vector<int8> fileData(ftell(f), 0); - fseek(f, 0, SEEK_SET); - VERIFY_EQUAL(fread(&fileData[0], fileData.size(), 1, f), 1); + std::vector<char> fileData(data.begin(), data.end()); FileReader file(&fileData[0], fileData.size()); std::vector<int8> sampleDataNew(sampleData.size(), 0); @@ -1628,19 +1625,13 @@ ITDecompression decompression(file, smp, it215); VERIFY_EQUAL_NONCONT(memcmp(&sampleData[0], &sampleDataNew[0], sampleData.size()), 0); - fclose(f); } - RemoveFile(filename); } static noinline void TestITCompression() //-------------------------------------- { - if(!ShouldRunTests()) - { - return; - } // Test loading / saving of IT-compressed samples const int sampleDataSize = 65536; std::vector<int8> sampleData(sampleDataSize, 0); @@ -1650,15 +1641,13 @@ sampleData[i] = (int8)std::rand(); } - int testcount = 0; - // Run each compression test with IT215 compression and without. for(int i = 0; i < 2; i++) { - RunITCompressionTest(sampleData, ChannelFlags(0), i == 0, testcount++); - RunITCompressionTest(sampleData, CHN_16BIT, i == 0, testcount++); - RunITCompressionTest(sampleData, CHN_STEREO, i == 0, testcount++); - RunITCompressionTest(sampleData, CHN_16BIT | CHN_STEREO, i == 0, testcount++); + RunITCompressionTest(sampleData, ChannelFlags(0), i == 0); + RunITCompressionTest(sampleData, CHN_16BIT, i == 0); + RunITCompressionTest(sampleData, CHN_STEREO, i == 0); + RunITCompressionTest(sampleData, CHN_16BIT | CHN_STEREO, i == 0); } } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |