티스토리 뷰

C언어

[C언어] 3-2. raw파일을 bmp파일로 변환하기

용감한 우디 2024. 11. 11. 20:35

안녕하세요!

오늘은 raw파일을 bmp파일로 바꾸는 방법에 대해 알아보겠습니다.

 

https://unrunhy.tistory.com/4

 

BMP파일 뜯어보기

안녕하세요!오늘은 BMP파일에 대해서 알아보겠습니다. ●BMP파일이란?비트맵(Bitmap) 이미지 파일 형식으로, 특히 windows 환경에서 많이 사용하는 이미지 형식입니다.BMP파일은 각 픽셀의 색상 정보

unrunhy.tistory.com

이 글을 먼저 읽고 오시는 것을 추천드립니다.

 

전 글에서, bmp파일을 raw파일로 바꾸는 방법에 대해 알아보았는데요

 

raw파일을 bmp파일로 변환하기 위해서는

bmp파일의 헤더부분을 먼저 기본값으로 초기화를 한 후에 

픽셀데이터를 옮기는 작업을 하면 됩니다!

 

각각의 헤더를 기본값으로 설정하는 것은 

위 링크를 들어가셔서 확인해보시면 되겠습니다.

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

#define VER 256
#define HOR 256
#define B_SIZE (VER*HOR*3)

#pragma pack(1)

struct BITMAPINFOHEADER {
   unsigned int biSize;
   int biWidth;
   int biHeight;
   unsigned short biPlanes;
   unsigned short biBitCount;
   unsigned int biCompression;
   unsigned int biSizeImage;
   int biXPelsPerMeter;
   int biYPelsPerMeter;
   unsigned int biClrUsed;
   unsigned int biClrImportant;
};

struct BITMAPFILEHEADER {
   unsigned short bfType;
   unsigned int bfSIZE;
   unsigned short bfReserved1;
   unsigned short bfReserved2;
   unsigned int bfOffBits;
};

#pragma pack()

unsigned char * alloc_pic (int SIZE);

int main()
{
   int i, j;
   BITMAPFILEHEADER bfh;
   BITMAPINFOHEADER bih;
   
   bfh.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
   bfh.bfReserved1 = 0;
   bfh.bfReserved2 = 0;
   bfh.bfSize = bfh.bfOffBits + B_SIZE;
   bfh.bfType = 0x4D42;
   
   bih.biSize = sizeof(BITMAPINFOHEADER);
   bih.biWidth = HOR;
   bih.biHeight = VER;
   bih.biPlanes = 1;
   bih.biBitCount = 24;
   bih.biCompression = 0;
   bih.biSizeImage = 0;
   bih.biXPelsPerMeter = 0;
   bih.biYPelsPerMeter = 0;
   bih.biClrUsed = 0;
   bih.biClrImportant = 0;
   
   FILE * fin = fopen("Lena_Color.raw","rb");
   FILE * fout = fopen("Lena_Color.bmp","wb");
   
   if(!fin) {printf("ERROR :: File Can't Read\n"); exit(1);}
   if(!fout) {printf("ERROR :: File Can't Save\n"); exit(1);}

	fwrite(&bfh,sizeof(BITMAPFILEHEADER),1,fout);
	fwrite(&bih,sizeof(BITMAPINFOHEADER),1,fout);

	unsigned char * bmp1 = alloc_pic(B_SIZE);
   unsigned char * raw1 = alloc_pic(B_SIZE);
   
   fread(raw1, sizeof(unsigned char),B_SIZE, fin);
   
   for( i = 0; i < VER; i++){
   	for( j = 0; j < HOR; j++){
   		bmp1[(VER*i+j)*3] = raw1[(VER*(VER-i-1)+j)*3+2];
   		bmp1[(VER*i+j)*3+1] = raw1[(VER*(VER-i-1)+j)*3+1];
   		bmp1[(VER*i+j)*3+2] = raw1[(VER*(VER-i-1)+j)*3];
	   }
   }
   
   fwrite(bmp1,sizeof(unsigned char), B_SIZE, fout);
   
   fclose(fin);
   fclose(fout);
   
   free(raw1);
   free(bmp1);
   
   printf("success");


	return 0;
}

/*** function ***/
unsigned char * alloc_pic (int SIZE)
{
   unsigned char * pic;
   
   if((pic=(unsigned char*)calloc(SIZE,sizeof(unsigned char))) == NULL) {
      printf("\n malloc_picture : Picture structure \n");
      exit(1);
   }
   return pic;
}

이렇게 하시면 raw파일을 bmp파일로 바꿀수있습니다.

 

감사합니다!

공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2025/05   »
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
글 보관함