问题描述
我正在制作一个16位汇编程序,而我正在使用的图形模式仅支持在单个字节中写入的每个像素256种颜色.换句话说,我只能指定一个字节的颜色值来绘制像素.我将图像转换为8位深度的位图,现在我尝试使用Visual Basic来获取每个像素的颜色值,以便可以在汇编程序中使用这些值,但是却得到RGB值,而当我尝试转换这些RGB时值变成一个字节后,我得到的数字大于256,所以我不能在图形模式下使用它.这是我所拥有的:
I'm making a 16 bit assembly program and the graphics mode I'm working only supports 256 colors per pixel that are written in a single byte. In other words, I can only specify one byte color value to draw a pixel. I converted an image into 8 bit depth bitmap and now I'm trying to take each pixel color value with Visual Basic so I can use those values in the assembly program but I'm getting a RGB value and when I try to convert those RGB values into a single byte I get numbers bigger than 256 so I can't use that in graphic mode. Here is what I have:
Dim myBitmap As New Bitmap("images.bmp")
Dim output As New System.IO.StreamWriter("colors.txt", False)
Dim pixelColor As Color
Dim rgb As Integer
For x As Integer = 1 To myBitmap.Width - 1 Step 1
pixelColor = myBitmap.GetPixel(x, y)
rgb = CUInt(pixelColor.B) + CUInt((pixelColor.G << 8)) + CUInt((pixelColor.R << 16))
output.WriteLine(rgb.ToString)
Next x
Next y
output.Close()
实际上给我的值大于256的行是:
The line that is actually giving me values higher than 256 is:
rgb = CUInt(pixelColor.B) + CUInt((pixelColor.G << 8)) + CUInt((pixelColor.R << 16))
实际上是否有任何方法可以将3个RGB值完全转换,合并或近似为一个字节? (不能超过255)
Is there actually any way to convert, merge or approximate the 3 RGB values altogether into a single byte? (which can't be more than 255)
既然我们已经进入VB网络,那么让我用转换逻辑 David 写道:
Since we are already into the VB net stuff let me write the working line of code with the conversion logic David wrote:
rgb = ((pixelColor.R / 32) << 5) + ((pixelColor.G / 32) << 2) + (pixelColor.B / 64)
谢谢大卫.
推荐答案
红色值使用3位,绿色使用3位,蓝色使用2位.在Wikipedia页面上,有关 8位颜色的解释很好.
Use 3 bits for the red value, 3 for green, and 2 for blue. It's explained well on the Wikipedia page about 8-bit color.
转换逻辑为:
[(Red / 32) << 5] + [(Green / 32) << 2] + (Blue / 64)
这篇关于如何将RGB对应的3个字节变成一个字节?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!