我在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)
谢谢.
最近创建的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)
.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)