在图像的Binary数组的标题中更改RGB的顺序 [英] Change the ordering of RGB in the header of the Binary array of the image

查看:96
本文介绍了在图像的Binary数组的标题中更改RGB的顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好

我有24个位图图像,我必须将其转换为字节数组以将其保存到二进制文件.

由于某些原因,我无法将24位图转换为32位图,问题是:

在32 bpp结果中,像素的顺序为蓝色,绿色,红色和(未使用). BIN文件中的顺序是树脂,红色,绿色和蓝色.因此,我的代码在编写BIN文件时必须翻转那些字节的顺序.

因此,此图像的二进制数组 Header 的顺序必须为(Red,Green,blue)而不是(Blue,Green,Red),并在其中复制我的图像而无需更改标题替换为(红色,绿色,蓝色).

用C#做到这一点的任何想法吗?

谢谢.

解决方案

您可以使用移位,屏蔽和运算来提取字节,然后以不同的顺序重新组合它们.

 rgbu =((bgr&  255 )<<  24  )| ((bgr&( 255 <<  8 ))<<  16 )| ((bgr&( 255 <<  16 ))<<  8 ); 


您可能必须将其中一些转换为无符号整数,以确保不会遇到负面影响.另外,我不确定源格式和目标格式是什么,所以我只是假设您正在从未使用的蓝色/绿色/红色转换为红色/绿色/蓝色/蓝色(最高有效字节到最低有效字节).


Hello all

I have 24 bitmap image, and i have to convert it to byte array to save it to binary file.

I cant convert my 24 bitmap to 32 bitmap for some reasons, and the problem is:

The ordering of pixels is blue, green, red and (not used) within the 32 bpp result. The ordering within the BIN file is resin, red, green and blue. So my code have to flip the ordering of those bytes when writing the BIN file.

So the ordering of the Header of the binary array for this image have to be (Red, Green, blue) instead of (Blue, Green, Red) and copy my image in it without change the header that replaced to (Red, Green, Blue).

Any idea to do this by C#?

Thank you.

解决方案

You can use bit shifting, masking, and ORing to extract bytes and then recombine them in a different order.

rgbu = ((bgr & 255) << 24) | ((bgr & (255 << 8)) << 16) | ((bgr & (255 << 16)) << 8);


You might have to cast some of those as unsigned integers to ensure you don''t encounter any negative funkiness. Also, I wasn''t sure what the source and destination format were, so I just assumed you were converting from blue/green/red to red/green/blue/unused (most significant byte to least significant byte).


这篇关于在图像的Binary数组的标题中更改RGB的顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