itsource

가장 간단한 RGB 이미지 형식은 무엇입니까?

mycopycode 2023. 7. 1. 08:36
반응형

가장 간단한 RGB 이미지 형식은 무엇입니까?

저는 C에서 물리학 실험과 영의 간섭 실험을 하고 있고, 저는 다음과 같이 인쇄하는 프로그램을 만들었습니다.file픽셀의 거대한 묶음:

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

어디에hue값 [0..255], 다음을 반환합니다.char *3바이트, 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를 사용하는 것이 좋습니다.

TARGA(파일 이름 확장자).tga압축을 사용하지 않고 확장자를 사용하지 않는 경우 가장 간단하게 지원되는 이진 이미지 파일 형식일 수 있습니다.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)));
}

최근에 만들어진 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

다음은 최소 PPM 헤더로 이미지 파일을 작성하는 최소 예제입니다.다행히도, 저는 당신이 제공한 정확한 루프로 작동할 수 있었습니다.

#include <math.h> // compile with gcc young.c -lm
#include <stdio.h>
#include <stdlib.h>

#define width 256

int main(){
    int x, y, i; unsigned char raster_matrix[width*width], h[256][3];
    #define WAVE(x,y) sin(sqrt( (x)*(x)+(y)*(y) ) * 30.0 / width)
    #define hue(i) h[i]

    /* Setup nice hue palette */
    for (i = 0; i <= 85; i++){
        h[i][0] = h[i+85][1] = h[i+170][2] = (i <= 42)? 255:    40+(85-i)*5;
        h[i][1] = h[i+85][2] = h[i+170][0] = (i <= 42)? 40+i*5: 255;
        h[i][2] = h[i+85][0] = h[i+170][1] = 40;
    }

    /* Setup Young's Interference image */
    for (i = 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;
}

헤더 코드는 http://netpbm.sourceforge.net/doc/ppm.html 의 사양을 기반으로 합니다.이 이미지의 경우 헤더는 15바이트 문자열에 불과합니다."P6 256 256 255\n".

언급URL : https://stackoverflow.com/questions/16636311/what-is-the-simplest-rgb-image-format

반응형