1 #include "../../../../include/freenet/captcha/freeimage/bitmap.h"
\r
14 Bitmap::Bitmap(const Bitmap &bmp)
\r
20 Bitmap::Bitmap(const std::string &typestr, const std::string &filename)
\r
23 Load(typestr,filename);
\r
26 Bitmap::Bitmap(FIBITMAP *bmp):m_bmp(bmp)
\r
31 Bitmap::Bitmap(const int width, const int height, const int bpp)
\r
33 m_bmp=FreeImage_Allocate(width,height,bpp,0,0,0);
\r
41 void Bitmap::AALine(const int x1, const int y1, const int x2, const int y2, const RGBQUAD color)
\r
44 int height=Height();
\r
53 if(fabs(sx)<=0.8 && fabs(sy)<=0.8)
\r
58 double hypot=sqrt((sx*sx)+(sy*sy));
\r
62 fromx<0 ? fromx=0 : false;
\r
63 fromx>=width ? fromx=width-1 : false;
\r
64 tox<0 ? tox=0 : false;
\r
65 tox>=width ? tox=width-1 : false;
\r
66 fromy<0 ? fromy=0 : false;
\r
67 fromy>=height ? fromy=height-1 : false;
\r
68 toy<0 ? toy=0 : false;
\r
69 toy>=height ? toy=height-1 : false;
\r
84 for(int i=lx; i<=rx; i++)
\r
86 for(int j=ty; j<=by; j++)
\r
96 for(ii=-2; ii<=1; ii++)
\r
98 for(jj=-2; jj<=1; jj++)
\r
102 double temp1 = LineFunction( sx, sy , fromx, fromy, x,y );
\r
103 temp1<=0.5 ? temp1=1.0 : temp1=0.0;
\r
109 double minval=0.03125;
\r
113 FreeImage_GetPixelColor(m_bmp,i,(height-1)-j,&tempcol);
\r
114 tempcol.rgbRed=((1.0-temp)*((double)tempcol.rgbRed)+temp*((double)color.rgbRed));
\r
115 tempcol.rgbGreen=((1.0-temp)*((double)tempcol.rgbGreen)+temp*((double)color.rgbGreen));
\r
116 tempcol.rgbBlue=((1.0-temp)*((double)tempcol.rgbBlue)+temp*((double)color.rgbBlue));
\r
117 FreeImage_SetPixelColor(m_bmp,i,(height-1)-j,&tempcol);
\r
125 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
127 if(m_bmp && bmp.m_bmp)
\r
131 if(destx+width-1>=Width())
\r
133 width=Width()-destx+1;
\r
135 if(desty+height-1>=Height())
\r
137 height=Height()-desty+1;
\r
139 FIBITMAP *temp=FreeImage_Copy(bmp.m_bmp,sourcex,sourcey,sourcex+width,sourcey+height);
\r
140 FreeImage_Paste(m_bmp,temp,destx,desty,alpha);
\r
141 FreeImage_Unload(temp);
\r
145 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
147 if(m_bmp && bmp.m_bmp)
\r
151 int sourcew=bmp.Width();
\r
152 int sourceh=bmp.Height();
\r
154 int desth=Height();
\r
158 for(int y=sourcey; y<sourcey+h; y++, dy++)
\r
161 for(int x=sourcex; x<sourcex+w; x++, dx++)
\r
163 if(y>=0 && y<sourceh && x>=0 && x<sourcew && dx>=0 && dx<destw && dy>=0 && dy<desth)
\r
165 FreeImage_GetPixelColor(bmp.m_bmp,x,(sourceh-1)-y,&sourcecol);
\r
166 if(sourcecol.rgbReserved==255) // opaque
\r
168 FreeImage_SetPixelColor(m_bmp,dx,(desth-1)-dy,&sourcecol);
\r
170 else if(sourcecol.rgbReserved>0) // some translucency
\r
172 FreeImage_GetPixelColor(m_bmp,dx,(desth-1)-dy,&destcol);
\r
173 alpha=(double)sourcecol.rgbReserved/255.0;
\r
174 sourcecol.rgbRed=(sourcecol.rgbRed*alpha)+(destcol.rgbRed*(1.0-alpha));
\r
175 sourcecol.rgbGreen=(sourcecol.rgbGreen*alpha)+(destcol.rgbGreen*(1.0-alpha));
\r
176 sourcecol.rgbBlue=(sourcecol.rgbBlue*alpha)+(destcol.rgbBlue*(1.0-alpha));
\r
177 sourcecol.rgbReserved<destcol.rgbReserved ? sourcecol.rgbReserved=destcol.rgbReserved : false;
\r
178 FreeImage_SetPixelColor(m_bmp,dx,(desth-1)-dy,&sourcecol);
\r
186 void Bitmap::Clear(RGBQUAD color)
\r
190 int h=FreeImage_GetHeight(m_bmp);
\r
191 int w=FreeImage_GetWidth(m_bmp);
\r
192 for(int y=0; y<h; y++)
\r
194 for(int x=0; x<w; x++)
\r
196 FreeImage_SetPixelColor(m_bmp,x,y,&color);
\r
202 void Bitmap::ClearTransparent()
\r
210 color.rgbReserved=0;
\r
213 for(int y=0; y<h; y++)
\r
215 for(int x=0; x<w; x++)
\r
217 FreeImage_SetPixelColor(m_bmp,x,y,&color);
\r
223 void Bitmap::ConvertBPP(const int bpp)
\r
231 temp=FreeImage_ConvertToGreyscale(m_bmp);
\r
236 temp=FreeImage_ConvertTo4Bits(m_bmp);
\r
241 temp=FreeImage_ConvertTo8Bits(m_bmp);
\r
246 temp=FreeImage_ConvertTo16Bits565(m_bmp);
\r
251 temp=FreeImage_ConvertTo24Bits(m_bmp);
\r
257 temp=FreeImage_ConvertTo32Bits(m_bmp);
\r
265 const bool Bitmap::Create(const int width, const int height, const int bpp)
\r
268 m_bmp=FreeImage_Allocate(width,height,bpp,0,0,0);
\r
276 void Bitmap::Destroy()
\r
280 FreeImage_Unload(m_bmp);
\r
285 void Bitmap::FastLine(const int x1, const int y1, const int x2, const int y2, RGBQUAD color)
\r
290 int height=Height();
\r
296 fromx<0 ? fromx=0 : false;
\r
297 fromx>=width ? fromx=width-1 : false;
\r
298 tox<0 ? tox=0 : false;
\r
299 tox>=width ? tox=width-1 : false;
\r
300 fromy<0 ? fromy=0 : false;
\r
301 fromy>=height ? fromy=height-1 : false;
\r
302 toy<0 ? toy=0 : false;
\r
303 toy>=height ? toy=height-1 : false;
\r
356 for(cpixel=0; cpixel<=numpixels; cpixel++)
\r
358 FreeImage_SetPixelColor(m_bmp,x,(height-1)-y,&color);
\r
372 RGBQUAD Bitmap::GetPixel(const int x, const int y) const
\r
378 color.rgbReserved=0;
\r
380 if(m_bmp && x>=0 && x<Width() && y>=0 && y<Height())
\r
382 FreeImage_GetPixelColor(m_bmp,x,Height()-1-y,&color);
\r
389 const int Bitmap::GetPixelIndex(const int x, const int y) const
\r
394 FreeImage_GetPixelIndex(m_bmp,x,Height()-1-y,&index);
\r
403 void Bitmap::HorizontalOffset(const int y, const double shift)
\r
408 int height=Height();
\r
409 int startx=width-1;
\r
410 int endx=ceil(shift);
\r
412 int offset1=-(floor(shift));
\r
413 int offset2=-(ceil(shift));
\r
417 double part2=shift-floor(shift);
\r
418 double part1=1.0-part2;
\r
423 endx=width-1-ceil(abs(shift));
\r
425 offset1=-ceil(shift);
\r
426 offset2=-floor(shift);
\r
427 part2=abs(shift-ceil(shift));
\r
431 FreeImage_GetPixelColor(m_bmp,startx+offset1,(height-1)-y,&color1);
\r
432 for(int x=startx+dx; x!=endx; x+=dx)
\r
434 FreeImage_GetPixelColor(m_bmp,x+offset2,(height-1)-y,&color2);
\r
436 newcolor.rgbRed=(color1.rgbRed*part1)+(color2.rgbRed*part2);
\r
437 newcolor.rgbGreen=(color1.rgbGreen*part1)+(color2.rgbGreen*part2);
\r
438 newcolor.rgbBlue=(color1.rgbBlue*part1)+(color2.rgbBlue*part2);
\r
440 FreeImage_SetPixelColor(m_bmp,x,(height-1)-y,&newcolor);
\r
447 void Bitmap::Line(const int x1, const int y1, const int x2, const int y2, const RGBQUAD color)
\r
451 if(x1!=x2 && y1!=y2)
\r
453 AALine(x1,y1,x2,y2,color);
\r
457 FastLine(x1,y1,x2,y2,color);
\r
462 double Bitmap::LineFunction(const double slopex, const double slopey, const int startx, const int starty, const double testx, const double testy)
\r
464 return fabs(slopex*(testy-starty)-slopey*(testx-startx));
\r
467 const bool Bitmap::Load(const std::string &typestr, const std::string &filename)
\r
470 FREE_IMAGE_FORMAT type=FIF_BMP;
\r
471 if(typestr.find("png")!=std::string::npos)
\r
475 m_bmp=FreeImage_Load(type,filename.c_str(),0);
\r
483 const bool Bitmap::LoadFromMemory(const std::string &typestr, std::vector<unsigned char> &data)
\r
489 FREE_IMAGE_FORMAT type=FIF_BMP;
\r
490 if(typestr.find("png")!=std::string::npos)
\r
495 FIMEMORY *mem=FreeImage_OpenMemory((BYTE *)&data[0],data.size());
\r
496 m_bmp=FreeImage_LoadFromMemory(type,mem,0);
\r
501 FreeImage_CloseMemory(mem);
\r
506 Bitmap &Bitmap::operator=(const Bitmap &rhs)
\r
511 if(rhs.Width()>0 && rhs.Height()>0)
\r
513 m_bmp=FreeImage_Allocate(rhs.Width(),rhs.Height(),rhs.BPP(),0,0,0);
\r
514 //FIBITMAP *temp=FreeImage_Copy(rhs.m_bmp,0,0,rhs.Width()-1,rhs.Height()-1);
\r
515 //FreeImage_Paste(m_bmp,rhs.m_bmp,0,0,-1);
\r
516 //FreeImage_Unload(temp);
\r
517 Blit(rhs,0,0,0,0,rhs.Width(),rhs.Height(),-1);
\r
523 void Bitmap::PutPixel(const int x, const int y, RGBQUAD color)
\r
525 if(m_bmp && x>=0 && x<FreeImage_GetWidth(m_bmp) && y>=0 && y<FreeImage_GetHeight(m_bmp))
\r
527 FreeImage_SetPixelColor(m_bmp,x,(Height()-1)-y,&color);
\r
531 void Bitmap::Rect(const int x1, const int y1, const int w, const int h, const bool filled, RGBQUAD color)
\r
535 int height=Height();
\r
537 for(int y=y1; y<y1+h; y++)
\r
539 if(y>=0 && y<height)
\r
541 for(int x=x1; x<x1+w; x++)
\r
543 if(x>=0 && x<width)
\r
545 if(x==x1 || x==x1+w-1 || y==y1 || y==y1+h-1 || filled)
\r
547 FreeImage_SetPixelColor(m_bmp,x,(height-1)-y,&color);
\r
556 void Bitmap::Rotate(const double angle, const int shiftx, const int shifty, const int originx, const int originy)
\r
560 int height=Height();
\r
561 FIBITMAP *old=m_bmp;
\r
562 m_bmp=FreeImage_RotateEx(m_bmp,angle,shiftx,-shifty,originx,(height-1)-originy,true);
\r
563 FreeImage_Unload(old);
\r
567 const bool Bitmap::Save(const std::string &filename) const
\r
571 FREE_IMAGE_FORMAT type=FIF_BMP;
\r
573 if(filename.find(".png")!=std::string::npos)
\r
578 if(FreeImage_Save(type,m_bmp,filename.c_str(),0))
\r
586 const bool Bitmap::SaveToMemory(const std::string &typestr, std::vector<unsigned char> &data) const
\r
591 FREE_IMAGE_FORMAT type=FIF_BMP;
\r
592 if(typestr.find("png")!=std::string::npos)
\r
596 FIMEMORY *mem=FreeImage_OpenMemory(0,0);
\r
597 if(FreeImage_SaveToMemory(type,m_bmp,mem,0))
\r
599 FreeImage_SeekMemory(mem,0,SEEK_END);
\r
600 data.resize(FreeImage_TellMemory(mem));
\r
601 FreeImage_SeekMemory(mem,0,SEEK_SET);
\r
602 FreeImage_ReadMemory(&data[0],1,data.size(),mem);
\r
605 FreeImage_CloseMemory(mem);
\r
610 void Bitmap::SetTransparent()
\r
614 FreeImage_SetTransparent(m_bmp,true);
\r
618 void Bitmap::VerticalOffset(const int x, const double shift)
\r
623 int height=Height();
\r
624 int starty=width-1;
\r
625 int endy=ceil(shift);
\r
627 int offset1=-(floor(shift));
\r
628 int offset2=-(ceil(shift));
\r
632 double part2=shift-floor(shift);
\r
633 double part1=1.0-part2;
\r
638 endy=width-1-ceil(abs(shift));
\r
640 offset1=-ceil(shift);
\r
641 offset2=-floor(shift);
\r
642 part2=abs(shift-ceil(shift));
\r
646 FreeImage_GetPixelColor(m_bmp,x,(height-1)-(starty+offset1),&color1);
\r
647 for(int y=starty+dy; y!=endy; y+=dy)
\r
649 FreeImage_GetPixelColor(m_bmp,x,(height-1)-(y+offset2),&color2);
\r
651 newcolor.rgbRed=(color1.rgbRed*part1)+(color2.rgbRed*part2);
\r
652 newcolor.rgbGreen=(color1.rgbGreen*part1)+(color2.rgbGreen*part2);
\r
653 newcolor.rgbBlue=(color1.rgbBlue*part1)+(color2.rgbBlue*part2);
\r
655 FreeImage_SetPixelColor(m_bmp,x,(height-1)-y,&newcolor);
\r
662 } // namespace FreeImage
\r