当前正在尝试将C用于之前在python(pypy)中完成的作业。我想我会尝试写在C(为最佳速度),并使用ctypes沟通。开箱(r,g,b)原始像素缓冲区C
现在我想要做的是从位图(bmp文件)中获取像素缓冲区,将其发送到C函数,该函数将原始缓冲区转换为R,G,B值的平面数组并返回它以蟒蛇。但是当我尝试将“缓冲区”转换为R,G,B值时,我陷入了困境。 在Python中,我会简单地使用“结构”模块:B,G,R = struct.unpack('<BBB', buffer[i:i+3])
我应该怎么做在C相同?
的Python:
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..
失败Ç试图转换像素:
#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;
}
你在'char * array'中有什么?每个字节一个样本? – leonbloy 2013-04-11 17:38:41
即使是这样:@leonbloy,这是正确的。 – JHolta 2013-04-11 18:02:19