1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
| //---------------------------------------------------------------------------
#include "Images.h"
//---------------------------------------------------------------------------
TImages::TImages(AnsiString fichier)
{
FBitmap=NULL;
FBitmapDest=NULL;
FAngle=0;
FFichier=fichier;
Traitement();
}
//---------------------------------------------------------------------------
TImages::~TImages()
{
if (FBitmap)
delete FBitmap;
if (FBitmapDest)
delete FBitmapDest;
}
//---------------------------------------------------------------------------
void TImages::Traitement(void)
{
TStringList *ext=new TStringList();
ext->Add(".jpg");
ext->Add(".bmp");
int index=ext->IndexOf(ExtractFileExt(FFichier));
delete ext;
if (index==-1)
return;
if (FBitmap)
delete FBitmap;
FBitmap=new Graphics::TBitmap();
if (FBitmapDest)
delete FBitmapDest;
FBitmapDest=new Graphics::TBitmap();
// Chargement Image d'entrée
TJPEGImage * jpg;
switch(index)
{
case 0:
jpg=new TJPEGImage();
jpg->LoadFromFile(FFichier);
FBitmap->Assign(jpg);
delete jpg;
break;
case 1:
FBitmap->LoadFromFile(FFichier);
break;
}
// Calcul Image de sortie
switch (FAngle%360)
{
case 0: FBitmapDest->Assign(FBitmap); break;
case 90:
case -270: TournePlusAngleDroit(); break;
case -90:
case 270: TourneMoinsAngleDroit(); break;
case 180:
case -180: TourneAnglePI(); break;
default: TourneAngleNonParticulier(); break;
}
}
//---------------------------------------------------------------------------
void TImages::TourneMoinsAngleDroit(void)
{
FBitmap->PixelFormat=pf32bit; FBitmapDest->PixelFormat=pf32bit;
FBitmapDest->Height=FBitmap->Width; FBitmapDest->Width=FBitmap->Height;
Byte ** tablesource=ImageVersTableau(FBitmap); Byte ** tabledestination=ImageVersTableau(FBitmapDest);
int i,j,k,pos,pos2;
for(j=0;j<FBitmap->Height;j++)
{
pos2=(FBitmapDest->Width-j-1)<<2;
for(i=0;i<FBitmap->Width;i++)
{
pos=i<<2;
for (k=0;k<4;k++)
tabledestination[i][pos2+k]=tablesource[j][pos+k];
}
}
if (tablesource) delete [] tablesource; if (tabledestination) delete [] tabledestination;
}
//---------------------------------------------------------------------------
void TImages::TournePlusAngleDroit(void)
{
FBitmap->PixelFormat=pf32bit; FBitmapDest->PixelFormat=pf32bit;
FBitmapDest->Height=FBitmap->Width; FBitmapDest->Width=FBitmap->Height;
Byte ** tablesource=ImageVersTableau(FBitmap); Byte ** tabledestination=ImageVersTableau(FBitmapDest);
int i,j,k,pos,pos2,pos3;
for(i=0;i<FBitmap->Height;i++)
{
pos2=i<<2;
for(j=0;j<FBitmap->Width;j++)
{
pos=j<<2;
pos3=FBitmapDest->Height-j-1;
for (k=0;k<4;k++)
tabledestination[pos3][pos2+k]=tablesource[i][pos+k];
}
}
delete [] tablesource;
delete [] tabledestination;
}
//---------------------------------------------------------------------------
int TImages::LireHauteur(void)
{
if (!FBitmapDest)
return 0;
return (FBitmapDest->Height);
}
//---------------------------------------------------------------------------
int TImages::LireLargeur(void)
{
if (!FBitmapDest)
return 0;
return (FBitmapDest->Width);
}
//---------------------------------------------------------------------------
void TImages::SetAngle(int angle)
{
FAngle=angle;
Traitement();
}
//---------------------------------------------------------------------------
void TImages::TourneAnglePI(void)
{
FBitmap->PixelFormat=pf32bit; FBitmapDest->PixelFormat=pf32bit;
FBitmapDest->Height=FBitmap->Height; FBitmapDest->Width=FBitmap->Width;
Byte ** tablesource=ImageVersTableau(FBitmap); Byte ** tabledestination=ImageVersTableau(FBitmapDest);
int i,j,k,pos,pos2,pos3;
for (int j=0;j<FBitmap->Height;j++)
{
pos3=FBitmap->Height-j-1;
for (int i=0;i<FBitmap->Width;i++)
{
pos=i<<2;
pos2=(FBitmap->Width-i-1)<<2;
for (k=0;k<4;k++)
tabledestination[pos3][pos2+k]=tablesource[j][pos+k];
}
}
delete [] tablesource;
delete [] tabledestination;
}
//---------------------------------------------------------------------------
void TImages::TourneAngleNonParticulier()
{
double radians=FAngle*2*M_PI/360.0;
double sinus=sin(radians); double cosinus=cos(radians);
int largeursource=FBitmap->Width; int hauteursource=FBitmap->Height;
int x2=largeursource*cosinus; int y2=-largeursource*sinus;
int x3=hauteursource*sinus; int y3=hauteursource*cosinus;
int x4=largeursource*cosinus+hauteursource*sinus; int y4=-largeursource*sinus+hauteursource*cosinus;
int largeurdest=Maxi(abs(x4),abs(x2-x3)); int hauteurdest=Maxi(abs(y4),abs(y2-y3));
FBitmapDest->PixelFormat=pf32bit; FBitmap->PixelFormat=pf32bit;
FBitmapDest->Width=largeurdest; FBitmapDest->Height=hauteurdest;
int decalagex=Mini(Mini(0,x2),Mini(x3,x4)); int decalagey=Mini(Mini(0,y2),Mini(y3,y4));
double offsetx=((double)decalagex)*cosinus-((double)decalagey)*sinus;double offsety=((double)decalagex)*sinus+((double)decalagey)*cosinus;
TCanvas *canvas=FBitmapDest->Canvas; canvas->Brush->Style=bsSolid;
canvas->Brush->Color=COULEURFOND; canvas->Rectangle(0,0,largeurdest,hauteurdest);
Byte ** tablesource=ImageVersTableau(FBitmap); Byte ** tabledestination=ImageVersTableau(FBitmapDest);
int x_0,x_1,y_0,y_1,x,y,x_0_d,x_1_d,j_d,i,j,k;
double fx0,fx1,fy0,fy1,fy1fx0,fy1fx1,fy0fx0,fy0fx1;
for (i=0;i<hauteurdest;i++)
for(j=0;j<largeurdest;j++)
{
x=((double)j)*cosinus-((double)i)*sinus+offsetx; y=((double)j)*sinus+((double)i)*cosinus+offsety;
x_0=floor(x); x_1=x_0+1; y_0=floor(y); y_1=y_0+1;
if (y_1<0 || y_1>=hauteursource) continue; if (y_0<0 || y_0>=hauteursource) continue;
if (x_1<0 || x_1>=largeursource) continue; if (x_0<0 || x_0>=largeursource) continue;
fx1=x-x_0; fx0=1-fx1; fy1=y-y_0; fy0=1-fy1;
fy1fx0=fy1*fx0; fy1fx1=fy1*fx1; fy0fx0=fy0*fx0; fy0fx1=fy0*fx1;
x_0_d=x_0<<2; x_1_d=x_1<<2; j_d=j<<2;
for (k=0;k<4;k++)
{
tabledestination[i][j_d+k]=(Byte)((
fy1fx0*(double)tablesource[y_1][x_0_d+k]+
fy1fx1*(double)tablesource[y_1][x_1_d+k]+
fy0fx0*(double)tablesource[y_0][x_0_d+k]+
fy0fx1*(double)tablesource[y_0][x_1_d+k]));
}
}
if (tablesource) delete [] tablesource; if (tabledestination) delete [] tabledestination;
}
//---------------------------------------------------------------------------
int TImages::Maxi(int x,int y)
{
return x > y ? x : y;
}
//---------------------------------------------------------------------------
int TImages::Mini(int x,int y)
{
return x < y ? x : y;
}
//---------------------------------------------------------------------------
Byte ** TImages::ImageVersTableau(Graphics::TBitmap * image)
{
Byte ** tableau=new Byte*[image->Height];
for (int i=0;i<image->Height;i++)
tableau[i]=(Byte *)image->ScanLine[i];
return tableau;
}
//--------------------------------------------------------------------------- |
Partager