这是部分源码献给大家吧!
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "MyPicture.h"
#include "jpeg.hpp"
#include <math.h>
#define MIN(A,B) (A<B)?A:B
#define MAX(A,B) (A>B)?A:B
#include "mainunit.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
void S_RevColor(Graphics::TBitmap *Bmp)//图片反色
{
BYTE *ptr;
for (int y = 0; y < Bmp->Height; y++)
{
ptr =(BYTE *) Bmp->ScanLine[y];
for (int x = 0; x < Bmp->Width*3; x+=3){
ptr[x]=BYTE(255-ptr[x]);
ptr[x+1]=BYTE(255-ptr[x+1]);
ptr[x+2]=BYTE(255-ptr[x+2]);
}
}
}
void S_Gray(Graphics::TBitmap *Bmp)//灰阶效果
{
long Gray;
BYTE *ptr;
for (int y = 0; y < Bmp->Height; y++)
{
ptr =(BYTE *) Bmp->ScanLine[y];
for (int x = 0; x < Bmp->Width*3; x+=3){
Gray=299*ptr[x+2]+587*ptr[x+1]
+114*ptr[x];
ptr[x]=BYTE(Gray/1000);
ptr[x+1]=BYTE(Gray/1000);
ptr[x+2]=BYTE(Gray/1000);
}
}
}
void S_ChgLight(Graphics::TBitmap *Bmp,int s)//改变亮度
{
s=s+100;
int XX;
BYTE *ptr;
for (int y = 0; y < Bmp->Height; y++)
{
ptr =(BYTE *) Bmp->ScanLine[y];
for (int x = 0; x < Bmp->Width*3; x+=3){
XX=((ptr[x+2]-128)*s+12800)/100;
XX=MAX(XX,0);XX=MIN(XX,255);ptr[x+2]=XX;
XX=((ptr[x+1]-128)*s+12800)/100;
XX=MAX(XX,0);XX=MIN(XX,255);ptr[x+1]=XX;
XX=((ptr[x]-128)*s+12800)/100;
XX=MAX(XX,0);XX=MIN(XX,255);ptr[x+0]=XX;
}
}
}
void S_ChgContrast(Graphics::TBitmap *Bmp,int b)//改变对比度
{
int XX;
BYTE *ptr;
for (int y = 0; y < Bmp->Height; y++)
{
ptr =(BYTE *) Bmp->ScanLine[y];
for (int x = 0; x < Bmp->Width*3; x+=3){
XX=ptr[x+2]+b;
XX=MAX(XX,0);XX=MIN(XX,255);ptr[x+2]=XX;
XX=ptr[x+1]+b;
XX=MAX(XX,0);XX=MIN(XX,255);ptr[x+1]=XX;
XX=ptr[x]+b;
XX=MAX(XX,0);XX=MIN(XX,255);ptr[x+0]=XX;
}
}
}
void S_Filter(Graphics::TBitmap *Source,Graphics::TBitmap *NewPic,int Div,int xg)//图片效果
{
//0:模糊1:锐化3:雕刻
int flt[9];
switch (xg)
{
case 0:{
flt[0]=5;flt[1]=5;flt[2]=5;
flt[3]=5;flt[4]=60;flt[5]=5;
flt[6]=5;flt[7]=5;flt[8]=5;
} ;
break;
case 1:{
flt[0]=0;flt[1]=-5;flt[2]=0;
flt[3]=-5;flt[4]=30;flt[5]=-5;
flt[6]=0;flt[7]=-5;flt[8]=0;
} ;
break;
default:{
flt[0]=-15;flt[1]=-15;flt[2]=0;
flt[3]=-15;flt[4]=15;flt[5]=15;
flt[6]=0;flt[7]=15;flt[8]=0;
} ;
}
int XX[3];
BYTE *ptr,*ptru,*ptrd,*ptr1;
NewPic->Assign(Source);
for (int y = 1; y < NewPic->Height-1; y++)
{
ptr=(BYTE *)NewPic->ScanLine[y];
ptr1=(BYTE *)Source->ScanLine[y];
ptru=(BYTE *)Source->ScanLine[y-1];
ptrd=(BYTE *)Source->ScanLine[y+1];
for (int x=3;x<(Source->Width-1)*3;x+=3){
XX[0]=0;XX[1]=0;XX[2]=0;
for (int i=-1;i<=1;i++)
for (int j=0;j<3;j++)
XX[j]+=ptr1[x+3*i+j]*flt[4+i];
for (int i=-1;i<=1;i++)
for (int j=0;j<3;j++)
XX[j]+=ptru[x+3*i+j]*flt[1+i];
for (int i=-1;i<=1;i++)
for (int j=0;j<3;j++)
XX[j]+=ptrd[x+3*i+j]*flt[7+i];
for (int i=0;i<3;i++){
XX=XX/Div;
XX=MAX(XX,0);
XX=MIN(XX,255);
ptr[x+i]=XX;
}
}
}
}
void S_ChgColor(Graphics::TBitmap *tmpBitmap,int X,int Y,int Z)//色彩平衡
{
for (int y = 0; y < tmpBitmap->Height; y++)
{
int XX;
BYTE *ptr;
ptr =(BYTE *) tmpBitmap->ScanLine[y];
for (int x = 0; x < tmpBitmap->Width*3; x+=3){
XX=ptr[x+2]+X;
XX=MAX(XX,0);XX=MIN(XX,255);ptr[x+2]=XX;
XX=ptr[x+1]+Y;
XX=MAX(XX,0);XX=MIN(XX,255);ptr[x+1]=XX;
XX=ptr[x]+Z;
XX=MAX(XX,0);XX=MIN(XX,255);ptr[x+0]=XX;
}
}
}
void S_ChgWH(Graphics::TBitmap *Source,Graphics::TBitmap *NewPic,int w,int h)//改变大小
{
NewPic->Width=w;
NewPic->Height=h;
NewPic->Canvas->StretchDraw(TRect(0,0,w,h),Source);
}
void S_PicXZ(Graphics::TBitmap *Source,Graphics::TBitmap *NewPic,int angle)//图片旋转
{
if(angle>180)angle=360-angle;
if(angle<-180)angle=360+angle;
float radians=3.1415/180.00*angle;
float cosine=(float)cos(radians);
float sine=(float)sin(radians);
if (cosine < 0)cosine = -cosine;
if (sine < 0)sine = -sine;
float Point1x=(-Source->Height*sine);
float Point1y=(Source->Height*cosine);
float Point2x=(Source->Width*cosine-Source->Height*sine);
float Point2y=(Source->Height*cosine+Source->Width*sine);
float Point3x=(Source->Width*cosine);
float Point3y=(Source->Width*sine);
float minx=min((float)0,min(Point1x,min(Point2x,Point3x)));
float miny=min((float)0,min(Point1y,min(Point2y,Point3y)));
float maxx=max(Point1x,max(Point2x,Point3x));
float maxy=max(Point1y,max(Point2y,Point3y));
int DestBitmapWidth,DestBitmapHeight;
if(angle>90&&angle<180)
DestBitmapWidth=(int)ceil(-minx);
else
DestBitmapWidth=(int)ceil(maxx-minx);
if(angle>-180&&angle<-90)
DestBitmapHeight=(int)ceil(-miny);
else
DestBitmapHeight=(int)ceil(maxy-miny);
NewPic->Height=DestBitmapHeight;
NewPic->Width=DestBitmapWidth;
for(int x=0;x<DestBitmapWidth;x++)
{
for(int y=0;y<DestBitmapHeight;y++){
int SrcBitmapx=(int)((x+minx)*cosine+(y+miny)*sine);
int SrcBitmapy=(int)((y+miny)*cosine-(x+minx)*sine);
if(SrcBitmapx>=0&&SrcBitmapx<Source->Width&&SrcBitmapy>=0&&
SrcBitmapy<Source->Height)
{
NewPic->Canvas->Pixels[x][y]=
Source->Canvas->Pixels[SrcBitmapx][SrcBitmapy];
}
}
}
}
void S_PicXZ1(Graphics::TBitmap *Source,Graphics::TBitmap *NewPic,int angle)
{
NewPic->Height=Source->Width;
int www=Source->Width;
NewPic->Width=Source->Height;
int g=Source->Height;
int k=Source->Width;
Byte *ptr;
Byte *newscanline;
NewPic->PixelFormat = Source->PixelFormat;
for (int y =0;y<k-1;y++)
{
newscanline =(Byte*)NewPic->ScanLine[y];
for (int x =0;x <g-1;x++){
ptr = (Byte*)Source->ScanLine[x];
int i,j;
i=x*3;
j=y*3;
if(angle==0)
{
newscanline = ptr[www*3-j];
newscanline[i-1] = ptr[www*3-j-1];
newscanline[i-2] = ptr[www*3-j-2];
// newscanline[i-3] = ptr[www*3-j-3];
}
if(angle==1)
{
newscanline[g*3-i] = ptr[j];
newscanline[g*3-i-1] = ptr[j-1];
newscanline[g*3-i-2] = ptr[j-2];
// newscanline[g*3-i-3] = ptr[j-3];
}
}
}
}
void S_SaveToJpg(Graphics::TBitmap *Source,AnsiString filename,int ys)//保存成JPG
{
TJPEGImage *jp = new TJPEGImage();
try
{
jp->Assign(Source);
jp->CompressionQuality=ys;
jp->Compress();
jp->SaveToFile(filename);
}
__finally
{
delete jp;
}
}
void S_Emboss(Graphics::TBitmap *Bmp)//雕刻
{
int x,y;
Byte * p1,*p2;
for(int y=0;y<Bmp->Height-1;y++)
{
p1=(Byte *)Bmp->ScanLine[y];
p2=(Byte *)Bmp->ScanLine[y+1];
for(int x=0;x<Bmp->Width-3;x++)
{
p1[x*3]=(p1[x*3]+(p2[(x+3)*3] ^ 0xFF))>> 1;
p1[x*3+1]=(p1[x*3+1]+(p2[(x+3)*3+1] ^ 0xFF))>>1;
p1[x*3+2]=(p1[x*3+2]+(p2[(x+3)*3+2] ^ 0xFF))>>1;
}
}
}
float ArcTan2(float xt,float yt)
{
float value;
if (xt ==0)
if(yt > 0)
value= 3.1416/2;
else
value= -(3.1416/2);
else{
value= atan(yt/xt);
if (xt < 0)
value=3.1416 + atan(yt/xt);
}
return value;
}
long Round(float value)
{
int a=(int)value;
if(value+0.5>a+1)a=a+1;
return a;
}
void Twist(Graphics::TBitmap *Bmp,Graphics::TBitmap *Dst,int Amount)
{
BYTE *sli,* slo ;
float fxmid, fymid, txmid, tymid ,fx,fy, tx2, ty2, r, theta,dx, dy,OFFSET;
int ifx, ify;
float weight_x[1], weight_y[1];
float weight;
int new_red, new_green, new_blue;
float total_red, total_green ;
float total_blue ;
OFFSET = -(Pi/2);
dx= Bmp->Width - 1;
dy = Bmp->Height - 1;
r = sqrt(dx * dx + dy * dy);
tx2 = r;
ty2 = r;
txmid = (Bmp->Width-1)/2; //Adjust these to move center of rotation
tymid = (Bmp->Height-1)/2; //Adjust these to move ->->->->->->
fxmid = (Bmp->Width-1)/2;
fymid = (Bmp->Height-1)/2;
if(tx2 >= Bmp->Width)tx2 = Bmp->Width-1;
if(ty2 >= Bmp->Height)ty2 = Bmp->Height-1;
for(int ty = 0;ty<Round(ty2);ty++)
{
for(int tx = 0;tx<Round(tx2);tx++)
{
dx = tx - txmid;
dy = ty - tymid;
r = sqrt(dx * dx + dy * dy);
if(r == 0)
{
fx = 0;
fy = 0;
}
else
{
theta = ArcTan2(dx,dy) - r/Amount - OFFSET;
fx = r * cos(theta);
fy = r * sin(theta);
}
fx = fx + fxmid;
fy = fy + fymid;
ify = (int)(fy);
ifx = (int)(fx);
if(fy >= 0)
{
weight_y[1] = fy - ify;
weight_y[0] = 1 - weight_y[1];
}
else
{
weight_y[0] = -(fy - ify);
weight_y[1] = 1 - weight_y[0];
}
if(fx >= 0)
{
weight_x[1] = fx - ifx;
weight_x[0] = 1 - weight_x[1];
}
else
{
weight_x[0] = -(fx - ifx);
weight_x[1] = 1 - weight_x[0];
}
if(ifx < 0)
ifx = Bmp->Width-1-(-ifx % Bmp->Width);
else
if(ifx > Bmp->Width-1)
ifx = ifx % Bmp->Width;
if(ify < 0)
ify = Bmp->Height-1-(-ify%Bmp->Height);
else
if(ify > Bmp->Height-1)
ify = ify % Bmp->Height;
total_red = 0.0;
total_green = 0.0;
total_blue = 0.0;
for(int ix = 0;ix<=1;ix++){
for(int iy = 0;iy<=1;iy++){
if(ify + iy < Bmp->Height)
sli =(BYTE *)Bmp->ScanLine[ify + iy];
else
sli =(BYTE *)Bmp->ScanLine[Bmp->Height - ify - iy];
if(ifx + ix < Bmp->Width){
new_red = sli[(ifx + ix)*3];
new_green = sli[(ifx + ix)*3+1];
new_blue = sli[(ifx + ix)*3+2];
}
else
{
new_red = sli[(Bmp->Width - ifx - ix)*3];
new_green = sli[(Bmp->Width - ifx - ix)*3+1];
new_blue = sli[(Bmp->Width - ifx - ix)*3+2];
}
weight = weight_x[ix] * weight_y[iy];
total_red = total_red + new_red * weight;
total_green = total_green + new_green * weight;
total_blue = total_blue + new_blue * weight;
}
}
slo =(BYTE *)Dst->ScanLine[ty];
slo[tx*3] = BYTE(Round(total_red));
slo[tx*3+1] = BYTE(Round(total_green));
slo[tx*3+2] = BYTE(Round(total_blue));
}
}
}
void ImageFlipV(Graphics::TBitmap *Source,Graphics::TBitmap *NewPic)
{
NewPic->Height=Source->Height;
NewPic->Width=Source->Width;
int g=Source->Height;
int k=Source->Width;
Byte *ptr;
Byte *newscanline;
NewPic->PixelFormat = Source->PixelFormat;
for (int y =1;y<g;y++)
{
newscanline =(Byte*)NewPic->ScanLine[y];
ptr = (Byte*)Source->ScanLine[y];
for (int x = 1;x <k;x++)
{
newscanline[x*3-1] = ptr[k*3-x*3-1];
newscanline[x*3-2] = ptr[k*3-x*3-2];
newscanline[x*3-3]=ptr[k*3-x*3-3];
}
}
}
void ImageFlipH(Graphics::TBitmap *Source,Graphics::TBitmap *NewPic)
{
NewPic->Height=Source->Height;
NewPic->Width=Source->Width;
int g=Source->Height;
int k=Source->Width;
Byte *ptr;
Byte *newscanline;
NewPic->PixelFormat = Source->PixelFormat;
for (int y =1;y<g;y++)
{
newscanline =(Byte*)NewPic->ScanLine[y];
ptr = (Byte*)Source->ScanLine[g-y];
for (int x = 1;x <k;x++)
{
newscanline[x*3-1] = ptr[x*3-1];
newscanline[x*3-2] = ptr[x*3-2];
newscanline[x*3-3] = ptr[x*3-3];
}
}
}
void ImageFlip90(Graphics::TBitmap *Source,Graphics::TBitmap *NewPic,int angle)
{
NewPic->Height=Source->Width;
int www=Source->Width;
NewPic->Width=Source->Height;
int g=Source->Height;
int k=Source->Width;
Byte *ptr;
Byte *newscanline;
NewPic->PixelFormat = Source->PixelFormat;
for (int y =0;y<k;y++)
{
newscanline = (Byte*)NewPic->ScanLine[y];
for (int x = 1;x < g;x++){
ptr = (Byte*)Source->ScanLine[g-x];
int i,j;
i=x*3;
j=y*3;
if(angle==0)
{
// newscanline = ptr[www*3-j];
newscanline[i-1] = ptr[www*3-j-1];
newscanline[i-2] = ptr[www*3-j-2];
newscanline[i-3] = ptr[www*3-j-3];
}
if(angle==1)
{
// newscanline[g*3-i] = ptr[j];
newscanline[g*3-i-1] = ptr[j-1];
newscanline[g*3-i-2] = ptr[j-2];
newscanline[g*3-i-3] = ptr[j-3];
}
}
}
}
//截屏
void copyscreen(Graphics::TBitmap *bmp1,int top,int left,int right,int bottom)
{
bmp1->Width=right-left;
bmp1->Height=bottom-top;
HDC ScreenDC;
ScreenDC=GetDC(0);
BitBlt(bmp1->Canvas->Handle, 0, 0, right-left,bottom-top, ScreenDC,left,top, SRCCOPY);
ReleaseDC(0, ScreenDC);
}
void getwindow(Graphics::TBitmap *bmp1)
{
TRect rect;
HWND AWnd;
AWnd = GetForegroundWindow();
GetWindowRect(AWnd,&rect);
copyscreen(bmp1,rect.Top,rect.Left,rect.Right,rect.Bottom);
}