X-Git-Url: https://git.pterodactylus.net/?p=fms.git;a=blobdiff_plain;f=src%2Ffreenet%2Fcaptcha%2Ffreeimage%2Fbitmap.cpp;fp=src%2Ffreenet%2Fcaptcha%2Ffreeimage%2Fbitmap.cpp;h=4c90fc1f44a4d2e27a2caace7131a7927274147c;hp=0000000000000000000000000000000000000000;hb=822f84f5dac64183c556bd86fea8cd7b0527f528;hpb=fcb124f8d6d3f5678e82049fb8e5e23c8cfaec6d diff --git a/src/freenet/captcha/freeimage/bitmap.cpp b/src/freenet/captcha/freeimage/bitmap.cpp new file mode 100644 index 0000000..4c90fc1 --- /dev/null +++ b/src/freenet/captcha/freeimage/bitmap.cpp @@ -0,0 +1,661 @@ +#include "../../../../include/freenet/captcha/freeimage/bitmap.h" + +#include + +namespace FreeImage +{ + +Bitmap::Bitmap() +{ + m_bmp=0; +} + +Bitmap::Bitmap(const Bitmap &bmp) +{ + m_bmp=0; + *this=bmp; +} + +Bitmap::Bitmap(const std::string &typestr, const std::string &filename) +{ + m_bmp=0; + Load(typestr,filename); +} + +Bitmap::Bitmap(FIBITMAP *bmp):m_bmp(bmp) +{ + +} + +Bitmap::Bitmap(const int width, const int height, const int bpp) +{ + m_bmp=FreeImage_Allocate(width,height,bpp,0,0,0); +} + +Bitmap::~Bitmap() +{ + Destroy(); +} + +void Bitmap::AALine(const int x1, const int y1, const int x2, const int y2, const RGBQUAD color) +{ + RGBQUAD tempcol; + int height=Height(); + int width=Width(); + int fromx=x1; + int fromy=y1; + int tox=x2; + int toy=y2; + double sx=x2-x1; + double sy=y2-y1; + + if(fabs(sx)<=0.8 && fabs(sy)<=0.8) + { + return; + } + + double hypot=sqrt((sx*sx)+(sy*sy)); + sx/=hypot; + sy/=hypot; + + fromx<0 ? fromx=0 : false; + fromx>=width ? fromx=width-1 : false; + tox<0 ? tox=0 : false; + tox>=width ? tox=width-1 : false; + fromy<0 ? fromy=0 : false; + fromy>=height ? fromy=height-1 : false; + toy<0 ? toy=0 : false; + toy>=height ? toy=height-1 : false; + + int lx=fromx; + int rx=tox; + if(rxminval) + { + FreeImage_GetPixelColor(m_bmp,i,(height-1)-j,&tempcol); + tempcol.rgbRed=((1.0-temp)*((double)tempcol.rgbRed)+temp*((double)color.rgbRed)); + tempcol.rgbGreen=((1.0-temp)*((double)tempcol.rgbGreen)+temp*((double)color.rgbGreen)); + tempcol.rgbBlue=((1.0-temp)*((double)tempcol.rgbBlue)+temp*((double)color.rgbBlue)); + FreeImage_SetPixelColor(m_bmp,i,(height-1)-j,&tempcol); + } + + } + } + +} + +void Bitmap::Blit(const Bitmap &bmp, const int destx, const int desty, const int sourcex, const int sourcey, const int w, const int h, const int alpha) +{ + if(m_bmp && bmp.m_bmp) + { + int width=w; + int height=h; + if(destx+width-1>=Width()) + { + width=Width()-destx+1; + } + if(desty+height-1>=Height()) + { + height=Height()-desty+1; + } + FIBITMAP *temp=FreeImage_Copy(bmp.m_bmp,sourcex,sourcey,sourcex+width-1,sourcey+height-1); + FreeImage_Paste(m_bmp,temp,destx,desty,alpha); + FreeImage_Unload(temp); + } +} + +void Bitmap::BlitTrans(const Bitmap &bmp, const int destx, const int desty, const int sourcex, const int sourcey, const int w, const int h) +{ + if(m_bmp && bmp.m_bmp) + { + RGBQUAD sourcecol; + RGBQUAD destcol; + int sourcew=bmp.Width(); + int sourceh=bmp.Height(); + int destw=Width(); + int desth=Height(); + int dx=destx; + int dy=desty; + double alpha=0.0; + for(int y=sourcey; y=0 && y=0 && x=0 && dx=0 && dy0) // some translucency + { + FreeImage_GetPixelColor(m_bmp,dx,(desth-1)-dy,&destcol); + alpha=(double)sourcecol.rgbReserved/255.0; + sourcecol.rgbRed=(sourcecol.rgbRed*alpha)+(destcol.rgbRed*(1.0-alpha)); + sourcecol.rgbGreen=(sourcecol.rgbGreen*alpha)+(destcol.rgbGreen*(1.0-alpha)); + sourcecol.rgbBlue=(sourcecol.rgbBlue*alpha)+(destcol.rgbBlue*(1.0-alpha)); + sourcecol.rgbReserved=width ? fromx=width-1 : false; + tox<0 ? tox=0 : false; + tox>=width ? tox=width-1 : false; + fromy<0 ? fromy=0 : false; + fromy>=height ? fromy=height-1 : false; + toy<0 ? toy=0 : false; + toy>=height ? toy=height-1 : false; + + int dx=tox-fromx; + int dy=toy-fromy; + + if(dx==0 && dy==0) + { + return; + } + + int xinc1=1; + if(dx<0) + { + xinc1=-1; + dx=-dx; + } + int yinc1=1; + if(dy<0) + { + yinc1=-1; + dy=-dy; + } + + int x=fromx; + int y=fromy; + int xinc2=xinc1; + int yinc2=yinc1; + + double den; + double num; + int numadd; + int numpixels; + + if(dx>=dy) + { + xinc1=0; + yinc2=0; + den=dx+0.0; + num=0.5*dx; + numadd=dy; + numpixels=dx; + } + else + { + xinc2=0; + yinc1=0; + den=dy+0.0; + num=0.5*dy; + numadd=dx; + numpixels=dy; + } + + int cpixel; + for(cpixel=0; cpixel<=numpixels; cpixel++) + { + FreeImage_SetPixelColor(m_bmp,x,(height-1)-y,&color); + num+=numadd; + if(num>=den) + { + num=-den; + x+=xinc1; + y+=yinc1; + } + x+=xinc2; + y+=yinc2; + } + } +} + +RGBQUAD Bitmap::GetPixel(const int x, const int y) const +{ + RGBQUAD color; + color.rgbRed=0; + color.rgbGreen=0; + color.rgbBlue=0; + color.rgbReserved=0; + + if(m_bmp && x>=0 && x=0 && y &data) +{ + bool loaded=false; + Destroy(); + if(data.size()>0) + { + FREE_IMAGE_FORMAT type=FIF_BMP; + if(typestr.find("png")!=std::string::npos) + { + type=FIF_PNG; + } + + FIMEMORY *mem=FreeImage_OpenMemory((BYTE *)&data[0],data.size()); + m_bmp=FreeImage_LoadFromMemory(type,mem,0); + if(m_bmp) + { + loaded=true; + } + FreeImage_CloseMemory(mem); + } + return loaded; +} + +Bitmap &Bitmap::operator=(const Bitmap &rhs) +{ + if(this!=&rhs) + { + Destroy(); + if(rhs.Width()>0 && rhs.Height()>0) + { + m_bmp=FreeImage_Allocate(rhs.Width(),rhs.Height(),rhs.BPP(),0,0,0); + //FIBITMAP *temp=FreeImage_Copy(rhs.m_bmp,0,0,rhs.Width()-1,rhs.Height()-1); + //FreeImage_Paste(m_bmp,rhs.m_bmp,0,0,-1); + //FreeImage_Unload(temp); + Blit(rhs,0,0,0,0,rhs.Width(),rhs.Height(),-1); + } + } + return *this; +} + +void Bitmap::PutPixel(const int x, const int y, RGBQUAD color) +{ + if(m_bmp && x>=0 && x=0 && y=0 && y=0 && x &data) const +{ + bool saved=false; + if(m_bmp) + { + FREE_IMAGE_FORMAT type=FIF_BMP; + if(typestr.find("png")!=std::string::npos) + { + type=FIF_PNG; + } + FIMEMORY *mem=FreeImage_OpenMemory(0,0); + if(FreeImage_SaveToMemory(type,m_bmp,mem,0)) + { + FreeImage_SeekMemory(mem,0,SEEK_END); + data.resize(FreeImage_TellMemory(mem)); + FreeImage_SeekMemory(mem,0,SEEK_SET); + FreeImage_ReadMemory(&data[0],1,data.size(),mem); + saved=true; + } + FreeImage_CloseMemory(mem); + } + return saved; +} + +void Bitmap::SetTransparent() +{ + if(m_bmp) + { + FreeImage_SetTransparent(m_bmp,true); + } +} + +void Bitmap::VerticalOffset(const int x, const double shift) +{ + if(m_bmp) + { + int width=Width(); + int height=Height(); + int starty=width-1; + int endy=ceil(shift); + int dy=-1; + int offset1=-(floor(shift)); + int offset2=-(ceil(shift)); + RGBQUAD color1; + RGBQUAD color2; + RGBQUAD newcolor; + double part2=shift-floor(shift); + double part1=1.0-part2; + + if(shift<0) + { + starty=0; + endy=width-1-ceil(abs(shift)); + dy=1; + offset1=-ceil(shift); + offset2=-floor(shift); + part2=abs(shift-ceil(shift)); + part1=1.0-part2; + } + + FreeImage_GetPixelColor(m_bmp,x,(height-1)-(starty+offset1),&color1); + for(int y=starty+dy; y!=endy; y+=dy) + { + FreeImage_GetPixelColor(m_bmp,x,(height-1)-(y+offset2),&color2); + + newcolor.rgbRed=(color1.rgbRed*part1)+(color2.rgbRed*part2); + newcolor.rgbGreen=(color1.rgbGreen*part1)+(color2.rgbGreen*part2); + newcolor.rgbBlue=(color1.rgbBlue*part1)+(color2.rgbBlue*part2); + + FreeImage_SetPixelColor(m_bmp,x,(height-1)-y,&newcolor); + + color1=color2; + } + } +} + +} // namespace FreeImage