Kraklog

Day.77 BMP 구현 본문

[Harman] 하만 반도체 설계/C언어

Day.77 BMP 구현

Krakens 2023. 11. 8. 09:36
728x90

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#pragma pack(1)
#define ee

#if defined(ee)

typedef struct _BITMAPFILEHEADER {
    char Signature[2];
    unsigned int FileSize;
    int Reserved;
    unsigned int DataOffset;
} BITMAPFILEHEADER;

typedef struct _InfoHeader {
    unsigned int Size;
    int Width;
    int Height;
    unsigned short Planes;
    unsigned short BitCount;
    unsigned int Compression;
    unsigned int ImageSize;
    int X_PixelsPerM;
    int Y_PixelsPerM;
    unsigned int ColorsUsed;
    unsigned int ColorsImportant;
} InfoHeader;

typedef struct _ColorTable {
    unsigned char rgb_Blue;
    unsigned char rgb_Green;
    unsigned char rgb_Red;
    unsigned char rgb_Reserved;
} ColorTable;

int main() {
    BITMAPFILEHEADER HEADER;
    InfoHeader Info;

    HEADER.Signature[0] = 'B';
    HEADER.Signature[1] = 'M';
    HEADER.FileSize = sizeof(BITMAPFILEHEADER) + sizeof(InfoHeader) + 256 * sizeof(ColorTable) + 640 * 480;
    HEADER.DataOffset = sizeof(BITMAPFILEHEADER) + sizeof(InfoHeader) + 256 * sizeof(ColorTable);

    Info.Size = 40;
    Info.Width = 640;
    Info.Height = 480;
    Info.Planes = 1;
    Info.BitCount = 8;
    Info.Compression = 0;
    Info.ImageSize = 0;
    Info.X_PixelsPerM = 0;
    Info.Y_PixelsPerM = 0;
    Info.ColorsUsed = 256;
    Info.ColorsImportant = 0;


    FILE* BMP_GEN;
    if (fopen_s(&BMP_GEN, "BMP_GEN.bmp", "wb") == 0) {
        fwrite(&HEADER, sizeof(BITMAPFILEHEADER), 1, BMP_GEN);
        fwrite(&Info, sizeof(InfoHeader), 1, BMP_GEN);

        ColorTable colorTable[256];
        // 색상 테이블 생성
        for (int i = 0; i < 256; i++) {
            if (i < 32) {
                // 흰색
                colorTable[i].rgb_Blue  = 100;
                colorTable[i].rgb_Green = 100;
                colorTable[i].rgb_Red   = 100;
            }
            else if (i < 64) {
                // 노란색
                colorTable[i].rgb_Blue  = 000;
                colorTable[i].rgb_Green = 100;
                colorTable[i].rgb_Red   = 100;
                
                
            }
            else if (i < 96) {
                // 옥색
                colorTable[i].rgb_Blue  = 100;
                colorTable[i].rgb_Green = 100;
                colorTable[i].rgb_Red   = 000;
            }
            else if (i < 128) {
                // 초록색
                colorTable[i].rgb_Blue  = 000;
                colorTable[i].rgb_Green = 100;
                colorTable[i].rgb_Red   = 000;
            }
            else if (i < 160) {
                // 자홍색
                colorTable[i].rgb_Blue  = 100;
                colorTable[i].rgb_Green = 000;
                colorTable[i].rgb_Red   = 100;
            }
            else if (i < 192) {
                // 빨간색
                colorTable[i].rgb_Blue  = 100;
                colorTable[i].rgb_Green = 000;
                colorTable[i].rgb_Red   = 000;
            }
            else if (i < 224) {
                // 파란색
                colorTable[i].rgb_Blue  = 000;
                colorTable[i].rgb_Green = 000;
                colorTable[i].rgb_Red   = 100;
            }
            else {
                // 검정색
                colorTable[i].rgb_Blue  = 000;
                colorTable[i].rgb_Green = 000;
                colorTable[i].rgb_Red   = 000;
            }
        }

        fwrite(colorTable, sizeof(ColorTable), 256, BMP_GEN);

        // 픽셀 데이터 생성 및 쓰기
        for (int y = 0; y < 480; y++) {
            for (int x = 0; x < 80; x++) {
                fputc(0, BMP_GEN);  // 파란색 컬러 인덱스 사용
            }
            for (int x = 80; x < 160; x++) {
                fputc(32, BMP_GEN);  // 초록색
            }
            for (int x = 160; x < 240; x++) {
                fputc(64, BMP_GEN);  // 노란색
            }
            for (int x = 240; x < 320; x++) {
                fputc(96, BMP_GEN);  // 빨간색
            }
            for (int x = 320; x < 400; x++) {
                fputc(128, BMP_GEN);  // 자홍색
            }
            for (int x = 400; x < 480; x++) {
                fputc(160, BMP_GEN);  // 파란색
            }
            for (int x = 480; x < 560; x++) {
                fputc(192, BMP_GEN);  // 초록색
            }
            for (int x = 560; x < 640; x++) {
                fputc(255, BMP_GEN);  // 검정색
            }
        }

        fclose(BMP_GEN);

        return 0;
    }
}
#elif defined(one)
typedef struct _BITMAPFILEHEADER //BITMAPFILEHEADER total : 40byte 
{
  char          Signature[2]      ;
  unsigned int  FileSize          ;
  int           Reserved          ;
  unsigned int  DataOffset        ;
}BITMAPFILEHEADER;

