Skip to content

Instantly share code, notes, and snippets.

@yin8086
Created October 1, 2014 09:59
Show Gist options
  • Save yin8086/4f04b542c335b318e0c0 to your computer and use it in GitHub Desktop.
Save yin8086/4f04b542c335b318e0c0 to your computer and use it in GitHub Desktop.
PySquish
// Squish bindings for python
// http://stackoverflow.com/questions/10972595/dxt-compression-for-python
// modify Stardrad
#include <squish.h>
#include "pysquish.h"
extern "C" {
void CompressMasked(u8 const* rgba, int mask, void* block, int flags) {
squish::CompressMasked(rgba, mask, block, flags);
}
void Compress(u8 const* rgba, void* block, int flags) {
squish::Compress(rgba, block, flags);
}
void Decompress(u8* rgba, void const* block, int flags) {
squish::Decompress(rgba, block, flags);
}
int GetStorageRequirements(int width, int height, int flags) {
return squish::GetStorageRequirements(width, height, flags);
}
void CompressImage(u8 const* rgba, int width, int height, void* blocks, int flags) {
squish::CompressImage(rgba, width, height, blocks, flags);
}
void DecompressImage(u8* rgba, int width, int height, void const* blocks, int flags) {
squish::DecompressImage(rgba, width, height, blocks, flags);
}
}
// Squish bindings for python
// http://stackoverflow.com/questions/10972595/dxt-compression-for-python
// modify Stardrad
#ifndef PYSQUISH
#define PYSQUISH
typedef unsigned char u8;
extern "C"
{
#define DllExport __declspec( dllexport )
DllExport void CompressMasked(u8 const* rgba, int mask, void* block, int flags);
DllExport void Compress(u8 const* rgba, void* block, int flags);
DllExport void Decompress(u8* rgba, void const* block, int flags);
DllExport int GetStorageRequirements(int width, int height, int flags);
DllExport void CompressImage(u8 const* rgba, int width, int height, void* blocks, int flags);
DllExport void DecompressImage(u8* rgba, int width, int height, void const* blocks, int flags);
}
#endif // !PYSQUISH
# Squish bindings for python
# http://stackoverflow.com/questions/10972595/dxt-compression-for-python
from ctypes import CDLL, c_int, byref, create_string_buffer
import os.path
libsquish_path = os.path.join(os.path.split(os.path.abspath(__file__))[0], 'libsquishc.so')
libsquish = CDLL(libsquish_path)
DXT1 = 1 << 0
DXT3 = 1 << 1
DXT5 = 1 << 2
COLOR_ITERATIVE_CLUSTER_FIT = 1 << 8
COLOR_CLUSTER_FIT = 1 << 3
COLOR_RANGE_FIT = 1 << 4
WEIGHT_COLOR_BY_ALPHA = 1 << 7
GetStorageRequirements = libsquish.GetStorageRequirements
GetStorageRequirements.argtypes = [c_int, c_int, c_int]
GetStorageRequirements.restype = c_int
def compress_image(rgba, width, height, flags):
rgba = create_string_buffer(rgba)
c = GetStorageRequirements(width, height, flags)
buffer = create_string_buffer(c)
libsquish.Compress(byref(rgba), byref(buffer), c_int(flags))
return buffer.raw
def decompress_image(block, width, height, flags):
block = create_string_buffer(block)
c = width*height*4
rgba = create_string_buffer(c)
libsquish.DecompressImage(byref(rgba), c_int(width), c_int(height), byref(block), c_int(flags))
return rgba.raw
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment