首页 > 解决方案 > 如何使用 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, w, h, stride);
    free(data);
}

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

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

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

imageSource = cairo_image_surface_create_for_data(data, CAIRO_FORMAT_A8, w, h, stride);
//free(data);

在此处输入图像描述

如果有任何方法可以从 stb_image 获取数据,包括步幅,如上图所示。我认为这也是开罗的要求。

标签: cairostb-image

解决方案


推荐阅读