-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathdct.cpp
More file actions
62 lines (52 loc) · 1.69 KB
/
dct.cpp
File metadata and controls
62 lines (52 loc) · 1.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
//
// Created by Nicola Pierazzo on 08/12/15.
//
#include <fftw3.h>
#include <iostream>
#include "Image.hpp"
#include "utils.hpp"
using namespace imgutils;
using std::string;
using std::cerr;
using std::cout;
using std::endl;
void dct_inplace(Image& img) {
int n[] = {img.rows(), img.columns()};
fftwf_r2r_kind dct2[] = {FFTW_REDFT10, FFTW_REDFT10};
fftwf_plan plan = fftwf_plan_many_r2r(2, n, img.channels(), img.data(), NULL,
img.channels(), 1, img.data(), NULL,
img.channels(), 1, dct2, FFTW_ESTIMATE);
fftwf_execute(plan);
fftwf_destroy_plan(plan);
// Normalization
for (int i = 0; i < img.rows() * img.columns() * img.channels(); ++i) {
img.val(i) /= 4 * img.rows() * img.columns();
}
}
void idct_inplace(Image& img) {
int n[] = {img.rows(), img.columns()};
fftwf_r2r_kind idct2[] = {FFTW_REDFT01, FFTW_REDFT01};
fftwf_plan plan = fftwf_plan_many_r2r(2, n, img.channels(), img.data(), NULL,
img.channels(), 1, img.data(), NULL,
img.channels(), 1, idct2, FFTW_ESTIMATE);
fftwf_execute(plan);
fftwf_destroy_plan(plan);
}
int main(int argc, char *argv[]) {
bool usage = pick_option(&argc, argv, "h", nullptr);
bool inverse = pick_option(&argc, argv, "i", nullptr);
if (usage) {
cerr << "usage: " << argv[0] << " [-i] [input [output]]" << endl;
return EXIT_SUCCESS;
}
string filename = argc > 1 ? argv[1] : "-";
Image im = read_image(filename);
if (inverse) {
idct_inplace(im);
} else {
dct_inplace(im);
}
filename = argc > 2 ? argv[2] : "-";
save_image(im, filename);
return EXIT_SUCCESS;
}