如何将18位色深表示为16位色深?
我正在移植一个从 16 位颜色深度构建到 18 位颜色深度的软件。 如何将 16 位颜色转换为 18 位颜色? 谢谢。
I'm porting a software that build from 16bit color depth to 18bit color depth. How can I convert the 16-bit colors to 18-bit colors? Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
在不了解设备的情况下,我只能推测。 设备通常是红色、绿色、蓝色,因此每种颜色都会有 6 位变化。 这意味着每种颜色有 64 种变体,总共 262,144 种颜色。
任何位图都可以缩放到此显示。 如果您采用每个分量(例如红色),将其标准化,然后乘以 64,您将得到缩放后的版本。
如果您有其他问题或想了解更多详细信息,请询问。
更新:
有两种 16 位位图格式。 一种是 5-5-5(每像素 5 位),另一种是 5-6-5(绿色获得额外的位)。 为了进行此转换,我将假设 5-5-5。
对于每个像素内的每种颜色,您需要如下所示:
这会将旧颜色转换为 0.0 到 1.0 之间的数字,然后将其放大到新的值范围。
Without knowing the device, I can only speculate. Devices are typically Red, Green, Blue so each color would get 6 bits of variation. That means 64 variations of each color and a total of 262,144 colors.
Any bitmap can be scaled to this display. If you take each component (say, red), normalize it, then multiply by 64, you'll have the scaled version.
If you are asking something else or want more detail, please ask.
Update:
There are two 16-bit bitmap formats. One is 5-5-5 (5 bits per pixel) and the other is 5-6-5 (green gets an extra bit). I'll assume 5-5-5 for the sake of this conversion.
For each color within each pixel you need something like this:
This will turn the old color into a number between 0.0 and 1.0 and then scale it up to the new value range.
假设16bit颜色为5-6-5格式:
Assuming that the 16bit color is in 5-6-5 format:
我没有看到其他答案中涵盖了这一点,但您希望从 5 位到 6 位的转换使得二进制 11111 映射到 111111,因为两者都代表完整。 一种方法是复制前导位。
如果您使用算术转换,请记住最大值是 31 和 63,而不是 32 和 64。
如果您关心其他中间值,例如如果您想要 16 映射 32,请调整公式(例如,将 15 更改为14)或者只是做一张桌子。
I don't see this covered in the other answers, but you want the conversion from 5 to 6 bits to be such that binary 11111 gets mapped to 111111, since both represent full on. One way to do that is to replicate the leading bit(s).
If you use an arithmetic conversion, keep in mind that the maximum values are 31 and 63, not 32 and 64.
If you care about other intermediate values, such as if you want 16 mapped 32 either tweak the formula (e.g., change 15 to 14) or just make a table.
回答转换问题
我不熟悉c++图像库,但我确信这段代码已经存在于某处。 但如果你愿意,你可以自己做。
每个像素具有三个通道:红、绿、蓝。 在 16 位位图中,每个通道的位数不同。 通常,绿色首先获得任何额外的位,因为人眼对该颜色最敏感。
大概是 565——5 代表红色,6 代表绿色,5 代表蓝色。
对于 18 位位图(如前所述),每个通道有 6 位。 所以绿色很容易! 只需将值从 16 位格式复制到 18 位格式即可。 另外两个通道稍微难一些。 你必须从 5 插值到 6:
然后你就得到了它。 如果您还不熟悉以下内容,我建议您研究一下:
--编辑--
确保你知道事物在内存中是如何排列的; RGB 或 BGR。 如果你做任何比这更复杂的事情并且你向后思考它,它会让你的头脑变得混乱(因为绿色在中间,所以对于这个转换来说并不那么重要)。
To answer the conversion question
I'm not familiar with c++ image libraries but I'm sure this code already exists somewhere. But if you'd like you can do it yourself.
Each pixel has three channels: red, green, and blue. In a 16-bit bitmap, each channel doesn't have the same number of bits per channel. Typically, green is first to get any extra bits because the human eye is most sensitive to that color.
It's probably 565 -- 5 for red, 6 for green, and 5 for blue.
For an 18-bit bitmap (as previously stated), each channel has 6 bits. So green is easy! Just copy the value over from the 16 bit format to the 18 bit format. The other two channels are slightly harder. You have to interpolate from 5 to 6:
And there you have it. If you aren't already familiar with the following, I'd suggest you research them:
--Edit--
Make sure you know how things are laid out in memory; RGB or BGR. It will mess with your head if you do anything more complicated than this and you're thinking about it backwards (since Green is in the middle it's not that important for this conversion).
您首先必须访问每个颜色分量(即提取 R 值、G 值和 B 值)。 执行此操作的方法完全取决于颜色在内存中存储的方式。 如果它存储为 RGB,分量有 5-6-5 位,您可以使用如下内容:
这将为您提取颜色分量,然后您可以使用上面概述的方法来转换每个分量(我假设 18 位将使用每个组件 6 位):
请注意,对于上述代码,重要的是使用 32.0 或其他一些方法来确保您的程序将数字表示为浮点数点数。 然后要将这些组件组合成 18 位颜色,请使用:
You will first of all have to access each of the colour components (i.e. extract the R value, the G value, and the B value). The way to do this will depend totally on the way that the colour is stored in memory. If it is stored as RGB, with 5-6-5 bits for the components, you could use something like this:
This will extract the colour components for you, then you can use the method outlined above to convert each of the components (I presume 18-bit will use 6 bits per component):
Note that with the above code, it is important that you use 32.0, or some other method that will ensure that your program represents the number as a floating point number. Then to combine the components into your 18-bit colour, use: