Fix our threadsafe streambuf to accept a sink that receives all of the proper output...

This commit is contained in:
Pavel Krajcevski 2013-09-28 21:44:50 -04:00
parent f1924bd221
commit c3cb8403b5
3 changed files with 33 additions and 27 deletions

View file

@ -41,14 +41,17 @@
* <http://gamma.cs.unc.edu/FasTC/> * <http://gamma.cs.unc.edu/FasTC/>
*/ */
#include <stdlib.h> #include <cassert>
#include <stdio.h> #include <cstdlib>
#include <string.h> #include <cstdio>
#include <cstring>
#include <iostream>
#include <fstream>
#include "BlockStats.h"
#include "TexComp.h"
#include "ImageFile.h"
#include "Image.h" #include "Image.h"
#include "ImageFile.h"
#include "TexComp.h"
#include "ThreadSafeStreambuf.h"
void PrintUsage() { void PrintUsage() {
fprintf(stderr, "Usage: tc [OPTIONS] imagefile\n"); fprintf(stderr, "Usage: tc [OPTIONS] imagefile\n");
@ -210,10 +213,13 @@ int main(int argc, char **argv) {
img.SetBlockStreamOrder(false); img.SetBlockStreamOrder(false);
} }
int numBlocks = (img.GetWidth() * img.GetHeight())/16; std::ofstream logFile;
BlockStatManager *statManager = NULL; ThreadSafeStreambuf streamBuf(std::cout);
std::ostream logStream(&streamBuf);
if(bSaveLog) { if(bSaveLog) {
statManager = new BlockStatManager(numBlocks); char logname[256];
sprintf(logname, "%s.log", basename);
logFile.open(logname);
} }
SCompressionSettings settings; SCompressionSettings settings;
@ -224,7 +230,7 @@ int main(int argc, char **argv) {
settings.iQuality = quality; settings.iQuality = quality;
settings.iNumCompressions = numCompressions; settings.iNumCompressions = numCompressions;
settings.iJobSize = numJobs; settings.iJobSize = numJobs;
settings.pStatManager = statManager; settings.logStream = &logStream;
CompressedImage *ci = CompressImage(&img, settings); CompressedImage *ci = CompressImage(&img, settings);
if(NULL == ci) { if(NULL == ci) {
@ -240,12 +246,6 @@ int main(int argc, char **argv) {
fprintf(stderr, "Error computing PSNR\n"); fprintf(stderr, "Error computing PSNR\n");
} }
if(bSaveLog) {
char logname[256];
sprintf(logname, "%s.log", basename);
statManager->ToFile(logname);
}
if(format == eCompressionFormat_BPTC) { if(format == eCompressionFormat_BPTC) {
strcat(basename, "-bc7.png"); strcat(basename, "-bc7.png");
} else if(format == eCompressionFormat_PVRTC) { } else if(format == eCompressionFormat_PVRTC) {
@ -257,8 +257,8 @@ int main(int argc, char **argv) {
// Cleanup // Cleanup
delete ci; delete ci;
if(statManager) if(bSaveLog) {
delete statManager; logFile.close();
}
return 0; return 0;
} }

View file

@ -57,18 +57,20 @@
class TCMutex; class TCMutex;
#include <streambuf> #include <streambuf>
#include <iosfwd>
class ThreadSafeStreambuf : public ::std::streambuf { class ThreadSafeStreambuf : public ::std::streambuf {
public: public:
ThreadSafeStreambuf(); ThreadSafeStreambuf(std::ostream &sink);
virtual ~ThreadSafeStreambuf(); virtual ~ThreadSafeStreambuf();
protected:
virtual std::streamsize xsputn(const char_type *s, std::streamsize count); virtual std::streamsize xsputn(const char_type *s, std::streamsize count);
private: private:
// Not implemented -- not allowed... // Not implemented -- not allowed...
ThreadSafeStreambuf(const ThreadSafeStreambuf &); ThreadSafeStreambuf(const ThreadSafeStreambuf &);
ThreadSafeStreambuf &operator=(const ThreadSafeStreambuf &); ThreadSafeStreambuf &operator=(const ThreadSafeStreambuf &);
std::ostream &m_Sink;
TCMutex *m_Mutex; TCMutex *m_Mutex;
}; };

View file

@ -53,14 +53,19 @@
#include "ThreadSafeStreambuf.h" #include "ThreadSafeStreambuf.h"
#include <cstdlib> #include <cstdlib>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <sstream>
#include <string>
#include "Thread.h" #include "Thread.h"
ThreadSafeStreambuf::ThreadSafeStreambuf() ThreadSafeStreambuf::ThreadSafeStreambuf(std::ostream &sink)
: ::std::streambuf() : ::std::streambuf()
, m_Sink(sink)
, m_Mutex(new TCMutex) , m_Mutex(new TCMutex)
{ { }
}
ThreadSafeStreambuf::~ThreadSafeStreambuf() { ThreadSafeStreambuf::~ThreadSafeStreambuf() {
if(m_Mutex) { if(m_Mutex) {
@ -72,7 +77,6 @@ ThreadSafeStreambuf::~ThreadSafeStreambuf() {
::std::streamsize count) { ::std::streamsize count) {
// Lock it. // Lock it.
TCLock lock(*m_Mutex); TCLock lock(*m_Mutex);
// then just do what you would have done... ::std::streambuf *sinkStream = m_Sink.rdbuf();
return ::std::streambuf::xsputn(s, count); return sinkStream->sputn(s, count);
} }