/***************************************
* copyright (c) Vanden Berghen Frank *
* V 1.0 *
* *************************************/
#include <stdlib.h>
#include <stdio.h>
#include <malloc.h>
#include <memory.h>
#include <string.h>
#include <math.h>
#include "image.h"
#ifdef WIN32
#pragma pack( push, enter_bitmap_def1 )
#pragma pack(1)
typedef struct tagBITMAPFILEHEADER {
WORD bfType;
DWORD bfSize;
WORD bfReserved1;
WORD bfReserved2;
DWORD bfOffBits;
} BITMAPFILEHEADER;
typedef struct tagBITMAPINFOHEADER{
DWORD biWidth;
DWORD biHeight;
WORD biPlanes;
WORD biBitCount;
DWORD biCompression;
DWORD biSizeImage;
DWORD biXPelsPerMeter;
DWORD biYPelsPerMeter;
DWORD biClrUsed;
DWORD biClrImportant;
} BITMAPINFOHEADER;
typedef struct tagBITMAPCOREHEADER {
WORD bcWidth;
WORD bcHeight;
WORD bcPlanes;
WORD bcBitCount;
} BITMAPCOREHEADER;
#pragma pack( pop, enter_bitmap_def1 )
#endif
#ifdef __GNUG__
struct tagBITMAPFILEHEADER {
WORD bfType;
DWORD bfSize;
WORD bfReserved1;
WORD bfReserved2;
DWORD bfOffBits;
} __attribute__((packed));
typedef struct tagBITMAPFILEHEADER BITMAPFILEHEADER;
struct tagBITMAPINFOHEADER{
DWORD biWidth;
DWORD biHeight;
WORD biPlanes;
WORD biBitCount;
DWORD biCompression;
DWORD biSizeImage;
DWORD biXPelsPerMeter;
DWORD biYPelsPerMeter;
DWORD biClrUsed;
DWORD biClrImportant;
} __attribute__((packed));
typedef struct tagBITMAPINFOHEADER BITMAPINFOHEADER;
struct tagBITMAPCOREHEADER {
WORD bcWidth;
WORD bcHeight;
WORD bcPlanes;
WORD bcBitCount;
} __attribute__((packed));
typedef struct tagBITMAPCOREHEADER BITMAPCOREHEADER;
#endif
DWORD invertbitsL(DWORD l)
{
DWORD r;
char *a=(char*)&l,*b=(char*)&r;
b[0]=a[3]; b[1]=a[2]; b[2]=a[1]; b[3]=a[0];
return r;
};
WORD invertbitsS(WORD s)
{
WORD r;
char *a=(char*)&s,*b=(char*)&r;
b[0]=a[1]; b[1]=a[0];
return r;
};
image::image(char *name) : fgl(NULL)
{
BYTE *tmp,*tmp2;
FILE *fr;
BITMAPFILEHEADER bmf;
BITMAPCOREHEADER bmc;
BITMAPINFOHEADER bmi;
DWORD a,w;
int h;
if ((fr=fopen(name,"rb"))!=NULL)
{
fread(&bmf,sizeof(BITMAPFILEHEADER),1,fr);
fread(&a,sizeof(a),1,fr);
#ifdef __sun__
a=invertbitsL(a);
#endif
if (a==12)
{
fread(&bmc,sizeof(BITMAPCOREHEADER),1,fr);
#ifdef __sun__
Width=invertbitsS(bmc.bcWidth);
Height=invertbitsS(bmc.bcHeight);
#else
Width=bmc.bcWidth;
Height=bmc.bcHeight;
#endif
fseek(fr,256*3,SEEK_CUR);
} else
{
fread(&bmi,sizeof(BITMAPINFOHEADER),1,fr);
#ifdef __sun
Width=(WORD)invertbitsL(bmi.biWidth);
Height=(WORD)invertbitsL(bmi.biHeight);
bmi.biClrUsed=invertbitsL(bmi.biClrUsed);
#else
Width=(WORD)bmi.biWidth;
Height=(WORD)bmi.biHeight;
#endif
fseek(fr,bmi.biClrUsed*4,SEEK_CUR);
};
if ((Width%4)==0) w=Width;
else w=Width+4-Width%4;
Size=Width*Height;
gl=(BYTE*)malloc(Size);
if (gl==NULL)
{
fprintf(stderr,"img_load: out of memory.\n");
exit(211);
};
h=Height-1;
while (h>=0)
{
tmp=gl+Width*h;
fread(tmp,Width,1,fr);
if (w!=Width) fseek(fr,w-Width,SEEK_CUR);
h--;
}
fclose(fr);
tmp=gl; tmp2=tmp+Size; while (tmp!=tmp2) { *tmp=MIN(254,*tmp); tmp++; };
return;
};
printf("image '%s' not found.\n",name); exit(236);
};
void image::save(char *name)
{
BITMAPFILEHEADER bmfh;
BITMAPINFOHEADER bmih;
FILE *fw;
DWORD a,w;
int i,h;
BYTE buffer[4];
long s;
BYTE *p;
if ((Width%4)==0) w=Width;
else w=Width+4-Width%4;
s=w*Height;
fw=fopen(name,"wb");
#ifdef __sun__
bmfh.bfType=invertbitsS(19778);
bmfh.bfReserved1=0;
bmfh.bfReserved2=0;
bmfh.bfOffBits=invertbitsL(sizeof(BITMAPFILEHEADER)+4+sizeof(BITMAPINFOHEADER)+256*4);
bmfh.bfSize=invertbitsL(bmfh.bfOffBits+s);
fwrite(&bmfh,sizeof(BITMAPFILEHEADER),1,fw);
a=invertbitsL(40); fwrite(&a,sizeof(a),1,fw);
bmih.biWidth=invertbitsL(Width);
bmih.biHeight=invertbitsL(Height);
bmih.biPlanes=invertbitsS(1);
bmih.biBitCount=invertbitsS(8);
bmih.biCompression=0;
bmih.biSizeImage=invertbitsL(s);
bmih.biXPelsPerMeter=0;
bmih.biYPelsPerMeter=0;
bmih.biClrUsed=invertbitsL(256);
bmih.biClrImportant=0;
#else
bmfh.bfType=19778;
bmfh.bfReserved1=0;
bmfh.bfReserved2=0;
bmfh.bfOffBits=sizeof(BITMAPFILEHEADER)+4+sizeof(BITMAPINFOHEADER)+256*4;
bmfh.bfSize=bmfh.bfOffBits+s;
fwrite(&bmfh,sizeof(BITMAPFILEHEADER),1,fw);
a=40; fwrite(&a,sizeof(a),1,fw);
bmih.biWidth=Width;
bmih.biHeight=Height;
bmih.biPlanes=1;
bmih.biBitCount=8;
bmih.biCompression=0;
bmih.biSizeImage=s;
bmih.biXPelsPerMeter=0;
bmih.biYPelsPerMeter=0;
bmih.biClrUsed=256;
bmih.biClrImportant=0;
#endif
buffer[3]=0;
fwrite(&bmih,sizeof(BITMAPINFOHEADER),1,fw);
for (i=0; i<256; i++)
{
buffer[0]=(BYTE)i; buffer[1]=(BYTE)i; buffer[2]=(BYTE)i;
fwrite(&buffer,4,1,fw);
};
buffer[0]=0; buffer[1]=0; buffer[2]=0; buffer[3]=0;
h=Height-1;
while (h>=0)
{
p=gl+Width*h;
fwrite(p,Width,1,fw);
if (w!=Width) fwrite(&buffer,1,w-Width,fw);
h--;
};
fclose(fw);
};
void image::find_max(WORD *x,WORD *y)
{
BYTE *glend=gl+Size,*glf=gl,*gli=gl+1,a=0,b;
DWORD l;
while (glend!=gli)
{
b=abs(*(gli++)-128);
if ((b>a)&&(b!=255)) { glf=gli-1; a=b; };
};
l=(DWORD)(glf-gl);
*x=(WORD)(l%Width);
*y=(WORD)(l/Width);
};
void image::find_min(WORD *x,WORD *y)
{
BYTE *glend,*glf,*gli;
DWORD l;
glend=gl+Size; glf=gl; gli=gl+1;
while (glend!=gli) if (*(gli++)<*glf) glf=gli;
l=(DWORD)(glf-gl);
*x=(WORD)(l%Width);
*y=(WORD)(l/Width);
};
image::~image()
{
if (gl!=NULL) free(gl);
if (fgl!=NULL) free(fgl);
};
image::image(WORD _Width,WORD _Height,BYTE c) : fgl(NULL), Width(_Width),
Height(_Height), Size(_Width*_Height)
{
gl=(BYTE*)malloc(Size*sizeof(BYTE));
if (gl==NULL)
{
fprintf(stderr,"img_create: out of memory.\n");
exit(212);
};
// for (i=0;i<Size;i++) gl[i]=c;
memset(gl,c,Size);
};
void image::initFrom(image *im)
{
if (gl!=NULL) free(gl);
if (fgl!=NULL) free(fgl);
fgl=NULL;
Width=im->Width;
Height=im->Height;
Size=im->Width*im->Height;
if (Size==0) {gl=NULL; return;}
gl=(BYTE*)malloc(Size*sizeof(BYTE));
if (gl==NULL)
{
fprintf(stderr,"img_initFrom: out of memory.\n");
exit(212);
};
memcpy(gl,im->gl,Size);
}
image::image(image *im,double scaleX, double scaleY) : fgl(NULL)
{
WORD i,j;
DWORD incX,incY,counterY,counterX,a;
BYTE *indexD,*indexS,*indexSstartline,*indexDstartline;
DWORD *surface,*surfaceStartline,*surfaceStartimage;
BYTE b;
Width=(WORD)ceil(im->Width*scaleX);
Height=(WORD)ceil(im->Height*scaleY);
Size=Width*Height;
gl=(BYTE*)malloc(Size*sizeof(BYTE));
if (gl==NULL)
{
fprintf(stderr,"img_resize: out of memory.\n");
exit(214);
};
if ((scaleX==1)&&(scaleY==1))
{
memcpy(gl,im->gl,Size);
gravityH=im->gravityH;
gravityW=im->gravityW;
total=im->total;
minGL=im->minGL;
maxGL=im->maxGL;
pixBsurN=im->pixBsurN;
Surface=im->Surface;
return;
}
if (scaleX<1)
{
incX=(DWORD)(65536*scaleX);
incY=(DWORD)(65536*scaleY);
memset(gl,0,Size);
indexDstartline=gl;
indexS=im->gl;
counterY=0;
surfaceStartline=surfaceStartimage=(DWORD*)malloc(Size*sizeof(DWORD));
if (surfaceStartline==NULL)
{
fprintf(stderr,"img_resize: out of memory.\n");
exit(214);
};
memset(surfaceStartline,0,Size*si
评论4
最新资源