BGR packed转换位BGR planar方法及其优化

基本的方法如下:

// the array with the BGRBGRBGR pixel data
byte[] source;
// the array with the BBBGGGRRR pixel data
byte[] result;
// the amount of pixels in one channel, width*height
int imageSize;

for (int i = 0; i < source.Length; i += 3)
{
    result[i/3] = source[i + 0]; // B
    result[i/3 + imageSize] = source[i + 1]; // G
    result[i/3 + imageSize * 2] = source[i+2]; // R
}

具体的优化策略可以参考如下网址:

http://www.howtobuildsoftware.com/index.php/how-do/b649/c-arrays-image-processing-intrinsics-pixelformat-speed-up-pixel-format-conversion-bgr-packed-to-rgb-planar

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
由于PNG是一种无损压缩格式,它不支持BGR格式,因此需要将BGR转换为RGB格式后再保存为PNG格式。下面是一个简单的示例代码: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #include <png.h> void bgr_to_rgb(unsigned char *image, int width, int height) { int i, j; unsigned char temp; for (i = 0; i < height; i++) { for (j = 0; j < width * 3; j += 3) { temp = image[i * width * 3 + j]; image[i * width * 3 + j] = image[i * width * 3 + j + 2]; image[i * width * 3 + j + 2] = temp; } } } int save_png(const char *filename, unsigned char *image, int width, int height) { FILE *fp; png_structp png_ptr; png_infop info_ptr; png_byte **row_pointers; int i; fp = fopen(filename, "wb"); if (!fp) { printf("Error: failed to open file %s\n", filename); return -1; } png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); if (!png_ptr) { printf("Error: failed to create png write struct\n"); fclose(fp); return -1; } info_ptr = png_create_info_struct(png_ptr); if (!info_ptr) { printf("Error: failed to create png info struct\n"); png_destroy_write_struct(&png_ptr, NULL); fclose(fp); return -1; } if (setjmp(png_jmpbuf(png_ptr))) { printf("Error: failed to set png jump buffer\n"); png_destroy_write_struct(&png_ptr, &info_ptr); fclose(fp); return -1; } png_init_io(png_ptr, fp); png_set_IHDR(png_ptr, info_ptr, width, height, 8, PNG_COLOR_TYPE_RGB, PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT); png_write_info(png_ptr, info_ptr); row_pointers = (png_byte **)png_malloc(png_ptr, height * sizeof(png_byte *)); for (i = 0; i < height; i++) { row_pointers[i] = image + i * width * 3; } png_write_image(png_ptr, row_pointers); png_write_end(png_ptr, NULL); png_free(png_ptr, row_pointers); png_destroy_write_struct(&png_ptr, &info_ptr); fclose(fp); return 0; } int main(int argc, char **argv) { int width = 640; int height = 480; unsigned char *image = (unsigned char *)malloc(width * height * 3); // 填充BGR格式数据 // ... // 转换为RGB格式 bgr_to_rgb(image, width, height); // 保存为PNG格式 save_png("output.png", image, width, height); free(image); return 0; } ``` 需要注意的是,以上代码只是一个简单的示例,实际应用中可能会有更多的错误检查和处理。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值