Hi,
Here i am sending the code for scaling the image and saving the
scaled image into another file .I think till scaling is fine and
after that the problem occurs can u please find me the solution how
to save the scaled image..
hoping for a sooner reply..
#include <stdio.h>
#include <stdlib.h>
#include <dos.h>
#include <mem.h>
/* the structure for a bitmap. */
typedef struct bitmap
{
int width;
int height;
char *data;
} bit_map;
void load_bmp(char *file,bit_map *b);
void scale(bit_map *source, bit_map *dest);
void putimage(bit_map *r, unsigned char * Buffer);
void main()
{
int i,x,y;
struct bitmap ibmp;
struct bitmap obmp={640,480};
load_bmp("cus3.bmp",&ibmp);
printf("\n\n in function main\n");
printf("\n\n i am in line %d",__LINE__);
scale(&ibmp,&obmp);
}
void load_bmp(char *file,bit_map *b)
{
FILE *fp;
long index;
int num_colors;
int x,i;
if ((fp = fopen(file,"rb")) == NULL)
{
printf("Error opening file %s.\n",file);
exit(1);
}
fseek(fp, 18, SEEK_SET);
fread(&b->width, sizeof(int), 1, fp);
fseek(fp, 22, SEEK_SET);
fread(&b->height,sizeof(int), 1, fp);
fseek(fp, 46, SEEK_SET);
fread(&num_colors,sizeof(int), 1, fp);
// assume we are working with an 8-bit file
if (num_colors==0)
num_colors=256;
// allocating memory
if ((b->data = (char *) malloc((int)(b->width*b->height))) == NULL)
{
fclose(fp);
printf("Error allocating memory for file %s.\n",file);
exit(1);
}
// read the bitmap
for(index=(b->height-1)*b->width;index>=0;index-=b->width)
{
for(x=0;x<b->width;x++)
{
b->data[(int)index+x]=(char)fgetc(fp);
}
}
fclose(fp);
}
void scale(bit_map *source,bit_map *dest)
{
unsigned char *Image, *screen, *PtrScreen;
float XRatio, YRatio, X, Y;
int Xc, Yc;
int outwidth, outheight;
int inwidth, inheight;
int *XPoints, *YPoints, Offset;
inwidth = source->width + 1;
inheight = source->height + 1;
outwidth = dest->width+1;
outheight = dest->height+1;
Image = (unsigned char *)malloc((source->width)*(source-
>height))
screen = (unsigned char *)malloc((dest->width)*(dest->height))
XRatio = (float) ((float)inwidth /(float)outwidth );
YRatio = (float) ((float)inheight /(float)outheight );
if((XPoints = (int *)malloc(outwidth * sizeof(int)))==NULL)
printf("\n malloc fails for xpoints");
for( Xc=0, X = 0 ; Xc < outwidth ; X+=XRatio, Xc++ )
XPoints[Xc] = X;
// for the y axis
if(( YPoints = (int *)malloc(outheight * sizeof(int)))==NULL)
printf("\n malloc fails for ypoints");
for( Yc=0, Y = 0 ; Yc < outheight ; Y+=YRatio, Yc++ )
YPoints[Yc] = Y;
//use a pointer to run through the array
PtrScreen = screen;
for( Yc = 0 ; Yc < outheight ; Yc++ )
{
Offset = YPoints[Yc] * inwidth;
for( Xc = 0 ; Xc < outwidth ; Xc++)
*PtrScreen++ = dest->data[Offset + XPoints[Xc]];
}
//put the result in the file
putimage(dest,screen);
//free up the allocated memory
free(screen);
free(Image);
free(YPoints);
free(XPoints);
}
*/
void putimage(bit_map *r, unsigned char * Buffer)
{
FILE *fp;
int y, Width;
unsigned char *Ptr = Buffer;
Width = r->width;
fp=fopen("output.bmp","w+");
for(y =0 ; y <= r->height ; y++, Ptr+=Width)
memcpy(fp, Ptr, Width);
}