如何使用 stbi_load 输出格式是 STBI_grey 并使用与 cairo 相同的步幅?

问题描述

我的代码如下,现在它可以工作,但我需要再运行一个循环来重新处理从 stb_image 加载后的图像数据。请看:

int w,h,c;
cairo_surface_t * imageSource;
//how to adjust this function to get full data row include stride instead of w * h
unsigned char* data = stbi_load(imagePath.c_str(),&w,&h,&c,STBI_grey);
if (data == NULL) {
    //create empty surface
    imageSource = cairo_image_surface_create(CAIRO_FORMAT_A8,w,h);
} else {
    int stride = cairo_format_stride_for_width(CAIRO_FORMAT_A8,w);
    unsigned char * dest_data = ( unsigned char * ) malloc(sizeof( unsigned char ) * stride * h);
    int j = 0;
    //this loop i need to process my data before render it to cairo
    for (int i = 0; i < h; i++) {
        memcpy(&dest_data [ i * stride ],&data [ j ],w * sizeof(unsigned char));
        j += w;
    }
    imageSource = cairo_image_surface_create_for_data(dest_data,CAIRO_FORMAT_A8,stride);
    free(data);
}

我的问题是,我不想在这种情况下运行 for 函数来提高速度。

for (int i = 0; i < h; i++) {
    memcpy(&dest_data [ i * stride ],w * sizeof(unsigned char));
    j += w;
}

在这种情况下,我将直接使用来自 stbi_load 的数据,如下所示:

imageSource = cairo_image_surface_create_for_data(data,stride);
//free(data);

enter image description here

如果有任何方法可以从 stb_image 获取数据,包括步幅,就像上图一样,那就太棒了。我认为这也是开罗的要求。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)