Initial decoupling of base library from core library. Includes a few formatting changes as well.

This commit is contained in:
Pavel Krajcevski 2013-09-13 19:36:37 -04:00
parent 2540032acc
commit 28cf254fe5
32 changed files with 492 additions and 408 deletions

View file

@ -40,106 +40,13 @@
# #
# <http://gamma.cs.unc.edu/FasTC/> # <http://gamma.cs.unc.edu/FasTC/>
INCLUDE_DIRECTORIES(${FasTC_SOURCE_DIR}/Core/include) INCLUDE_DIRECTORIES(${FasTC_SOURCE_DIR}/Base/include)
INCLUDE_DIRECTORIES(${FasTC_SOURCE_DIR}/BPTCEncoder/include) INCLUDE_DIRECTORIES(${FasTC_SOURCE_DIR}/BPTCEncoder/include)
INCLUDE_DIRECTORIES(${FasTC_BINARY_DIR}/BPTCEncoder/include) INCLUDE_DIRECTORIES(${FasTC_BINARY_DIR}/BPTCEncoder/include)
INCLUDE(CheckCXXSourceRuns) INCLUDE(CheckCXXSourceRuns)
#SET(OLD_CMAKE_REQUIRED_FLAGS ${CMAKE_REQUIRED_FLAGS})
#IF(CMAKE_COMPILER_IS_GNUCC)
#
# # Test whether or not the compiler allows inline
# # assmbly...
# CHECK_CXX_SOURCE_RUNS("
# \#ifdef _MSC_VER
# int main() {
# int x = 1, y = 0;
# __asm {
# mov eax, x
# mov y, eax
# }
# return !y;
# }
# \#else
# int main() {
# int x = 1, y = 0;
# __asm__ (\"movl %1, %%eax;\"
# \"movl %%eax, %0;\"
# :\"=r\"(y) /* output */
# :\"r\"(x) /* input */
# :\"%eax\" /* clobbered register */
# );
#
# return !y;
# }
# \#endif"
# HAS_INLINE_ASSEMBLY
# )
#
# # If the compiler doesn't allow it, then try with a
# # compiler flag...
# IF( NOT HAS_INLINE_ASSEMBLY )
# SET(CMAKE_REQUIRED_FLAGS -fasm-blocks)
# CHECK_CXX_SOURCE_RUNS("
# \#ifdef _MSC_VER
# int main() {
# int x = 1, y = 0;
# __asm {
# mov eax, x
# mov y, eax
# }
# return !y;
# }
# \#else
# int main() {
# int x = 1, y = 0;
# __asm__ (\"movl %1, %%eax;\"
# \"movl %%eax, %0;\"
# :\"=r\"(y) /* output */
# :\"r\"(x) /* input */
# :\"%eax\" /* clobbered register */
# );
#
# return !y;
# }
# \#endif"
# HAS_INLINE_ASSEMBLY_WITH_FLAGS
# )
# ENDIF()
#
# SET(CMAKE_REQUIRED_FLAGS -msse4.1)
# CHECK_CXX_SOURCE_RUNS("
# #include <smmintrin.h>
# int main() {
# const __m128 fv = _mm_set1_ps(1.0f);
# const __m128 fv2 = _mm_set1_ps(2.0f);
#
# const __m128 ans = _mm_blend_ps(fv, fv2, 2);
#
# return ((int *)(&ans))[0];
# }"
# HAS_SSE_41
# )
#
# IF(HAS_SSE_41)
# SET(CMAKE_REQUIRED_FLAGS -msse4.2)
# CHECK_CXX_SOURCE_RUNS("
# #include <smmintrin.h>
# int main() {
# const unsigned int testMe = 5;
# return !(2 == _mm_popcnt_u32(testMe));
# }"
# HAS_SSE_POPCNT
# )
# ENDIF(HAS_SSE_41)
#
#ELSEIF(MSVC)
##!FIXME!
#ENDIF()
#SET(CMAKE_REQUIRED_FLAGS ${OLD_CMAKE_REQUIRED_FLAGS})
IF( NOT HAS_INLINE_ASSEMBLY AND NOT HAS_INLINE_ASSEMBLY_WITH_FLAGS ) IF( NOT HAS_INLINE_ASSEMBLY AND NOT HAS_INLINE_ASSEMBLY_WITH_FLAGS )
SET( NO_INLINE_ASSEMBLY true ) SET( NO_INLINE_ASSEMBLY true )
ENDIF() ENDIF()
@ -178,55 +85,56 @@ ELSEIF( MSVC )
) )
ENDIF() ENDIF()
IF( HAS_MSVC_ATOMICS OR HAS_GCC_ATOMICS ) IF( HAS_MSVC_ATOMICS OR HAS_GCC_ATOMICS )
SET(HAS_ATOMICS true) SET(HAS_ATOMICS true)
ENDIF() ENDIF()
CONFIGURE_FILE( CONFIGURE_FILE(
"config/BC7Config.h.in" "config/BC7Config.h.in"
"include/BC7Config.h" "include/BC7Config.h"
) )
SET( HEADERS SET( HEADERS
src/BC7CompressionMode.h src/BC7CompressionMode.h
src/BitStream.h src/BitStream.h
src/RGBAEndpoints.h src/RGBAEndpoints.h
src/ParallelStage.h src/ParallelStage.h
) )
SET( SOURCES SET( SOURCES
src/BC7Compressor.cpp src/BC7Compressor.cpp
src/RGBAEndpoints.cpp src/RGBAEndpoints.cpp
src/ParallelStage.cpp src/ParallelStage.cpp
) )
IF( HAS_SSE_41 ) IF( HAS_SSE_41 )
IF ( HAS_SSE_POPCNT ) IF ( HAS_SSE_POPCNT )
IF( MSVC ) IF( MSVC )
ADD_DEFINITIONS( /arch:SSE4.2 ) ADD_DEFINITIONS( /arch:SSE4.2 )
ELSE() #Assume GCC ELSE() #Assume GCC
ADD_DEFINITIONS( -msse4.2 ) ADD_DEFINITIONS( -msse4.2 )
ENDIF()
ELSE()
IF( MSVC )
ADD_DEFINITIONS( /arch:SSE4.1 )
ELSE() #Assume GCC
ADD_DEFINITIONS( -msse4.1 )
ENDIF()
ENDIF() ENDIF()
ELSE()
IF( MSVC )
ADD_DEFINITIONS( /arch:SSE4.1 )
ELSE() #Assume GCC
ADD_DEFINITIONS( -msse4.1 )
ENDIF()
ENDIF()
SET( HEADERS SET( HEADERS
${HEADERS} ${HEADERS}
src/RGBAEndpointsSIMD.h src/RGBAEndpointsSIMD.h
src/BC7CompressionModeSIMD.h src/BC7CompressionModeSIMD.h
) )
SET( SOURCES SET( SOURCES
${SOURCES} ${SOURCES}
src/BC7CompressorSIMD.cpp src/BC7CompressorSIMD.cpp
src/RGBAEndpointsSIMD.cpp src/RGBAEndpointsSIMD.cpp
) )
ENDIF( HAS_SSE_41 ) ENDIF( HAS_SSE_41 )
IF( HAS_INLINE_ASSEMBLY_WITH_FLAGS ) IF( HAS_INLINE_ASSEMBLY_WITH_FLAGS )
@ -238,6 +146,8 @@ IF( HAS_INLINE_ASSEMBLY_WITH_FLAGS )
ENDIF() ENDIF()
ADD_LIBRARY( BPTCEncoder ADD_LIBRARY( BPTCEncoder
${HEADERS} ${HEADERS}
${SOURCES} ${SOURCES}
) )
TARGET_LINK_LIBRARIES( BPTCEncoder FasTCBase )

View file

