首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Python的DXT压缩

Python的DXT压缩
EN

Stack Overflow用户
提问于 2012-06-10 21:46:28
回答 2查看 1.5K关注 0票数 2

我目前使用的是图片,有些是DXT压缩的,我需要一种简单的方法来用Python来解压缩这些文件。不幸的是,我找不到任何为我这样做的图书馆。

有没有人知道一个好的Python DXT压缩库,或者一个压缩库的接口?

-- dav1d

编辑:

libsquish是这里的方法,但不幸的是Python绑定不起作用,所以这里是解决方案。

在C++中制作一个压缩包装器,导出内部访问libsquish的函数:

代码语言:javascript
复制
#include <squish.h>

typedef unsigned char u8;

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);
    }
}

创建一个动态库( windows上的dll或linux上的so,我称之为libsquishc.so)并使用ctypes打开它。

我的方法(只是导出我需要的函数):

代码语言:javascript
复制
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
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-06-10 22:17:19

libSquish添加Python绑定的修补程序

编辑:安装过程似乎是

  1. 下载squish-1.11.zip
  2. 解压缩和编译--应该会产生一个libsquish.file
  3. 下载并安装Cython (听起来你这样做了)
  4. 创建一个临时目录并“应用”修补程序-它会删除一堆新文件,这些文件是绑定代码。
  5. 运行安装程序(sudo python setup.py安装)

如果您这样做了,并且仍然有错误,那么您可能应该(a)共享实际的错误消息,这样我们就可以找出原因,或者(b)直接联系补丁作者- mat (at) kivy.org。

Edit2:编译错误足够短,因此我将在这里包括它:

代码语言:javascript
复制
running install
running build
running build_ext
skipping 'squish.c' Cython extension (up-to-date)
building 'squish' extension
gcc -pthread -fno-strict-aliasing -march=i686 -mtune=generic -O2 -pipe -fstack-protector --param=ssp-buffer-size=4 -D_FORTIFY_SOURCE=2 -DNDEBUG -march=i686 -mtune=generic -O2 -pipe -fstack-protector --param=ssp-buffer-size=4 -D_FORTIFY_SOURCE=2 -fPIC -I.. -I/usr/include/python2.7 -c squish.c -o build/temp.linux-i686-2.7/squish.o
In file included from squish.c:274:0:
/usr/include/squish.h:32:1: error: unknown type name 'namespace'
/usr/include/squish.h:32:18: error: expected '=', ',', ';', 'asm' or '__attribute__' before '{' token
squish.c: In function '__pyx_pf_6squish_compressImage':
squish.c:790:22: error: 'squish' undeclared (first use in this function)
squish.c:790:22: note: each undeclared identifier is reported only once for each function it appears in
squish.c:790:28: error: expected ';' before ':' token
squish.c:866:10: error: expected expression before ':' token
squish.c: In function '__pyx_pf_6squish_2decompressImage':
squish.c:1202:10: error: expected expression before ':' token
error: command 'gcc' failed with exit status 1

H的相关部分看起来

代码语言:javascript
复制
#ifndef SQUISH_H
#define SQUISH_H

//! All squish API functions live in this namespace.
namespace squish {

// -----------------------------------------------------------------------------

看起来它被namespace关键字堵住了,我会说这意味着它正在编译为C,而它应该被编译为C++。

票数 1
EN

Stack Overflow用户

发布于 2012-06-10 22:17:14

libsquish有一些有贡献的python绑定:但是,http://code.google.com/p/libsquish/issues/detail?id=17我还没有使用它们。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10972595

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档