- Main Page
- Data Structures
- Files

- File List

# E:/DevIL/src-ILU/src/ilu_rotate.c

    00001 //-----------------------------------------------------------------------------
    00002 //
    00003 // ImageLib Utility Sources
    00004 // Copyright (C) 2000-2002 by Denton Woods
    00005 // Last modified: 05/25/2002 <--Y2K Compliant! =]
    00006 //
    00007 // Filename: src-ILU/src/ilu_rotate.c
    00008 //
    00009 // Description: Rotates an image.
    00010 //
    00011 //-----------------------------------------------------------------------------
    00012
    00013
    00014 #include "ilu_internal.h"
    00015 #include "ilu_states.h"
    00016
    00017
    00018 ILboolean ILAPIENTRY iluRotate(ILfloat Angle)
    00019 {
    00020         ILimage *Temp, *Temp1, *CurImage = NULL;
    00021         ILenum  PalType = 0;
    00022
    00023         iluCurImage = ilGetCurImage();
    00024         if (iluCurImage == NULL) {
    00025                 ilSetError(ILU_ILLEGAL_OPERATION);
    00026                 return IL_FALSE;
    00027         }
    00028
    00029         if (iluCurImage->Format == IL_COLOUR_INDEX) {
    00030                 PalType = iluCurImage->Pal.PalType;
    00031                 CurImage = iluCurImage;
    00032                 iluCurImage = iConvertImage(iluCurImage, ilGetPalBaseType(CurImage->Pal.PalType), IL_UNSIGNED_BYTE);
    00033         }
    00034
    00035         Temp = iluRotate_(iluCurImage, Angle);
    00036         if (Temp != NULL) {
    00037                 if (PalType != 0) {
    00038                         ilCloseImage(iluCurImage);
    00039                         Temp1 = iConvertImage(Temp, IL_COLOUR_INDEX, IL_UNSIGNED_BYTE);
    00040                         ilCloseImage(Temp);
    00041                         Temp = Temp1;
    00042                         ilSetCurImage(CurImage);
    00043                 }
    00044                 ilTexImage(Temp->Width, Temp->Height, Temp->Depth, Temp->Bpp, Temp->Format, Temp->Type, Temp->Data);
    00045                 if (PalType != 0) {
    00046                         iluCurImage = ilGetCurImage();
    00047                         iluCurImage->Pal.PalSize = Temp->Pal.PalSize;
    00048                         iluCurImage->Pal.PalType = Temp->Pal.PalType;
    00049                         iluCurImage->Pal.Palette = (ILubyte*)ialloc(Temp->Pal.PalSize);
    00050                         if (iluCurImage->Pal.Palette == NULL) {
    00051                                 ilCloseImage(Temp);
    00052                                 return IL_FALSE;
    00053                         }
    00054                         memcpy(iluCurImage->Pal.Palette, Temp->Pal.Palette, Temp->Pal.PalSize);
    00055                 }
    00056
    00057                 iluCurImage->Origin = Temp->Origin;
    00058                 ilCloseImage(Temp);
    00059                 return IL_TRUE;
    00060         }
    00061         return IL_FALSE;
    00062 }
    00063
    00064
    00065 ILboolean ILAPIENTRY iluRotate3D(ILfloat x, ILfloat y, ILfloat z, ILfloat Angle)
    00066 {
    00067         ILimage *Temp;
    00068
    00069 // return IL_FALSE;
    00070
    00071         iluCurImage = ilGetCurImage();
    00072         Temp = iluRotate3D_(iluCurImage, x, y, z, Angle);
    00073         if (Temp != NULL) {
    00074                 ilTexImage(Temp->Width, Temp->Height, Temp->Depth, Temp->Bpp, Temp->Format, Temp->Type, Temp->Data);
    00075                 iluCurImage->Origin = Temp->Origin;
    00076                 ilSetPal(&Temp->Pal);
    00077                 ilCloseImage(Temp);
    00078                 return IL_TRUE;
    00079         }
    00080         return IL_FALSE;
    00081 }
    00082
    00083
    00085 //  Code help comes from http://www.leunen.com/cbuilder/rotbmp.html.
    00086 ILAPI ILimage* ILAPIENTRY iluRotate_(ILimage *Image, ILfloat Angle)
    00087 {
    00088         ILimage         *Rotated = NULL;
    00089         ILint           x, y, c;
    00090         ILdouble        Cos, Sin;
    00091         ILuint          RotOffset, ImgOffset;
    00092         ILint           MinX, MinY, MaxX, MaxY;
    00093         ILushort        *ShortPtr;
    00094         ILuint          *IntPtr;
    00095         ILdouble        *DblPtr;
    00096         ILdouble        Point1x, Point1y, Point2x, Point2y, Point3x, Point3y;
    00097         ILint           SrcX, SrcY;
    00098
    00099         // Multiples of 90 are special.
    00100         Angle = (ILfloat)fmod((ILdouble)Angle, 360.0);
    00101         if (Angle < 0)
    00102                 Angle = 360.0f + Angle;
    00103
    00104         Cos = (ILdouble)cos((IL_PI * Angle) / 180.0);
    00105         Sin = (ILdouble)sin((IL_PI * Angle) / 180.0);
    00106
    00107         Point1x = (-(ILint)Image->Height*Sin);
    00108         Point1y = (Image->Height*Cos);
    00109         Point2x = (Image->Width*Cos-Image->Height*Sin);
    00110         Point2y = (Image->Height*Cos+Image->Width*Sin);
    00111         Point3x = (Image->Width*Cos);
    00112         Point3y = (Image->Width*Sin);
    00113
    00114         MinX = (ILint)min(0, min(Point1x, min(Point2x, Point3x)));
    00115         MinY = (ILint)min(0, min(Point1y, min(Point2y, Point3y)));
    00116         MaxX = (ILint)max(Point1x, max(Point2x, Point3x));
    00117         MaxY = (ILint)max(Point1y, max(Point2y, Point3y));
    00118
    00119         Rotated = (ILimage*)icalloc(1, sizeof(ILimage));
    00120         if (Rotated == NULL)
    00121                 return NULL;
    00122         if (ilCopyImageAttr(Rotated, Image) == IL_FALSE) {
    00123                 ilCloseImage(Rotated);
    00124                 return NULL;
    00125         }
    00126
    00127         if (ilResizeImage(Rotated, (ILuint)ceil(fabs(MaxX) - MinX), (ILuint)ceil(fabs(MaxY) - MinY), 1, Image->Bpp, Image->Bpc) == IL_FALSE) {
    00128                 ilCloseImage(Rotated);
    00129                 return IL_FALSE;
    00130         }
    00131
    00132         ilClearImage_(Rotated);
    00133
    00134         ShortPtr = (ILushort*)iluCurImage->Data;
    00135         IntPtr = (ILuint*)iluCurImage->Data;
    00136         DblPtr = (ILdouble*)iluCurImage->Data;
    00137
    00138         //if (iluFilter == ILU_NEAREST) {
    00139         switch (iluCurImage->Bpc)
    00140         {
    00141                 case 1:  // Byte-based (most images)
    00142                         if (Angle == 90.0) {
    00143                                 for (x = 0; x < (ILint)Image->Width; x++) {
    00144                                         for (y = 0; y < (ILint)Image->Height; y++) {
    00145                                                 RotOffset = x * Rotated->Bps + (Image->Width - 1 - y) * Rotated->Bpp;
    00146                                                 ImgOffset = y * Image->Bps + x * Image->Bpp;
    00147                                                 for (c = 0; c < Rotated->Bpp; c++) {
    00148                                                         Rotated->Data[RotOffset + c] = Image->Data[ImgOffset + c];
    00149                                                 }
    00150                                         }
    00151                                 }
    00152                         }
    00153                         else if (Angle == 180.0) {
    00154                                 for (x = 0; x < (ILint)Image->Width; x++) {
    00155                                         for (y = 0; y < (ILint)Image->Height; y++) {
    00156                                                 RotOffset = (Image->Height - 1 - y) * Rotated->Bps + x * Rotated->Bpp;
    00157                                                 ImgOffset = y * Image->Bps + x * Image->Bpp;
    00158                                                 for (c = 0; c < Rotated->Bpp; c++) {
    00159                                                         Rotated->Data[RotOffset + c] = Image->Data[ImgOffset + c];
    00160                                                 }
    00161                                         }
    00162                                 }
    00163                         }
    00164                         else if (Angle == 270.0) {
    00165                                 for (x = 0; x < (ILint)Image->Width; x++) {
    00166                                         for (y = 0; y < (ILint)Image->Height; y++) {
    00167                                                 RotOffset = (Image->Height - 1 - x) * Rotated->Bps + y * Rotated->Bpp;
    00168                                                 ImgOffset = y * Image->Bps + x * Image->Bpp;
    00169                                                 for (c = 0; c < Rotated->Bpp; c++) {
    00170                                                         Rotated->Data[RotOffset + c] = Image->Data[ImgOffset + c];
    00171                                                 }
    00172                                         }
    00173                                 }
    00174                         }
    00175                         else {
    00176                                 for (x = 0; x < (ILint)Rotated->Width; x++) {
    00177                                         for (y = 0; y < (ILint)Rotated->Height; y++) {
    00178                                                 SrcX = (ILint)((x + MinX) * Cos + (y + MinY) * Sin);
    00179                                                 SrcY = (ILint)((y + MinY) * Cos - (x + MinX) * Sin);
    00180                                                 if (SrcX >= 0 && SrcX < (ILint)Image->Width && SrcY >= 0 && SrcY < (ILint)Image->Height) {
    00181                                                         RotOffset = y * Rotated->Bps + x * Rotated->Bpp;
    00182                                                         ImgOffset = (ILuint)SrcY * Image->Bps + (ILuint)SrcX * Image->Bpp;
    00183                                                         for (c = 0; c < Rotated->Bpp; c++) {
    00184                                                                 Rotated->Data[RotOffset + c] = Image->Data[ImgOffset + c];
    00185                                                         }
    00186                                                 }
    00187                                         }
    00188                                 }
    00189                         }
    00190                         break;
    00191
    00192                 case 2:  // Short-based
    00193                         Image->Bps /= 2;   // Makes it easier to just
    00194                         Rotated->Bps /= 2; //   cast to short.
    00195
    00196                         if (Angle == 90.0) {
    00197                                 for (x = 0; x < (ILint)Image->Width; x++) {
    00198                                         for (y = 0; y < (ILint)Image->Height; y++) {
    00199                                                 RotOffset = x * Rotated->Bps + (Image->Width - 1 - y) * Rotated->Bpp;
    00200                                                 ImgOffset = y * Image->Bps + x * Image->Bpp;
    00201                                                 for (c = 0; c < Rotated->Bpp; c++) {
    00202                                                         ((ILushort*)(Rotated->Data))[RotOffset + c] = ShortPtr[ImgOffset + c];
    00203                                                 }
    00204                                         }
    00205                                 }
    00206                         }
    00207                         else if (Angle == 180.0) {
    00208                                 for (x = 0; x < (ILint)Image->Width; x++) {
    00209                                         for (y = 0; y < (ILint)Image->Height; y++) {
    00210                                                 RotOffset = (Image->Height - 1 - y) * Rotated->Bps + x * Rotated->Bpp;
    00211                                                 ImgOffset = y * Image->Bps + x * Image->Bpp;
    00212                                                 for (c = 0; c < Rotated->Bpp; c++) {
    00213                                                         ((ILushort*)(Rotated->Data))[RotOffset + c] = ShortPtr[ImgOffset + c];
    00214                                                 }
    00215                                         }
    00216                                 }
    00217                         }
    00218                         else if (Angle == 270.0) {
    00219                                 for (x = 0; x < (ILint)Image->Width; x++) {
    00220                                         for (y = 0; y < (ILint)Image->Height; y++) {
    00221                                                 RotOffset = (Image->Height - 1 - x) * Rotated->Bps + y * Rotated->Bpp;
    00222                                                 ImgOffset = y * Image->Bps + x * Image->Bpp;
    00223                                                 for (c = 0; c < Rotated->Bpp; c++) {
    00224                                                         ((ILushort*)(Rotated->Data))[RotOffset + c] = ShortPtr[ImgOffset + c];
    00225                                                 }
    00226                                         }
    00227                                 }
    00228                         }
    00229                         else {
    00230                                 for (x = 0; x < (ILint)Rotated->Width; x++) {
    00231                                         for (y = 0; y < (ILint)Rotated->Height; y++) {
    00232                                                 SrcX = (ILint)((x + MinX) * Cos + (y + MinY) * Sin);
    00233                                                 SrcY = (ILint)((y + MinY) * Cos - (x + MinX) * Sin);
    00234                                                 if (SrcX >= 0 && SrcX < (ILint)Image->Width && SrcY >= 0 && SrcY < (ILint)Image->Height) {
    00235                                                         RotOffset = y * Rotated->Bps + x * Rotated->Bpp;
    00236                                                         ImgOffset = (ILuint)SrcY * Image->Bps + (ILuint)SrcX * Image->Bpp;
    00237                                                         for (c = 0; c < Rotated->Bpp; c++) {
    00238                                                                 ((ILushort*)(Rotated->Data))[RotOffset + c] = ShortPtr[ImgOffset + c];
    00239                                                         }
    00240                                                 }
    00241                                         }
    00242                                 }
    00243                         }
    00244                         Image->Bps *= 2;
    00245                         Rotated->Bps *= 2;
    00246                         break;
    00247
    00248                 case 4:  // Floats or 32-bit integers
    00249                         Image->Bps /= 4;
    00250                         Rotated->Bps /= 4;
    00251
    00252                         if (Angle == 90.0) {
    00253                                 for (x = 0; x < (ILint)Image->Width; x++) {
    00254                                         for (y = 0; y < (ILint)Image->Height; y++) {
    00255                                                 RotOffset = x * Rotated->Bps + (Image->Width - 1 - y) * Rotated->Bpp;
    00256                                                 ImgOffset = y * Image->Bps + x * Image->Bpp;
    00257                                                 for (c = 0; c < Rotated->Bpp; c++) {
    00258                                                         ((ILuint*)(Rotated->Data))[RotOffset + c] = IntPtr[ImgOffset + c];
    00259                                                 }
    00260                                         }
    00261                                 }
    00262                         }
    00263                         else if (Angle == 180.0) {
    00264                                 for (x = 0; x < (ILint)Image->Width; x++) {
    00265                                         for (y = 0; y < (ILint)Image->Height; y++) {
    00266                                                 RotOffset = (Image->Height - 1 - y) * Rotated->Bps + x * Rotated->Bpp;
    00267                                                 ImgOffset = y * Image->Bps + x * Image->Bpp;
    00268                                                 for (c = 0; c < Rotated->Bpp; c++) {
    00269                                                         ((ILuint*)(Rotated->Data))[RotOffset + c] = IntPtr[ImgOffset + c];
    00270                                                 }
    00271                                         }
    00272                                 }
    00273                         }
    00274                         else if (Angle == 270.0) {
    00275                                 for (x = 0; x < (ILint)Image->Width; x++) {
    00276                                         for (y = 0; y < (ILint)Image->Height; y++) {
    00277                                                 RotOffset = (Image->Height - 1 - x) * Rotated->Bps + y * Rotated->Bpp;
    00278                                                 ImgOffset = y * Image->Bps + x * Image->Bpp;
    00279                                                 for (c = 0; c < Rotated->Bpp; c++) {
    00280                                                         ((ILuint*)(Rotated->Data))[RotOffset + c] = IntPtr[ImgOffset + c];
    00281                                                 }
    00282                                         }
    00283                                 }
    00284                         }
    00285                         else {
    00286                                 for (x = 0; x < (ILint)Rotated->Width; x++) {
    00287                                         for (y = 0; y < (ILint)Rotated->Height; y++) {
    00288                                                 SrcX = (ILint)((x + MinX) * Cos + (y + MinY) * Sin);
    00289                                                 SrcY = (ILint)((y + MinY) * Cos - (x + MinX) * Sin);
    00290                                                 if (SrcX >= 0 && SrcX < (ILint)Image->Width && SrcY >= 0 && SrcY < (ILint)Image->Height) {
    00291                                                         RotOffset = y * Rotated->Bps + x * Rotated->Bpp;
    00292                                                         ImgOffset = (ILuint)SrcY * Image->Bps + (ILuint)SrcX * Image->Bpp;
    00293                                                         for (c = 0; c < Rotated->Bpp; c++) {
    00294                                                                 ((ILuint*)(Rotated->Data))[RotOffset + c] = IntPtr[ImgOffset + c];
    00295                                                         }
    00296                                                 }
    00297                                         }
    00298                                 }
    00299                         }
    00300                         Image->Bps *= 4;
    00301                         Rotated->Bps *= 4;
    00302                         break;
    00303
    00304                 case 8:  // Double or 64-bit integers
    00305                         Image->Bps /= 8;
    00306                         Rotated->Bps /= 8;
    00307
    00308                         if (Angle == 90.0) {
    00309                                 for (x = 0; x < (ILint)Image->Width; x++) {
    00310                                         for (y = 0; y < (ILint)Image->Height; y++) {
    00311                                                 RotOffset = x * Rotated->Bps + (Image->Width - 1 - y) * Rotated->Bpp;
    00312                                                 ImgOffset = y * Image->Bps + x * Image->Bpp;
    00313                                                 for (c = 0; c < Rotated->Bpp; c++) {
    00314                                                         ((ILdouble*)(Rotated->Data))[RotOffset + c] = DblPtr[ImgOffset + c];
    00315                                                 }
    00316                                         }
    00317                                 }
    00318                         }
    00319                         else if (Angle == 180.0) {
    00320                                 for (x = 0; x < (ILint)Image->Width; x++) {
    00321                                         for (y = 0; y < (ILint)Image->Height; y++) {
    00322                                                 RotOffset = (Image->Height - 1 - y) * Rotated->Bps + x * Rotated->Bpp;
    00323                                                 ImgOffset = y * Image->Bps + x * Image->Bpp;
    00324                                                 for (c = 0; c < Rotated->Bpp; c++) {
    00325                                                         ((ILdouble*)(Rotated->Data))[RotOffset + c] = DblPtr[ImgOffset + c];
    00326                                                 }
    00327                                         }
    00328                                 }
    00329                         }
    00330                         else if (Angle == 270.0) {
    00331                                 for (x = 0; x < (ILint)Image->Width; x++) {
    00332                                         for (y = 0; y < (ILint)Image->Height; y++) {
    00333                                                 RotOffset = (Image->Height - 1 - x) * Rotated->Bps + y * Rotated->Bpp;
    00334                                                 ImgOffset = y * Image->Bps + x * Image->Bpp;
    00335                                                 for (c = 0; c < Rotated->Bpp; c++) {
    00336                                                         ((ILdouble*)(Rotated->Data))[RotOffset + c] = DblPtr[ImgOffset + c];
    00337                                                 }
    00338                                         }
    00339                                 }
    00340                         }
    00341                         else {
    00342                                 for (x = 0; x < (ILint)Rotated->Width; x++) {
    00343                                         for (y = 0; y < (ILint)Rotated->Height; y++) {
    00344                                                 SrcX = (ILint)((x + MinX) * Cos + (y + MinY) * Sin);
    00345                                                 SrcY = (ILint)((y + MinY) * Cos - (x + MinX) * Sin);
    00346                                                 if (SrcX >= 0 && SrcX < (ILint)Image->Width && SrcY >= 0 && SrcY < (ILint)Image->Height) {
    00347                                                         RotOffset = y * Rotated->Bps + x * Rotated->Bpp;
    00348                                                         ImgOffset = (ILuint)SrcY * Image->Bps + (ILuint)SrcX * Image->Bpp;
    00349                                                         for (c = 0; c < Rotated->Bpp; c++) {
    00350                                                                 ((ILdouble*)(Rotated->Data))[RotOffset + c] = DblPtr[ImgOffset + c];
    00351                                                         }
    00352                                                 }
    00353                                         }
    00354                                 }
    00355                         }
    00356                         Image->Bps *= 8;
    00357                         Rotated->Bps *= 8;
    00358                         break;
    00359         }
    00360
    00361         return Rotated;
    00362 }
    00363
    00364
    00365 ILAPI ILimage* ILAPIENTRY iluRotate3D_(ILimage *Image, ILfloat x, ILfloat y, ILfloat z, ILfloat Angle)
    00366 {
    00367         Image; x; y; z; Angle;
    00368         return NULL;
    00369 }