如何使用 stbi_load 从 cairo_image_surface_create_for_data 渲染图像?

问题描述

我正在使用 Cairo 渲染图像,但有一个问题,即画布始终以空白绘制(未绘制图像)。请参考我下面的代码

int width,height,channels;
unsigned char* data = stbi_load(imagePath.c_str(),&width,&height,&channels,STBI_rgb_alpha);
int stride = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32,width);
this->imageSource = cairo_image_surface_create_for_data(data,CAIRO_FORMAT_ARGB32,width,stride);
free(data);

但是如果我使用当前支持的来自 Cairo 的函数渲染 png 文件,它运行良好,我的代码如下:

this->imageSource = cairo_image_surface_create_from_png(imagePath.c_str());

解决方法

问题是我自己发现的。因为内存空闲,所以cairo的数据指针指向空数据。 我通过使用cairo的其他api(cairo_image_surface_create)而不是cairo_image_surface_create_for_data来解决它。请参阅下面的代码:

//define params
int width,height,channels;
//read image data from file using stb_image.h
unsigned char* data = stbi_load(imagePath.c_str(),&width,&height,&channels,STBI_rgb_alpha);
//create surface with image size and format is ARGB32
this->imageSource = cairo_image_surface_create(CAIRO_FORMAT_ARGB32,width,height);
//get pointer of cairo data
unsigned char * surface_data = cairo_image_surface_get_data(this->imageSource);
//copy current data to surface pointer
memcpy(surface_data,data,width * height * 4 * sizeof(unsigned char));
//mark as dirty to refresh surface
cairo_surface_mark_dirty(this->imageSource);
//free image data
free(data);