2

私は現在Imagesを使用しており、一部はDXT圧縮されています。これらのファイルを、Pythonで解凍および圧縮する簡単な方法が必要です。残念ながら、私のためにそれを行うライブラリを見つけることができませんでした。

Python用の優れたDXT-Compressionライブラリ、または圧縮ライブラリへのインターフェイスを知っている人はいますか?

--dav1d

編集:

libsquishがここに行く方法でしたが、残念ながらPython-Bindingsは機能しないので、ここで解決策を示します。

libsquishに内部的にアクセスする関数をエクスポートするC++でsquish-Wrapperを作成します。

#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ではdllなど)を作成し、libsquishc.soで開きますctypes

私のアプローチ(必要な関数をエクスポートするだけです):

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
4

2 に答える 2

1

libSquishには、Pythonバインディングを追加するためのパッチがあります

編集:インストール手順は

  1. squish-1.11.zipをダウンロード
  2. 解凍してコンパイルします-結果はlibsquish.aファイルになります
  3. Cythonをダウンロードしてインストールします(これを行ったように聞こえます)
  4. 一時ディレクトリを作成し、パッチを「適用」します-バインディングコードである新しいファイルの束をドロップします
  5. セットアップを実行します(sudo python setup.py install)

これを行ってもエラーが発生する場合は、(a)実際のエラーメッセージを共有して理由を把握するか、(b)パッチの作成者に直接連絡する必要があります-mat(at)kivy.org

Edit2:コンパイルエラーは十分に短いので、ここに含めます:

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

squish.hの関連セクションは次のようになります

#ifndef SQUISH_H
#define SQUISH_H

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

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

キーワードが詰まっているように見えますnamespace。これは、C++としてコンパイルする必要があるときにCとしてコンパイルしていることを意味します。

于 2012-06-10T22:17:19.010 に答える
0

libsquishにはいくつかの貢献したPythonバインディングがあります:http ://code.google.com/p/libsquish/issues/detail?id = 17しかし、私はそれらを使用していません。

于 2012-06-10T22:17:14.343 に答える