cairo - 如何使用 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 获取数据,包括步幅,如上图所示。我认为这也是开罗的要求。
解决方案
推荐阅读
- android - 模拟器 Android 15 - 黑屏
- amazon-web-services - 您可以使用基于 AWS Route 53 延迟的路由来解析 AWS 之外的服务器吗?
- r - 如何检查 r 中数据框中的列的值是否对应于数据框每一行的另一列的单次出现?
- docker - windows/amd64 没有匹配的清单
- ruby-on-rails - 尝试诊断 ActiveRecord::ConcurrentMigrationError 时缺少 pg_locks 和 pg_stat_activity 表
- javascript - 使用reduce计算以小时为单位的日期差异
- python - 在 python 中为 Google Translate API 添加 time.sleep() 方法
- java - 与声音相关的应用程序,飞出模拟器
- typescript - 启用标志 --strictNullChecks 时,null 不可分配给 void
- python-3.x - 验证用户的输入