什么是最简单的RGB图像格式?

我在C上进行物理实验, Young的干涉实验 ,我制作了一个程序,可以打印出一大堆像素:

 for (i=0; i < width*width; i++) { fwrite(hue(raster_matrix[i]), 1, 3, file); } 

hue给定值[0..255]时,返回一个带有3个字节的char * ,R,G,B。

我想在我的图像文件中放一个最小的标题,以使这个原始文件成为有效的图像文件。

更简洁:切换自:

 offset 0000 : height * width : data } my data, 24bit RGB pixels 

 offset 0000 : dword : magic \ : /* ?? */ \ 0012 : dword : height } Header  common image file 0016 : dword : width / : /* ?? */ / 0040 : height * width : data } my data, 24bit RGB pixels 

谢谢。

您可能想要使用您正在寻找的PPM格式 :最小标头后跟原始RGB。

最近创建的farbfeld格式很少,尽管没有太多软件支持它(至少到目前为止)。

如果您不使用压缩并且不使用任何扩展,则TARGA (文件扩展名.tga )可能是最简单的广泛支持的二进制映像文件格式。 它甚至比Windows .bmp文件更简单,并且受ImageMagick和许多绘图程序的支持。 当我只需要从一次性程序输出一些像素时,这是我的首选格式。

这是一个用于生成标准输出图像的最小C程序:

 #include  #include  enum { width = 550, height = 400 }; int main(void) { static unsigned char pixels[width * height * 3]; static unsigned char tga[18]; unsigned char *p; size_t x, y; p = pixels; for (y = 0; y < height; y++) { for (x = 0; x < width; x++) { *p++ = 255 * ((float)y / height); *p++ = 255 * ((float)x / width); *p++ = 255 * ((float)y / height); } } tga[2] = 2; tga[12] = 255 & width; tga[13] = 255 & (width >> 8); tga[14] = 255 & height; tga[15] = 255 & (height >> 8); tga[16] = 24; tga[17] = 32; return !((1 == fwrite(tga, sizeof(tga), 1, stdout)) && (1 == fwrite(pixels, sizeof(pixels), 1, stdout))); }