#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문만 고치면 될거가튼데..