1 #include "../../../../include/freenet/captcha/freeimage/bitmap.h"
\r
13 Bitmap::Bitmap(const Bitmap &bmp)
\r
19 Bitmap::Bitmap(const std::string &typestr, const std::string &filename)
\r
22 Load(typestr,filename);
\r
25 Bitmap::Bitmap(FIBITMAP *bmp):m_bmp(bmp)
\r
30 Bitmap::Bitmap(const int width, const int height, const int bpp)
\r
32 m_bmp=FreeImage_Allocate(width,height,bpp,0,0,0);
\r
40 void Bitmap::AALine(const int x1, const int y1, const int x2, const int y2, const RGBQUAD color)
\r
43 int height=Height();
\r
52 if(fabs(sx)<=0.8 && fabs(sy)<=0.8)
\r
57 double hypot=sqrt((sx*sx)+(sy*sy));
\r
61 fromx<0 ? fromx=0 : false;
\r
62 fromx>=width ? fromx=width-1 : false;
\r
63 tox<0 ? tox=0 : false;
\r
64 tox>=width ? tox=width-1 : false;
\r
65 fromy<0 ? fromy=0 : false;
\r
66 fromy>=height ? fromy=height-1 : false;
\r
67 toy<0 ? toy=0 : false;
\r
68 toy>=height ? toy=height-1 : false;
\r
83 for(int i=lx; i<=rx; i++)
\r
85 for(int j=ty; j<=by; j++)
\r
95 for(ii=-2; ii<=1; ii++)
\r
97 for(jj=-2; jj<=1; jj++)
\r
101 double temp1 = LineFunction( sx, sy , fromx, fromy, x,y );
\r
102 temp1<=0.5 ? temp1=1.0 : temp1=0.0;
\r
108 double minval=0.03125;
\r
112 FreeImage_GetPixelColor(m_bmp,i,(height-1)-j,&tempcol);
\r
113 tempcol.rgbRed=((1.0-temp)*((double)tempcol.rgbRed)+temp*((double)color.rgbRed));
\r
114 tempcol.rgbGreen=((1.0-temp)*((double)tempcol.rgbGreen)+temp*((double)color.rgbGreen));
\r
115 tempcol.rgbBlue=((1.0-temp)*((double)tempcol.rgbBlue)+temp*((double)color.rgbBlue));
\r
116 FreeImage_SetPixelColor(m_bmp,i,(height-1)-j,&tempcol);
\r
124 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)
\r
126 if(m_bmp && bmp.m_bmp)
\r
130 if(destx+width-1>=Width())
\r
132 width=Width()-destx+1;
\r
134 if(desty+height-1>=Height())
\r
136 height=Height()-desty+1;
\r
138 FIBITMAP *temp=FreeImage_Copy(bmp.m_bmp,sourcex,sourcey,sourcex+width-1,sourcey+height-1);
\r
139 FreeImage_Paste(m_bmp,temp,destx,desty,alpha);
\r
140 FreeImage_Unload(temp);
\r
144 void Bitmap::BlitTrans(const Bitmap &bmp, const int destx, const int desty, const int sourcex, const int sourcey, const int w, const int h)
\r
146 if(m_bmp && bmp.m_bmp)
\r
150 int sourcew=bmp.Width();
\r
151 int sourceh=bmp.Height();
\r
153 int desth=Height();
\r
157 for(int y=sourcey; y<sourcey+h; y++, dy++)
\r
160 for(int x=sourcex; x<sourcex+w; x++, dx++)
\r
162 if(y>=0 && y<sourceh && x>=0 && x<sourcew && dx>=0 && dx<destw && dy>=0 && dy<desth)
\r
164 FreeImage_GetPixelColor(bmp.m_bmp,x,(sourceh-1)-y,&sourcecol);
\r
165 if(sourcecol.rgbReserved==255) // opaque
\r
167 FreeImage_SetPixelColor(m_bmp,dx,(desth-1)-dy,&sourcecol);
\r
169 else if(sourcecol.rgbReserved>0) // some translucency
\r
171 FreeImage_GetPixelColor(m_bmp,dx,(desth-1)-dy,&destcol);
\r
172 alpha=(double)sourcecol.rgbReserved/255.0;
\r
173 sourcecol.rgbRed=(sourcecol.rgbRed*alpha)+(destcol.rgbRed*(1.0-alpha));
\r
174 sourcecol.rgbGreen=(sourcecol.rgbGreen*alpha)+(destcol.rgbGreen*(1.0-alpha));
\r
175 sourcecol.rgbBlue=(sourcecol.rgbBlue*alpha)+(destcol.rgbBlue*(1.0-alpha));
\r
176 sourcecol.rgbReserved<destcol.rgbReserved ? sourcecol.rgbReserved=destcol.rgbReserved : false;
\r
177 FreeImage_SetPixelColor(m_bmp,dx,(desth-1)-dy,&sourcecol);
\r
185 void Bitmap::Clear(RGBQUAD color)
\r
189 int h=FreeImage_GetHeight(m_bmp);
\r
190 int w=FreeImage_GetWidth(m_bmp);
\r
191 for(int y=0; y<h; y++)
\r
193 for(int x=0; x<w; x++)
\r
195 FreeImage_SetPixelColor(m_bmp,x,y,&color);
\r
201 void Bitmap::ClearTransparent()
\r
209 color.rgbReserved=0;
\r
212 for(int y=0; y<h; y++)
\r
214 for(int x=0; x<w; x++)
\r
216 FreeImage_SetPixelColor(m_bmp,x,y,&color);
\r
222 void Bitmap::ConvertBPP(const int bpp)
\r
230 temp=FreeImage_ConvertToGreyscale(m_bmp);
\r
235 temp=FreeImage_ConvertTo4Bits(m_bmp);
\r
240 temp=FreeImage_ConvertTo8Bits(m_bmp);
\r
245 temp=FreeImage_ConvertTo16Bits565(m_bmp);
\r
250 temp=FreeImage_ConvertTo24Bits(m_bmp);
\r
256 temp=FreeImage_ConvertTo32Bits(m_bmp);
\r
264 const bool Bitmap::Create(const int width, const int height, const int bpp)
\r
267 m_bmp=FreeImage_Allocate(width,height,bpp,0,0,0);
\r
275 void Bitmap::Destroy()
\r
279 FreeImage_Unload(m_bmp);
\r
284 void Bitmap::FastLine(const int x1, const int y1, const int x2, const int y2, RGBQUAD color)
\r
289 int height=Height();
\r
295 fromx<0 ? fromx=0 : false;
\r
296 fromx>=width ? fromx=width-1 : false;
\r
297 tox<0 ? tox=0 : false;
\r
298 tox>=width ? tox=width-1 : false;
\r
299 fromy<0 ? fromy=0 : false;
\r
300 fromy>=height ? fromy=height-1 : false;
\r
301 toy<0 ? toy=0 : false;
\r
302 toy>=height ? toy=height-1 : false;
\r
355 for(cpixel=0; cpixel<=numpixels; cpixel++)
\r
357 FreeImage_SetPixelColor(m_bmp,x,(height-1)-y,&color);
\r
371 RGBQUAD Bitmap::GetPixel(const int x, const int y) const
\r
377 color.rgbReserved=0;
\r
379 if(m_bmp && x>=0 && x<Width() && y>=0 && y<Height())
\r
381 FreeImage_GetPixelColor(m_bmp,x,Height()-1-y,&color);
\r
388 const int Bitmap::GetPixelIndex(const int x, const int y) const
\r
393 FreeImage_GetPixelIndex(m_bmp,x,Height()-1-y,&index);
\r
402 void Bitmap::HorizontalOffset(const int y, const double shift)
\r
407 int height=Height();
\r
408 int startx=width-1;
\r
409 int endx=ceil(shift);
\r
411 int offset1=-(floor(shift));
\r
412 int offset2=-(ceil(shift));
\r
416 double part2=shift-floor(shift);
\r
417 double part1=1.0-part2;
\r
422 endx=width-1-ceil(abs(shift));
\r
424 offset1=-ceil(shift);
\r
425 offset2=-floor(shift);
\r
426 part2=abs(shift-ceil(shift));
\r
430 FreeImage_GetPixelColor(m_bmp,startx+offset1,(height-1)-y,&color1);
\r
431 for(int x=startx+dx; x!=endx; x+=dx)
\r
433 FreeImage_GetPixelColor(m_bmp,x+offset2,(height-1)-y,&color2);
\r
435 newcolor.rgbRed=(color1.rgbRed*part1)+(color2.rgbRed*part2);
\r
436 newcolor.rgbGreen=(color1.rgbGreen*part1)+(color2.rgbGreen*part2);
\r
437 newcolor.rgbBlue=(color1.rgbBlue*part1)+(color2.rgbBlue*part2);
\r
439 FreeImage_SetPixelColor(m_bmp,x,(height-1)-y,&newcolor);
\r
446 void Bitmap::Line(const int x1, const int y1, const int x2, const int y2, const RGBQUAD color)
\r
450 if(x1!=x2 && y1!=y2)
\r
452 AALine(x1,y1,x2,y2,color);
\r
456 FastLine(x1,y1,x2,y2,color);
\r
461 double Bitmap::LineFunction(const double slopex, const double slopey, const int startx, const int starty, const double testx, const double testy)
\r
463 return fabs(slopex*(testy-starty)-slopey*(testx-startx));
\r
466 const bool Bitmap::Load(const std::string &typestr, const std::string &filename)
\r
469 FREE_IMAGE_FORMAT type=FIF_BMP;
\r
470 if(typestr.find("png")!=std::string::npos)
\r
474 m_bmp=FreeImage_Load(type,filename.c_str(),0);
\r
482 const bool Bitmap::LoadFromMemory(const std::string &typestr, std::vector<unsigned char> &data)
\r
488 FREE_IMAGE_FORMAT type=FIF_BMP;
\r
489 if(typestr.find("png")!=std::string::npos)
\r
494 FIMEMORY *mem=FreeImage_OpenMemory((BYTE *)&data[0],data.size());
\r
495 m_bmp=FreeImage_LoadFromMemory(type,mem,0);
\r
500 FreeImage_CloseMemory(mem);
\r
505 Bitmap &Bitmap::operator=(const Bitmap &rhs)
\r
510 if(rhs.Width()>0 && rhs.Height()>0)
\r
512 m_bmp=FreeImage_Allocate(rhs.Width(),rhs.Height(),rhs.BPP(),0,0,0);
\r
513 //FIBITMAP *temp=FreeImage_Copy(rhs.m_bmp,0,0,rhs.Width()-1,rhs.Height()-1);
\r
514 //FreeImage_Paste(m_bmp,rhs.m_bmp,0,0,-1);
\r
515 //FreeImage_Unload(temp);
\r
516 Blit(rhs,0,0,0,0,rhs.Width(),rhs.Height(),-1);
\r
522 void Bitmap::PutPixel(const int x, const int y, RGBQUAD color)
\r
524 if(m_bmp && x>=0 && x<FreeImage_GetWidth(m_bmp) && y>=0 && y<FreeImage_GetHeight(m_bmp))
\r
526 FreeImage_SetPixelColor(m_bmp,x,(Height()-1)-y,&color);
\r
530 void Bitmap::Rect(const int x1, const int y1, const int w, const int h, const bool filled, RGBQUAD color)
\r
534 int height=Height();
\r
536 for(int y=y1; y<y1+h; y++)
\r
538 if(y>=0 && y<height)
\r
540 for(int x=x1; x<x1+w; x++)
\r
542 if(x>=0 && x<width)
\r
544 if(x==x1 || x==x1+w-1 || y==y1 || y==y1+h-1 || filled)
\r
546 FreeImage_SetPixelColor(m_bmp,x,(height-1)-y,&color);
\r
555 void Bitmap::Rotate(const double angle, const int shiftx, const int shifty, const int originx, const int originy)
\r
559 int height=Height();
\r
560 FIBITMAP *old=m_bmp;
\r
561 m_bmp=FreeImage_RotateEx(m_bmp,angle,shiftx,-shifty,originx,(height-1)-originy,true);
\r
562 FreeImage_Unload(old);
\r
566 const bool Bitmap::Save(const std::string &filename) const
\r
570 FREE_IMAGE_FORMAT type=FIF_BMP;
\r
572 if(filename.find(".png")!=std::string::npos)
\r
577 if(FreeImage_Save(type,m_bmp,filename.c_str(),0))
\r
585 const bool Bitmap::SaveToMemory(const std::string &typestr, std::vector<unsigned char> &data) const
\r
590 FREE_IMAGE_FORMAT type=FIF_BMP;
\r
591 if(typestr.find("png")!=std::string::npos)
\r
595 FIMEMORY *mem=FreeImage_OpenMemory(0,0);
\r
596 if(FreeImage_SaveToMemory(type,m_bmp,mem,0))
\r
598 FreeImage_SeekMemory(mem,0,SEEK_END);
\r
599 data.resize(FreeImage_TellMemory(mem));
\r
600 FreeImage_SeekMemory(mem,0,SEEK_SET);
\r
601 FreeImage_ReadMemory(&data[0],1,data.size(),mem);
\r
604 FreeImage_CloseMemory(mem);
\r
609 void Bitmap::SetTransparent()
\r
613 FreeImage_SetTransparent(m_bmp,true);
\r
617 void Bitmap::VerticalOffset(const int x, const double shift)
\r
622 int height=Height();
\r
623 int starty=width-1;
\r
624 int endy=ceil(shift);
\r
626 int offset1=-(floor(shift));
\r
627 int offset2=-(ceil(shift));
\r
631 double part2=shift-floor(shift);
\r
632 double part1=1.0-part2;
\r
637 endy=width-1-ceil(abs(shift));
\r
639 offset1=-ceil(shift);
\r
640 offset2=-floor(shift);
\r
641 part2=abs(shift-ceil(shift));
\r
645 FreeImage_GetPixelColor(m_bmp,x,(height-1)-(starty+offset1),&color1);
\r
646 for(int y=starty+dy; y!=endy; y+=dy)
\r
648 FreeImage_GetPixelColor(m_bmp,x,(height-1)-(y+offset2),&color2);
\r
650 newcolor.rgbRed=(color1.rgbRed*part1)+(color2.rgbRed*part2);
\r
651 newcolor.rgbGreen=(color1.rgbGreen*part1)+(color2.rgbGreen*part2);
\r
652 newcolor.rgbBlue=(color1.rgbBlue*part1)+(color2.rgbBlue*part2);
\r
654 FreeImage_SetPixelColor(m_bmp,x,(height-1)-y,&newcolor);
\r
661 } // namespace FreeImage
\r