-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimage_compressor.c
More file actions
527 lines (451 loc) · 17.4 KB
/
image_compressor.c
File metadata and controls
527 lines (451 loc) · 17.4 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
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
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include <stdbool.h>
#define PI 3.14159265359
#define ROWS 2048
#define COLS 2048
#define INPUT_IMAGE "sweet_p_grayscale.bmp"
#define OUTPUT_IMAGE "sweet_p_compressed.bmp"
#define NORMALIZE_PIXELS 1
#define THRESHOLD_PERCENT 0.98f
void fft(float* rex, float* imx, int sampleCount);
void ifft(float* rex, float* imx, int sampleCount);
void printFFTData(FILE* stream, float* rex, float* imx, int sampleCount);
int isLittleEndian();
int compareFloats(const void *a, const void *b);
int main() {
FILE* outputLogFile = fopen("logs.txt", "w");
if(outputLogFile == NULL) {
perror("Error opening file");
return 1;
}
printf("isLittleEndian: %d\n", isLittleEndian());
FILE *file;
unsigned char header[54]; // BMP header is 54 bytes
unsigned int dataPos; // Position where actual pixel data begins
// Open the BMP file in binary mode for reading
file = fopen(INPUT_IMAGE, "rb");
if (file == NULL) {
perror("Error opening file");
return 1;
}
// Read the BMP header (first 54 bytes)
fread(header, 1, 54, file);
// BMP files start with "BM" (ASCII code 66 and 77)
if (header[0] != 'B' || header[1] != 'M') {
printf("Not a BMP file\n");
fclose(file);
fclose(outputLogFile);
return 1;
}
// Extract the image dimensions and data offset from the header
dataPos = *(int*)&(header[0x0A]);
printf("Data Offset: %u\n", dataPos);
int rows = ROWS;
int cols = COLS;
int totalPixelCount = rows * cols;
unsigned char* pixelData;
pixelData = (unsigned char*)malloc(totalPixelCount);
if(pixelData == NULL) {
printf("Failed to allocate memory\n");
fclose(file);
fclose(outputLogFile);
return 1;
}
// skip to the pixel data portion of file
if(fseek(file, dataPos, SEEK_SET) != 0) {
printf("failed to seek to data position in input file\n");
fclose(file);
fclose(outputLogFile);
return 1;
}
int pixelsRead = fread(pixelData, 1, totalPixelCount, file);
if(pixelsRead != totalPixelCount) {
printf("Failed to read from file. Bytes read did not match expected\n");
fclose(file);
fclose(outputLogFile);
return 1;
}
printf("Data check: %d %d %d\n", (int)pixelData[0], (int)pixelData[1], (int)pixelData[totalPixelCount-1]);
// Perform compression algorithm
float* reAllCoefficients = (float*)malloc(totalPixelCount * sizeof(float));
float* imAllCoefficients = (float*)malloc(totalPixelCount * sizeof(float));
memset(reAllCoefficients, 0, totalPixelCount * sizeof(float));
memset(imAllCoefficients, 0, totalPixelCount * sizeof(float));
float* reRowCoefficients = (float*)malloc(cols * sizeof(float));
float* imRowCoefficients = (float*)malloc(cols * sizeof(float));
if(reAllCoefficients == NULL || imAllCoefficients == NULL || reRowCoefficients == NULL || imRowCoefficients == NULL) {
printf("Failed to allocate memory for coefficients\n");
fclose(outputLogFile);
fclose(file);
free(reAllCoefficients);
free(imAllCoefficients);
free(reRowCoefficients);
free(imRowCoefficients);
return 1;
}
// load in normalized samples and perform FFT
printf("Beginning FFT of rows\n");
for(int row = 0; row < rows; row++) {
memset(reRowCoefficients, 0, cols * sizeof(float));
memset(imRowCoefficients, 0, cols * sizeof(float));
for(int col = 0; col < cols; col++) {
if(NORMALIZE_PIXELS) {
reRowCoefficients[col] = pixelData[row * cols + col] / 256.f;
}
else {
reRowCoefficients[col] = pixelData[row * cols + col];
}
}
fft(reRowCoefficients, imRowCoefficients, cols);
memcpy(reAllCoefficients + row * cols, reRowCoefficients, cols * sizeof(float));
memcpy(imAllCoefficients + row * cols, imRowCoefficients, cols * sizeof(float));
// debugging purposes
if(reAllCoefficients[50 + row * cols] != reRowCoefficients[50]) {
printf("%.02f ", reAllCoefficients[50 + row * cols]);
printf("%.02f \n", reRowCoefficients[50]);
printf("row: %d\n", row);
printf("panic!!!\n");
fclose(outputLogFile);
fclose(file);
free(reAllCoefficients);
free(imAllCoefficients);
free(reRowCoefficients);
free(imRowCoefficients);
return 1;
}
}
free(reRowCoefficients);
free(imRowCoefficients);
reRowCoefficients = NULL;
imRowCoefficients = NULL;
printf("Beginning FFT of columns\n");
// FFT the columns
float* reColCoefficients = (float*)malloc(rows * sizeof(float));
float* imColCoefficients = (float*)malloc(rows * sizeof(float));
if(reColCoefficients == NULL || imColCoefficients == NULL) {
printf("Failed to allocate memory for column coefficients\n");
fclose(outputLogFile);
fclose(file);
free(reAllCoefficients);
free(imAllCoefficients);
free(reColCoefficients);
free(imColCoefficients);
return 1;
}
for(int col = 0; col < cols; col++) {
memset(reColCoefficients, 0, rows * sizeof(float));
memset(imColCoefficients, 0, rows * sizeof(float));
for(int row = 0; row < rows; row++) {
reColCoefficients[row] = reAllCoefficients[row * cols + col];
imColCoefficients[row] = imAllCoefficients[row * cols + col];
}
fft(reColCoefficients, imColCoefficients, rows);
// logging values for 1 example to showcase data transformation
if(col == 0) {
fprintf(outputLogFile, "FFT coefficients after initial column-wise FFT:\n");
printFFTData(outputLogFile, reColCoefficients, imColCoefficients, rows);
}
// save coefficients to set of all coefficients
for(int row = 0; row < rows; row++) {
reAllCoefficients[row * cols + col] = reColCoefficients[row];
imAllCoefficients[row * cols + col] = imColCoefficients[row];
}
// debugging purposes
if(reAllCoefficients[50 * cols + col] != reColCoefficients[50]) {
printf("%.02f ", reAllCoefficients[50 * cols + col]);
printf("%.02f \n", reColCoefficients[50]);
printf("row: %d\n", col);
printf("panic!!!\n");
fclose(outputLogFile);
fclose(file);
free(reAllCoefficients);
free(imAllCoefficients);
free(reColCoefficients);
free(imColCoefficients);
return 1;
}
}
// zero the lowest N% of frequencies by amplitude
float* sortedByMagnitudes;
sortedByMagnitudes = (float*)malloc(totalPixelCount * sizeof(float));
if(sortedByMagnitudes == NULL) {
printf("Failed to allocate memory when zeroing data\n");
fclose(outputLogFile);
fclose(file);
free(reAllCoefficients);
free(imAllCoefficients);
free(reColCoefficients);
free(imColCoefficients);
return 1;
}
for(int row = 0; row < rows; row++) {
for(int col = 0; col < cols; col++) {
int index = row * cols + col;
sortedByMagnitudes[index] = reAllCoefficients[index] * reAllCoefficients[index];
sortedByMagnitudes[index] += imAllCoefficients[index] * imAllCoefficients[index];
//sortedByMagnitudes[row] = sqrt(sortedByMagnitudes[row]);
}
}
qsort(sortedByMagnitudes, totalPixelCount, sizeof(float), compareFloats);
int thresholdIndex = totalPixelCount * THRESHOLD_PERCENT;
float threshold = sortedByMagnitudes[thresholdIndex];
// zero low thresholds
for(int row = 0; row < rows; row++) {
for(int col = 0; col < cols; col++) {
int index = row * cols + col;
float magnitude;
magnitude = reAllCoefficients[index] * reAllCoefficients[index];
magnitude += imAllCoefficients[index] * imAllCoefficients[index];
//magnitude = sqrt(magnitude);
if(magnitude < threshold) {
reAllCoefficients[index] = 0;
imAllCoefficients[index] = 0;
}
}
// logging values for 1 example to showcase data transformation
if(row == 256) {
fprintf(outputLogFile, "FFT coefficients after zeroing %.02f of low frequencies:\nREX: ", THRESHOLD_PERCENT);
for(int col = 0; col < cols; col++) {
fprintf(outputLogFile, "%.02f ", reAllCoefficients[row * cols + col]);
}
fprintf(outputLogFile, "\n\n");
}
}
// Image is now ready to be compressed
// Pretend we did compress and save the image
// To actually compress the file, you could use a simple algorithm like run-length compression
// Because of all the consecutive zeros, it would compress each contigous set of zeros to just ~8 bytes each
// Calculation to see how much data would have been saved
printf("Pixel data byte count with no compression: %d\n", totalPixelCount);
int hypotheticalByteCount = 0;
bool reConnectedToZero = false;
bool imConnectedToZero = false;
for(int i = 0; i < totalPixelCount; i++) {
float reCoefficient = reAllCoefficients[i];
if(reCoefficient == 0 && !reConnectedToZero) {
hypotheticalByteCount += sizeof(float) + sizeof(int);
reConnectedToZero = true;
}
else if (reCoefficient != 0){
hypotheticalByteCount += sizeof(float);
reConnectedToZero = false;
}
float imCoefficient = imAllCoefficients[i];
if(imCoefficient == 0 && !imConnectedToZero) {
hypotheticalByteCount += sizeof(float) + sizeof(int);
imConnectedToZero = true;
}
else if (imCoefficient != 0){
hypotheticalByteCount += sizeof(float);
imConnectedToZero = false;
}
}
printf("Pixel data byte count with some basic compression: %d\n", hypotheticalByteCount);
// Now to recreate image from compressed data
// If we had done run-length compression, this is the stage we would decompress at
printf("Beginning IFFT of columns\n");
for(int col = 0; col < cols; col++) {
memset(reColCoefficients, 0, rows * sizeof(float));
memset(imColCoefficients, 0, rows * sizeof(float));
for(int row = 0; row < rows; row++) {
reColCoefficients[row] = reAllCoefficients[row * cols + col];
imColCoefficients[row] = imAllCoefficients[row * cols + col];
}
ifft(reColCoefficients, imColCoefficients, rows);
// save coefficients to set of all coefficients
for(int row = 0; row < rows; row++) {
reAllCoefficients[row * cols + col] = reColCoefficients[row];
imAllCoefficients[row * cols + col] = imColCoefficients[row];
}
// debugging purposes
if(reAllCoefficients[50 * cols + col] != reColCoefficients[50]) {
printf("%.02f ", reAllCoefficients[50 * cols + col]);
printf("%.02f \n", reColCoefficients[50]);
printf("row: %d\n", col);
printf("panic!!!\n");
fclose(outputLogFile);
fclose(file);
free(reAllCoefficients);
free(imAllCoefficients);
free(reColCoefficients);
free(imColCoefficients);
return 1;
}
}
free(reColCoefficients);
free(imColCoefficients);
reColCoefficients = NULL;
imColCoefficients = NULL;
printf("Beginning IFFT of rows\n");
reRowCoefficients = (float*)malloc(cols * sizeof(float));
imRowCoefficients = (float*)malloc(cols * sizeof(float));
if(reAllCoefficients == NULL || imAllCoefficients == NULL || reRowCoefficients == NULL || imRowCoefficients == NULL) {
printf("Failed to allocate memory for coefficients\n");
fclose(outputLogFile);
fclose(file);
free(reAllCoefficients);
free(imAllCoefficients);
free(reRowCoefficients);
free(imRowCoefficients);
return 1;
}
for(int row = 0; row < rows; row++) {
memset(reRowCoefficients, 0, cols * sizeof(float));
memset(imRowCoefficients, 0, cols * sizeof(float));
for(int col = 0; col < cols; col++) {
reRowCoefficients[col] = reAllCoefficients[row * cols + col];
imRowCoefficients[col] = imAllCoefficients[row * cols + col];
}
ifft(reRowCoefficients, imRowCoefficients, cols);
memcpy(reAllCoefficients + row * cols, reRowCoefficients, cols * sizeof(float));
memcpy(imAllCoefficients + row * cols, imRowCoefficients, cols * sizeof(float));
// debugging purposes
if(reAllCoefficients[50 + row * cols] != reRowCoefficients[50]) {
printf("%.02f ", reAllCoefficients[50 + row * cols]);
printf("%.02f \n", reRowCoefficients[50]);
printf("row: %d\n", row);
printf("panic!!!\n");
fclose(outputLogFile);
fclose(file);
free(reAllCoefficients);
free(imAllCoefficients);
free(reRowCoefficients);
free(imRowCoefficients);
return 1;
}
}
free(reRowCoefficients);
free(imRowCoefficients);
free(imAllCoefficients);
reRowCoefficients = NULL;
imRowCoefficients = NULL;
imAllCoefficients = NULL;
// save image to file
if(fseek(file, 0, SEEK_SET) != 0) {
printf("failed to seek to beginning input file\n");
fclose(file);
free(reAllCoefficients);
free(pixelData);
fclose(outputLogFile);
return 1;
}
char* prePixelData = (char*)malloc(dataPos);
int bytesRead = fread(prePixelData, sizeof(char), dataPos, file);
fclose(file);
if(bytesRead != dataPos) {
printf("failed to read pre data portion of input file\n");
free(reAllCoefficients);
free(pixelData);
fclose(outputLogFile);
return 1;
}
for(int i = 0; i < totalPixelCount; i++) {
unsigned char pixelValue;
if(NORMALIZE_PIXELS) {
pixelValue = reAllCoefficients[i] * 256;
}
else {
pixelValue = reAllCoefficients[i];
}
pixelData[i] = pixelValue;
}
FILE* outputFile = fopen(OUTPUT_IMAGE, "wb");
if(outputFile != NULL) {
if((fwrite(prePixelData, sizeof(char), dataPos, outputFile) == dataPos) &&
(fwrite(pixelData, sizeof(char), totalPixelCount, outputFile) == totalPixelCount)) {
printf("Image compressed successfully!\n");
}
}
else {
printf("Failed to open output file\n");
}
// clean up the mess
free(reAllCoefficients);
free(pixelData);
fclose(outputLogFile);
fclose(outputFile);
return 0;
}
void fft(float* rex, float* imx, int sampleCount) {
// constants
int last = sampleCount - 1;
int halfCount = sampleCount / 2;
int log = ilogb(sampleCount);
int j = halfCount;
// bit reversal ordering
for(int i = 1; i < last; i++) {
if(i < j) {
float tempReal = rex[j];
float tempImag = imx[j];
rex[j] = rex[i];
imx[j] = imx[i];
rex[i] = tempReal;
imx[i] = tempImag;
}
int k = halfCount;
while(k <= j) {
j -= k;
k /= 2;
}
j += k;
}
// Iterative FFT algorithm
for(int stage = 1; stage <= log; stage++) {
int stageSampleCount = exp2(stage);
halfCount = stageSampleCount / 2;
float ur = 1;
float ui = 0;
float realSinusoid = cos(PI / halfCount);
float imagSinusoid = -sin(PI / halfCount);
for(int j = 0; j < halfCount; j++) {
for(int i = j; i < sampleCount; i += stageSampleCount) {
int ptr = i + halfCount;
float tempReal = rex[ptr] * ur - imx[ptr] * ui;
float tempImag = rex[ptr] * ui + imx[ptr] * ur;
rex[ptr] = rex[i] - tempReal;
imx[ptr] = imx[i] - tempImag;
rex[i] += tempReal;
imx[i] += tempImag;
}
float tempUr = ur;
ur = ur * realSinusoid - ui * imagSinusoid;
ui = tempUr * imagSinusoid + ui * realSinusoid;
}
}
}
void ifft(float* rex, float* imx, int sampleCount) {
for(int i = 0; i < sampleCount; i++) {
imx[i] = -imx[i];
}
fft(rex, imx, sampleCount);
for(int i = 0; i < sampleCount; i++) {
rex[i] /= sampleCount;
imx[i] /= -sampleCount;
}
}
void printFFTData(FILE* stream, float* rex, float* imx, int sampleCount) {
fprintf(stream, "REX: ");
for(int i = 0; i < sampleCount; i++) {
fprintf(stream, "%.02f ", rex[i]);
}
fprintf(stream, "\n");
fprintf(stream, "IMX: ");
for(int i = 0; i < sampleCount; i++) {
fprintf(stream, "%.02f ", imx[i]);
}
fprintf(stream, "\n\n");
}
int isLittleEndian() {
unsigned int num = 1;
// Cast the integer to a byte pointer
unsigned char *ptr = (unsigned char *)#
// If the first byte is the least significant byte (little endian), return 1
// Otherwise, return 0 (big endian)
return (*ptr == 1);
}
int compareFloats(const void *a, const void *b) {
return (*(float*)a - *(float*)b);
}