現在、Python (pypy) で以前に行われたジョブに C を使用しようとしています。私は(最適な速度のために)Cでそれを書き、ctypesを使用して通信しようと考えました。
今私がしたいのは、ピクセルバッファをビットマップ (bmp ファイル) から取得し、生のバッファを R、G、B 値のフラットな配列に変換する C 関数に送信し、それを python に返すことです。しかし、「バッファ」をR、G、B値に変換しようとすると行き詰まりました。Python では、単純に「struct」モジュールを使用します。B,G,R = struct.unpack('<BBB', buffer[i:i+3])
Cで同じことを行うにはどうすればよいですか?
パイソン:
from bmplib import Bitmap
import ctypes
lib = ctypes.CDLL('_bitmap.dll')
bmp = Bitmap()
bmp.open('4x4.bmp')
buf = bmp._rawAsArray() #Returns a array.array() of char (raw pixel-data)
addr, count = buf.buffer_info()
lib.getData.argtypes = []
arr = ctypes.cast(addr, ctypes.POINTER(ctypes.c_char))
lib.getData(arr, count) #Does not return anything yet..
C のピクセル変換に失敗しました:
#include <stdio.h>
void getData(char *, const int);
void getData(char * array, const int length) {
int i = 0;
while(i < length) {
/* ----- Clearly wrong as i got some HUGE number----- */
printf("%d, ", ((int *) array)[i] << 8); //B
printf("%d, ", ((int *) array)[i+1] << 8); //G
printf("%d\n", ((int *) array)[i+2] << 8); //R
i += 3;
}
//return total;
}