Skip to content
Snippets Groups Projects
main.cpp 2.02 KiB
Newer Older
Milan Jaros's avatar
Milan Jaros committed
/*
************************************
* Fast Fourier transform  FFT
************************************
*/

#include <iostream>
#include "bmp.h"
#include "fft.h"

int main(int argc, char** args) {
    //Check args
Milan Jaros's avatar
Milan Jaros committed
    if (argc < 2) {
Milan Jaros's avatar
Milan Jaros committed
        printf("USAGE: fft lena_gray.bmp\n");
        return 0;
    }
    //Read input file
    const char* filename = args[1];
	BMP lena_bmp(filename);
    int image_size = lena_bmp.bmp_info_header.width * lena_bmp.bmp_info_header.height;
    int image_channels = lena_bmp.bmp_info_header.bit_count / 8;

    //Allocate memory for data
    double* d_input = new double[(size_t)2 * image_size * image_channels]; //complex value
    double* d_output = new double[(size_t)2 * image_size * image_channels]; //complex value

    double* d_w = new double[image_size];
    
    //Fill signal array with data
    for (int c = 0; c < image_channels; c++) {
        double* in = d_input + c * 2 * image_size;
        for (int i = 0; i < image_size; i++) {
            //Set only RE part, IM is 0
            in[i * 2 + 0] = (double)lena_bmp.data[image_channels * i + c]; //real part
            in[i * 2 + 1] = 0; //im part
        }
    }

    //Init FFT
    cffti(image_size, d_w);

    //Apply FFT
    for (int c = 0; c < image_channels; c++) {
        double* in = d_input + c * 2 * image_size;        
        double* out = d_output + c * 2 * image_size;

        //Forward
        cfft2(image_size, in, out, d_w, +1.0);
        //Backward
        cfft2(image_size, out, in, d_w, -1.0);
    }

    //Fill signal array with data
    for (int c = 0; c < image_channels; c++) {
        double* in = d_input + c * 2 * image_size;
        for (int i = 0; i < image_size; i++) {
            lena_bmp.data[image_channels * i + c] = in[i * 2 + 0] / (double)image_size; //real part
        }
    }

    //Write to output file
    std::string out_filename = std::string(filename) + ".out.bmp";
    lena_bmp.write(out_filename.c_str());

    //Free memory
    delete[] d_input;
    delete[] d_output;

    delete[] d_w;
    
    return 0;
}