@ -79,7 +79,7 @@
#include "BC7Config.h" #include "BC7Config.h"
#include "CompressionJob.h" #include "CompressionJob.h"
class BlockStatManager; #include <iosfwd>
namespace BC7C { namespace BC7C {
// This is the error metric that is applied to our error measurement algorithm // This is the error metric that is applied to our error measurement algorithm
@ -120,7 +120,7 @@ namespace BC7C {
// made into a list of statistics. We can use this to see whether or not // made into a list of statistics. We can use this to see whether or not
// certain heuristics are working, such as whether or not certain modes are // certain heuristics are working, such as whether or not certain modes are
// being chosen more often than others, etc. // being chosen more often than others, etc.
void CompressWithStats(const CompressionJob &, BlockStatManager &statManager); void CompressWithStats(const CompressionJob &, std::ostream *logStream);
#ifdef HAS_SSE_41 #ifdef HAS_SSE_41
// Compress the image given as RGBA data to BC7 format using an algorithm // Compress the image given as RGBA data to BC7 format using an algorithm

View file

@ -76,14 +76,11 @@
#include "BC7Compressor.h" #include "BC7Compressor.h"
#include "BC7CompressionMode.h" #include "BC7CompressionMode.h"
#include "TexComp.h"
#include "TexCompTypes.h" #include "TexCompTypes.h"
#include "BCLookupTables.h" #include "BCLookupTables.h"
#include "RGBAEndpoints.h" #include "RGBAEndpoints.h"
#include "BitStream.h" #include "BitStream.h"
#include "BlockStats.h"
#ifdef HAS_MSVC_ATOMICS #ifdef HAS_MSVC_ATOMICS
# include "Windows.h" # include "Windows.h"
#endif #endif
@ -100,6 +97,7 @@
#include <cassert> #include <cassert>
#include <cfloat> #include <cfloat>
#include <ctime> #include <ctime>
#include <iostream>
// #define USE_PCA_FOR_SHAPE_ESTIMATION // #define USE_PCA_FOR_SHAPE_ESTIMATION
@ -1543,10 +1541,27 @@ namespace BC7C {
const float *GetErrorMetric() { return kErrorMetrics[GetErrorMetricEnum()]; } const float *GetErrorMetric() { return kErrorMetrics[GetErrorMetricEnum()]; }
ErrorMetric GetErrorMetricEnum() { return gErrorMetric; } ErrorMetric GetErrorMetricEnum() { return gErrorMetric; }
class BlockLogger {
public:
BlockLogger(uint32 blockIdx, std::ostream &os)
: m_BlockIdx(blockIdx), m_Stream(os) { }
template<typename T>
friend std::ostream &operator<<(const BlockLogger &bl, const T &v);
uint32 m_BlockIdx;
std::ostream &m_Stream;
};
template<typename T>
std::ostream &operator<<(const BlockLogger &bl, const T &v) {
return bl.m_Stream << bl.m_BlockIdx << ": " << v;
}
// Function prototypes // Function prototypes
static void CompressBC7Block(const uint32 *block, uint8 *outBuf); static void CompressBC7Block(const uint32 *block, uint8 *outBuf);
static void CompressBC7Block( static void CompressBC7Block(
const uint32 *block, uint8 *outBuf, BlockStatManager &statManager const uint32 *block, uint8 *outBuf, const BlockLogger &logStream
); );
static int gQualityLevel = 50; static int gQualityLevel = 50;
@ -1710,17 +1725,20 @@ namespace BC7C {
} }
#endif // HAS_ATOMICS #endif // HAS_ATOMICS
void CompressWithStats( void CompressWithStats(const CompressionJob &cj, std::ostream *logStream) {
const CompressionJob &cj,
BlockStatManager &statManager
) {
const unsigned char *inBuf = cj.inBuf; const unsigned char *inBuf = cj.inBuf;
unsigned char *outBuf = cj.outBuf; unsigned char *outBuf = cj.outBuf;
for(uint32 j = 0; j < cj.height; j += 4) { for(uint32 j = 0; j < cj.height; j += 4) {
for(uint32 i = 0; i < cj.width; i += 4) { for(uint32 i = 0; i < cj.width; i += 4) {
CompressBC7Block((const uint32 *)inBuf, outBuf, statManager); const uint32 *pixelBuf = reinterpret_cast<const uint32 *>(inBuf);
if(logStream) {
uint32 blockIdx = (j/4) * (cj.width/4) + (i/4);
CompressBC7Block(pixelBuf, outBuf, BlockLogger(blockIdx, *logStream));
} else {
CompressBC7Block(pixelBuf, outBuf);
}
#ifndef NDEBUG #ifndef NDEBUG
uint8 *block = outBuf; uint8 *block = outBuf;
@ -2203,23 +2221,27 @@ namespace BC7C {
} }
} }
template<typename T>
static void PrintStat(const BlockLogger &lgr, const char *stat, const T &v) {
lgr << stat << " -- " << v << std::endl;
}
// Compress a single block but collect statistics as well... // Compress a single block but collect statistics as well...
static void CompressBC7Block( static void CompressBC7Block(
const uint32 *block, uint8 *outBuf, BlockStatManager &statManager const uint32 *block, uint8 *outBuf, const BlockLogger &logStream
) { ) {
class RAIIStatSaver { class RAIIStatSaver {
private: private:
uint32 m_BlockIdx; const BlockLogger &m_Logger;
BlockStatManager &m_BSM;
int *m_ModePtr; int *m_ModePtr;
double *m_Estimates; double *m_Estimates;
double *m_Errors; double *m_Errors;
public: public:
RAIIStatSaver(uint32 blockIdx, BlockStatManager &m) RAIIStatSaver(const BlockLogger &logger)
: m_BlockIdx(blockIdx), m_BSM(m) : m_Logger(logger)
, m_ModePtr(NULL), m_Estimates(NULL), m_Errors(NULL) { } , m_ModePtr(NULL), m_Estimates(NULL), m_Errors(NULL) { }
void SetMode(int *modePtr) { m_ModePtr = modePtr; } void SetMode(int *modePtr) { m_ModePtr = modePtr; }
void SetEstimates(double *estimates) { m_Estimates = estimates; } void SetEstimates(double *estimates) { m_Estimates = estimates; }
@ -2231,20 +2253,16 @@ namespace BC7C {
assert(m_Estimates); assert(m_Estimates);
assert(m_Errors); assert(m_Errors);
BlockStat s (kBlockStatString[eBlockStat_Mode], *m_ModePtr); PrintStat(m_Logger, kBlockStatString[eBlockStat_Mode], *m_ModePtr);
m_BSM.AddStat(m_BlockIdx, s);
for(uint32 i = 0; i < BC7CompressionMode::kNumModes; i++) { for(uint32 i = 0; i < BC7CompressionMode::kNumModes; i++) {
s = BlockStat( PrintStat(m_Logger,
kBlockStatString[eBlockStat_ModeZeroEstimate + i], m_Estimates[i] kBlockStatString[eBlockStat_ModeZeroEstimate + i],
); m_Estimates[i]);
m_BSM.AddStat(m_BlockIdx, s); PrintStat(m_Logger,
kBlockStatString[eBlockStat_ModeZeroError + i],
s = BlockStat( m_Errors[i]);
kBlockStatString[eBlockStat_ModeZeroError + i], m_Errors[i]
);
m_BSM.AddStat(m_BlockIdx, s);
} }
} }
}; };
@ -2259,12 +2277,7 @@ namespace BC7C {
modeError[i] = modeEstimate[i] = -1.0; modeError[i] = modeEstimate[i] = -1.0;
} }
uint32 blockIdx = statManager.BeginBlock(); RAIIStatSaver __statsaver__(logStream);
for(int i = 0; i < kNumBlockStats; i++) {
statManager.AddStat(blockIdx, BlockStat(kBlockStatString[i], 0));
}
RAIIStatSaver __statsaver__(blockIdx, statManager);
__statsaver__.SetMode(&bestMode); __statsaver__.SetMode(&bestMode);
__statsaver__.SetEstimates(modeEstimate); __statsaver__.SetEstimates(modeEstimate);
__statsaver__.SetErrors(modeError); __statsaver__.SetErrors(modeError);
@ -2275,9 +2288,7 @@ namespace BC7C {
CompressOptimalColorBC7(*block, bStrm); CompressOptimalColorBC7(*block, bStrm);
bestMode = 5; bestMode = 5;
BlockStat s = BlockStat(kBlockStatString[eBlockStat_Path], 0); PrintStat(logStream, kBlockStatString[eBlockStat_Path], 0);
statManager.AddStat(blockIdx, s);
return; return;
} }
@ -2303,9 +2314,7 @@ namespace BC7C {
WriteTransparentBlock(bStrm); WriteTransparentBlock(bStrm);
bestMode = 6; bestMode = 6;
BlockStat s = BlockStat(kBlockStatString[eBlockStat_Path], 1); PrintStat(logStream, kBlockStatString[eBlockStat_Path], 1);
statManager.AddStat(blockIdx, s);
return; return;
} }
@ -2334,8 +2343,7 @@ namespace BC7C {
error = 1.0; error = 1.0;
} }
BlockStat s (kBlockStatString[eBlockStat_SingleShapeEstimate], error); PrintStream(logStream, kBlockStatString[eBlockStat_SingleShapeEstimate], error);
statManager.AddStat(blockIdx, s);
#endif #endif
} }
} }
@ -2380,10 +2388,9 @@ namespace BC7C {
} }
if(err < bestError[0]) { if(err < bestError[0]) {
BlockStat s = BlockStat( PrintStat(logStream,
kBlockStatString[eBlockStat_TwoShapeEstimate], err kBlockStatString[eBlockStat_TwoShapeEstimate], err
); );
statManager.AddStat(blockIdx, s);
} }
// If it's small, we'll take it! // If it's small, we'll take it!
@ -2394,8 +2401,7 @@ namespace BC7C {
); );
bestMode = modeChosen; bestMode = modeChosen;
BlockStat s = BlockStat(kBlockStatString[eBlockStat_Path], 2); PrintStat(logStream, kBlockStatString[eBlockStat_Path], 2);
statManager.AddStat(blockIdx, s);
return; return;
} }
@ -2445,10 +2451,9 @@ namespace BC7C {
} }
if(err < bestError[1]) { if(err < bestError[1]) {
BlockStat s = BlockStat( PrintStat(logStream,
kBlockStatString[eBlockStat_ThreeShapeEstimate], err kBlockStatString[eBlockStat_ThreeShapeEstimate], err
); );
statManager.AddStat(blockIdx, s);
} }
// If it's small, we'll take it! // If it's small, we'll take it!
@ -2459,9 +2464,7 @@ namespace BC7C {
); );
bestMode = modeChosen; bestMode = modeChosen;
BlockStat s = BlockStat(kBlockStatString[eBlockStat_Path], 2); PrintStat(logStream, kBlockStatString[eBlockStat_Path], 2);
statManager.AddStat(blockIdx, s);
return; return;
} }
@ -2475,8 +2478,7 @@ namespace BC7C {
} }
} }
BlockStat s = BlockStat(kBlockStatString[eBlockStat_Path], 3); PrintStat(logStream, kBlockStatString[eBlockStat_Path], 3);
statManager.AddStat(blockIdx, s);
uint8 tempBuf1[16], tempBuf2[16]; uint8 tempBuf1[16], tempBuf2[16];

