상하 반전하는 프로그램 거의 다 짬..

컴파일 에러가 안뜨는데

 

실행 종료되는게 문제ㅠㅠㅠ

내가 오늘 고민해서 포인터 배열을 두개를 만든담에

pixel_ptr 을 거꾸로 수행할 p배열을 만들어봤는데..

대충 이렇게 하는게 맞을려나? 아무튼 보기힘들고 병신같은거 나도알아..


실행만 되게 도와줘




#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->width, sizeof (unsigned int), 1, fp);
fread (&bmp_header->height, sizeof (unsigned int), 1, fp);
fread (&bmp_header->planes_n_and_bits_per_pixel_n, 4, 1, fp);
fread (&bmp_header->c_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->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->width, sizeof (unsigned int), 1, wp);
fwrite (&bmp_header->height, sizeof (unsigned int), 1, wp);
fwrite (&bmp_header->planes_n_and_bits_per_pixel_n, 4,1, wp);
fwrite (&bmp_header->c_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->important_colors, sizeof (unsigned int),1, wp);
}


char *p=(char *)malloc(bmp_header.image_size_with_padding_in_bytes);
 


void image_flip (char*pixel_ptr, BMP_Header * bmp_header, FILE *fp){
 
 fread (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_line = bmp_header->width*3;

 unsigned int padding_size = (4 - (pure_number_of_bytes_per_line % 4)) %4;


   // (pure_number_of_bytes_per_line % 4);

 unsigned int one_line_width_with_padding = bmp_header->width*3 + padding_size;

 for (unsigned int i = 0; i < bmp_header->height; i++)

  for (unsigned int j = 0; j < bmp_header->width; j++){
  int first_byte_postion_in_a_line = i * one_line_width_with_padding;
   
  p[bmp_header->height-1-i]=pixel_ptr[i];
  }


     /*
     for (int i = 0; i < 10; i++) //bmp_header->height
     for (int j = 0; j < bmp_header->width; j++ )
     {
     int first_byte_postion_in_a_line =
     i*one_line_width_with_padding;
     pixel_ptr[first_byte_postion_in_a_line+(j*3)] =255;
     pixel_ptr[first_byte_postion_in_a_line+(j*2)] =0;
     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 * pixel_ptr;
 pixel_ptr  = (char *)malloc(bmp_header.image_size_with_padding_in_bytes);
 
 image_flip (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 (p, bmp_header.image_size_with_padding_in_bytes,1, wp);

 fclose(fp);

 fclose(wp);

 free(pixel_ptr);
 
}