- Main Page
- Data Structures
- Files

- File List

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

    00001 //-----------------------------------------------------------------------------
    00002 //
    00003 // ImageLib Utility Sources
    00004 // Copyright (C) 2000-2002 by Denton Woods
    00005 // Last modified: 05/25/2001 <--Y2K Compliant! =]
    00006 //
    00007 // Filename: src-ILU/src/ilu_scale.c
    00008 //
    00009 // Description: Scales an image.
    00010 //
    00011 //-----------------------------------------------------------------------------
    00012
    00013
    00014 // NOTE:  Don't look at this file if you wish to preserve your sanity!
    00015
    00016
    00017 #include "ilu_internal.h"
    00018 #include "ilu_states.h"
    00019
    00020
    00021 ILimage *iluScale3DNear_(ILimage *Image, ILimage *Scaled, ILuint Width, ILuint Height, ILuint Depth);
    00022 ILimage *iluScale3DLinear_(ILimage *Image, ILimage *Scaled, ILuint Width, ILuint Height, ILuint Depth);
    00023 ILimage *iluScale3DBilinear_(ILimage *Image, ILimage *Scaled, ILuint Width, ILuint Height, ILuint Depth);
    00024
    00025 static ILuint           Size, NewX1, NewX2, NewY1, NewY2, NewZ1, NewZ2, x, y, z, c;
    00026 static ILdouble ScaleX, ScaleY, ScaleZ, x1, x2, t1, t2, t4, f, ft;
    00027 //ILdouble      Table[2][2][4];  // Assumes we don't have larger than 32-bit images.
    00028 static ILuint           ImgBps, SclBps, ImgPlane, SclPlane;
    00029 static ILushort *ShortPtr, *SShortPtr;
    00030 static ILuint           *IntPtr, *SIntPtr;
    00031
    00032
    00033 ILimage *iluScale3D_(ILimage *Image, ILimage *Scaled, ILuint Width, ILuint Height, ILuint Depth)
    00034 {
    00035         if (Image == NULL) {
    00036                 ilSetError(ILU_ILLEGAL_OPERATION);
    00037                 return IL_FALSE;
    00038         }
    00039
    00040         ScaleX = (ILfloat)Width / Image->Width;
    00041         ScaleY = (ILfloat)Height / Image->Height;
    00042         ScaleZ = (ILfloat)Depth / Image->Depth;
    00043
    00044         //if (iluFilter == ILU_NEAREST)
    00045                 return iluScale3DNear_(Image, Scaled, Width, Height, Depth);
    00046         //else if (iluFilter == ILU_LINEAR)
    00047                 //return iluScale3DLinear_(Image, Scaled, Width, Height, Depth);
    00048
    00049         // iluFilter == ILU_BILINEAR
    00050         //return iluScale3DBilinear_(Image, Scaled, Width, Height, Depth);
    00051 }
    00052
    00053
    00054 ILimage *iluScale3DNear_(ILimage *Image, ILimage *Scaled, ILuint Width, ILuint Height, ILuint Depth)
    00055 {
    00056         ImgBps = Image->Bps / Image->Bpc;
    00057         SclBps = Scaled->Bps / Scaled->Bpc;
    00058         ImgPlane = Image->SizeOfPlane / Image->Bpc;
    00059         SclPlane = Scaled->SizeOfPlane / Scaled->Bpc;
    00060
    00061         switch (Image->Bpc)
    00062         {
    00063                 case 1:
    00064                         for (z = 0; z < Depth; z++) {
    00065                                 NewZ1 = z * SclPlane;
    00066                                 NewZ2 = (ILuint)(z / ScaleZ) * ImgPlane;
    00067                                 for (y = 0; y < Height; y++) {
    00068                                         NewY1 = y * SclBps;
    00069                                         NewY2 = (ILuint)(y / ScaleY) * ImgBps;
    00070                                         for (x = 0; x < Width; x++) {
    00071                                                 NewX1 = x * Scaled->Bpp;
    00072                                                 NewX2 = (ILuint)(x / ScaleX) * Image->Bpp;
    00073                                                 for (c = 0; c < Scaled->Bpp; c++) {
    00074                                                         Scaled->Data[NewZ1 + NewY1 + NewX1 + c] =
    00075                                                                 Image->Data[NewZ2 + NewY2 + NewX2 + c];
    00076                                                 }
    00077                                         }
    00078                                 }
    00079                         }
    00080                         break;
    00081
    00082                 case 2:
    00083                         ShortPtr = (ILushort*)Image->Data;
    00084                         SShortPtr = (ILushort*)Scaled->Data;
    00085                         for (z = 0; z < Depth; z++) {
    00086                                 NewZ1 = z * SclPlane;
    00087                                 NewZ2 = (ILuint)(z / ScaleZ) * ImgPlane;
    00088                                 for (y = 0; y < Height; y++) {
    00089                                         NewY1 = y * SclBps;
    00090                                         NewY2 = (ILuint)(y / ScaleY) * ImgBps;
    00091                                         for (x = 0; x < Width; x++) {
    00092                                                 NewX1 = x * Scaled->Bpp;
    00093                                                 NewX2 = (ILuint)(x / ScaleX) * Image->Bpp;
    00094                                                 for (c = 0; c < Scaled->Bpp; c++) {
    00095                                                         SShortPtr[NewZ1 + NewY1 + NewX1 + c] =
    00096                                                                 ShortPtr[NewZ2 + NewY2 + NewX2 + c];
    00097                                                 }
    00098                                         }
    00099                                 }
    00100                         }
    00101                         break;
    00102
    00103                 case 4:
    00104                         IntPtr = (ILuint*)Image->Data;
    00105                         SIntPtr = (ILuint*)Scaled->Data;
    00106                         for (z = 0; z < Depth; z++) {
    00107                                 NewZ1 = z * SclPlane;
    00108                                 NewZ2 = (ILuint)(z / ScaleZ) * ImgPlane;
    00109                                 for (y = 0; y < Height; y++) {
    00110                                         NewY1 = y * SclBps;
    00111                                         NewY2 = (ILuint)(y / ScaleY) * ImgBps;
    00112                                         for (x = 0; x < Width; x++) {
    00113                                                 NewX1 = x * Scaled->Bpp;
    00114                                                 NewX2 = (ILuint)(x / ScaleX) * Image->Bpp;
    00115                                                 for (c = 0; c < Scaled->Bpp; c++) {
    00116                                                         SIntPtr[NewZ1 + NewY1 + NewX1 + c] =
    00117                                                                 IntPtr[NewZ2 + NewY2 + NewX2 + c];
    00118                                                 }
    00119                                         }
    00120                                 }
    00121                         }
    00122                         break;
    00123         }
    00124
    00125         return Scaled;
    00126 }
    00127
    00128
    00129 ILimage *iluScale3DLinear_(ILimage *Image, ILimage *Scaled, ILuint Width, ILuint Height, ILuint Depth)
    00130 {
    00131         ImgBps = Image->Bps / Image->Bpc;
    00132         SclBps = Scaled->Bps / Scaled->Bpc;
    00133         ImgPlane = Image->SizeOfPlane / Image->Bpc;
    00134         SclPlane = Scaled->SizeOfPlane / Scaled->Bpc;
    00135
    00136         switch (Image->Bpc)
    00137         {
    00138                 case 1:
    00139                         for (z = 0; z < Depth; z++) {
    00140                                 NewZ1 = (ILuint)(z / ScaleZ) * ImgPlane;
    00141                                 for (y = 0; y < Height; y++) {
    00142                                         NewY1 = (ILuint)(y / ScaleY) * ImgBps;
    00143                                         for (x = 0; x < Width; x++) {
    00144                                                 t1 = x / (ILdouble)Width;
    00145                                                 t4 = t1 * Width;
    00146                                                 t2 = t4 - (ILuint)(t4);
    00147                                                 ft = t2 * IL_PI;
    00148                                                 f = (1.0 - cos(ft)) * .5;
    00149                                                 NewX1 = ((ILuint)(t4 / ScaleX)) * Image->Bpp;
    00150                                                 NewX2 = ((ILuint)(t4 / ScaleX) + 1) * Image->Bpp;
    00151
    00152                                                 Size = z * SclPlane + y * SclBps + x * Scaled->Bpp;
    00153                                                 for (c = 0; c < Scaled->Bpp; c++) {
    00154                                                         x1 = Image->Data[NewZ1 + NewY1 + NewX1 + c];
    00155                                                         x2 = Image->Data[NewZ1 + NewY1 + NewX2 + c];
    00156                                                         Scaled->Data[Size + c] = (ILubyte)((1.0 - f) * x1 + f * x2);
    00157                                                 }
    00158                                         }
    00159                                 }
    00160                         }
    00161                         break;
    00162
    00163                 case 2:
    00164                         ShortPtr = (ILushort*)Image->Data;
    00165                         SShortPtr = (ILushort*)Scaled->Data;
    00166                         for (z = 0; z < Depth; z++) {
    00167                                 NewZ1 = (ILuint)(z / ScaleZ) * ImgPlane;
    00168                                 for (y = 0; y < Height; y++) {
    00169                                         NewY1 = (ILuint)(y / ScaleY) * ImgBps;
    00170                                         for (x = 0; x < Width; x++) {
    00171                                                 t1 = x / (ILdouble)Width;
    00172                                                 t4 = t1 * Width;
    00173                                                 t2 = t4 - (ILuint)(t4);
    00174                                                 ft = t2 * IL_PI;
    00175                                                 f = (1.0 - cos(ft)) * .5;
    00176                                                 NewX1 = ((ILuint)(t4 / ScaleX)) * Image->Bpp;
    00177                                                 NewX2 = ((ILuint)(t4 / ScaleX) + 1) * Image->Bpp;
    00178
    00179                                                 Size = z * SclPlane + y * SclBps + x * Scaled->Bpp;
    00180                                                 for (c = 0; c < Scaled->Bpp; c++) {
    00181                                                         x1 = ShortPtr[NewZ1 + NewY1 + NewX1 + c];
    00182                                                         x2 = ShortPtr[NewZ1 + NewY1 + NewX2 + c];
    00183                                                         SShortPtr[Size + c] = (ILubyte)((1.0 - f) * x1 + f * x2);
    00184                                                 }
    00185                                         }
    00186                                 }
    00187                         }
    00188                         break;
    00189
    00190                 case 4:
    00191                         IntPtr = (ILuint*)Image->Data;
    00192                         SIntPtr = (ILuint*)Scaled->Data;
    00193                         for (z = 0; z < Depth; z++) {
    00194                                 NewZ1 = (ILuint)(z / ScaleZ) * ImgPlane;
    00195                                 for (y = 0; y < Height; y++) {
    00196                                         NewY1 = (ILuint)(y / ScaleY) * ImgBps;
    00197                                         for (x = 0; x < Width; x++) {
    00198                                                 t1 = x / (ILdouble)Width;
    00199                                                 t4 = t1 * Width;
    00200                                                 t2 = t4 - (ILuint)(t4);
    00201                                                 ft = t2 * IL_PI;
    00202                                                 f = (1.0 - cos(ft)) * .5;
    00203                                                 NewX1 = ((ILuint)(t4 / ScaleX)) * Image->Bpp;
    00204                                                 NewX2 = ((ILuint)(t4 / ScaleX) + 1) * Image->Bpp;
    00205
    00206                                                 Size = z * SclPlane + y * SclBps + x * Scaled->Bpp;
    00207                                                 for (c = 0; c < Scaled->Bpp; c++) {
    00208                                                         x1 = IntPtr[NewZ1 + NewY1 + NewX1 + c];
    00209                                                         x2 = IntPtr[NewZ1 + NewY1 + NewX2 + c];
    00210                                                         SIntPtr[Size + c] = (ILubyte)((1.0 - f) * x1 + f * x2);
    00211                                                 }
    00212                                         }
    00213                                 }
    00214                         }
    00215                         break;
    00216         }
    00217
    00218         return Scaled;
    00219 }
    00220
    00221
    00222 /*ILimage *iluScale3DBilinear_(ILimage *Image, ILimage *Scaled, ILuint Width, ILuint Height, ILuint Depth);
    00223 {
    00224                 Depth--;  // Only use regular Depth once in the following loop.
    00225                 Height--;  // Only use regular Height once in the following loop.
    00226                 for (z = 0; z < Depth; z++) {
    00227                         NewZ1 = (ILuint)(z / ScaleZ) * Image->SizeOfPlane;
    00228                         NewZ2 = (ILuint)((z+1) / ScaleZ) * Image->SizeOfPlane;
    00229                         for (y = 0; y < Height; y++) {
    00230                                 NewY1 = (ILuint)(y / ScaleY) * Image->Bps;
    00231                                 NewY2 = (ILuint)((y+1) / ScaleY) * Image->Bps;
    00232                                 for (x = 0; x < Width; x++) {
    00233                                         NewX = Width / ScaleX;
    00234                                         t1 = x / (ILdouble)Width;
    00235                                         t4 = t1 * Width;
    00236                                         t2 = t4 - (ILuint)(t4);
    00237                                         t3 = (1.0 - t2);
    00238                                         t4 = t1 * NewX;
    00239                                         NewX1 = (ILuint)(t4) * Image->Bpp;
    00240                                         NewX2 = (ILuint)(t4 + 1) * Image->Bpp;
    00241
    00242                                         for (c = 0; c < Scaled->Bpp; c++) {
    00243                                                 Table[0][0][c] = t3 * Image->Data[NewZ1 + NewY1 + NewX1 + c] +
    00244                                                         t2 * Image->Data[NewZ1 + NewY1 + NewX2 + c];
    00245
    00246                                                 Table[0][1][c] = t3 * Image->Data[NewZ1 + NewY2 + NewX1 + c] +
    00247                                                         t2 * Image->Data[NewZ1 + NewY2 + NewX2 + c];
    00248
    00249                                                 Table[1][0][c] = t3 * Image->Data[NewZ2 + NewY1 + NewX1 + c] +
    00250                                                         t2 * Image->Data[NewZ2 + NewY1 + NewX2 + c];
    00251
    00252                                                 Table[1][1][c] = t3 * Image->Data[NewZ2 + NewY2 + NewX1 + c] +
    00253                                                         t2 * Image->Data[NewZ2 + NewY2 + NewX2 + c];
    00254                                         }
    00255
    00256                                         // Linearly interpolate between the table values.
    00257                                         t1 = y / (ILdouble)(Height + 1);  // Height+1 is the real height now.
    00258                                         t2 = z / (ILdouble)(Depth + 1);   // Depth+1 is the real depth now.
    00259                                         t3 = (1.0 - t1);
    00260                                         Size = z * Scaled->SizeOfPlane + y * Scaled->Bps + x * Scaled->Bpp;
    00261                                         for (c = 0; c < Scaled->Bpp; c++) {
    00262                                                 x1 = t3 * Table[0][0][c] + t1 * Table[0][1][c];
    00263                                                 x2 = t3 * Table[1][0][c] + t1 * Table[1][1][c];
    00264                                                 Scaled->Data[Size + c] = (ILubyte)((1.0 - t2) * x1 + t2 * x2);
    00265                                         }
    00266                                 }
    00267                         }
    00268
    00269                         // Calculate the last row.
    00270                         NewY1 = (ILuint)(Height / ScaleY) * Image->Bps;
    00271                         for (x = 0; x < Width; x++) {
    00272                                 NewX = Width / ScaleX;
    00273                                 t1 = x / (ILdouble)Width;
    00274                                 t4 = t1 * Width;
    00275                                 ft = (t4 - (ILuint)(t4)) * IL_PI;
    00276                                 f = (1.0 - cos(ft)) * .5;  // Cosine interpolation
    00277                                 NewX1 = (ILuint)(t1 * NewX) * Image->Bpp;
    00278                                 NewX2 = (ILuint)(t1 * NewX + 1) * Image->Bpp;
    00279
    00280                                 Size = Height * Scaled->Bps + x * Image->Bpp;
    00281                                 for (c = 0; c < Scaled->Bpp; c++) {
    00282                                         Scaled->Data[Size + c] = (ILubyte)((1.0 - f) * Image->Data[NewY1 + NewX1 + c] +
    00283                                                 f * Image->Data[NewY1 + NewX2 + c]);
    00284                                 }
    00285                         }
    00286                 }
    00287
    00288                 NewZ1 = (ILuint)(Depth / ScaleZ) * Image->SizeOfPlane;
    00289                 for (y = 0; y < Height; y++) {
    00290                         NewY1 = (ILuint)(y / ScaleY) * Image->Bps;
    00291                         for (x = 0; x < Width; x++) {
    00292                                 t1 = x / (ILdouble)Width;
    00293                                 t4 = t1 * Width;
    00294                                 t2 = t4 - (ILuint)(t4);
    00295                                 ft = t2 * IL_PI;
    00296                                 f = (1.0 - cos(ft)) * .5;
    00297                                 NewX1 = ((ILuint)(t4 / ScaleX)) * Image->Bpp;
    00298                                 NewX2 = ((ILuint)(t4 / ScaleX) + 1) * Image->Bpp;
    00299
    00300                                 Size = (Depth) * Scaled->SizeOfPlane + y * Scaled->Bps + x * Scaled->Bpp;
    00301                                 for (c = 0; c < Scaled->Bpp; c++) {
    00302                                         x1 = Image->Data[NewZ1 + NewY1 + NewX1 + c];
    00303                                         x2 = Image->Data[NewZ1 + NewY1 + NewX2 + c];
    00304                                         Scaled->Data[Size + c] = (ILubyte)((1.0 - f) * x1 + f * x2);
    00305                                 }
    00306                         }
    00307                 }
    00308         }
    00309
    00310         return Scaled;
    00311 }
    00312 */
    00313