가장 간단한 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
'itsource' 카테고리의 다른 글
SQL Server 2008:테이블의 열이 기존 기본 키 또는 고유 제약 조건과 일치하지 않습니다. (0) | 2023.07.06 |
---|---|
git-diff 및 git 로그가 새 파일 및 삭제된 파일을 무시하도록 만드는 방법은 무엇입니까? (0) | 2023.07.06 |
iOS 11의 앱에 자동 푸시가 제공되지 않음 (0) | 2023.07.01 |
SQL 서버에서 쿼리의 텍스트를 연결하려면 어떻게 해야 합니까? (0) | 2023.07.01 |
숫자 열거형을 사용하여 유형 스크립트 '기록 <> (0) | 2023.07.01 |