160 likes | 363 Vues
02 장. 영상의 구성 방법. RAW File Format BMP File Format Gray / Color Image. Image format. File information. +. Image information. +. Palette information. No information. +. File information. Image information. 20. 20. 20. 20. 20. 20. 20. 20. 20. 20. 20. 20. 20. 20.
E N D
02장 영상의 구성 방법 • RAW File Format • BMP File Format • Gray / Color Image 한빛미디어(주)
Image format File information + Image information + Palette information No information +
File information Image information 20 20 20 20 20 20 20 20 20 20 20 20 20 20 Palette information 50 50 50 50 60 60 60 60 60 60 60 60 60 60 50 50 50 50 60 60 70 70 70 70 70 70 60 60 60 60 60 60 60 60 70 70 70 70 80 80 70 70 60 60 70 70 70 70 70 70 80 80 80 80 70 70 Raw Image
Example #include <stdio.h> void main() { int i, j; unsigned char OrgImg[256][256]; FILE *infile = fopen("coin.raw","rb"); if(infile==NULL) { printf("File open error !!"); return; } fread(OrgImg,sizeof(char),256*256,infile); fclose(infile); for(i=0; i<256; i++) { for(j=0; j<256; j++) { OrgImg[i][j] = 255-OrgImg[i][j]; } } FILE *outfile = fopen("coin_inv.raw","wb"); fwrite(OrgImg,sizeof(char),256*256,outfile); fclose(outfile); }
Preparation Image information 없음 Viewer Program 요구 Image Size
BITMAPFILEHEADER File Header BITMAPINFOHEADER 20 20 20 20 20 20 20 + RGBQUAD Image Header + 50 50 60 60 60 60 60 Palette information + 50 50 60 70 70 70 60 60 60 60 70 70 80 70 60 70 70 70 80 80 70 Bitmap Image
BITMAPFILEHEADER BITMAPINFOHEADER 20 20 20 20 20 20 20 RGBQUAD 50 50 60 60 60 60 60 50 50 60 70 70 70 60 60 60 60 70 70 80 70 60 70 70 70 80 80 70 HEADER INFORMATION BMP Header BMP Body
BITMAPFILEHEADER BITMAPFILEHEADER typedefstructtagBITMATFILEHEADER { WORD bfType; DWORD bfSize; WORD bfReserved1; WORD bfReserved2; DWORD bfOffBits; }BITMAPFILEHEADER;
BITMAPFILEHEADER #include <stdio.h> #include <windows.h> #define WIDTHBYTES(bits) (((bits)+31)/32*4) #define BYTE unsigned char void main() { FILE *infile; infile=fopen("talent.bmp", "rb"); if(infile==NULL) { printf("There is no file!!!\n"); exit(1); } BITMAPFILEHEADER hf; fread(&hf,sizeof(BITMAPFILEHEADER),1,infile); if(hf.bfType!=0x4D42) exit(1); printf("bfType\t\t: %ox\n",hf.bfType); printf("bfSize\t\t: %d\n",hf.bfSize); printf("bfReserved1\t: %d\n",hf.bfReserved1); printf("bfReserved2\t: %d\n",hf.bfReserved2); printf("bfOffBits\t: %d\n",hf.bfOffBits); fclose(infile); }
BITMAPFILEHEADER BITMAPINFOHEADER typedefstructtagBITMATINFOHEADER { DWORD biSize; LONG biWidth; LONG biHeight; WORD biPlanes; WORD biBitCount; DWORD bicompression; DWORD biSizeImage; LONG biXPelsPerMeter; LONG biYPelsPerMeter; WORD biClrUsed; WORD biClrImportant; }BITMAPFILEHEADER;
BITMAPINFOHEADER #include <stdio.h> #include <windows.h> #define WIDTHBYTES(bits) (((bits)+31)/32*4) #define BYTE unsigned char void main() { FILE *infile; infile=fopen("talent.bmp", "rb"); if(infile==NULL) { printf("There is no file!!!\n"); exit(1); } BITMAPFILEHEADER hf; fread(&hf,sizeof(BITMAPFILEHEADER),1,infile); if(hf.bfType!=0x4D42) exit(1); BITMAPINFOHEADER hInfo; fread(&hInfo,sizeof(BITMAPINFOHEADER),1,infile); printf("Image Size: (%3dx%3d)\n",hInfo.biWidth,hInfo.biHeight); printf("Pallete Type: %dbit Colors\n",hInfo.biBitCount); }
RGBQUAD RGBQUAD typedefstructtagBITMATFILEHEADER { BYTE rgbBlue; BYTE rgbGreen; BYTE rgbRed; BYTE bfReserved2; }RGBQUAD;
BITMAPINFOHEADER #include <stdio.h> #include <windows.h> #define WIDTHBYTES(bits) (((bits)+31)/32*4) #define BYTE unsigned char void main() { FILE *infile; infile=fopen("talent.bmp", "rb"); if(infile==NULL) { printf("There is no file!!!\n"); exit(1); } BITMAPFILEHEADER hf; fread(&hf,sizeof(BITMAPFILEHEADER),1,infile); if(hf.bfType!=0x4D42) exit(1); BITMAPINFOHEADER hInfo; fread(&hInfo,sizeof(BITMAPINFOHEADER),1,infile); printf("Image Size: (%3dx%3d)\n",hInfo.biWidth,hInfo.biHeight); printf("Pallete Type: %dbit Colors\n",hInfo.biBitCount); if(hInfo.biBitCount!=8 ) { printf("Bad File format!!"); exit(1); } RGBQUAD hRGB[256]; fread(hRGB,sizeof(RGBQUAD),256,infile); }
Example #include <stdio.h> #include <windows.h> #define WIDTHBYTES(bits) (((bits)+31)/32*4) // 영상 가로길이는 4바이트의 배수여야 함 #define BYTE unsigned char void main() { FILE *infile; infile=fopen("talent.bmp", "rb"); if(infile==NULL) { printf("There is no file!!!\n"); exit(1); } BITMAPFILEHEADER hf; fread(&hf,sizeof(BITMAPFILEHEADER),1,infile); // 파일헤드를 읽음 if(hf.bfType!=0x4D42) exit(1); BITMAPINFOHEADER hInfo; fread(&hInfo,sizeof(BITMAPINFOHEADER),1,infile); // 영상헤드를 읽음 printf("Image Size: (%3dx%3d)\n",hInfo.biWidth,hInfo.biHeight); printf("Pallete Type: %dbit Colors\n",hInfo.biBitCount); // 256칼라 이하의 경우는 취급하지 않음 if(hInfo.biBitCount!=8 ) { printf("Bad File format!!"); exit(1); } RGBQUAD hRGB[256]; fread(hRGB,sizeof(RGBQUAD),256,infile); // 팔레트를 파일에서 읽음 // 영상데이타를 저장할 메모리 할당 BYTE *lpImg = new BYTE [hInfo.biSizeImage]; fread(lpImg,sizeof(char),hInfo.biSizeImage,infile); fclose(infile); // 역상의 이미지 구하기 int rwsize = WIDTHBYTES(hInfo.biBitCount*hInfo.biWidth); for(int i=0; i<hInfo.biHeight; i++) { for(int j=0; j<hInfo.biWidth; j++) { lpImg[i*rwsize+j] = 255-lpImg[i*rwsize+j]; } } // 영상 출력 FILE *outfile = fopen("OutImg.bmp","wb"); fwrite(&hf,sizeof(char),sizeof(BITMAPFILEHEADER),outfile); fwrite(&hInfo,sizeof(char),sizeof(BITMAPINFOHEADER),outfile); fwrite(hRGB,sizeof(RGBQUAD),256,outfile); fwrite(lpImg,sizeof(char),hInfo.biSizeImage,outfile); fclose(outfile); // 메모리 해제 delete []lpImg; }