H
Harry
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
void scramble(void);
struct bmp_header
{
short int sig;
int size_bmp;
short int res1;
short int res2;
int offset;
int size_bmpinfo_header;
int width;
int height;
short int no_planes;
short int bits_pixel;
int comp_type;
int size_imgdata;
int hor_res;
int ver_res;
int no_colors;
int no_impcolors;
};
FILE *fp;
FILE *fout;
struct bmp_header *info;
main()
{
char ch;
info=malloc(54);
fp=fopen("duck.bmp","r");
fout=fopen("scramble.bmp","w");
if(fp==NULL)
printf("Error opening file\n");
fread(&info->sig,2,1,fp);
fread(&info->size_bmp,4,1,fp);
fread(&info->res1,2,1,fp);
fread(&info->res2,2,1,fp);
fread(&info->offset,4,1,fp);
fread(&info->size_bmpinfo_header,4,1,fp);
fread(&info->width,4,1,fp);
fread(&info->height,4,1,fp);
fread(&info->no_planes,2,1,fp);
fread(&info->bits_pixel,2,1,fp);
fread(&info->comp_type,4,1,fp);
fread(&info->size_imgdata,4,1,fp);
fread(&info->hor_res,4,1,fp);
fread(&info->ver_res,4,1,fp);
fread(&info->no_colors,4,1,fp);
fread(&info->no_impcolors,4,1,fp);
printf("Signature-----------%X\n",info->sig);
printf("Size of Bmp File----%d\n",info->size_bmp);
printf("Offset to Image data-%d\n",info->offset);
printf("Size of Header-------%d\n",info->size_bmpinfo_header);
printf("width----------------%d\n",info->width);
printf("Height---------------%d\n",info->height);
printf("Bits per pixel-------%d\n",info->bits_pixel);
printf("size of image data---%d\n",info->size_imgdata);
printf("Do you want to scramble imagey/n)\n");
while(getchar(ch)=='y');
scramble();
fclose(fp);
}
void scramble()
{
char *top,*bottom;
top=(char*)malloc((info->height/2)*(info->width)*(info->bits_pixel/
8));
if(top==NULL)
printf("Error allocating memory\n");
bottom=(char*)malloc((info->height/2)*(info->width)*(info->bits_pixel/
8));
if(bottom==NULL)
printf("Error allocating memory\n");
fread(top,1,((info->height/2)*(info->width)*(info->bits_pixel/
8)),fp);
if(ferror(fp)!=0)
printf("Error reading stream\n");
if(feof(fp)!=0)
printf("End of file reached\n");
fread(bottom,((info->height/2)*(info->width)*(info->bits_pixel/8)),
1,fp);
fwrite(&info->sig,1,2,fout);
fwrite(&info->size_bmp,1,4,fout);
fwrite(&info->res1,1,2,fout);
fwrite(&info->res2,1,2,fout);
fwrite(&info->offset,1,4,fout);
fwrite(&info->size_bmpinfo_header,1,4,fout);
fwrite(&info->width,1,4,fout);
fwrite(&info->height,1,4,fout);
fwrite(&info->no_planes,1,2,fout);
fwrite(&info->bits_pixel,1,2,fout);
fwrite(&info->comp_type,1,4,fout);
fwrite(&info->size_imgdata,1,4,fout);
fwrite(&info->hor_res,1,4,fout);
fwrite(&info->ver_res,1,4,fout);
fwrite(&info->no_colors,1,4,fout);
fwrite(&info->no_impcolors,1,4,fout);
fwrite(bottom,1,((info->height/2)*(info->width)*(info->bits_pixel/
8)),fout);
fwrite(top,1,((info->height/2)*(info->width)*(info->bits_pixel/
8)),fout);
fclose(fout);
}
Hi all,in this code i am trying to open a bmp file and display it's
header information which is first 54 bytes which i have succesfully
done.In the same code,I am trying to invert the image .i.e I have to
make the bottom half of the image to appear in the top half and vice-
versa.
But my file pointer reaches the end of file when i am in the function
scramble().
Why does this happen and how to eliminate it?
2)Is it possible to find the size of integer without using the
sizeof() operator?
#include<stdlib.h>
#include<malloc.h>
void scramble(void);
struct bmp_header
{
short int sig;
int size_bmp;
short int res1;
short int res2;
int offset;
int size_bmpinfo_header;
int width;
int height;
short int no_planes;
short int bits_pixel;
int comp_type;
int size_imgdata;
int hor_res;
int ver_res;
int no_colors;
int no_impcolors;
};
FILE *fp;
FILE *fout;
struct bmp_header *info;
main()
{
char ch;
info=malloc(54);
fp=fopen("duck.bmp","r");
fout=fopen("scramble.bmp","w");
if(fp==NULL)
printf("Error opening file\n");
fread(&info->sig,2,1,fp);
fread(&info->size_bmp,4,1,fp);
fread(&info->res1,2,1,fp);
fread(&info->res2,2,1,fp);
fread(&info->offset,4,1,fp);
fread(&info->size_bmpinfo_header,4,1,fp);
fread(&info->width,4,1,fp);
fread(&info->height,4,1,fp);
fread(&info->no_planes,2,1,fp);
fread(&info->bits_pixel,2,1,fp);
fread(&info->comp_type,4,1,fp);
fread(&info->size_imgdata,4,1,fp);
fread(&info->hor_res,4,1,fp);
fread(&info->ver_res,4,1,fp);
fread(&info->no_colors,4,1,fp);
fread(&info->no_impcolors,4,1,fp);
printf("Signature-----------%X\n",info->sig);
printf("Size of Bmp File----%d\n",info->size_bmp);
printf("Offset to Image data-%d\n",info->offset);
printf("Size of Header-------%d\n",info->size_bmpinfo_header);
printf("width----------------%d\n",info->width);
printf("Height---------------%d\n",info->height);
printf("Bits per pixel-------%d\n",info->bits_pixel);
printf("size of image data---%d\n",info->size_imgdata);
printf("Do you want to scramble imagey/n)\n");
while(getchar(ch)=='y');
scramble();
fclose(fp);
}
void scramble()
{
char *top,*bottom;
top=(char*)malloc((info->height/2)*(info->width)*(info->bits_pixel/
8));
if(top==NULL)
printf("Error allocating memory\n");
bottom=(char*)malloc((info->height/2)*(info->width)*(info->bits_pixel/
8));
if(bottom==NULL)
printf("Error allocating memory\n");
fread(top,1,((info->height/2)*(info->width)*(info->bits_pixel/
8)),fp);
if(ferror(fp)!=0)
printf("Error reading stream\n");
if(feof(fp)!=0)
printf("End of file reached\n");
fread(bottom,((info->height/2)*(info->width)*(info->bits_pixel/8)),
1,fp);
fwrite(&info->sig,1,2,fout);
fwrite(&info->size_bmp,1,4,fout);
fwrite(&info->res1,1,2,fout);
fwrite(&info->res2,1,2,fout);
fwrite(&info->offset,1,4,fout);
fwrite(&info->size_bmpinfo_header,1,4,fout);
fwrite(&info->width,1,4,fout);
fwrite(&info->height,1,4,fout);
fwrite(&info->no_planes,1,2,fout);
fwrite(&info->bits_pixel,1,2,fout);
fwrite(&info->comp_type,1,4,fout);
fwrite(&info->size_imgdata,1,4,fout);
fwrite(&info->hor_res,1,4,fout);
fwrite(&info->ver_res,1,4,fout);
fwrite(&info->no_colors,1,4,fout);
fwrite(&info->no_impcolors,1,4,fout);
fwrite(bottom,1,((info->height/2)*(info->width)*(info->bits_pixel/
8)),fout);
fwrite(top,1,((info->height/2)*(info->width)*(info->bits_pixel/
8)),fout);
fclose(fout);
}
Hi all,in this code i am trying to open a bmp file and display it's
header information which is first 54 bytes which i have succesfully
done.In the same code,I am trying to invert the image .i.e I have to
make the bottom half of the image to appear in the top half and vice-
versa.
But my file pointer reaches the end of file when i am in the function
scramble().
Why does this happen and how to eliminate it?
2)Is it possible to find the size of integer without using the
sizeof() operator?