Add Rich Geldreich's ETC1 compressor.

This commit is contained in:
Pavel Krajcevski 2013-10-21 17:23:33 -04:00
parent 692cfbcf77
commit bae71bbe4f
12 changed files with 2848 additions and 3 deletions

View file

@ -60,7 +60,7 @@
void PrintUsage() {
fprintf(stderr, "Usage: tc [OPTIONS] imagefile\n");
fprintf(stderr, "\n");
fprintf(stderr, "\t-f\t\tFormat to use. Either \"BPTC\" or \"PVRTC\". Default: BPTC\n");
fprintf(stderr, "\t-f\t\tFormat to use. Either \"ETC1\", \"BPTC\" or \"PVRTC\". Default: BPTC\n");
fprintf(stderr, "\t-l\t\tSave an output log.\n");
fprintf(stderr, "\t-q <quality>\tSet compression quality level. Default: 50\n");
fprintf(stderr, "\t-n <num>\tCompress the image num times and give the average time and PSNR. Default: 1\n");
@ -137,6 +137,8 @@ int _tmain(int argc, _TCHAR* argv[])
} else if(!strcmp(argv[fileArg], "PVRTCLib")) {
format = eCompressionFormat_PVRTC;
bUsePVRTexLib = true;
} else if(!strcmp(argv[fileArg], "ETC1")) {
format = eCompressionFormat_ETC1;
}
}
@ -275,6 +277,8 @@ int _tmain(int argc, _TCHAR* argv[])
strcat_s(basename, "-bc7.png");
} else if(format == eCompressionFormat_PVRTC) {
strcat_s(basename, "-pvrtc.png");
} else if(format == eCompressionFormat_ETC1) {
strcat_s(basename, "-etc1.png");
}
ImageFile cImgFile (basename, eFileFormat_PNG, *ci);

View file

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

View file

@ -77,6 +77,7 @@ ENDIF()
INCLUDE_DIRECTORIES( ${FasTC_SOURCE_DIR}/Base/include )
INCLUDE_DIRECTORIES( ${FasTC_SOURCE_DIR} )
INCLUDE_DIRECTORIES( ${FasTC_SOURCE_DIR}/ETCEncoder/include )
INCLUDE_DIRECTORIES( ${FasTC_SOURCE_DIR}/DXTEncoder/include )
INCLUDE_DIRECTORIES( ${FasTC_SOURCE_DIR}/PVRTCEncoder/include )
@ -162,6 +163,7 @@ ADD_LIBRARY( FasTCCore
TARGET_LINK_LIBRARIES( FasTCCore FasTCBase )
TARGET_LINK_LIBRARIES( FasTCCore FasTCIO )
TARGET_LINK_LIBRARIES( FasTCCore ETCEncoder )
TARGET_LINK_LIBRARIES( FasTCCore DXTEncoder )
TARGET_LINK_LIBRARIES( FasTCCore BPTCEncoder )
TARGET_LINK_LIBRARIES( FasTCCore PVRTCEncoder )

View file

@ -49,6 +49,7 @@
enum ECompressionFormat {
eCompressionFormat_DXT1,
eCompressionFormat_DXT5,
eCompressionFormat_ETC1,
eCompressionFormat_BPTC,
eCompressionFormat_PVRTC,

View file

@ -54,6 +54,7 @@
#include "BC7Compressor.h"
#include "PVRTCCompressor.h"
#include "DXTCompressor.h"
#include "ETCCompressor.h"
CompressedImage::CompressedImage( const CompressedImage &other )
: Image(other)
@ -116,6 +117,10 @@ bool CompressedImage::DecompressImage(unsigned char *outBuf, unsigned int outBuf
DXTC::DecompressDXT1(dj);
break;
case eCompressionFormat_ETC1:
ETCC::Decompress(dj);
break;
case eCompressionFormat_PVRTC:
{
#ifndef NDEBUG
@ -168,6 +173,7 @@ uint32 CompressedImage::GetCompressedSize(uint32 uncompressedSize, ECompressionF
default:
assert(!"Not implemented!");
// Fall through V
case eCompressionFormat_ETC1:
case eCompressionFormat_DXT1:
case eCompressionFormat_PVRTC:
cmpDataSzNeeded = uncompressedSize / 8;

View file

@ -50,6 +50,7 @@
#include <cassert>
#include <iostream>
#include "ETCCompressor.h"
#include "DXTCompressor.h"
#include "BC7Compressor.h"
#include "CompressionFuncs.h"
@ -102,7 +103,8 @@ static CompressionFuncWithStats ChooseFuncFromSettingsWithStats(const SCompress
return BC7C::CompressWithStats;
}
break;
case eCompressionFormat_ETC1:
case eCompressionFormat_DXT1:
case eCompressionFormat_DXT5:
case eCompressionFormat_PVRTC:
@ -149,6 +151,9 @@ static CompressionFunc ChooseFuncFromSettings(const SCompressionSettings &s) {
}
}
case eCompressionFormat_ETC1:
return ETCC::Compress_RG;
default:
{
assert(!"Not implemented!");

64
ETCEncoder/CMakeLists.txt Normal file
View file

@ -0,0 +1,64 @@
# FasTC
# Copyright (c) 2012 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/>
INCLUDE_DIRECTORIES(${FasTC_SOURCE_DIR}/Base/include)
INCLUDE_DIRECTORIES(${FasTC_SOURCE_DIR}/ETCEncoder/include)
INCLUDE_DIRECTORIES(${FasTC_BINARY_DIR}/ETCEncoder/include)
SET( HEADERS
include/ETCCompressor.h
src/rg_etc1.h
)
SET( SOURCES
src/Compressor.cpp
src/Decompressor.cpp
src/rg_etc1.cpp
)
ADD_LIBRARY( ETCEncoder
${HEADERS}
${SOURCES}
)
TARGET_LINK_LIBRARIES( ETCEncoder FasTCBase )

View file

@ -0,0 +1,74 @@
/* 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 ETCENCODER_INCLUDE_ETCCOMPRESSOR_H_
#define ETCENCODER_INCLUDE_ETCCOMPRESSOR_H_
#include "CompressionJob.h"
#include "TexCompTypes.h"
namespace ETCC {
// Takes a stream of compressed ETC1 data and decompresses it into R8G8B8A8
// format. The width and height must be specified in order to properly
// decompress the data.
void Decompress(const DecompressionJob &);
// Takes a stream of uncompressed RGBA8 data and compresses it into ETC1
// version one. The width and height must be specified in order to properly
// decompress the data. This uses the library created by Rich Geldreich found here:
// https://code.google.com/p/rg-etc1
void Compress_RG(const CompressionJob &);
} // namespace PVRTCC
#endif // ETCENCODER_INCLUDE_ETCCOMPRESSOR_H_

View file

@ -0,0 +1,83 @@
/* 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/>
*/
#include "rg_etc1.h"
#include "ETCCompressor.h"
namespace ETCC {
void Compress_RG(const CompressionJob &cj) {
rg_etc1::etc1_pack_params params;
params.m_quality = rg_etc1::cLowQuality;
rg_etc1::pack_etc1_block_init();
// Assume block-stream order
uint32 blockSizeX = cj.width / 4;
uint32 blockSizeY = cj.height / 4;
for(uint32 j = 0; j < blockSizeY; j++)
for(uint32 i = 0; i < blockSizeX; i++) {
uint32 pixels[16];
uint32 blockIdx = j*blockSizeX + i;
for(uint32 y = 0; y < 4; y++) {
for(uint32 x = 0; x < 4; x++) {
const uint32 *in = reinterpret_cast<const uint32 *>(cj.inBuf);
pixels[y*4 + x] = in[(j*4 + y)*cj.width + (i*4 + x)];
}
}
pack_etc1_block(cj.outBuf + blockIdx * 8, pixels, params);
}
}
} // namespace PVRTCC

View file

@ -0,0 +1,77 @@
/* 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/>
*/
#include "rg_etc1.h"
#include "ETCCompressor.h"
namespace ETCC {
void Decompress(const DecompressionJob &cj) {
uint32 blocksX = cj.width / 4;
uint32 blocksY = cj.width / 4;
for(uint32 j = 0; j < blocksX; j++) {
for(uint32 i = 0; i < blocksY; i++) {
uint32 pixels[16];
uint32 blockIdx = j*blocksX + i;
rg_etc1::unpack_etc1_block(cj.inBuf + blockIdx * 8, pixels);
for(uint32 y = 0; y < 4; y++)
for(uint32 x = 0; x < 4; x++) {
uint32 *out = reinterpret_cast<uint32 *>(cj.outBuf);
out[(j*4 + y)*cj.width + (i*4 + x)] = pixels[y*4 + x];
}
}
}
}
} // namespace PVRTCC

2453
ETCEncoder/src/rg_etc1.cpp Executable file

File diff suppressed because it is too large Load diff

76
ETCEncoder/src/rg_etc1.h Executable file
View file

@ -0,0 +1,76 @@
// File: rg_etc1.h - Fast, high quality ETC1 block packer/unpacker - Rich Geldreich <richgel99@gmail.com>
// Please see ZLIB license at the end of this file.
#pragma once
namespace rg_etc1
{
// Unpacks an 8-byte ETC1 compressed block to a block of 4x4 32bpp RGBA pixels.
// Returns false if the block is invalid. Invalid blocks will still be unpacked with clamping.
// This function is thread safe, and does not dynamically allocate any memory.
// If preserve_alpha is true, the alpha channel of the destination pixels will not be overwritten. Otherwise, alpha will be set to 255.
bool unpack_etc1_block(const void *pETC1_block, unsigned int* pDst_pixels_rgba, bool preserve_alpha = false);
// Quality setting = the higher the quality, the slower.
// To pack large textures, it is highly recommended to call pack_etc1_block() in parallel, on different blocks, from multiple threads (particularly when using cHighQuality).
enum etc1_quality
{
cLowQuality,
cMediumQuality,
cHighQuality,
};
struct etc1_pack_params
{
etc1_quality m_quality;
bool m_dithering;
inline etc1_pack_params()
{
clear();
}
void clear()
{
m_quality = cHighQuality;
m_dithering = false;
}
};
// Important: pack_etc1_block_init() must be called before calling pack_etc1_block().
void pack_etc1_block_init();
// Packs a 4x4 block of 32bpp RGBA pixels to an 8-byte ETC1 block.
// 32-bit RGBA pixels must always be arranged as (R,G,B,A) (R first, A last) in memory, independent of platform endianness. A should always be 255.
// Returns squared error of result.
// This function is thread safe, and does not dynamically allocate any memory.
// pack_etc1_block() does not currently support "perceptual" colorspace metrics - it primarily optimizes for RGB RMSE.
unsigned int pack_etc1_block(void* pETC1_block, const unsigned int* pSrc_pixels_rgba, etc1_pack_params& pack_params);
} // namespace rg_etc1
//------------------------------------------------------------------------------
//
// rg_etc1 uses the ZLIB license:
// http://opensource.org/licenses/Zlib
//
// Copyright (c) 2012 Rich Geldreich
//
// This software is provided 'as-is', without any express or implied
// warranty. In no event will the authors be held liable for any damages
// arising from the use of this software.
//
// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it
// freely, subject to the following restrictions:
//
// 1. The origin of this software must not be misrepresented; you must not
// claim that you wrote the original software. If you use this software
// in a product, an acknowledgment in the product documentation would be
// appreciated but is not required.
//
// 2. Altered source versions must be plainly marked as such, and must not be
// misrepresented as being the original software.
//
// 3. This notice may not be removed or altered from any source distribution.
//
//------------------------------------------------------------------------------