[英]8bit to 16bit conversion
I have an image which captures 8 bit. 我有一个捕获8位的图像。 I'm looking to convert the 8 bit values to 16 bit. 我想将8位值转换为16位。 I used the following 我使用了以下内容
short temp16 = (short)val[i] << 8 ;
where val
is an array of 8 bit samples. 其中val
是8位样本的数组。
The above statement makes noisy. 以上陈述令人吵闹。 Can anybody suggest a method for 8bit to 16bit conversion? 任何人都可以建议一种8位到16位转换的方法吗?
Is val[] signed or unsigned 8bit? val []是签名还是未签名8位? Cast it to unsigned (assuming you've got the usual convention of 0=darkest, 255=brightest) then cast it to signed short (I assume that's what you want, since plain 'short' is by default signed). 把它投射到无符号(假设你有通常的惯例0 =最暗,255 =最亮)然后把它投射到签名的短(我假设你想要的,因为普通的'短'是默认签名)。
Pure bitshifting won't give you pure white. 纯粹的位移不会给你纯白色。 0xff << 8 == 0xff00, not 0xffff as expected. 0xff << 8 == 0xff00,而不是预期的0xffff。
One trick is to use val[i] << 8 + val[i] and remember proper datatypes (size, signedness). 一个技巧是使用val [i] << 8 + val [i]并记住正确的数据类型(大小,符号)。 That way you get 0x00 -> 0x0000 and 0xff -> 0xffff. 这样你得到0x00 - > 0x0000和0xff - > 0xffff。
这里给出了一个很好的例子: 将两个uint8_t单词转换为uint16_t和uint16_t中的一个单词转换为两个uint8_t 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.