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

Nop*_*ope 36 c image raster

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

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

其中hue,当给定值[0..255]时,给出char *3个字节,R,G,B.

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

更简洁:切换自:

offset
0000 : height * width : data } my data, 24bit RGB pixels
Run Code Online (Sandbox Code Playgroud)

offset
0000 : dword : magic        \
     : /* ?? */              \
0012 : dword : height         } Header <--> common image file
0016 : dword : width         /
     : /* ?? */             /
0040 : height * width : data  } my data, 24bit RGB pixels
Run Code Online (Sandbox Code Playgroud)

谢谢.

Pat*_*que 36

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


Ian*_*ott 7

最近创建的farbfeld格式非常小,尽管支持它的软件并不多(至少到目前为止)。

Bytes                  ? Description
8                      ? "farbfeld" magic value
4                      ? 32-Bit BE unsigned integer (width)
4                      ? 32-Bit BE unsigned integer (height)
(2+2+2+2)*width*height ? 4*16-Bit BE unsigned integers [RGBA] / pixel, row-major
Run Code Online (Sandbox Code Playgroud)


Las*_*ssi 5

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

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

#include <stdio.h>
#include <string.h>

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)));
}
Run Code Online (Sandbox Code Playgroud)

  • 什么时候初始化 tga 数组中偏移量 0、1、3 到 11 处的字节?由于 BSS 链接器部分或其他原因,它是否保证为零? (2认同)
  • 好问题。事实上,全局变量和静态变量由 C 保证为零初始化。如果您从 `malloc()` 获取数组或将其分配到堆栈上,则必须手动将 `memset()` 设置为零(或者使用“calloc()”,它会自行完成此操作)。 (2认同)