-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrevquantize.c
More file actions
70 lines (58 loc) · 1.43 KB
/
revquantize.c
File metadata and controls
70 lines (58 loc) · 1.43 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
63
64
65
66
67
68
69
70
#include <stdint.h>
#include <stdio.h>
#include <math.h>
int main(){
uint8_t Q, S;
uint8_t base[8][8] = {16, 11, 10, 16, 24, 40, 51, 61,
12, 12, 14, 19, 26, 58, 60, 55,
14, 13, 16, 24, 40, 57, 69, 56,
14, 17, 22, 29, 51, 87, 80, 62,
18, 22, 37, 56, 68, 109, 103, 77,
24, 35, 55, 64, 81, 104, 113, 92,
49, 64, 78, 87, 103, 121, 120, 101,
72, 92, 95, 98, 112, 100, 103, 99
}; //Base matrix, quality factor = 50
uint8_t quantMat[8][8]; //Quantization matrix, Quality factor = Q;
int originDCT[8][8];
scanf("%u", &Q);
int a;
if(Q == 50){
/* Calculate original DCT coefficients */
for(int i = 0; i < 8; i++){
for(int j = 0; j < 8; j++){
scanf("%d", &a);
originDCT[i][j] = a * base[i][j];
}
}
}
else{
/* Calculate S based on quality factor*/
if(Q < 50)
S = 5000 / Q;
else
S = 200 - 2 * Q;
/*Calculate quantization matrix*/
for(int i = 0; i < 8; i++){
for(int j = 0; j < 8; j++){
quantMat[i][j] = floor((S * base[i][j] + 50) / 100);
if(quantMat[i][j] == 0)
quantMat[i][j] = 1;
}
}
/* Calculate original DCT coefficients */
for(int i = 0; i < 8; i++){
for(int j = 0; j < 8; j++){
scanf("%d", &a);
originDCT[i][j] = a * quantMat[i][j];
}
}
}
/* Print recalculated DCT coeff matrix */
for(int i = 0; i < 8; i++){
for(int j = 0; j < 8; j++){
printf("%d ", originDCT[i][j]);
}
printf("\n");
}
return 0;
}