#include <stdio.h>
#include "BMP_header.h"
#include <malloc.h>
BMP_Header bmp_header;
void BMP_file_header_read (FILE *fp, BMP_Header * bmp_header)
{
fread (&bmp_header->charB, sizeof (char), 1, fp);
fread (&bmp_header->charM, sizeof (char), 1, fp);
fread (&bmp_header->filesize, sizeof (unsigned int), 1, fp);
fread (&bmp_header->reserved, sizeof (unsigned int), 1, fp);
fread (&bmp_header->pixel_offset, sizeof (unsigned int), 1, fp);
fread (&bmp_header->header_size, sizeof (unsigned int), 1, fp);
fread (&bmp_header->image_width, sizeof (unsigned int), 1, fp);
fread (&bmp_header->image_height, sizeof (unsigned int), 1, fp);
fread (&bmp_header->number_of_planes_and_number_of_bits_per_pixel, 4, 1, fp);
fread (&bmp_header->compression_type, sizeof (unsigned int), 1, fp);
fread (&bmp_header->image_size_with_padding_in_bytes, sizeof (unsigned int), 1, fp);
fread (&bmp_header->horizontal_resolution, sizeof (unsigned int), 1, fp);
fread (&bmp_header->veritical_resolution, sizeof (unsigned int), 1, fp);
fread (&bmp_header->number_of_colors, sizeof (unsigned int), 1, fp);
fread (&bmp_header->number_of_important_colors, sizeof (unsigned int), 1, fp);
}
void BMP_file_header_write (FILE *wp, BMP_Header * bmp_header)
{
fwrite (&bmp_header->charB, sizeof (char), 1, wp);
fwrite (&bmp_header->charM, sizeof (char), 1, wp);
fwrite (&bmp_header->filesize, sizeof (unsigned int), 1, wp);
fwrite (&bmp_header->reserved, sizeof (unsigned int), 1, wp);
fwrite (&bmp_header->pixel_offset, sizeof (unsigned int), 1, wp);
fwrite (&bmp_header->header_size, sizeof (unsigned int), 1, wp);
fwrite (&bmp_header->image_width, sizeof (unsigned int), 1, wp);
fwrite (&bmp_header->image_height, sizeof (unsigned int), 1, wp);
fwrite (&bmp_header->number_of_planes_and_number_of_bits_per_pixel, 4,
1, wp);
fwrite (&bmp_header->compression_type, sizeof (unsigned int), 1,
wp);
fwrite (&bmp_header->image_size_with_padding_in_bytes,
sizeof (unsigned int), 1, wp);
fwrite (&bmp_header->horizontal_resolution, sizeof (unsigned int),
1, wp);
fwrite (&bmp_header->veritical_resolution, sizeof (unsigned int),
1, wp);
fwrite (&bmp_header->number_of_colors, sizeof (unsigned int),
1, wp);
fwrite (&bmp_header->number_of_important_colors, sizeof (unsigned int),
1, wp);
}
void change_color_BluetoZero (char * image_pixel_ptr,
BMP_Header * bmp_header, FILE *fp)
{
fread (image_pixel_ptr,bmp_header->image_size_with_padding_in_bytes
,1,fp);
// change the Blue part of each pixel to zero
// first calculate the padding size
unsigned int pure_number_of_bytes_per_one_line
= bmp_header->image_width*3;
unsigned int padding_size =
(4 - (pure_number_of_bytes_per_one_line % 4)) %4;
// (pure_number_of_bytes_per_one_line % 4);
char *p = image_pixel_ptr;
unsigned int one_line_width_with_padding
= bmp_header->image_width*3 + padding_size;
for (unsigned int i = 0; i < bmp_header->image_height; i++)
for (unsigned int j = 0; j < bmp_header->image_width; j++)
{
int first_byte_postion_in_a_line = i * one_line_width_with_padding;
image_pixel_ptr[first_byte_postion_in_a_line+(j*3)] =image_pixel_ptr[first_byte_postion_in_a_line+(j*3)] - 10;
}
/*
for (int i = 0; i < 10; i++) //bmp_header->image_height
for (int j = 0; j < bmp_header->image_width; j++ )
{
int first_byte_postion_in_a_line =
i*one_line_width_with_padding;
image_pixel_ptr[first_byte_postion_in_a_line+(j*3)] =255;
image_pixel_ptr[first_byte_postion_in_a_line+(j*2)] =0;
image_pixel_ptr[first_byte_postion_in_a_line+(j)] =0;
}
*/
}
int main (void)
{
// first read a bitmap file
FILE * fp = fopen ("D:\\Desert.bmp", "rb"); // file open
BMP_file_header_read (fp, &bmp_header);
// BMP header information completely read
// now read the pixel information
// first prepare memory for the pixel data
// with image_size_with_padding_in_bytes
char * image_pixel_ptr;
image_pixel_ptr = (char *)malloc(bmp_header.image_size_with_padding_in_bytes);
change_color_BluetoZero (image_pixel_ptr, &bmp_header, fp);
// write a new bitmap file
FILE * wp = fopen ("D:\\Blue_changed_BMP2.bmp", "wb");
BMP_file_header_write (wp, &bmp_header);
fwrite (image_pixel_ptr, bmp_header.image_size_with_padding_in_bytes
, 1, wp);
fclose(fp);
fclose(wp);
free(image_pixel_ptr);
}
변수가 너무 길다거나 하는건 제발 무시해줘..
bmp 파일을 오픈해서 이것저것 해보는중인데
내가 성공한건 사진에서 원하는 색을 변경하는것까지..
이제 더 나아가 사진 좌우대칭을 바꾼다거나 상하를 돌려보고싶은데
이건 언어문제가 아니라 내가 돌머리라서 어떻게 짜야할지모르겠다.
대충 for문만 고치면 될거가튼데..
Bmp좌표계는 수학에 좌표계랑 같음 0,0에 해당하는 픽셀이 젤 밑 왼쪽임.
char *pDC; pDC = new (char*)malloc(sizeof(image_pixel_ptr));
for (unsigned int i = 0; i < bmp_header->image_height; i++) for (unsigned int j = 0; j < bmp_header->image_width; j++) { int first_byte_postion_in_a_line = i * one_line_width_with_padding; pDC[first_byte_postion_in_a_line+(j*3)] =image_pixel_ptr[first_byte_postion_in_a_line+(j*3)]; }
한달반 됬는데 쓸데없이 과도한걸 하는거 같음 , 맨날 저기서 진도가 안나가잖아요
이런식으로 임시 파레트를 하나 만들어놓고 써도 될듯
반복문 조건부분에 unsigned 변수가 들어가는것은 안좋은 습관임
아 맞다.. 포인터로 취득한 변수를 사이즈오프 해봐야 헛짓이지. 알아서 가공해 ㅠㅠ 동적할당 다 까먹었당
함수명은 길어도 동작의 의미를 나타내면 좋다고 생각하는데 변수는 ㅋㅋㅋ 너무 자세해도
우와 미친거아니야 어떻게한달반에저런걸 우와 대단해요!! [핡]