72
Base/CMakeLists.txt Normal file
View file

@ -0,0 +1,72 @@
# FasTC
# Copyright (c) 2013 University of North Carolina at Chapel Hill.
# All rights reserved.
#
# Permission to use, copy, modify, and distribute this software and its
# documentation for educational, research, and non-profit purposes, without
# fee, and without a written agreement is hereby granted, provided that the
# above copyright notice, this paragraph, and the following four paragraphs
# appear in all copies.
#
# Permission to incorporate this software into commercial products may be
# obtained by contacting the authors or the Office of Technology Development
# at the University of North Carolina at Chapel Hill <otd@unc.edu>.
#
# This software program and documentation are copyrighted by the University of
# North Carolina at Chapel Hill. The software program and documentation are
# supplied "as is," without any accompanying services from the University of
# North Carolina at Chapel Hill or the authors. The University of North
# Carolina at Chapel Hill and the authors do not warrant that the operation of
# the program will be uninterrupted or error-free. The end-user understands
# that the program was developed for research purposes and is advised not to
# rely exclusively on the program for any reason.
#
# IN NO EVENT SHALL THE UNIVERSITY OF NORTH CAROLINA AT CHAPEL HILL OR THE
# AUTHORS BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL,
# OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS, ARISING OUT OF THE USE OF
# THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF NORTH CAROLINA
# AT CHAPEL HILL OR THE AUTHORS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH
# DAMAGE.
#
# THE UNIVERSITY OF NORTH CAROLINA AT CHAPEL HILL AND THE AUTHORS SPECIFICALLY
# DISCLAIM ANY WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE AND ANY
# STATUTORY WARRANTY OF NON-INFRINGEMENT. THE SOFTWARE PROVIDED HEREUNDER IS ON
# AN "AS IS" BASIS, AND THE UNIVERSITY OF NORTH CAROLINA AT CHAPEL HILL AND
# THE AUTHORS HAVE NO OBLIGATIONS TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
# ENHANCEMENTS, OR MODIFICATIONS.
#
# Please send all BUG REPORTS to <pavel@cs.unc.edu>.
#
# The authors may be contacted via:
#
# Pavel Krajcevski
# Dept of Computer Science
# 201 S Columbia St
# Frederick P. Brooks, Jr. Computer Science Bldg
# Chapel Hill, NC 27599-3175
# USA
#
# <http://gamma.cs.unc.edu/FasTC/>
SET( SOURCES
"src/Image.cpp"
"src/CompressionJob.cpp"
)
SET( HEADERS
"include/TexCompTypes.h"
"include/Image.h"
"include/CompressionJob.h"
)
INCLUDE_DIRECTORIES(${FasTC_SOURCE_DIR}/Base/include)
ADD_LIBRARY( FasTCBase
${HEADERS}
${SOURCES}
)
IF( NOT WIN32 AND NOT APPLE )
TARGET_LINK_LIBRARIES( FasTCBase rt )
ENDIF()

View file

