我有一个原始的二进制图像文件,其中每个像素都包含12位数据(灰度)。例如,原始文件中的前四个像素:
0x0 0xC0
0x1 0x05
0x2 0x5C
0x3 0xC0
0x4 0x05
0x5 0x5C
这对应于值为0x5C0(小字节序)的4个像素值。
不幸的是,使用以下命令:
convert -size 384x184 -depth 12 gray:frame_0.raw out.tiff
错误地解释了像素值(大端),从而导致像素值0xC00 0x55C 0xC00 0x55C
。
我尝试了选项-endian LSB
和-endian MSB
,但不幸的是,它们仅更改输出字节顺序,而不更改输入字节顺序。
如何获得转换以将原始图像打开为12位小字节序数据?
我对此进行了快速尝试,但没有测试数据,但它应该非常接近并且很容易检测到图像中的错误:
// pad12to16.c
// Mark Setchell
// Pad 12-bit data to 16-bit
//
// Compile with:
// gcc pad12to16.c -o pad12to16
//
// Run with:
// ./pad12to16 < 12-bit.dat > 16-bit.dat
#include <stdio.h>
#include <sys/uio.h>
#include <unistd.h>
#include <sys/types.h>
#define BYTESPERREAD 6
#define PIXPERWRITE 4
int main(){
unsigned char buf[BYTESPERREAD];
unsigned short pixel[PIXPERWRITE];
// Read 6 bytes at a time and decode to 4 off 16-bit pixels
while(read(0,buf,BYTESPERREAD)==BYTESPERREAD){
pixel[0] = buf[0] | ((buf[1] & 0xf) << 8);
pixel[1] = (buf[2] << 4) | ((buf[1] & 0xf0) >> 4);
pixel[2] = buf[3] | ((buf[2] & 0xf) << 8);
pixel[3] = (buf[5] << 4) | ((buf[4] & 0xf0) >> 4);
write(1,pixel,PIXPERWRITE*2);
}
return 0;
}
因此,您可以运行以下命令(我认为):
./pad12to16 < 12-bit.dat | convert -size 384x184 -depth 16 gray:- result.tif
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句