-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpa1.c
156 lines (136 loc) · 4.27 KB
/
pa1.c
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
//---------------------------------------------------------------
//
// 4190.308 Computer Architecture (Fall 2022)
//
// Project #1:
//
// September 6, 2022
//
// Seongyeop Jeong ([email protected])
// Jaehoon Shim ([email protected])
// IlKueon Kang ([email protected])
// Wookje Han ([email protected])
// Jinsol Park ([email protected])
// Systems Software & Architecture Laboratory
// Dept. of Computer Science and Engineering
// Seoul National University
//
//---------------------------------------------------------------
typedef unsigned char u8;
int numberOfBits(u8 num) {
if(num == 0) {
return 0;
}
if(num == 1) {
return 1;
}
if(num < 4) {
return 2;
}
if(num < 8) {
return 3;
}
if(num < 16){
return 4;
}
if(num < 32) {
return 5;
}
if(num < 64) {
return 6;
}
if(num < 128) {
return 7;
}
return 8;
}
u8 getFilter(const u8* src, int width, int j) {
u8 left_up = j%width > 0 && j> width-1 ? *(src+j-width-1) : 0;
u8 up = j > width-1 ? *(src+j-width) : 0;
u8 left = j%width > 0 ? *(src+j-1) : 0;
int avg =(up + left + left_up);
if(j%width > 0 && j> width-1){
avg = avg/3;
}
u8 filter = *(src+j) - avg;
filter = filter | filter+256;
return filter;
}
/* TODO: Implement this function */
int encode(const u8* src, int width, int height, u8* result)
{
int i,j = 0;
int lengthWritten = 0;//update whenever you write any bit to result; It needs to get the last u8 index to write
for(i = 0; i < height; i++) { //Iterate one time for each row
u8 Base = 255;
u8 NoB = 0;
//getting Base (min{Filter})
for(j = i*width; j < i*width+width; j++) {
u8 filter = getFilter(src, width, j);
if(Base > filter) {
Base = filter;
}
}
// add Base to Result and update lengthWritten
int shiftRight = lengthWritten%8;
u8 temp1 = Base >> shiftRight;
u8 temp2;
*(result+lengthWritten/8) = 255 << (8-shiftRight) & *(result + lengthWritten/8);
if(shiftRight>0){
int shiftLeft = 8 - shiftRight;
temp2 = Base << shiftLeft;
lengthWritten+= shiftLeft;
*(result+lengthWritten/8) = temp2; // shift and check;
lengthWritten+=shiftRight;
}else {
lengthWritten+=8;
}
for(j = i*width; j < i*width+width; j++) {
u8 filter = getFilter(src, width, j);
u8 delta = filter - Base;
if(NoB < numberOfBits(delta)){
NoB = numberOfBits(delta);
}
}
// add NoB to Result
u8 shouldBeWritten = NoB << 4;
shiftRight = lengthWritten % 8;
temp1 = shouldBeWritten >> shiftRight;
*(result+lengthWritten/8) = 255 << (8-shiftRight) & *(result + lengthWritten/8); //setting to zero
*(result+lengthWritten / 8) = temp1 | *(result+lengthWritten / 8);
if (shiftRight > 4) {
int canWriteBits = 8 - shiftRight;
lengthWritten+= canWriteBits;
temp2 = shouldBeWritten << canWriteBits;
*(result+lengthWritten/8) = temp2;
lengthWritten+= 4-canWriteBits;
}else {
lengthWritten+=4;
}
if(NoB == 0) {
continue;
}
for(j = i*width; j < i*width+width; j++) {
u8 filter = getFilter(src, width, j);
u8 delta = filter - Base;
shouldBeWritten = delta << (8-NoB);
shiftRight = lengthWritten % 8;
temp1 = shouldBeWritten >> shiftRight;
*(result+lengthWritten/8) = 255 << (8-shiftRight) & *(result + lengthWritten/8);
*(result+lengthWritten / 8) = temp1 | *(result+lengthWritten / 8);
if (shiftRight > 8 - NoB) {
int canWriteBits = 8 - shiftRight;
lengthWritten+= canWriteBits;
temp2 = shouldBeWritten << canWriteBits;
*(result+lengthWritten/8) = temp2;
lengthWritten+= NoB-canWriteBits;
}else {
lengthWritten+=NoB;
}
}
}
if(lengthWritten%8) {
lengthWritten+= 8;
}
return lengthWritten/8;
}