@ -45,27 +45,17 @@
#define __TEXCOMP_IMAGE_H__ #define __TEXCOMP_IMAGE_H__
#include "TexCompTypes.h" #include "TexCompTypes.h"
#include "TexComp.h"
// Forward declarations
class ImageLoader;
// Class definition // Class definition
class Image { class Image {
public: public:
Image(const CompressedImage &);
Image(const ImageLoader &);
Image(uint32 width, uint32 height, const uint32 *pixels); Image(uint32 width, uint32 height, const uint32 *pixels);
~Image();
Image(const Image &); Image(const Image &);
Image &operator=(const Image &); Image &operator=(const Image &);
virtual ~Image();
const uint8 *RawData() const { return m_PixelData; } const uint8 *RawData() const { return m_Data; }
CompressedImage *Compress(const SCompressionSettings &settings) const;
double ComputePSNR(const CompressedImage &ci) const;
uint32 GetWidth() const { return m_Width; } uint32 GetWidth() const { return m_Width; }
uint32 GetHeight() const { return m_Height; } uint32 GetHeight() const { return m_Height; }
@ -73,13 +63,21 @@ class Image {
void SetBlockStreamOrder(bool flag) { m_bBlockStreamOrder = flag; } void SetBlockStreamOrder(bool flag) { m_bBlockStreamOrder = flag; }
bool GetBlockStreamOrder() const { return m_bBlockStreamOrder; } bool GetBlockStreamOrder() const { return m_bBlockStreamOrder; }
double ComputePSNR(Image *other);
virtual void ComputeRGBA() { }
virtual const uint32 *GetRGBA() const {
return reinterpret_cast<const uint32 *>(RawData());
}
private: private:
uint32 m_Width; uint32 m_Width;
uint32 m_Height; uint32 m_Height;
bool m_bBlockStreamOrder; bool m_bBlockStreamOrder;
uint8 *m_PixelData; protected:
uint8 *m_Data;
}; };
#endif // __TEXCOMP_IMAGE_H__ #endif // __TEXCOMP_IMAGE_H__

View file

@ -42,13 +42,12 @@
*/ */
#include "Image.h" #include "Image.h"
#include "ImageLoader.h"
#include <stdlib.h> #include <cstdlib>
#include <stdio.h> #include <cstdio>
#include <string.h> #include <cstring>
#include <assert.h> #include <cassert>
#include <math.h> #include <cmath>
template <typename T> template <typename T>
static inline T sad( const T &a, const T &b ) { static inline T sad( const T &a, const T &b ) {
@ -59,12 +58,11 @@ Image::Image(const Image &other)
: m_Width(other.m_Width) : m_Width(other.m_Width)
, m_Height(other.m_Height) , m_Height(other.m_Height)
, m_bBlockStreamOrder(other.GetBlockStreamOrder()) , m_bBlockStreamOrder(other.GetBlockStreamOrder())
, m_PixelData(new uint8[m_Width * m_Height * 4]) , m_Data(new uint8[m_Width * m_Height * 4])
{ {
if(m_PixelData) { if(m_Data) {
memcpy(m_PixelData, other.m_PixelData, m_Width * m_Height * 4); memcpy(m_Data, other.m_Data, m_Width * m_Height * 4);
} } else {
else {
fprintf(stderr, "Out of memory!\n"); fprintf(stderr, "Out of memory!\n");
} }
} }
@ -73,10 +71,13 @@ Image::Image(uint32 width, uint32 height, const uint32 *pixels)
: m_Width(width) : m_Width(width)
, m_Height(height) , m_Height(height)
, m_bBlockStreamOrder(false) , m_bBlockStreamOrder(false)
, m_PixelData(new uint8[4 * m_Width * m_Height])
{ {
if(m_PixelData && pixels) if(pixels) {
memcpy(m_PixelData, pixels, m_Width * m_Height * sizeof(uint32)); m_Data = new uint8[4 * m_Width * m_Height];
memcpy(m_Data, pixels, m_Width * m_Height * sizeof(uint32));
} else {
m_Data = NULL;
}
} }
@ -86,110 +87,68 @@ Image &Image::operator=(const Image &other) {
m_Height = other.m_Height; m_Height = other.m_Height;
m_bBlockStreamOrder = other.GetBlockStreamOrder(); m_bBlockStreamOrder = other.GetBlockStreamOrder();
if(m_PixelData) { if(m_Data) {
delete [] m_PixelData; delete [] m_Data;
} }
if(other.m_PixelData) { if(other.m_Data) {
m_PixelData = new uint8[m_Width * m_Height * 4]; m_Data = new uint8[m_Width * m_Height * 4];
if(m_PixelData) if(m_Data)
memcpy(m_PixelData, other.m_PixelData, m_Width * m_Height * 4); memcpy(m_Data, other.m_Data, m_Width * m_Height * 4);
else else
fprintf(stderr, "Out of memory!\n"); fprintf(stderr, "Out of memory!\n");
} }
else { else {
m_PixelData = other.m_PixelData; m_Data = other.m_Data;
} }
return *this; return *this;
} }
Image::Image(const CompressedImage &ci)
: m_Width(ci.GetWidth())
, m_Height(ci.GetHeight())
, m_bBlockStreamOrder(true)
{
unsigned int bufSz = ci.GetWidth() * ci.GetHeight() * 4;
m_PixelData = new uint8[ bufSz ];
if(!m_PixelData) { fprintf(stderr, "%s\n", "Out of memory!"); return; }
if(!ci.DecompressImage(m_PixelData, bufSz)) {
fprintf(stderr, "Error decompressing image!\n");
return;
}
}
Image::Image(const ImageLoader &loader)
: m_Width(loader.GetWidth())
, m_Height(loader.GetHeight())
, m_bBlockStreamOrder(true)
, m_PixelData(0)
{
if(loader.GetImageData()) {
m_PixelData = new uint8[ loader.GetImageDataSz() ];
if(!m_PixelData) { fprintf(stderr, "%s\n", "Out of memory!"); return; }
memcpy(m_PixelData, loader.GetImageData(), loader.GetImageDataSz());
}
else {
fprintf(stderr, "%s\n", "Failed to get data from image loader!");
}
}
Image::~Image() { Image::~Image() {
if(m_PixelData) { if(m_Data) {
delete [] m_PixelData; delete [] m_Data;
m_PixelData = 0; m_Data = 0;
} }
} }
CompressedImage *Image::Compress(const SCompressionSettings &settings) const { double Image::ComputePSNR(Image *other) {
CompressedImage *outImg = NULL; if(!other)
const unsigned int dataSz = GetWidth() * GetHeight() * 4; return -1.0;
assert(dataSz > 0); if(other->GetWidth() != GetWidth() ||
other->GetHeight() != GetHeight()) {
// Allocate data based on the compression method return -1.0;
int cmpDataSz = 0;
switch(settings.format) {
default: assert(!"Not implemented!"); // Fall Through V
case eCompressionFormat_DXT1: cmpDataSz = dataSz / 8; break;
case eCompressionFormat_DXT5: cmpDataSz = dataSz / 4; break;
case eCompressionFormat_BPTC: cmpDataSz = dataSz / 4; break;
} }
unsigned char *cmpData = new unsigned char[cmpDataSz]; // Compute raw 8-bit RGBA data...
CompressImageData(m_PixelData, dataSz, cmpData, cmpDataSz, settings); other->ComputeRGBA();
ComputeRGBA();
outImg = new CompressedImage(GetWidth(), GetHeight(), settings.format, cmpData); const uint8 *ourData =
reinterpret_cast<const uint8 *>(GetRGBA());
delete [] cmpData; const uint8 *otherData =
return outImg; reinterpret_cast<const uint8 *>(other->GetRGBA());
}
double Image::ComputePSNR(const CompressedImage &ci) const {
unsigned int imageSz = 4 * GetWidth() * GetHeight();
unsigned char *unCompData = new unsigned char[imageSz];
if(!(ci.DecompressImage(unCompData, imageSz))) {
fprintf(stderr, "%s\n", "Failed to decompress image.");
delete [] unCompData;
return -1.0f;
}
const double wr = 1.0; const double wr = 1.0;
const double wg = 1.0; const double wg = 1.0;
const double wb = 1.0; const double wb = 1.0;
double MSE = 0.0; double MSE = 0.0;
const uint32 imageSz = GetWidth() * GetHeight() * 4;
for(uint32 i = 0; i < imageSz; i+=4) { for(uint32 i = 0; i < imageSz; i+=4) {
const unsigned char *pixelDataRaw = m_PixelData + i; const unsigned char *ourPixel = ourData + i;
const unsigned char *pixelDataUncomp = unCompData + i; const unsigned char *otherPixel = otherData + i;
double rawAlphaScale = double(pixelDataRaw[3]) / 255.0; double ourAlphaScale = double(ourPixel[3]) / 255.0;
double uncompAlphaScale = double(pixelDataUncomp[3]) / 255.0; double otherAlphaScale = double(otherPixel[3]) / 255.0;
double dr = double(sad(rawAlphaScale * pixelDataRaw[0], uncompAlphaScale * pixelDataUncomp[0])) * wr; double dr = double(sad(ourAlphaScale * ourPixel[0],
double dg = double(sad(rawAlphaScale * pixelDataRaw[1], uncompAlphaScale * pixelDataUncomp[1])) * wg; otherAlphaScale * otherPixel[0])) * wr;
double db = double(sad(rawAlphaScale * pixelDataRaw[2], uncompAlphaScale * pixelDataUncomp[2])) * wb; double dg = double(sad(ourAlphaScale * ourPixel[1],
otherAlphaScale * otherPixel[1])) * wg;
double db = double(sad(ourAlphaScale * ourPixel[2],
otherAlphaScale * otherPixel[2])) * wb;
const double pixelMSE = const double pixelMSE =
(double(dr) * double(dr)) + (double(dr) * double(dr)) +
@ -208,8 +167,5 @@ double Image::ComputePSNR(const CompressedImage &ci) const {
(255.0 * wb) * (255.0 * wb); (255.0 * wb) * (255.0 * wb);
double PSNR = 10 * log10(MAXI/MSE); double PSNR = 10 * log10(MAXI/MSE);
// Cleanup
delete unCompData;
return PSNR; return PSNR;
} }

View file

@ -46,6 +46,7 @@ ELSE()
SET( SOURCES "src/clunix.cpp" ) SET( SOURCES "src/clunix.cpp" )
ENDIF() ENDIF()
INCLUDE_DIRECTORIES( ${FasTC_SOURCE_DIR}/Base/include )
INCLUDE_DIRECTORIES( ${FasTC_SOURCE_DIR}/Core/include ) INCLUDE_DIRECTORIES( ${FasTC_SOURCE_DIR}/Core/include )
INCLUDE_DIRECTORIES( ${FasTC_SOURCE_DIR}/IO/include ) INCLUDE_DIRECTORIES( ${FasTC_SOURCE_DIR}/IO/include )
@ -61,5 +62,6 @@ IF( MSVC )
ENDIF() ENDIF()
TARGET_LINK_LIBRARIES( tc BPTCEncoder ) TARGET_LINK_LIBRARIES( tc BPTCEncoder )
TARGET_LINK_LIBRARIES( tc FasTCBase )
TARGET_LINK_LIBRARIES( tc FasTCIO ) TARGET_LINK_LIBRARIES( tc FasTCIO )
TARGET_LINK_LIBRARIES( tc FasTCCore ) TARGET_LINK_LIBRARIES( tc FasTCCore )

View file

@ -186,9 +186,9 @@ int main(int argc, char **argv) {
return 1; return 1;
} }
const Image *img = file.GetImage(); Image img = Image(*file.GetImage());
int numBlocks = (img->GetWidth() * img->GetHeight())/16; int numBlocks = (img.GetWidth() * img.GetHeight())/16;
BlockStatManager *statManager = NULL; BlockStatManager *statManager = NULL;
if(bSaveLog) { if(bSaveLog) {
statManager = new BlockStatManager(numBlocks); statManager = new BlockStatManager(numBlocks);
@ -203,13 +203,13 @@ int main(int argc, char **argv) {
settings.iJobSize = numJobs; settings.iJobSize = numJobs;
settings.pStatManager = statManager; settings.pStatManager = statManager;
CompressedImage *ci = img->Compress(settings); CompressedImage *ci = CompressImage(&img, settings);
if(NULL == ci) { if(NULL == ci) {
fprintf(stderr, "Error compressing image!\n"); fprintf(stderr, "Error compressing image!\n");
return 1; return 1;
} }
double PSNR = img->ComputePSNR(*ci); double PSNR = img.ComputePSNR(ci);
if(PSNR > 0.0) { if(PSNR > 0.0) {
fprintf(stdout, "PSNR: %.3f\n", PSNR); fprintf(stdout, "PSNR: %.3f\n", PSNR);
} }
@ -223,7 +223,7 @@ int main(int argc, char **argv) {
statManager->ToFile(logname); statManager->ToFile(logname);
} }
Image cImg (*ci); Image cImg(*ci);
ImageFile cImgFile (strcat(basename, "-bc7.png"), eFileFormat_PNG, cImg); ImageFile cImgFile (strcat(basename, "-bc7.png"), eFileFormat_PNG, cImg);
cImgFile.Write(); cImgFile.Write();

View file

@ -93,7 +93,7 @@ SET(CMAKE_MODULE_PATH "${FasTC_SOURCE_DIR}/CMakeModules" ${CMAKE_MODULE_PATH})
FIND_PACKAGE(PVRTexLib) FIND_PACKAGE(PVRTexLib)
SET(FASTC_DIRECTORIES SET(FASTC_DIRECTORIES
BPTCEncoder PVRTCEncoder IO Core BPTCEncoder PVRTCEncoder IO Core Base
) )
FOREACH(DIR ${FASTC_DIRECTORIES}) FOREACH(DIR ${FASTC_DIRECTORIES})

View file

@ -1,30 +1,39 @@
# FasTC # FasTC
# Copyright (c) 2012 University of North Carolina at Chapel Hill. All rights reserved. # Copyright (c) 2013 University of North Carolina at Chapel Hill.
# All rights reserved.
# #
# Permission to use, copy, modify, and distribute this software and its documentation for educational, # Permission to use, copy, modify, and distribute this software and its
# research, and non-profit purposes, without fee, and without a written agreement is hereby granted, # documentation for educational, research, and non-profit purposes, without
# provided that the above copyright notice, this paragraph, and the following four paragraphs appear # fee, and without a written agreement is hereby granted, provided that the
# in all copies. # above copyright notice, this paragraph, and the following four paragraphs
# appear in all copies.
# #
# Permission to incorporate this software into commercial products may be obtained by contacting the # Permission to incorporate this software into commercial products may be
# authors or the Office of Technology Development at the University of North Carolina at Chapel Hill <otd@unc.edu>. # obtained by contacting the authors or the Office of Technology Development
# at the University of North Carolina at Chapel Hill <otd@unc.edu>.
# #
# This software program and documentation are copyrighted by the University of North Carolina at Chapel Hill. # This software program and documentation are copyrighted by the University of
# The software program and documentation are supplied "as is," without any accompanying services from the # North Carolina at Chapel Hill. The software program and documentation are
# University of North Carolina at Chapel Hill or the authors. The University of North Carolina at Chapel Hill # supplied "as is," without any accompanying services from the University of
# and the authors do not warrant that the operation of the program will be uninterrupted or error-free. The # North Carolina at Chapel Hill or the authors. The University of North
# end-user understands that the program was developed for research purposes and is advised not to rely # Carolina at Chapel Hill and the authors do not warrant that the operation of
# exclusively on the program for any reason. # the program will be uninterrupted or error-free. The end-user understands
# that the program was developed for research purposes and is advised not to
# rely exclusively on the program for any reason.
# #
# IN NO EVENT SHALL THE UNIVERSITY OF NORTH CAROLINA AT CHAPEL HILL OR THE AUTHORS BE LIABLE TO ANY PARTY FOR # IN NO EVENT SHALL THE UNIVERSITY OF NORTH CAROLINA AT CHAPEL HILL OR THE
# DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS, ARISING OUT OF THE # AUTHORS BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL,
# USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF NORTH CAROLINA AT CHAPEL HILL OR THE # OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS, ARISING OUT OF THE USE OF
# AUTHORS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. # THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF NORTH CAROLINA
# AT CHAPEL HILL OR THE AUTHORS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH
# DAMAGE.
# #
# THE UNIVERSITY OF NORTH CAROLINA AT CHAPEL HILL AND THE AUTHORS SPECIFICALLY DISCLAIM ANY WARRANTIES, INCLUDING, # THE UNIVERSITY OF NORTH CAROLINA AT CHAPEL HILL AND THE AUTHORS SPECIFICALLY
# BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE AND ANY # DISCLAIM ANY WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# STATUTORY WARRANTY OF NON-INFRINGEMENT. THE SOFTWARE PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE AND ANY
# OF NORTH CAROLINA AT CHAPEL HILL AND THE AUTHORS HAVE NO OBLIGATIONS TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, # STATUTORY WARRANTY OF NON-INFRINGEMENT. THE SOFTWARE PROVIDED HEREUNDER IS ON
# AN "AS IS" BASIS, AND THE UNIVERSITY OF NORTH CAROLINA AT CHAPEL HILL AND
# THE AUTHORS HAVE NO OBLIGATIONS TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
# ENHANCEMENTS, OR MODIFICATIONS. # ENHANCEMENTS, OR MODIFICATIONS.
# #
# Please send all BUG REPORTS to <pavel@cs.unc.edu>. # Please send all BUG REPORTS to <pavel@cs.unc.edu>.
@ -43,18 +52,14 @@
SET( SOURCES SET( SOURCES
"src/TexComp.cpp" "src/TexComp.cpp"
"src/CompressedImage.cpp" "src/CompressedImage.cpp"
"src/Image.cpp"
"src/BlockStats.cpp" "src/BlockStats.cpp"
"src/CompressionJob.cpp"
) )
SET( HEADERS SET( HEADERS
"include/TexComp.h" "include/TexComp.h"
"include/CompressedImage.h" "include/CompressedImage.h"
"include/TexCompTypes.h"
"include/Image.h"
"include/BlockStats.h" "include/BlockStats.h"
"include/CompressionJob.h" "src/CompressionFuncs.h"
) )
# Make sure to add the appropriate stopwatch files... # Make sure to add the appropriate stopwatch files...
@ -71,6 +76,8 @@ ELSE()
SET( LINK_FLAGS -lrt ${LINK_FLAGS} ) SET( LINK_FLAGS -lrt ${LINK_FLAGS} )
ENDIF() ENDIF()
INCLUDE_DIRECTORIES( ${FasTC_SOURCE_DIR}/Base/include )
INCLUDE_DIRECTORIES( ${FasTC_SOURCE_DIR}/BPTCEncoder/include ) INCLUDE_DIRECTORIES( ${FasTC_SOURCE_DIR}/BPTCEncoder/include )
INCLUDE_DIRECTORIES( ${FasTC_BINARY_DIR}/BPTCEncoder/include ) INCLUDE_DIRECTORIES( ${FasTC_BINARY_DIR}/BPTCEncoder/include )
@ -85,8 +92,8 @@ SET( THREAD_APIS_AVAILABLE "None" )
###### Find Boost... ###### Find Boost...
IF( MSVC ) IF( MSVC )
SET(ENV{BOOSTLIBDIR} "${MSVC_LIB_DIR}") SET(ENV{BOOSTLIBDIR} "${MSVC_LIB_DIR}")
SET(ENV{BOOSTINCLUDEDIR} "${MSVC_INSTALL_PATH}/include") SET(ENV{BOOSTINCLUDEDIR} "${MSVC_INSTALL_PATH}/include")
ENDIF( MSVC ) ENDIF( MSVC )
SET( Boost_USE_STATIC_LIBS ON ) SET( Boost_USE_STATIC_LIBS ON )
@ -147,6 +154,7 @@ ADD_LIBRARY( FasTCCore
${SOURCES} ${SOURCES}
) )
TARGET_LINK_LIBRARIES( FasTCCore FasTCBase )
TARGET_LINK_LIBRARIES( FasTCCore FasTCIO ) TARGET_LINK_LIBRARIES( FasTCCore FasTCIO )
TARGET_LINK_LIBRARIES( FasTCCore BPTCEncoder ) TARGET_LINK_LIBRARIES( FasTCCore BPTCEncoder )

View file

@ -1,30 +1,39 @@
/* FasTC /* FasTC
* Copyright (c) 2012 University of North Carolina at Chapel Hill. All rights reserved. * Copyright (c) 2013 University of North Carolina at Chapel Hill.
* All rights reserved.
* *
* Permission to use, copy, modify, and distribute this software and its documentation for educational, * Permission to use, copy, modify, and distribute this software and its
* research, and non-profit purposes, without fee, and without a written agreement is hereby granted, * documentation for educational, research, and non-profit purposes, without
* provided that the above copyright notice, this paragraph, and the following four paragraphs appear * fee, and without a written agreement is hereby granted, provided that the
* in all copies. * above copyright notice, this paragraph, and the following four paragraphs
* appear in all copies.
* *
* Permission to incorporate this software into commercial products may be obtained by contacting the * Permission to incorporate this software into commercial products may be
* authors or the Office of Technology Development at the University of North Carolina at Chapel Hill <otd@unc.edu>. * obtained by contacting the authors or the Office of Technology Development
* at the University of North Carolina at Chapel Hill <otd@unc.edu>.
* *
* This software program and documentation are copyrighted by the University of North Carolina at Chapel Hill. * This software program and documentation are copyrighted by the University of
* The software program and documentation are supplied "as is," without any accompanying services from the * North Carolina at Chapel Hill. The software program and documentation are
* University of North Carolina at Chapel Hill or the authors. The University of North Carolina at Chapel Hill * supplied "as is," without any accompanying services from the University of
* and the authors do not warrant that the operation of the program will be uninterrupted or error-free. The * North Carolina at Chapel Hill or the authors. The University of North
* end-user understands that the program was developed for research purposes and is advised not to rely * Carolina at Chapel Hill and the authors do not warrant that the operation of
* exclusively on the program for any reason. * the program will be uninterrupted or error-free. The end-user understands
* that the program was developed for research purposes and is advised not to
* rely exclusively on the program for any reason.
* *
* IN NO EVENT SHALL THE UNIVERSITY OF NORTH CAROLINA AT CHAPEL HILL OR THE AUTHORS BE LIABLE TO ANY PARTY FOR * IN NO EVENT SHALL THE UNIVERSITY OF NORTH CAROLINA AT CHAPEL HILL OR THE
* DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS, ARISING OUT OF THE * AUTHORS BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL,
* USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF NORTH CAROLINA AT CHAPEL HILL OR THE * OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS, ARISING OUT OF THE USE OF
* AUTHORS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF NORTH CAROLINA
* AT CHAPEL HILL OR THE AUTHORS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH
* DAMAGE.
* *
* THE UNIVERSITY OF NORTH CAROLINA AT CHAPEL HILL AND THE AUTHORS SPECIFICALLY DISCLAIM ANY WARRANTIES, INCLUDING, * THE UNIVERSITY OF NORTH CAROLINA AT CHAPEL HILL AND THE AUTHORS SPECIFICALLY
* BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE AND ANY * DISCLAIM ANY WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* STATUTORY WARRANTY OF NON-INFRINGEMENT. THE SOFTWARE PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE AND ANY
* OF NORTH CAROLINA AT CHAPEL HILL AND THE AUTHORS HAVE NO OBLIGATIONS TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, * STATUTORY WARRANTY OF NON-INFRINGEMENT. THE SOFTWARE PROVIDED HEREUNDER IS ON
* AN "AS IS" BASIS, AND THE UNIVERSITY OF NORTH CAROLINA AT CHAPEL HILL AND
* THE AUTHORS HAVE NO OBLIGATIONS TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
* ENHANCEMENTS, OR MODIFICATIONS. * ENHANCEMENTS, OR MODIFICATIONS.
* *
* Please send all BUG REPORTS to <pavel@cs.unc.edu>. * Please send all BUG REPORTS to <pavel@cs.unc.edu>.

View file

@ -52,16 +52,14 @@ enum ECompressionFormat {
kNumCompressionFormats kNumCompressionFormats
}; };
class CompressedImage { #include "Image.h"
class CompressedImage : public Image {
private: private:
unsigned int m_Width;
unsigned int m_Height;
ECompressionFormat m_Format; ECompressionFormat m_Format;
uint32 m_DataSz;
unsigned char *m_Data; uint32 *m_RGBAData;
unsigned int m_DataSz;
void InitData(const unsigned char *withData); void InitData(const unsigned char *withData);
public: public:
@ -77,11 +75,11 @@ class CompressedImage {
const unsigned char *data const unsigned char *data
); );
unsigned int GetHeight() const { return m_Height; }
unsigned int GetWidth() const { return m_Width; }
CompressedImage( const CompressedImage &other ); CompressedImage( const CompressedImage &other );
~CompressedImage(); virtual ~CompressedImage();
virtual void ComputeRGBA();
virtual const uint32 *GetRGBA() const { return m_RGBAData; }
// Decompress the compressed image data into outBuf. outBufSz is expected // Decompress the compressed image data into outBuf. outBufSz is expected
// to be the proper size determined by the width, height, and format. // to be the proper size determined by the width, height, and format.

View file

@ -48,6 +48,7 @@
#include "CompressionJob.h" #include "CompressionJob.h"
// Forward declarations // Forward declarations
class Image;
class ImageFile; class ImageFile;
class BlockStatManager; class BlockStatManager;
@ -91,6 +92,8 @@ struct SCompressionSettings {
BlockStatManager *pStatManager; BlockStatManager *pStatManager;
}; };
extern CompressedImage *CompressImage(Image *img, const SCompressionSettings &settings);
extern bool CompressImageData( extern bool CompressImageData(
const unsigned char *data, const unsigned char *data,
const unsigned int dataSz, const unsigned int dataSz,
@ -99,18 +102,6 @@ extern bool CompressImageData(
const SCompressionSettings &settings const SCompressionSettings &settings
); );
// A compression function format. It takes the raw data and image dimensions and
// returns the compressed image data into outData. It is assumed that there is
// enough space allocated for outData to store the compressed data. Allocation
// is dependent on the compression format.
typedef void (* CompressionFunc)(const CompressionJob &);
// A compression function format. It takes the raw data and image dimensions and
// returns the compressed image data into outData. It is assumed that there is
// enough space allocated for outData to store the compressed data. Allocation
// is dependent on the compression format.
typedef void (* CompressionFuncWithStats)(const CompressionJob &, BlockStatManager &statManager);
// This function computes the Peak Signal to Noise Ratio between a // This function computes the Peak Signal to Noise Ratio between a
// compressed image and a raw image. // compressed image and a raw image.
extern double ComputePSNR(const CompressedImage &ci, const ImageFile &file); extern double ComputePSNR(const CompressedImage &ci, const ImageFile &file);

View file

@ -51,19 +51,10 @@
#include "TexCompTypes.h" #include "TexCompTypes.h"
#include "BC7Compressor.h" #include "BC7Compressor.h"
CompressedImage::CompressedImage()
: m_Width(0)
, m_Height(0)
, m_Format(ECompressionFormat(-1))
, m_Data(0)
, m_DataSz(0)
{ }
CompressedImage::CompressedImage( const CompressedImage &other ) CompressedImage::CompressedImage( const CompressedImage &other )
: m_Width(other.m_Width) : Image(other)
, m_Height(other.m_Height)
, m_Format(other.m_Format) , m_Format(other.m_Format)
, m_Data(0) , m_RGBAData(0)
, m_DataSz(0) , m_DataSz(0)
{ {
InitData(other.m_Data); InitData(other.m_Data);
@ -74,11 +65,10 @@ CompressedImage::CompressedImage(
const unsigned int height, const unsigned int height,
const ECompressionFormat format, const ECompressionFormat format,
const unsigned char *data const unsigned char *data
) )
: m_Width(width) : Image(width, height, NULL)
, m_Height(height)
, m_Format(format) , m_Format(format)
, m_Data(0) , m_RGBAData(0)
, m_DataSz(0) , m_DataSz(0)
{ {
InitData(data); InitData(data);
@ -86,7 +76,7 @@ CompressedImage::CompressedImage(
void CompressedImage::InitData(const unsigned char *withData) { void CompressedImage::InitData(const unsigned char *withData) {
m_DataSz = 0; m_DataSz = 0;
int uncompDataSz = m_Width * m_Height * 4; int uncompDataSz = GetWidth() * GetHeight() * 4;
switch(m_Format) { switch(m_Format) {
default: assert(!"Not implemented!"); // Fall through V default: assert(!"Not implemented!"); // Fall through V
@ -122,23 +112,37 @@ bool CompressedImage::DecompressImage(unsigned char *outBuf, unsigned int outBuf
if(dataSz > outBufSz) { if(dataSz > outBufSz) {
fprintf(stderr, "Not enough space to store entire decompressed image! " fprintf(stderr, "Not enough space to store entire decompressed image! "
"Got %d bytes, but need %d!\n", outBufSz, dataSz); "Got %d bytes, but need %d!\n", outBufSz, dataSz);
assert(false);
return false; return false;
} }
DecompressionJob dj (m_Data, outBuf, GetWidth(), GetHeight());
switch(m_Format) { switch(m_Format) {
case eCompressionFormat_BPTC: case eCompressionFormat_BPTC:
{ {
DecompressionJob dj (m_Data, outBuf, m_Width, m_Height);
BC7C::Decompress(dj); BC7C::Decompress(dj);
} }
break; break;
default: default:
const char *errStr = "Have not implemented decompression method."; {
fprintf(stderr, "%s\n", errStr); const char *errStr = "Have not implemented decompression method.";
assert(!errStr); fprintf(stderr, "%s\n", errStr);
assert(!errStr);
}
return false; return false;
} }
return true; return true;
} }
void CompressedImage::ComputeRGBA() {
if(m_RGBAData) {
delete m_RGBAData;
}
m_RGBAData = new uint32[GetWidth() * GetHeight()];
uint8 *pixelData = reinterpret_cast<uint8 *>(m_RGBAData);
DecompressImage(pixelData, GetWidth() * GetHeight() * 4);
}

View file

@ -0,0 +1,71 @@
/* FasTC
* Copyright (c) 2013 University of North Carolina at Chapel Hill.
* All rights reserved.
*
* Permission to use, copy, modify, and distribute this software and its
* documentation for educational, research, and non-profit purposes, without
* fee, and without a written agreement is hereby granted, provided that the
* above copyright notice, this paragraph, and the following four paragraphs
* appear in all copies.
*
* Permission to incorporate this software into commercial products may be
* obtained by contacting the authors or the Office of Technology Development
* at the University of North Carolina at Chapel Hill <otd@unc.edu>.
*
* This software program and documentation are copyrighted by the University of
* North Carolina at Chapel Hill. The software program and documentation are
* supplied "as is," without any accompanying services from the University of
* North Carolina at Chapel Hill or the authors. The University of North
* Carolina at Chapel Hill and the authors do not warrant that the operation of
* the program will be uninterrupted or error-free. The end-user understands
* that the program was developed for research purposes and is advised not to
* rely exclusively on the program for any reason.
*
* IN NO EVENT SHALL THE UNIVERSITY OF NORTH CAROLINA AT CHAPEL HILL OR THE
* AUTHORS BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL,
* OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS, ARISING OUT OF THE USE OF
* THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF NORTH CAROLINA
* AT CHAPEL HILL OR THE AUTHORS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH
* DAMAGE.
*
* THE UNIVERSITY OF NORTH CAROLINA AT CHAPEL HILL AND THE AUTHORS SPECIFICALLY
* DISCLAIM ANY WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE AND ANY
* STATUTORY WARRANTY OF NON-INFRINGEMENT. THE SOFTWARE PROVIDED HEREUNDER IS ON
* AN "AS IS" BASIS, AND THE UNIVERSITY OF NORTH CAROLINA AT CHAPEL HILL AND
* THE AUTHORS HAVE NO OBLIGATIONS TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
* ENHANCEMENTS, OR MODIFICATIONS.
*
* Please send all BUG REPORTS to <pavel@cs.unc.edu>.
*
* The authors may be contacted via:
*
* Pavel Krajcevski
* Dept of Computer Science
* 201 S Columbia St
* Frederick P. Brooks, Jr. Computer Science Bldg
* Chapel Hill, NC 27599-3175
* USA
*
* <http://gamma.cs.unc.edu/FasTC/>
*/
#ifndef CORE_SRC_COMPRESSIONFUNCS_H_
#define CORE_SRC_COMPRESSIONFUNCS_H_
#include "CompressionJob.h"
#include <iosfwd>
// A compression function format. It takes the raw data and image dimensions and
// returns the compressed image data into outData. It is assumed that there is
// enough space allocated for outData to store the compressed data. Allocation
// is dependent on the compression format.
typedef void (* CompressionFunc)(const CompressionJob &);
// A compression function format. It takes the raw data and image dimensions and
// returns the compressed image data into outData. It is assumed that there is
// enough space allocated for outData to store the compressed data. Allocation
// is dependent on the compression format.
typedef void (* CompressionFuncWithStats)(const CompressionJob &, std::ostream *logStream);
#endif // CORE_SRC_COMPRESSIONFUNCS_H_

View file

@ -48,7 +48,9 @@
#include <cstdlib> #include <cstdlib>
#include <cstdio> #include <cstdio>
#include <cassert> #include <cassert>
#include <iostream>
#include "CompressionFuncs.h"
#include "BC7Compressor.h" #include "BC7Compressor.h"
#include "Thread.h" #include "Thread.h"
#include "WorkerQueue.h" #include "WorkerQueue.h"
@ -57,7 +59,6 @@
#include "ImageFile.h" #include "ImageFile.h"
#include "Image.h" #include "Image.h"
template <typename T> template <typename T>
static void clamp(T &x, const T &minX, const T &maxX) { static void clamp(T &x, const T &minX, const T &maxX) {
x = std::max(std::min(maxX, x), minX); x = std::max(std::min(maxX, x), minX);
@ -142,7 +143,9 @@ static double CompressImageInSerial(
// !FIXME! We're assuming that we have 4x4 blocks here... // !FIXME! We're assuming that we have 4x4 blocks here...
CompressionJob cj (imgData, outBuf, imgDataSz / 16, 4); CompressionJob cj (imgData, outBuf, imgDataSz / 16, 4);
if(fStats && settings.pStatManager) { if(fStats && settings.pStatManager) {
(*fStats)(cj, *(settings.pStatManager)); // !FIXME! Actually use the stat manager...
//(*fStats)(cj, *(settings.pStatManager));
(*fStats)(cj, &std::cout);
} }
else { else {
(*f)(cj); (*f)(cj);
@ -330,6 +333,41 @@ static double CompressImageWithWorkerQueue(
return cmpTimeTotal / double(settings.iNumCompressions); return cmpTimeTotal / double(settings.iNumCompressions);
} }
CompressedImage *CompressImage(
Image *img, const SCompressionSettings &settings
) {
if(!img) return NULL;
const uint32 w = img->GetWidth();
const uint32 h = img->GetHeight();
CompressedImage *outImg = NULL;
const unsigned int dataSz = w * h * 4;
assert(dataSz > 0);
// Allocate data based on the compression method
int cmpDataSz = 0;
switch(settings.format) {
default: assert(!"Not implemented!"); // Fall Through V
case eCompressionFormat_DXT1: cmpDataSz = dataSz / 8; break;
case eCompressionFormat_DXT5: cmpDataSz = dataSz / 4; break;
case eCompressionFormat_BPTC: cmpDataSz = dataSz / 4; break;
}
// Make sure that we have RGBA data...
img->ComputeRGBA();
unsigned char *cmpData = new unsigned char[cmpDataSz];
const uint8 *pixelData = reinterpret_cast<const uint8 *>(img->GetRGBA());
CompressImageData(pixelData, dataSz, cmpData, cmpDataSz, settings);
outImg = new CompressedImage(w, h, settings.format, cmpData);
delete [] cmpData;
return outImg;
}
bool CompressImageData( bool CompressImageData(
const unsigned char *data, const unsigned char *data,
const unsigned int dataSz, const unsigned int dataSz,

View file

@ -44,9 +44,10 @@
#include "ThreadGroup.h" #include "ThreadGroup.h"
#include "BC7Compressor.h" #include "BC7Compressor.h"
#include <stdlib.h> #include <cstdlib>
#include <stdio.h> #include <cstdio>
#include <assert.h> #include <cassert>
#include <iostream>
CmpThread::CmpThread() CmpThread::CmpThread()
: m_StartBarrier(NULL) : m_StartBarrier(NULL)
@ -90,7 +91,9 @@ void CmpThread::operator()() {
if(m_CmpFunc) if(m_CmpFunc)
(*m_CmpFunc)(cj); (*m_CmpFunc)(cj);
else else
(*m_CmpFuncWithStats)(cj, *m_StatManager); // !FIXME! Actually use the block stat manager...
// (*m_CmpFuncWithStats)(cj, *m_StatManager);
(*m_CmpFuncWithStats)(cj, &std::cout);
{ {
TCLock lock(*m_ParentCounterLock); TCLock lock(*m_ParentCounterLock);

View file

@ -44,9 +44,10 @@
#ifndef _THREAD_GROUP_H_ #ifndef _THREAD_GROUP_H_
#define _THREAD_GROUP_H_ #define _THREAD_GROUP_H_
#include "TexComp.h" #include "CompressionFuncs.h"
#include "Thread.h" #include "Thread.h"
#include "StopWatch.h" #include "StopWatch.h"
#include "BlockStats.h"
struct CmpThread : public TCCallable { struct CmpThread : public TCCallable {
friend class ThreadGroup; friend class ThreadGroup;

View file

@ -43,10 +43,11 @@
#include "WorkerQueue.h" #include "WorkerQueue.h"
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#include <algorithm> #include <algorithm>
#include <cstdlib>
#include <cstdio>
#include <cassert>
#include <iostream>
#include "BC7Compressor.h" #include "BC7Compressor.h"
@ -104,7 +105,9 @@ void WorkerThread::operator()() {
if(f) if(f)
(*f)(cj); (*f)(cj);
else else
(*fStat)(cj, *statManager); // !FIXME! Actually use stat manager...
// (*fStat)(cj, *statManager);
(*fStat)(cj, &std::cout);
break; break;
} }

View file

@ -46,12 +46,13 @@
// Forward declare... // Forward declare...
class WorkerQueue; class WorkerQueue;
class BlockStatManager;
// Necessary includes... // Necessary includes...
#include "TexCompTypes.h" #include "TexCompTypes.h"
#include "TexComp.h"
#include "Thread.h" #include "Thread.h"
#include "StopWatch.h" #include "StopWatch.h"
#include "CompressionFuncs.h"
class WorkerThread : public TCCallable { class WorkerThread : public TCCallable {
friend class WorkerQueue; friend class WorkerQueue;

View file

@ -90,6 +90,7 @@ CONFIGURE_FILE(
INCLUDE_DIRECTORIES( ${FasTC_BINARY_DIR}/IO/include ) INCLUDE_DIRECTORIES( ${FasTC_BINARY_DIR}/IO/include )
INCLUDE_DIRECTORIES( ${FasTC_SOURCE_DIR}/IO/include ) INCLUDE_DIRECTORIES( ${FasTC_SOURCE_DIR}/IO/include )
INCLUDE_DIRECTORIES( ${FasTC_SOURCE_DIR}/Base/include )
INCLUDE_DIRECTORIES( ${FasTC_SOURCE_DIR}/Core/include ) INCLUDE_DIRECTORIES( ${FasTC_SOURCE_DIR}/Core/include )
ADD_LIBRARY(FasTCIO ADD_LIBRARY(FasTCIO
@ -97,6 +98,8 @@ ADD_LIBRARY(FasTCIO
${HEADERS} ${HEADERS}
) )
TARGET_LINK_LIBRARIES( FasTCIO FasTCBase )
IF( PNG_FOUND ) IF( PNG_FOUND )
TARGET_LINK_LIBRARIES( FasTCIO ${PNG_LIBRARY} ) TARGET_LINK_LIBRARIES( FasTCIO ${PNG_LIBRARY} )
TARGET_LINK_LIBRARIES( FasTCIO ${ZLIB_LIBRARY} ) TARGET_LINK_LIBRARIES( FasTCIO ${ZLIB_LIBRARY} )

View file

@ -43,11 +43,11 @@
#include "ImageFile.h" #include "ImageFile.h"
#include <string.h> #include <cstring>
#include <stdlib.h> #include <cstdlib>
#include <stdio.h> #include <cstdio>
#include <limits.h> #include <climits>
#include <assert.h> #include <cassert>
#include <algorithm> #include <algorithm>
#include "ImageWriter.h" #include "ImageWriter.h"
@ -196,8 +196,22 @@ Image *ImageFile::LoadImage(const unsigned char *rawImageData) const {
return NULL; return NULL;
} }
Image *i = new Image(*loader); uint8 *pixelData = NULL;
if(loader->GetImageData()) {
pixelData = new uint8[ loader->GetImageDataSz() ];
if(!pixelData) { fprintf(stderr, "%s\n", "Out of memory!"); exit(1); }
memcpy(pixelData, loader->GetImageData(), loader->GetImageDataSz());
}
else {
fprintf(stderr, "%s\n", "Failed to get data from image loader!");
}
uint32 *pixels = reinterpret_cast<uint32 *>(pixelData);
Image *i = new Image(loader->GetWidth(), loader->GetHeight(), pixels);
// Cleanup
delete loader; delete loader;
delete pixelData;
return i; return i;
} }

View file

@ -54,7 +54,7 @@ INCLUDE_DIRECTORIES(${FasTC_SOURCE_DIR})
INCLUDE_DIRECTORIES(${FasTC_SOURCE_DIR}/PVRTCEncoder/include) INCLUDE_DIRECTORIES(${FasTC_SOURCE_DIR}/PVRTCEncoder/include)
INCLUDE_DIRECTORIES(${FasTC_BINARY_DIR}/PVRTCEncoder/include) INCLUDE_DIRECTORIES(${FasTC_BINARY_DIR}/PVRTCEncoder/include)
INCLUDE_DIRECTORIES(${FasTC_SOURCE_DIR}/Core/include) INCLUDE_DIRECTORIES(${FasTC_SOURCE_DIR}/Base/include)
SET( HEADERS SET( HEADERS
include/PVRTCCompressor.h include/PVRTCCompressor.h

View file

@ -53,7 +53,7 @@
#ifndef PVRTCENCODER_INCLUDE_PVRTCCOMPRESSOR_H_ #ifndef PVRTCENCODER_INCLUDE_PVRTCCOMPRESSOR_H_
#define PVRTCENCODER_INCLUDE_PVRTCCOMPRESSOR_H_ #define PVRTCENCODER_INCLUDE_PVRTCCOMPRESSOR_H_
#include "Core/include/CompressionJob.h" #include "Base/include/CompressionJob.h"
namespace PVRTCC { namespace PVRTCC {

View file

@ -53,7 +53,7 @@
#ifndef PVRTCENCODER_SRC_BLOCK_H_ #ifndef PVRTCENCODER_SRC_BLOCK_H_
#define PVRTCENCODER_SRC_BLOCK_H_ #define PVRTCENCODER_SRC_BLOCK_H_
#include "Core/include/TexCompTypes.h" #include "Base/include/TexCompTypes.h"
#include "Pixel.h" #include "Pixel.h"

View file

@ -58,7 +58,7 @@
#include "Pixel.h" #include "Pixel.h"
#include "Core/include/Image.h" #include "Base/include/Image.h"
#include "IO/include/ImageFile.h" #include "IO/include/ImageFile.h"
namespace PVRTCC { namespace PVRTCC {

View file

@ -53,7 +53,7 @@
#ifndef PVRTCENCODER_SRC_IMAGE_H_ #ifndef PVRTCENCODER_SRC_IMAGE_H_
#define PVRTCENCODER_SRC_IMAGE_H_ #define PVRTCENCODER_SRC_IMAGE_H_
#include "Core/include/TexCompTypes.h" #include "Base/include/TexCompTypes.h"
#include "PVRTCCompressor.h" #include "PVRTCCompressor.h"
namespace PVRTCC { namespace PVRTCC {

View file

@ -53,7 +53,7 @@
#ifndef PVRTCENCODER_SRC_PIXEL_H_ #ifndef PVRTCENCODER_SRC_PIXEL_H_
#define PVRTCENCODER_SRC_PIXEL_H_ #define PVRTCENCODER_SRC_PIXEL_H_
#include "Core/include/TexCompTypes.h" #include "Base/include/TexCompTypes.h"
namespace PVRTCC { namespace PVRTCC {

View file

@ -53,7 +53,7 @@
#ifndef PVRTCENCODER_TEST_TESTUTILS_H_ #ifndef PVRTCENCODER_TEST_TESTUTILS_H_
#define PVRTCENCODER_TEST_TESTUTILS_H_ #define PVRTCENCODER_TEST_TESTUTILS_H_
#include "Core/include/TexCompTypes.h" #include "Base/include/TexCompTypes.h"
class PixelPrinter { class PixelPrinter {
private: private: