Last active
December 19, 2015 11:29
-
-
Save mortennobel/5948376 to your computer and use it in GitHub Desktop.
QtImageCompressionTest
Uses http://code.google.com/p/libsquish/
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <QCoreApplication> | |
#include <QByteArray> | |
#include <QBuffer> | |
#include <QFile> | |
#include <QImage> | |
#include <QDebug> | |
#include <squish.h> | |
#include <iostream> | |
#if !defined(_WIN64) && !defined(_WIN32) | |
# include <sys/time.h> | |
typedef timeval sys_time_t; | |
inline void system_time(sys_time_t* t) { | |
gettimeofday(t, NULL); | |
} | |
inline long long time_to_msec(const sys_time_t& t) { | |
return t.tv_sec * 1000LL + t.tv_usec / 1000; | |
} | |
#else | |
# include <sys/timeb.h> | |
typedef _timeb sys_time_t; | |
inline void system_time(sys_time_t* t) { _ftime(t); } | |
inline long long time_to_msec(const sys_time_t& t) { | |
return t.time * 1000LL + t.millitm; | |
} | |
#endif | |
#define PERF_INIT() sys_time_t t; long long startTimeMs; long long endTimeMs; | |
#define PERF_(x,file,line) system_time(&t); \ | |
startTimeMs = time_to_msec(t); \ | |
x; \ | |
system_time(&t); \ | |
endTimeMs = time_to_msec(t); \ | |
std::cout << "file:\t"<<file << "\tline:\t"<<line<< "\ttime:\t"<<(endTimeMs - startTimeMs)<<"\tms"<<std::endl; | |
#define PERF(x) PERF_(x,__FILE__,__LINE__); | |
QByteArray load_binary_resource(const std::string &qtResource){ | |
QFile file( qtResource.c_str()); | |
if (!file.open(QIODevice::ReadOnly)){ | |
qWarning() << "Cannot open file " << qtResource.c_str(); | |
exit( EXIT_FAILURE ); | |
} | |
return file.readAll(); | |
} | |
int main(int argc, char *argv[]) | |
{ | |
QCoreApplication a(argc, argv); | |
static QImage img; | |
QByteArray blockBuffer = load_binary_resource(":/ThrowBlurLarge.jpg"); | |
qDebug() << "length "<<blockBuffer.length(); | |
img.loadFromData((uchar *)blockBuffer.data(), blockBuffer.length(), "JPG"); | |
// modify the image | |
img.setPixel(0,0,0x123); | |
QByteArray ba; | |
QBuffer buffer(&ba); | |
buffer.open(QIODevice::WriteOnly); | |
qDebug() << "Raw length "<<img.byteCount(); | |
PERF_INIT(); | |
PERF(img.save(&buffer, "jpg")); | |
qDebug() << "JPEG Size "<<ba.length(); | |
ba.clear(); | |
PERF(img.save(&buffer, "png")); | |
qDebug() << "PNG Size "<<ba.length(); | |
ba.clear(); | |
squish::u8 *pixels = (squish::u8*)img.bits(); | |
int storage = squish::GetStorageRequirements(img.width(),img.height(),squish::kDxt1 ); | |
squish::u8* blocks = new squish::u8[storage]; | |
PERF(squish::CompressImage( pixels,img.width(), img.height(), blocks, squish::kDxt1 | squish::kColourRangeFit)); | |
qDebug() << "DXT1 Size "<<(storage); | |
ba.clear(); | |
// verify result | |
squish::DecompressImage(pixels,img.width(), img.height(), blocks, squish::kDxt1 | squish::kColourRangeFit); | |
img.save("tmp.png"); | |
return a.exec(); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#------------------------------------------------- | |
# | |
# Project created by QtCreator 2013-07-08T13:26:39 | |
# | |
#------------------------------------------------- | |
QT += core gui widgets opengl network | |
TARGET = QtImageCompressionTest | |
CONFIG += console | |
CONFIG -= app_bundle | |
LIBS += /Users/morten/Downloads/squish-1.11/libsquish.a | |
INCLUDEPATH += /Users/morten/Downloads/squish-1.11/ | |
TEMPLATE = app | |
SOURCES += main.cpp | |
HEADERS += PerformanceTool.h | |
RESOURCES += \ | |
res.qrc |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment