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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16636311/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 06:23:10  来源:igfitidea点击:

What is the simplest RGB image format?

cimageraster

提问by Nope

I am working in C on a physics experiment, Young's interference experimentand i made a program who prints to filea huge bunch of pixels :

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

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

Where hue, when given a value [0..255], gives back a char *with 3 bytes, R,G,B.

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

I would like to put a minimal header in my image file in order to make this raw file a validimage file.

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

More concise : Switching from :

更简洁:切换自:

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

to

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

Thank you.

谢谢你。

回答by Patrice Levesque

You probably want to use the PPM formatwhich is what you're looking for: a minimal header followed by raw RGB.

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

回答by Ian D. Scott

The recently created farbfeldformat is quite minimal, though there is not much software supporting it (at least so far).

最近创建的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

回答by Lassi

TARGA(file name extension .tga) may be the simplest widely supported binary image file format if you don't use compression and don't use any of its extensions. It's even simpler than Windows .bmpfiles and is supported by ImageMagick and many paint programs. It has been my go-to format when I just need to output some pixels from a throwaway program.

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

Here's a minimal C program to generate an image to standard output:

这是一个生成图像到标准输出的最小 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)));
}

回答by mwfearnley

Here's a minimal example that writes your image file with a minimal PPM header:

这是一个使用最小 PPM 标头写入图像文件的最小示例:

#include <stdio.h>
#include <stdlib.h>

#include <math.h> // compile with gcc -lm

int main(){
    /* Setup for Young's interference image */
    #define width 256
    unsigned char raster_matrix[width*width], h[3];
    #define WAVE(x,y) sin(sqrt( (x)*(x)+(y)*(y) ) / 3.0)
    #define hue(c) (h[0] = c, h[1] = 128, h[2] = 255-c, h)

    int x, y, i = 0;
    for (y = 0; y < width; y++) for (x = 0; x < width; x++)
        raster_matrix[i++] = 128 + 64*(WAVE(x,y) + WAVE(x,width-y));


    /* Open PPM File */
    FILE *file = fopen("young.ppm", "wb"); if (!file) return -1;

    /* Write PPM Header */
    fprintf(file, "P6 %d %d %d\n", width, width, 255); /* width, height, maxval */

    /* Write Image Data */
    for (i=0; i < width*width; i++)
        fwrite(hue(raster_matrix[i]), 1, 3, file);

    /* Close PPM File */
    fclose(file);

    /* All done */
    return 0;
}

I wrote the header code based on the specs at http://netpbm.sourceforge.net/doc/ppm.html. For this image, the header is just a string of fifteen bytes: "P6 256 256 255\n".

我根据http://netpbm.sourceforge.net/doc/ppm.html上的规范编写了标头代码。对于此图像,标题只是一个 15 个字节的字符串:"P6 256 256 255\n".

The setup code here is slightly hackish, but makes it possible to concisely demonstrate the code with the exact forloop body given in the question.

此处的设置代码略显笨拙,但可以for使用问题中给出的确切循环体简洁地演示代码。