typedef struct _InfoHeader {
	unsigned int    Size            ;//Bitmap info Header size
	unsigned int    Width           ; //Bitmap info Header width
	unsigned int    Height          ;//Bitmap info Header Height
	unsigned short  Planes          ;
	unsigned short  BitCount        ;
	unsigned int    Compression     ;
	unsigned int    ImageSize       ;
	int			        X_PixelsPerM    ;
	int			        Y_PixelsPerM    ; 
	unsigned int    ColorsUsed      ;
	unsigned int    ColorsImportant ;

}InfoHeader;

typedef struct _ColorTable {
	unsigned char   rgb_Blue      ;
	unsigned char   rgb_Green     ;
	unsigned char   rgb_Red       ; 
	unsigned char   rgb_Reserved  ;
}ColorTable;

int main()
{
	BITMAPFILEHEADER HEADER;
	InfoHeader Info;
	ColorTable RGB;

  HEADER.Signature[0] ='B';
  HEADER.Signature[1] ='M';
  HEADER.FileSize = sizeof(BITMAPFILEHEADER) + sizeof(InfoHeader) + sizeof(ColorTable) + 640*480*3;//x pixel * y pixel * 3 byte (8+8+8 bits)
  HEADER.DataOffset = sizeof(BITMAPFILEHEADER) + sizeof(InfoHeader)+ sizeof(ColorTable);
  Info.Size         = 40;          
  Info.Width        = 640;          
  Info.Height       = 480;     
  Info.Planes       = 1;         
  Info.BitCount     = 24; //bits per pixel 
  Info.Compression  = 0;
  Info.ImageSize    = 0; //bitcount에 따라서 결정 됨
 /* Info.X_PixelsPerM   (dpi)
  Info.Y_PixelsPerM   
  Info.ColorsUsed     
  Info.ColorsImportant //할당하지 않으면 all(0=all)  */

  FILE *BMP_GEN;
   BMP_GEN= fopen("BMP_GEN.bmp", "wb");//wb는 읽고 쓰기 모드
 
     ColorTable ColorTable;

     for (int y = 0; y < 480; y++) {
            for (int x = 0; x < 640; x++) {
             
			}
            
     }
  fclose(BMP_GEN);//fopen을 하면 닫아줘야함
  return 0;
}
#endif

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include "bmp_gen_h.h"
#pragma pack(1)
#define _GRAY_SCALE_

