-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathblockImage.cc
executable file
·201 lines (168 loc) · 5.23 KB
/
blockImage.cc
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
#include "blockImage.h"
#include <ostream>
#include <istream>
#include <iostream>
#include <math.h>
using namespace std;
void blockImage::readMatrixPNM(istream &infile){
int width,height,data_range;
infile>>type>>height>>width>>data_range;
blockRowCount = (height/blockDimension);
blockColCount = (width/blockDimension);
blockCount = blockColCount*blockRowCount;
blockStore = new double * [blockCount];
for(int i = 0; i < blockCount; i++){
blockStore[i] = new double[blockDimension*blockDimension];
}
//for each 'block row'
for(int i = 0; i < blockColCount; i++){
//each 'block row' consists of blockDimension number of pixel rows
for(int j = 0; j< blockDimension; j++){
//each 'block collumn' section within a row
for(int k = 0; k < blockRowCount;k++){
//each 'block collumn is blockDimension pixels wide
for(int m = 0; m < blockDimension; m++){
//populate the array correctly in order that it is read
infile>>blockStore[i*blockRowCount + k][j*blockDimension + m];
}
}
}
}
}
void blockImage::writeMatrixPNM(ostream &out ){
//for each 'block row'
out<<type<<" " <<blockDimension*blockRowCount<<" " <<blockDimension*blockColCount << " 255" <<endl;
for(int i = 0; i < blockColCount; i++){
//each 'block row' consists of blockDimension number of pixel rows
for(int j = 0; j< blockDimension; j++){
//each 'block collumn' section within a row
for(int k = 0; k < blockRowCount;k++){
//each 'block collumn is blockDimension pixels wide
for(int m = 0; m < blockDimension; m++){
//populate the array correctly in order that it is read
if(round){
int data = (int)(blockStore[i*blockRowCount + k][j*blockDimension + m]+ 0.5);
if(data < 0){
data = 0;
}
else if(data > 255){
data = 255;
}
out<<data<<endl;
}
else{
out<<blockStore[i*blockRowCount + k][j*blockDimension + m]<<endl;
}
}
}
}
}
}
double * blockImage::blurMatrix(double * target, int blockDimensionOriginal){
int blockDimension = blockDimensionOriginal/2;
double * a = new double[blockDimension*blockDimension];
for(int i = 0; i < blockDimension; i++){
for(int j = 0; j < blockDimension; j++){
a[i*blockDimension + j] = target[i*4*blockDimension + j*2];
a[i*blockDimension + j] +=target[i*4*blockDimension + j*2 + 1] ;
a[i*blockDimension + j] += target[(i*4+2)*blockDimension + j*2];
a[i*blockDimension + j] += target[(i*4+2)*blockDimension + j*2 + 1];
a[i*blockDimension + j] /=4;
}
}
return a;
}
blockImage::blockImage(int blockDimension):blockDimension(blockDimension){}
void blockImage::blur(){
for(int i = 0; i < blockCount; i++){
double * ap = blurMatrix(blockStore[i],blockDimension);
delete [] blockStore[i];
blockStore[i] = ap;
}
blockDimension/=2;
//column count and row count should still stay the same
}
blockImage::~blockImage(){
for(int i = 0; i < blockCount; i++){
delete [] blockStore[i];
}
delete [] blockStore;
}
int blockImage::getBlockDimension(){
return blockDimension;
}
int blockImage::getBlockColCount(){
return blockColCount;
}
int blockImage::getBlockRowCount(){
return blockRowCount;
}
int blockImage::getBlockCount(){
return blockCount;
}
double blockImage::differenceNormalized(blockImage * bi){
//error condition parsing
if(blockDimension!=bi->getBlockDimension()){
return -1;
}
else if(blockColCount!=bi->getBlockColCount()){
return -1;
}
else if(blockRowCount!=bi->getBlockRowCount()){
return -1;
}
double sum = 0;
for(int i = 0; i < blockCount; i++){
double blockSum = 0;
for(int j = 0; j < blockDimension*blockDimension; j++){
blockSum += pow(blockStore[i][j] - bi->blockStore[i][j],2);
}
}
return sqrt(sum)/((double)blockDimension*blockDimension*blockRowCount*blockColCount);
}
void blockImage::updateMap(int bd){
//delete the old
blockDimension = bd;
for(int i = 0; i < blockCount; i++){
delete [] blockStore[i];
blockStore[i] = new double[bd*bd];
}
}
void blockImage::writeRoundOFF(bool isItOFF){
round = !isItOFF;
}
double * blockImage::expandMatrix(double * matrix, int originalBlockDimension){
int newDimension = originalBlockDimension*2;
double * a = new double [newDimension*newDimension];
for(int i = 0; i < originalBlockDimension; i++){
for(int j = 0; j < originalBlockDimension; j++){
a[i*4*originalBlockDimension + j*2] = matrix[i*originalBlockDimension + j];
a[i*4*originalBlockDimension + j*2 + 1] = matrix[i*originalBlockDimension + j];
a[(i*4+2)*originalBlockDimension + j*2] = matrix[i*originalBlockDimension + j];
a[(i*4+2)*originalBlockDimension + j*2 + 1] = matrix[i*originalBlockDimension + j];
}
}
return a;
}
void blockImage::expand(){
for(int i = 0; i < blockCount; i++){
double * replacement = expandMatrix(blockStore[i],blockDimension);
delete [] blockStore[i];
blockStore[i] = replacement;
}
blockDimension*=2;
}
void blockImage::makeEmpty(int brc, int blc, string t){
type = t;
blockRowCount = brc;
blockColCount = blc;
blockCount = blockColCount * blockRowCount;
//memory leak potential. read blockImage.h for details
blockStore = new double * [blockCount];
for(int i = 0; i < blockCount; i++){
blockStore[i] = new double[blockDimension*blockDimension];
}
}
string blockImage::getType(){
return type;
}