본문 바로가기
Computer Science/영상처리

(C/C++) Image Format

by BaekDaBang 2024. 3. 27.

1. Image File Format

Original

  • RAW

Lossy Compression

  • JPG

Lossless Compression

  • BMP
  • TIFF
  • PNG

 

2. RAW File Format

RAW 파일은 압축 및 가공이 되지 않은 이미지 데이터를 포함하고 있다.

사진을 찍을 때 뷰파인더로 보이는 모든 세부 정보를 담을 수 있다.

RAW 파일 포맷은 모든 래스터 파일 유형 중 가장 많은 양의 세부 정보를 저장한다.

 

3. BMP File Format

 

4. BMP File Input

// BMP File Input
BITMAPFILEHEADER bmpFile;
BITMAPINFOHEADER bmpInfo;
FILE* inputFile = NULL;
inputFile = fopen("AICenter.bmp", "rb");
fread(&bmpFile, sizeof(BITMAPFILEHEADER), 1, inputFile);
fread(&bmpInfo, sizeof(BITMAPINFOHEADER), 1, inputFile);

int width = bmpInfo.biWidth;
int height = bmpInfo.biHeight;
int size = bmpInfo.biSizeImage;
int bitCnt = bmpInfo.biBitCount;
int stride = (((bitCnt/8)*width)+3)/4*4;
printf("W: %d(%d)\nH: %d\nS: %d\nD: %d\n\n", width, stride, height, size, bitCnt);

unsigned char* inputImg = NULL, * outputImg = NULL;
inputImg = (unsigned char*)calloc(size, sizeof(unsigned char));
outputImg = (unsigned char*)calloc(size, sizeof(unsigned char));
fread(inputImg, sizeof(unsigned char), size, inputFile);

 

5. BMP File Output

// Original Copy
for (int i = 0; i < height; i++) {
   for (int j = 0; j < width; j++) {
      outputImg[j * stride + 3 * i + 0] = inputImg[j * stride + 3 * i + 0];
      outputImg[j * stride + 3 * i + 1] = inputImg[j * stride + 3 * i + 1];
      outputImg[j * stride + 3 * i + 2] = inputImg[j * stride + 3 * i + 2];
   }
}

// BMP File Output
FILE* outputFile = fopen("Output_1.bmp", "wb");
FILE* outputFile_2 = fopen("Output_2.bmp", "wb");
fwrite(&bmpFile, sizeof(BITMAPFILEHEADER), 1, outputFile);     //RGB
fwrite(&bmpInfo, sizeof(BITMAPINFOHEADER), 1, outputFile);
fwrite(outputImg, sizeof(unsigned char), size, outputFile);

fwrite(&bmpFile, sizeof(BITMAPFILEHEADER), 1, outputFile_2);   //YYY
fwrite(&bmpInfo, sizeof(BITMAPINFOHEADER), 1, outputFile_2);
fwrite(outputImg_2, sizeof(unsigned char), size, outputFile_2);

free(outputImg);
free(inputImg);
free(outputImg_2);
fclose(inputFile);
fclose(outputFile);
fclose(outputFile_2);

 

6. Conclusion

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

int main(int argc, char* argv[])
{
    //BMP File Input
    BITMAPFILEHEADER bmpFile;
    BITMAPINFOHEADER bmpInfo;
    FILE* inputFile = NULL;
    inputFile = fopen("AICenter.bmp", "rb");
    fread(&bmpFile, sizeof(BITMAPFILEHEADER), 1, inputFile);
    fread(&bmpInfo, sizeof(BITMAPINFOHEADER), 1, inputFile);

    int width = bmpInfo.biWidth;
    int height = bmpInfo.biHeight;
    int size = bmpInfo.biSizeImage;
    int bitCnt = bmpInfo.biBitCount;
    int stride = (((bitCnt/8)*width)+3)/4*4;
    printf("W: %d(%d)\nH: %d\nS: %d\nD: %d\n\n", width, stride, height, size, bitCnt);

    unsigned char* inputImg = NULL, * outputImg = NULL;
    inputImg = (unsigned char*)calloc(size, sizeof(unsigned char));
    outputImg = (unsigned char*)calloc(size, sizeof(unsigned char));
    fread(inputImg, sizeof(unsigned char), size, inputFile);

    // Original Copy
    for (int i = 0; i < height; i++) 
    {
        for (int j = 0; j < width; j++) 
        {
            outputImg[j * stride + 3 * i + 0] = inputImg[j * stride + 3 * i + 0];
            outputImg[j * stride + 3 * i + 1] = inputImg[j * stride + 3 * i + 1];
            outputImg[j * stride + 3 * i + 2] = inputImg[j * stride + 3 * i + 2];
        }
    }

    // BMP File Output
    FILE* outputFile = fopen("Output_1.bmp", "wb");
    FILE* outputFile_2 = fopen("Output_2.bmp", "wb");
    fwrite(&bmpFile, sizeof(BITMAPFILEHEADER), 1, outputFile);     //RGB
    fwrite(&bmpInfo, sizeof(BITMAPINFOHEADER), 1, outputFile);
    fwrite(outputImg, sizeof(unsigned char), size, outputFile);

    free(outputImg);
    free(inputImg);
    fclose(inputFile);
    fclose(outputFile);
    
    return 0;
}

'Computer Science > 영상처리' 카테고리의 다른 글

(C/C++) Filter (Denoising)  (0) 2024.04.01
(C/C++) Quality Evaluation(PSNR)  (0) 2024.03.27
(C/C++) Pixel Operation  (0) 2024.03.27
(C/C++) Color Model  (0) 2024.03.27
(C/C++) Resolution(해상도)  (0) 2024.03.27