void print_prt(void* p, int size) {
    int i;
    for (i = 0; i < size; i++) {
        printf("\t p[%d]=0x%x \n", i, *((unsigned char*)p + i));
    }
}

int main(void)
{
    int bmpHeaderSig_sz;
    int bmpHeader_sz;
    int bmpInfoHeader_sz;

    unsigned char bmp_img[VERTICAL][HORIZONTAL][PIXELS];


    bmp_Header_Sig      headerSig;
    bmp_Header_Sig      *phd_Sig;

    bmp_Header           header;
    bmp_Header          *pHD;
    
    bmp_InfoHeader      infoHeader;
    bmp_InfoHeader      * pInfoHD;
    
    bmpHeaderSig_sz = sizeof(bmp_Header); //12byte 
    bmpHeader_sz = sizeof(bmp_Header_Sig); //14 - 2 = 12byte
    bmpInfoHeader_sz = sizeof(bmp_InfoHeader); //40byte

    printf("BMP generator !!\n"); 
    printf("bmp_Header_Sig size = %d \n", bmpHeader_sz);//2
    printf("bmp_Header     size = %d \n", bmpHeaderSig_sz);//12
    printf("bmp_InfoHeader size = %d \n", bmpInfoHeader_sz);//40
    printf("pInfoHD size = %d\n", sizeof(pInfoHD));
    printf("pInfoHD size = %d\n", sizeof(*pInfoHD));

    memset(&headerSig   , 0 ,  sizeof(bmp_Header_Sig));
    memset(&header      , 0 ,  sizeof(bmp_Header));
    memset(&infoHeader  , 0 ,  sizeof(bmp_InfoHeader));
    

    headerSig.Signature = ('M' << 8 | 'B');
    //headerSig.Signature = "BM"; //char[0] char[1] 형식으로 주던가 해야 제대로 들어가짐
    pHD = &header; //포인터로 선언
    pHD->FileSize =bmpHeaderSig_sz+bmpHeader_sz+bmpInfoHeader_sz+HORIZONTAL*VERTICAL*PIXELS ; //이렇게 작성해야 File size안에 값이 들어감
    pHD->DataOffset = bmpHeaderSig_sz + bmpHeader_sz + bmpInfoHeader_sz;
    print_prt((void*)pHD , bmpHeaderSig_sz);
    //bmp_InfoHeader      infoHeader;
    //bmp_InfoHeader* pInfoHD;
    infoHeader.Size = sizeof(bmp_Header); // 40byte가 이미 할당되어 있으니 하나하나 채워넣거나

    pInfoHD = &infoHeader;
    pInfoHD->Size = sizeof(bmp_InfoHeader);
    pInfoHD->Width = HORIZONTAL;
    pInfoHD->Height = VERTICAL;
    pInfoHD->Planes =1;
    pInfoHD->BitCount=8*PIXELS;
    pInfoHD->Compression = 0; //

    printf("\nbmpInfo Header \n");
    print_prt((void*)pInfoHD, bmpInfoHeader_sz);
    
    /*for (int y = 0; y<VERTICAL; y++) {
        for (int x = 0; x < 80; x++) {
            bmp_img[y][x][2] = 255;//R
            bmp_img[y][x][1] = 255;//G
            bmp_img[y][x][0] = 255;//B
        }
        for (int x = 80; x < 160; x++) {//128
            bmp_img[y][x][2] = 192;
            bmp_img[y][x][1] = 192;
            bmp_img[y][x][0] = 0;
        }
        for (int x = 160; x < 240; x++) {
            bmp_img[y][x][2] = 0;
            bmp_img[y][x][1] = 192;
            bmp_img[y][x][0] = 192;
        }
        for (int x = 240; x < 320; x++) {
            bmp_img[y][x][2] = 0;
            bmp_img[y][x][1] = 192;
            bmp_img[y][x][0] = 0;
        }
        for (int x = 320; x < 400; x++) {
            bmp_img[y][x][2] = 192;
            bmp_img[y][x][1] = 0;
            bmp_img[y][x][0] = 192;
        }
        for (int x = 400; x < 480; x++) {
            bmp_img[y][x][2] = 192;
            bmp_img[y][x][1] = 0;
            bmp_img[y][x][0] = 0;
        }
        for (int x = 480; x < 560; x++) {
            bmp_img[y][x][2] = 0;
            bmp_img[y][x][1] = 0;
            bmp_img[y][x][0] = 192;
        }
        for (int x = 560; x < HORIZONTAL; x++) {
            bmp_img[y][x][2] = 0;
            bmp_img[y][x][1] = 0;
            bmp_img[y][x][0] = 0;
        }
    }*/

#if defined (_GRAY_SCALE_) 
    unsigned char gray;
#endif

    unsigned char r, g, b;


    for (int y = 0; y < VERTICAL; y++) {
        for (int x = 0; x < HORIZONTAL; x++) {
            if (x < 1*CLOLR_BAR_WD) { r = 255; g = 255; b = 255; }
            else if (x >= 1 * CLOLR_BAR_WD  && x <2 * CLOLR_BAR_WD) { r = 192; g = 192; b = 0; }
            else if (x >= 2 * CLOLR_BAR_WD && x < 3 * CLOLR_BAR_WD) { r = 0; g = 192; b = 192; }
            else if (x >= 3 * CLOLR_BAR_WD && x < 4 * CLOLR_BAR_WD) { r = 0; g = 192; b = 0;   }
            else if (x >= 4 * CLOLR_BAR_WD && x < 5 * CLOLR_BAR_WD) { r = 192; g = 0; b = 192; }
            else if (x >= 5 * CLOLR_BAR_WD && x < 6 * CLOLR_BAR_WD) { r = 192; g = 0; b = 0;   }
            else if (x >= 6 * CLOLR_BAR_WD && x < 7 * CLOLR_BAR_WD) { r = 0; g = 0; b = 192; }
            else  { r = 0; g = 0; b = 0 ; }

#if defined (_GRAY_SCALE_)
            gray = 0.3 * r + 0.59 * g + 0.11 * b;
            r = gray;
            g = gray;
            b = gray;
#endif
            
            bmp_img[y][x][0] = r;
            bmp_img[y][x][1] = g;
            bmp_img[y][x][2] = b; //frame buffer

        }
    }
    
    FILE* fp = fopen(BMP_FILE_NAME, "wb");
    fwrite((unsigned char*)&headerSig, 1, sizeof(bmp_Header_Sig), fp);
    fwrite((unsigned char*)&header, 1, sizeof(header), fp);
    fwrite((unsigned char*)&infoHeader, 1, sizeof(infoHeader), fp);
    fwrite((unsigned char*)&bmp_img, 1, sizeof(bmp_img), fp);
    printf("BMP generated... \n");



    fclose(fp);
    return 0;


}
#pragma once
#if !defined(_BMP_GEN_H_)
#define _BMP_GEN_H_

#define HORIZONTAL  (640)
#define VERTICAL	(480)
#define PIXELS      (3)
#define BMP_FILE_NAME   ("generated_bmp.bmp")

typedef struct {
	short   Signature;
}bmp_Header_Sig; //2byte


typedef struct {
	//short   Signature[2];
	unsigned int   FileSize;
	unsigned int   reserved;
	unsigned int   DataOffset;
}bmp_Header; //12byte

#if 1
typedef struct {
	int			Size;
	int			Width;
	int			Height;
	short		Planes;
	short		BitCount;
	int			Compression;
	int			ImageSize;
	int			X_PixelsPerM;
	int			Y_PixelsPerM;
	int			ColorsUsed;
	int			ColorsImportant;
}bmp_InfoHeader; //40byte
#endif



#endif

gray_scale 적용

728x90