-
Notifications
You must be signed in to change notification settings - Fork 0
/
dct-single.c
604 lines (483 loc) · 17.7 KB
/
dct-single.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
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include <pthread.h>
#define PSUDO_WIDTH 16
#define PSUDO_HEIGHT 16
// TEST IMAGE ('campus.pgm') RESOLUTION
//
// #define PSUDO_WIDTH 1200
// #define PSUDO_HEIGHT 800
/*
// RESOLUTION: QVGA
#define PSUDO_WIDTH 320
#define PSUDO_HEIGHT 240
// RESOLUTION: VGA
#define PSUDO_WIDTH 640
#define PSUDO_HEIGHT 480
// RESOLUTION: HD
#define PSUDO_WIDTH 1280
#define PSUDO_HEIGHT 720
// RESOLUTION: WQHD
#define PSUDO_WIDTH 2560
#define PSUDO_HEIGHT 1440
*/
// Controller for the pixel value types (e.g. floats, doubles, etc.)
typedef double valueType;
// Pixel structure definition
// --------------------------
//
// r : Value of intensity of 'red' of this pixel
// g : Value of intensity of 'green' of this pixel
// b : Value of intensity of 'blue' of this pixel
// i : Value of grayscale intensity of this pixel
//
typedef struct {
valueType r, g, b;
valueType i;
} pixel;
// Image structure definition
// --------------------------
//
// width : Amount of pixels in the x-direction
// height : Amount of pixels in the y-direction
// m : The matrix itself containing all pixels
//
typedef struct {
int width;
int height;
int channels;
pixel** m;
} image;
// Struct to pass to each thread so it knows the
// information needed for accessing
struct info {
int threadIndex;
int start;
int end;
int paddingStart;
double error;
image* srcIMG;
image* dctIMG;
image* idctIMG;
};
// ALLOCATION & INITIALIZATION
image* allocateImage(int width, int height, int channels);
image* generateImage(int width, int height, int channels, int fitAmnt);
int imGetPadSize(int totalThreads, int startingSize);
void imUnfit(image* srcIMG, image* dctIMG, image* idctIMG, int fitAmnt);
void imread(image* im, FILE *inFile);
void imwrite(image* im, char* fileName);
void imDelete(image* srcIMG, image* dctIMG, image* idctIMG);
// VALIDATION
int imValidate(image* imA, image* imB, double threshold);
double imERR(image* imA, image* imB);
double imERR2(image* imA, image* imB);
long double imMSE(image* imA, image* imB);
// OPERATIONS
void imPrint(image* im);
void imDCT(image* inIMG, image* outIMG);
void imIDCT(image* inIMG, image* outIMG);
void* imProcess(void* arg);
double runTest(int height, int width, int bits, int channels, int totalThreads);
void imBlockIDCT(image* inIMG, image* outIMG, int i, int j);
void imBlockDCT(image* inIMG, image* outIMG, int i, int j);
int main(int argc, char* argv[]) {
///////////////////////////////////////////
// INTITIAL SETUP //
///////////////////////////////////////////
int height, width, bits, channels,
totalThreads, paddedSize, fitFlag, padding;
channels = 1, width = PSUDO_WIDTH, height = PSUDO_HEIGHT,
fitFlag = 0, padding = 0, bits = 255;
// Print the object information for verification
printf("Height: %i\nWidth: %i\nBits: %.0f\nChannels: %i\n",
height, width, log2(bits+1), channels);
// Get the amount of threads to use and
// then check for padding requirements
totalThreads = (argc < 2? 1 : atoi(argv[1]));
paddedSize = imGetPadSize(totalThreads, height);
if (paddedSize != height && totalThreads != 1)
padding = paddedSize - height;
printf("Total Threads: %i\n\n", totalThreads);
// Read in or randomly generate the source
// image given the image characteristics
image* srcIMG = generateImage(width, height, channels, padding);
/*
READING IN IMAGE ALTERNATIVE:
-----------------------------
image* srcIMG = allocateImage(width, (height + padding), channels);
FILE* inputFile = fopen("campus.pgm", "r+");
imread(srcIMG, inputFile);
*/
// Allocate for the DCT & IDCT images
image* dctIMG = allocateImage(width, (height + padding), channels);
image* idctIMG = allocateImage(width, (height + padding), channels);
///////////////////////////////////////////
// THREADING SETUP //
///////////////////////////////////////////
// Create threading structure
pthread_t tid[totalThreads];
// Initialize structure to hold information for each thread
struct info* s = (struct info*)malloc(totalThreads*sizeof(struct info));
for (int i = 0; i < totalThreads; i++) {
s[i].threadIndex = i;
s[i].start = i*(height + padding)/totalThreads;
s[i].end = (i + 1)*(height + padding)/totalThreads;
s[i].paddingStart = height;
s[i].srcIMG = srcIMG;
s[i].dctIMG = dctIMG;
s[i].idctIMG = idctIMG;
s[i].error = 0.0;
}
///////////////////////////////////////////
// THREADING RUNTIME //
///////////////////////////////////////////
// Start timing the total time elapsed for process
struct timespec start, end;
clock_gettime(CLOCK_REALTIME, &start);
// Launch all threads
for (int i = 0; i < totalThreads; i++)
pthread_create(&tid[i], NULL, (void*)imProcess, &s[i]);
// Collect all threads launched
for (int i = 0; i < totalThreads; i++)
pthread_join(tid[i], NULL);
// Stop timer and set time elapsed value for process
clock_gettime(CLOCK_REALTIME, &end);
double time_spent = (end.tv_sec - start.tv_sec) +
(end.tv_nsec - start.tv_nsec) / 1e9;
// Unpad the image for cases of thread amount & height mismatch
if (paddedSize != -1)
imUnfit(srcIMG, dctIMG, idctIMG, (paddedSize - height));
///////////////////////////////////////////
// VERIFYING RESULTS //
///////////////////////////////////////////
// Manual image verification
printf("Original Image:\n");
imPrint(srcIMG);
printf("\n\n");
printf("DCT Image:\n");
imPrint(dctIMG);
printf("\n\n");
printf("IDCT Image:\n");
imPrint(idctIMG);
printf("\n\n");
// Get accuracy/percision of the process
int DOP = 12;
double precision = (double)(1.0/(pow(10.0, (double)DOP)));
printf("imValidate() return value: %i\n", imValidate(srcIMG, idctIMG, precision));
printf(" imERR1() return value: %.25f\n", imERR(srcIMG, idctIMG));
printf(" imERR2() return value: %.25f\n", imERR2(srcIMG, idctIMG));
printf(" imMSE() return value: %.15Le\n", imMSE(srcIMG, idctIMG));
// Write the output images for verificiation
imwrite(srcIMG, "srcIMG.pgm");
imwrite(dctIMG, "dctIMG.pgm");
imwrite(idctIMG, "idctIMG.pgm");
return 0;
}
void* imProcess(void* arg) {
// Parse the argument pointer into a local (struct info*) structure
struct info* input = (struct info*)arg;
// Iterate through the rows corrosponding to this thread
for (int y = input->start; (y < input->end) && (y < input->paddingStart); y += 8) {
// Iterate through the columns corrosponding to this thread
for (int x = 0, width = input->srcIMG->width; x < width; x += 8) {
imBlockDCT(input->srcIMG, input->dctIMG, x, y);
imBlockIDCT(input->dctIMG, input->idctIMG, x, y);
}
}
}
void imBlockDCT(image* inIMG, image* outIMG, int i, int j) {
double OOSQT = 1.0/sqrt(2.0);
double sum = 0.0;
double HPW = (double)16.0;
for (int u = 0; u < 8; u++) {
for (int v = 0; v < 8; v++) {
sum = 0.0;
for (int x = 0; x < 8; x++) {
for (int y = 0; y < 8; y++) {
sum += (double)inIMG->m[i + x][j + y].i *
cos(((2.0*(double)(x)+1.0) * (double)u * M_PI)/HPW) *
cos(((2.0*(double)(y)+1.0) * (double)v * M_PI)/HPW);
}
}
outIMG->m[i + u][j + v].i = 0.25
* (u == 0? OOSQT:1.0)
* (v == 0? OOSQT:1.0)
* sum;
}
}
}
void imBlockIDCT(image* inIMG, image* outIMG, int i, int j) {
double OOSQT = 1.0/sqrt(2.0);
double sum = 0.0;
double MPOL = M_PI/(double)16.0;
for (int x = 0; x < 8; x++) {
for (int y = 0; y < 8; y++) {
sum = 0.0;
for (int u = 0; u < 8; u++) {
for (int v = 0; v < 8; v++) {
sum += inIMG->m[i + u][j + v].i *
(u == 0? OOSQT:1.0) *
(v == 0? OOSQT:1.0) *
cos((2.0*(double)(x)+1.0) * (double)u * MPOL) *
cos((2.0*(double)(y)+1.0) * (double)v * MPOL);
}
}
outIMG->m[i + x][j + y].i = sum * 0.25;
}
}
}
void imDCT(image* inIMG, image* outIMG) {
double OOSQT = 1.0/sqrt(2.0);
double sum = 0.0;
double HPW = (double)(inIMG->height + inIMG->width);
for (int u = 0; u < inIMG->width; u++) {
for (int v = 0; v < inIMG->height; v++) {
sum = 0.0;
for (int x = 0; x < inIMG->width; x++) {
for (int y = 0; y < inIMG->height; y++) {
sum += (double)inIMG->m[x][y].i *
cos(((2.0*(double)x+1.0) * (double)u*M_PI)/HPW) *
cos(((2.0*(double)y+1.0) * (double)v*M_PI)/HPW);
}
}
outIMG->m[u][v].i = 1.0/((double)inIMG->height/2.0) *
(!u? OOSQT:1.0)*(!v? OOSQT:1.0) *
sum;
}
}
}
void imIDCT(image* inIMG, image* outIMG) {
double OOSQT = 1.0/sqrt(2.0);
double sum = 0.0;
double HPW = (double)(inIMG->height + inIMG->width);
double MPOL = M_PI/(double)(HPW);
for (int x = 0; x < inIMG->width; x++) {
for (int y = 0; y < inIMG->height; y++) {
sum = 0.0;
for (int u = 0; u < inIMG->width; u++) {
for (int v = 0; v < inIMG->height; v++) {
sum += inIMG->m[u][v].i *
(!u? OOSQT:1.0) *
(!v? OOSQT:1.0) *
cos((2.0*(double)x+1.0) * (double)u*MPOL) *
cos((2.0*(double)y+1.0) * (double)v*MPOL);
}
}
outIMG->m[x][y].i = sum/((double)inIMG->height/2.0);
}
}
}
void imPrint(image* im) {
// If input image is a grayscale image
if (im->channels == 1) {
for (int y = 0; y < im->height; y++) {
for (int x = 0; x < im->width; x++) {
// NOTE: This should be a %X.Yf, where
// X = Y+5 for spacing values and
// the sign of the values neatly
//
printf("[%8.3f] ", im->m[x][y].i); // %18.13f for demo
}
printf("\n");
}
}
// Else the input is a RGB image
else {
for (int y = 0; y < im->height; y++) {
for (int x = 0; x < im->width; x++) {
printf("[%3f,%3f,%3f]\t",
im->m[x][y].r,
im->m[x][y].g,
im->m[x][y].b);
}
printf("\n");
}
}
}
// This should give the average error per ELEMENT
double imERR(image* imA, image* imB) {
double avg_err = 0.0;
for (int y = 0; y < imA->height; y++) {
for (int x = 0; x < imA->width; x++) {
// In the case of either being zero you get really bad
// values, and I'm not sure what else to do but skip it
if (imA->m[x][y].i == 0.0 || imB->m[x][y].i == 0.0)
continue;
avg_err += fabs(imB->m[x][y].i - imA->m[x][y].i)/imA->m[x][y].i;
}
}
return avg_err;
}
// This should give the average error overall,
// as in for the entire image
double imERR2(image* imA, image* imB) {
double total_A = 0.0;
double total_B = 0.0;
for (int y = 0; y < imA->height; y++) {
for (int x = 0; x < imA->width; x++) {
total_A += (double)fabs(imA->m[x][y].i);
total_B += (double)fabs(imB->m[x][y].i);
}
}
return (100.0 * fabs(total_B - total_A)/total_A);
}
// This should give the average error overall,
// as in for the entire image
long double imMSE(image* imA, image* imB) {
image* imC = allocateImage(imA->width,
imA->height,
imA->channels);
long double MSE = (long double)0.0;
for (int y = 0; y < imA->height; y++) {
for (int x = 0; x < imA->width; x++) {
MSE += pow((long double)(imB->m[x][y].i - imA->m[x][y].i),
(long double)2.0);
}
}
return (MSE / (long double)(imA->height * imA->width));
}
int imValidate(image* imA, image* imB, double threshold) {
if (imA->height != imB->height)
return -7;
if (imA->width != imB->width)
return -6;
if (imA->channels != imB->channels)
return -5;
// If input image is a grayscale image
if (imA->channels == 1) {
for (int y = 0; y < imA->height; y++) {
for (int x = 0; x < imA->width; x++) {
if (fabs(imA->m[x][y].i - imB->m[x][y].i) >= threshold) {
printf("INVALID POINT: (%i, %i)\n", x, y);
printf("A(%i, %i): [%.20f]\n", x, y, imA->m[x][y].i);
printf("B(%i, %i): [%.20f]\n", x, y, imB->m[x][y].i);
printf("A(x,y) - B(x,y): %.38f\n\n",
imA->m[x][y].i - imB->m[x][y].i);
return -4;
}
}
}
}
// Else the input is a RGB image
else {
for (int y = 0; y < imA->height; y++) {
for (int x = 0; x < imA->width; x++) {
if (fabs(imA->m[x][y].r - imB->m[x][y].r) >= threshold)
return -1;
if (fabs(imA->m[x][y].g - imB->m[x][y].g) >= threshold)
return -2;
if (fabs(imA->m[x][y].b - imB->m[x][y].b) >= threshold)
return -3;
}
}
}
return 0;
}
image* allocateImage(int width, int height, int channels) {
// Allocate an image structure
//
// NOTE:
//
// To be able to index with im[x][y], I need
// to initialize with WIDTH, and then iterate over
// WIDTH allocating for each column of a certain HEIGHT
//
image* im = (image*)malloc(1*sizeof(image));
im->m = (pixel**)malloc((width)*sizeof(pixel));
// Allocate each column of the image matrix
for (int x = 0; x < width; x++)
im->m[x] = (pixel*)malloc((height)*sizeof(pixel));
im->height = height;
im->width = width;
im->channels = channels;
return im;
}
image* generateImage(int width, int height, int channels, int fitAmnt) {
image* im = allocateImage(width, height + fitAmnt, channels);
srand((unsigned)time(NULL));
for (int y = 0; y < height; y++)
for (int x = 0; x < width; x++)
im->m[x][y].i = (double)(rand() % 255);
// Initialize all padded area to 0's
for (int y = height; y < height+fitAmnt; y++)
for (int x = 0; x < width; x++)
im->m[x][y].i = 0;
return im;
}
int imGetPadSize(int totalThreads, int startingSize) {
int found = 0;
int sizeFound = 0;
for (int s = startingSize; s < 1600; s++) {
for (int idx = 0; idx < totalThreads; idx++) {
int start = idx * s / totalThreads;
int end = (idx+1) * s / totalThreads;
if (((end-start) % 8) == 0)
found = 1;
else {
found = 0;
break;
}
}
if (found == 1)
return s;
}
return -1;
}
void imUnfit(image* srcIMG, image* dctIMG, image* idctIMG, int fitAmnt) {
for (int y = srcIMG->height, nSize = (srcIMG->height - fitAmnt); y >= nSize; y--) {
free(srcIMG->m[y]);
free(dctIMG->m[y]);
free(idctIMG->m[y]);
}
srcIMG->height = (srcIMG->height - fitAmnt);
dctIMG->height = (dctIMG->height - fitAmnt);
idctIMG->height = (idctIMG->height - fitAmnt);
}
void imDelete(image* srcIMG, image* dctIMG, image* idctIMG) {
for (int y = 0; y < srcIMG->height; y++) {
free(srcIMG->m[y]);
free(dctIMG->m[y]);
free(idctIMG->m[y]);
}
}
void imread(image* im, FILE *inFile) {
// If input image is a grayscale image
if (im->channels == 1) {
int value;
fscanf(inFile, "P2\n%i %i\n%i", &value, &value, &value);
for (int y = 0; y < im->height; y++) {
for (int x = 0; x < im->width; x++) {
fscanf(inFile, "\n%i", &value);
im->m[x][y].i = (valueType)value;
}
}
}
// Else the input is a RGB image
else {
int rv, gv, bv;
for (int y = 0; y < im->height; y++) {
for (int x = 0; x < im->width; x++) {
fscanf(inFile, "\n%i %i %i",
&rv, &gv, &bv);
im->m[x][y].r = (valueType)rv;
im->m[x][y].g = (valueType)gv;
im->m[x][y].b = (valueType)bv;
}
}
}
}
void imwrite(image* im, char* fileName) {
FILE *inFile = fopen(fileName, "w+");
fprintf(inFile, "P2\n%i %i\n255\n", im->width, im->height);
for (int y = 0; y < im->height; y++) {
for (int x = 0; x < im->width; x++) {
if (im->m[x][y].i >= (valueType)0.0)
fprintf(inFile, "%.0f\n", im->m[x][y].i);
else
fprintf(inFile, "0\n");
}
}
}