001/*******************************************************************************
002 * This software is provided as a supplement to the authors' textbooks on digital
003 * image processing published by Springer-Verlag in various languages and editions.
004 * Permission to use and distribute this software is granted under the BSD 2-Clause
005 * "Simplified" License (see http://opensource.org/licenses/BSD-2-Clause).
006 * Copyright (c) 2006-2023 Wilhelm Burger, Mark J. Burge. All rights reserved.
007 * Visit https://imagingbook.com for additional details.
008 ******************************************************************************/
009
010package imagingbook.spectral.dct;
011
012
013import org.jtransforms.dct.DoubleDCT_1D;
014import org.jtransforms.dct.FloatDCT_1D;
015
016/**
017 * <p>
018 * This is fast implementation of the DCT, based on the JTransforms package by
019 * Piotr Wendykier (<a href="https://github.com/wendykierp/JTransforms">
020 * https://github.com/wendykierp/JTransforms</a>). Note that this class has no
021 * public constructor - instantiate sub-class {@link Dct1dFast.Float} or
022 * {@link Dct1dFast.Double} instead, as shown below. See Sec. 20.1 of [1] for
023 * additional details.
024 * </p>
025 * <p>
026 * Usage example (for {@code float} data):
027 * </p>
028 * <pre>
029 * float[] data = {1, 2, 3, 4, 5, 3, 0};
030 * Dct1d.Float dct = new Dct1dFast.Float(data.length);
031 * dct.forward(data);
032 * dct.inverse(data);
033 * ...
034 * </pre>
035 * <p>
036 * [1] W. Burger, M.J. Burge, <em>Digital Image Processing &ndash; An Algorithmic
037 * Introduction</em>, 3rd ed, Springer (2022).
038 * </p>
039 */
040public abstract class Dct1dFast extends Dct1dImp {
041
042        final double s;                         // common scale factor
043
044        private Dct1dFast(int M) {
045                super(M);
046                this.s = Math.sqrt(2.0 / M); 
047        }
048        
049        // ------------------------------------------------------------------------------
050        
051        /**
052         * One-dimensional DCT implementation using {@code float} data. 
053         */
054        public static class Float extends Dct1dFast implements Dct1d.Float {
055                
056                private final FloatDCT_1D fct;
057                
058                /**
059                 * Constructor.
060                 * @param M the data size
061                 */
062                public Float(int M) {
063                        super(M);
064                        this.fct = new FloatDCT_1D(M);
065                }
066
067                @Override
068                public void forward(float[] g) {
069                        checkSize(g);
070                        fct.forward(g, true);
071                }
072
073                @Override
074                public void inverse(float[] G) {
075                        checkSize(G);
076                        fct.inverse(G, true);
077                } 
078                
079        }
080
081        // ------------------------------------------------------------------------------
082        
083        /**
084         * One-dimensional DCT implementation using {@code double} data. 
085         */
086        public static class Double extends Dct1dFast implements Dct1d.Double {
087                
088                private final DoubleDCT_1D fct;
089                
090                /**
091                 * Constructor.
092                 * @param M the data size
093                 */
094                public Double(int M) {
095                        super(M);
096                        this.fct = new DoubleDCT_1D(M);
097                }
098
099                @Override
100                public void forward(double[] g) {
101                        checkSize(g);
102                        fct.forward(g, true);
103                }
104
105                @Override
106                public void inverse(double[] G) {
107                        checkSize(G);
108                        fct.inverse(G, true);
109                }
110        }
111
112}