TurboWavelets.Net
Compact C# Implementation for very fast and flexible wavelet transformations
TurboWavelets/HaarWavelet2D.cs
00001 // 
00002 // HaarWavelet2D.cs
00003 //  
00004 // Author:
00005 //       Stefan Moebius
00006 // Date:
00007 //       2016-04-24
00008 // 
00009 // Copyright (c) 2016 Stefan Moebius
00010 // 
00011 // Permission is hereby granted, free of charge, to any person obtaining a copy
00012 // of this software and associated documentation files (the "Software"), to deal
00013 // in the Software without restriction, including without limitation the rights
00014 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00015 // copies of the Software, and to permit persons to whom the Software is
00016 // furnished to do so, subject to the following conditions:
00017 // 
00018 // The above copyright notice and this permission notice shall be included in
00019 // all copies or substantial portions of the Software.
00020 // 
00021 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00022 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00023 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00024 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00025 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00026 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00027 // THE SOFTWARE.
00028 
00029 namespace TurboWavelets
00030 {
00034     public class HaarWavelet2D : Wavelet2D
00035     {
00039         protected const int   AllowedMinSize    = 2;
00043         protected const float Scale             = 2.0f;
00047         protected const float InvScale          = 0.5f;
00051         protected const float Mean              = 0.5f;
00055         protected const float InvMean           = 2.0f;
00056 
00064         public HaarWavelet2D (int width, int height)
00065             : base(AllowedMinSize, AllowedMinSize, width, height)
00066         {   
00067         }
00068 
00077         public HaarWavelet2D (int width, int height, int minSize)
00078             : base(minSize, AllowedMinSize, width, height)
00079         {
00080         }
00081 
00082         #pragma warning disable 1591 // do not show compiler warnings of the missing descriptions
00083         override protected void TransformRow (float[,] src, float[,] dst, int y, int length)
00084         {
00085             if (length >= AllowedMinSize) {
00086                 int half = length >> 1;
00087                 int offSrc = 0;
00088                 // number of low-pass values
00089                 int numLFValues = half + (length & 1);
00090 
00091                 for (int i = 0; i < half; i++) {
00092                     float a = src [offSrc    , y];
00093                     float b = src [offSrc + 1, y];
00094                     //calculate the mean of a and b and scale with factor 2
00095                     //So no multiplication needed at all
00096                     dst [i              , y] = (a + b);
00097                     dst [i + numLFValues, y] = (b - a) * Mean;
00098                     offSrc += 2;
00099                 }                           
00100                 if ((length & 1) != 0) {
00101                     dst [numLFValues - 1, y] = src [length - 1, y] * Scale;
00102                 }       
00103             } else {
00104                 for (int i = 0; i < length; i++)
00105                     dst [i, y] = src [i, y];
00106             }
00107         }
00108 
00109         override protected void TransformCol (float[,] src, float[,] dst, int x, int length)
00110         {
00111             if (length >= AllowedMinSize) {
00112                 int half = length >> 1;
00113                 int offSrc = 0;
00114                 // number of low-pass values
00115                 int numLFValues = half + (length & 1);
00116 
00117                 for (int i = 0; i < half; i++) {
00118                     float a = src [x, offSrc    ];
00119                     float b = src [x, offSrc + 1];
00120                     //calculate the mean of a and b and scale with factor 2
00121                     //So no multiplication needed at all
00122                     dst [x, i              ] = (a + b);
00123                     dst [x, i + numLFValues] = (b - a) * Mean;
00124                     offSrc += 2;
00125                 }                           
00126                 if ((length & 1) != 0) {
00127                     dst [x, numLFValues - 1] = src [x, length - 1] * Scale;
00128                 }   
00129             } else {
00130                 for (int i = 0; i < length; i++)
00131                     dst [x, i] = src [x, i];
00132             }
00133         }
00134 
00135         override protected void InvTransformRow (float[,] src, float[,] dst, int y, int length)
00136         {
00137             if (length >= AllowedMinSize) {
00138                 int half = length >> 1;
00139                 int offDst = 0;
00140                 // number of low-pass values
00141                 int numLFValues = half + (length & 1);
00142 
00143                 for (int i = 0; i < half; i++) {
00144                     float a = src [i, y              ] * InvScale;
00145                     float b = src [i + numLFValues, y];
00146                     dst [offDst,     y] = a - b;
00147                     dst [offDst + 1, y] = a + b;
00148                     offDst += 2;
00149                 }                           
00150                 if ((length & 1) != 0) {
00151                     dst [length - 1, y] = src [numLFValues - 1, y] * InvScale; 
00152                 }
00153             } else {
00154                 for (int i = 0; i < length; i++)
00155                     dst [i, y] = src [i, y];
00156             }
00157         }
00158 
00159         override protected void InvTransformCol (float[,] src, float[,] dst, int x, int length)
00160         {
00161             if (length >= AllowedMinSize) {
00162                 int half = length >> 1;
00163                 int offDst = 0;
00164                 // number of low-pass values
00165                 int numLFValues = half + (length & 1);
00166 
00167                 for (int i = 0; i < half; i++) {
00168                     float a = src [x, i              ] * InvScale;
00169                     float b = src [x, i + numLFValues];
00170                     dst [x,     offDst] = a - b;
00171                     dst [x, offDst + 1] = a + b;
00172                     offDst += 2;
00173                 }                           
00174                 if ((length & 1) != 0) {
00175                     dst [x, length - 1] = src [x, numLFValues - 1] * InvScale;
00176                 } 
00177             } else {
00178                 for (int i = 0; i < length; i++)
00179                     dst [x, i] = src [x, i];
00180             }
00181         }
00182         #pragma warning restore 1591
00183     }
00184 }
 All Classes Namespaces Functions Variables Properties