-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbmp_compress.c
More file actions
605 lines (518 loc) · 16.5 KB
/
Copy pathbmp_compress.c
File metadata and controls
605 lines (518 loc) · 16.5 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
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
605
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "types.h"
#include "bmp_compress.h"
//Reads pixel data from a BMP file row by row, handles padding, and stores image pixels in correct order (top-down or bottom-up).
static int readGrayscalePixels(FILE *fp, unsigned char *imageData,
int width, int height, int isBottomUp,
int rowPadding, long pixelOffset)
{
unsigned char padBuf[4];
fseek(fp, pixelOffset, SEEK_SET);
for (int i = 0; i < height; i++)
{
int row;
if (isBottomUp==1)
{
row = height - 1 - i;
}
else
{
row = i;
}
size_t got = fread(imageData + row * width, 1, width, fp);
if (got < (size_t)width)//A special type for sizes and memory
{
printf("Error reading pixel data\n");
return 0;
}
if (rowPadding > 0)
fread(padBuf, 1, rowPadding, fp);
}
return 1;
}
//Creates and writes grayscale palette (0–255 intensity values) into the BMP file.
static void writeGrayscalePalette(FILE *fp)
{
unsigned char palette[1024];
for (int i = 0; i < 256; i++) {
palette[i * 4] = (unsigned char)i;
palette[i * 4 + 1] = (unsigned char)i;
palette[i * 4 + 2] = (unsigned char)i;
palette[i * 4 + 3] = 0; // unused alpha
}
fwrite(palette, 1, 1024, fp);
}
//Writes a full 8-bit grayscale BMP image to a file
static void writeGrayscaleBMP(const char *filename,
unsigned char *pixels,
int width, int height,
int isBottomUp)
{
int rowPadding;
int remainder = width % 4;
rowPadding = 4 - remainder;
if (rowPadding == 4)
{
rowPadding = 0;
}
BMPFileHeader fh;
BMPInfoHeader ih;
fh.bfType = 0x4D42;
fh.bfReserved1 = 0;
fh.bfReserved2 = 0;
fh.bfOffBits = sizeof(BMPFileHeader) + sizeof(BMPInfoHeader) + 1024;
fh.bfSize = fh.bfOffBits + (unsigned int)((width + rowPadding) * height);
ih.biSize = sizeof(BMPInfoHeader);
ih.biWidth = width;
ih.biHeight = isBottomUp ? height : -height;
ih.biPlanes = 1;
ih.biBitCount = 8;
ih.biCompression = 0;
ih.biSizeImage = (unsigned int)((width + rowPadding) * height);
ih.biXPelsPerMeter = 2835;
ih.biYPelsPerMeter = 2835;
ih.biClrUsed = 0;
ih.biClrImportant = 0;
FILE *fp = fopen(filename, "wb");
if (!fp)
{
printf("Error creating file\n");
return;
}
fwrite(&fh, sizeof(BMPFileHeader), 1, fp);
fwrite(&ih, sizeof(BMPInfoHeader), 1, fp);
writeGrayscalePalette(fp);
unsigned char pad[3] = {0, 0, 0};
for (int i = 0; i < height; i++)
{
int row;
if (isBottomUp)
{
row = height - 1 - i;
}
else
{
row = i;
}
fwrite(pixels + row * width, 1, width, fp);
if (rowPadding > 0)
fwrite(pad, 1, rowPadding, fp);
}
fclose(fp);
}
//Opens a BMP file, reads(8-bit grayscale BMP), and extracts image metadata like width, height, and padding.
static int openGrayscaleBMP(const char *filename,
BMPFileHeader *fh,
BMPInfoHeader *ih,
int *width, int *height,
int *isBottomUp,
int *rowPadding)
{
FILE *fp = fopen(filename, "rb");
if (!fp)
{
printf("Error opening file\n");
return 0;
}
fread(fh, sizeof(BMPFileHeader), 1, fp);
fread(ih, sizeof(BMPInfoHeader), 1, fp);
fclose(fp);
if (fh->bfType != 0x4D42)
{
printf("Not a BMP file.\n");
return 0;
}
if (ih->biBitCount != 8)
{
printf("Only 8-bit grayscale BMP images are supported\n");
return 0;
}
*width = ih->biWidth;
*isBottomUp = (ih->biHeight > 0);
if (*isBottomUp == 1)
{
*height = ih->biHeight;
}
else
{
*height = -ih->biHeight;
}
int remainder = *width % 4;
if (remainder == 0)
{
*rowPadding = 0;
}
else
{
*rowPadding = 4 - remainder;
}
return 1;
}
long getFileSize(const char *filename) {
FILE *fp = fopen(filename, "rb");
if (!fp)
{
return 0;
}
fseek(fp, 0, SEEK_END);
long size = ftell(fp);
fclose(fp);
return size;
}
static void printResultsBox(const char *title,const char *outFile,
const char *decFile, long origSize,
long compSize,long decSize)
{
double reduction = 0.0;
if (origSize > 0 && compSize < origSize)
{
reduction = (1.0 - (double)compSize / origSize) * 100.0;
}
double ratio = 1.0;
if (compSize > 0)
{
ratio = (double)origSize / compSize;
}
int filled = (int)(reduction / 10.0); //rating'****'
if (filled > 10)
filled = 10;
if (filled < 0)
filled = 0;
char bar[11];
for (int i = 0; i < 10; i++)
{
if (i < filled) {
bar[i] = '#';
}
else
{
bar[i] = '-';
}
}
bar[10] = '\0';
const char *stars, *verdict;
if(reduction >= 85)
{
stars = "*****";
verdict = "EXCELLENT";
}
else if(reduction >= 70)
{
stars = "**** ";
verdict = "GREAT";
}
else if(reduction >= 50)
{
stars = "*** ";
verdict = "GOOD";
}
else if(reduction >= 30)
{
stars = "** ";
verdict = "FAIR";
}
else
{
stars = "* ";
verdict = "LOW";
}
printf("\n");
printf("+---------------------------------------------------------------+\n");
printf("| %-58s |\n", title);
printf("+---------------------------------------------------------------+\n");
printf("| Output File : %-43s|\n", outFile);
printf("+---------------------------------------------------------------+\n");
printf("| Original Size : %-5ld bytes |\n", origSize);
printf("| Compressed : %-5ld bytes |\n", compSize);
// printf("| Decompressed : %-5ld bytes |\n", decSize);
printf("+---------------------------------------------------------------+\n");
printf("| Space Saved : %-6.2f%% [%-10s] |\n", reduction, bar);
printf("| Ratio (OG/COMP) : %-5.2f : 1 |\n", ratio);
printf("| Rating : %-10s %-5s |\n", verdict, stars);
printf("+---------------------------------------------------------------+\n");
printf("| Decompressed : %-43s|\n", decFile);
printf("+---------------------------------------------------------------+\n\n");
}
static void compressBMPBitPlaneMSB(const char *inputFile) // MSB (lossy) compression
{
printf("\n=== BMP IMAGE COMPRESSION (MSB) ===\n");
printf("Detected: BMP ImageFile\n");
printf("Bit Plane Slicing (MSB) is used for compression.\n");
BMPFileHeader fh;
BMPInfoHeader ih;
int width, height, isBottomUp, rowPadding;
if (!openGrayscaleBMP(inputFile, &fh, &ih, &width, &height, &isBottomUp, &rowPadding))
return;
int imageSize = width * height;
printf("Image infos:\n");
printf(" Dimensions : %d x %d pixels\n", width, height);
printf(" Bit depth : 8-bit grayscale\n");
printf(" Total px : %d\n\n", imageSize);
FILE *fp = fopen(inputFile, "rb");
fread(&fh, sizeof(BMPFileHeader), 1, fp);
fread(&ih, sizeof(BMPInfoHeader), 1, fp);
unsigned char palette[1024];
fread(palette, 1, 1024, fp);
unsigned char *imageData = (unsigned char *)malloc(imageSize);
readGrayscalePixels(fp, imageData, width, height, isBottomUp,
rowPadding, fh.bfOffBits);
fclose(fp);
// Step 1: keep only the most-significant bit of each pixel
unsigned char *msbPlane = (unsigned char *)malloc(imageSize);
for (int i = 0; i < imageSize; i++)
{
if (imageData[i] >= 128)
msbPlane[i] = 1;
else
msbPlane[i] = 0;
}
// ("Step 2: Packing MSB bits into bytes...\n")
int packedSize = (imageSize + 7) / 8;
unsigned char *packed = (unsigned char *)calloc(packedSize, 1);
for (int i = 0; i < imageSize; i++)
{
if (msbPlane[i] == 1) //single plane packing
{
int byteIndex = i / 8;
int bitPosition = 7 - (i % 8);
int value = 1;
for (int j = 0; j < bitPosition; j++) // calculate 2^(bitPosition)
{
value *= 2;
}
packed[byteIndex] += value;
}
}
/* Write compressed file: width + height + packed bits */
char outFile[MAX_FILENAME];
strcpy(outFile, inputFile);
char *dot = strrchr(outFile, '.');
if (dot)
*dot = '\0';
strcat(outFile, "_compressed.bps");
fp = fopen(outFile, "wb");
fwrite(&width, sizeof(int), 1, fp);
fwrite(&height, sizeof(int), 1, fp);
fwrite(packed, 1, packedSize, fp);
fclose(fp);
/* Step 3: decompress*/
unsigned char *unpackedMSB = (unsigned char *)malloc(imageSize);
unsigned char *reconstructed = (unsigned char *)calloc(imageSize, 1);
for (int i = 0; i < imageSize; i++)
{
int byteIndex = i / 8;
int bitPosition = 7 - (i % 8);
int value = packed[byteIndex];
for (int j = 0; j < bitPosition; j++)
{
value /= 2;
}
int bit = value % 2;
unpackedMSB[i] = bit;
if (bit == 1)
reconstructed[i] = 128;
else
reconstructed[i] = 0;
}
char decFile[MAX_FILENAME];
strcpy(decFile, inputFile);
dot = strrchr(decFile, '.');
if (dot)
*dot = '\0';
strcat(decFile, "_decompressed.bmp");
writeGrayscaleBMP(decFile, reconstructed, width, height, isBottomUp);
long origSize = fh.bfSize;
long compSize = (long)(sizeof(int) * 2) + packedSize;
//int rowPad = (4 - (width % 4)) % 4;
// long decSize = sizeof(BMPFileHeader) + sizeof(BMPInfoHeader) + 1024 + (long)(width + rowPad) * height;
long decSize = getFileSize(decFile);
printResultsBox("COMPRESSION SUMMARY (MSB - LOSSY)",
outFile, decFile,origSize,
compSize,decSize);
free(imageData);
free(msbPlane);
free(packed);
free(unpackedMSB);
free(reconstructed);
}
static void compressBMPBitPlaneFull(const char *inputFile) //full bit compression
{
printf("\n=== BMP IMAGE COMPRESSION (FULL) ===\n");
printf("Detected: BMP ImageFile\n");
printf("Bit Plane Slicing (Full Lossless) is used for compression.\n");
BMPFileHeader fh;
BMPInfoHeader ih;
int width, height, isBottomUp, rowPadding;
if (!openGrayscaleBMP(inputFile, &fh, &ih, &width, &height, &isBottomUp, &rowPadding))
return;
int imageSize = width * height;
FILE *fp = fopen(inputFile, "rb");
fread(&fh, sizeof(BMPFileHeader), 1, fp);
fread(&ih, sizeof(BMPInfoHeader), 1, fp);
unsigned char palette[1024];
fread(palette, 1, 1024, fp);
unsigned char *imageData = (unsigned char *)malloc(imageSize);
for (int i = 0; i < height; i++)
{
int row;
if (isBottomUp)
{
row = height - 1 - i;
}
else
{
row = i;
}
fread(imageData + row * width, 1, width, fp);
fseek(fp, rowPadding, SEEK_CUR);
}
fclose(fp);
// FILE *fp = fopen(inputFile, "rb");
// unsigned char *imageData = (unsigned char *)malloc(imageSize);
// if (!readGrayscalePixels(fp, imageData, width, height, isBottomUp, rowPadding, fh.bfOffBits))
// {
// fclose(fp);
// free(imageData);
// return;
// }
// fclose(fp); /* Step 1: extract all 8 bit-planes */
unsigned char *bitPlanes[8];
for (int p = 0; p < 8; p++)
{
bitPlanes[p] = (unsigned char *)malloc(imageSize);
for (int i = 0; i < imageSize; i++)
{
int divisor = (int)pow(2, p);
bitPlanes[p][i] = (imageData[i] / divisor) % 2;
}
}
/* Step 2: pack each plane's bits into bytes, store back-to-back */
int packedPerPlane = (imageSize + 7) / 8;
int totalPacked = packedPerPlane * 8;
unsigned char *allPacked = (unsigned char *)calloc(totalPacked, 1);
for (int p = 0; p < 8; p++)
{
int base = p * packedPerPlane;
for (int i = 0; i < imageSize; i++)
{
if (bitPlanes[p][i])
{
int byteIndex = base + (i / 8);
int bitPosition = 7 - (i % 8);
int value = (int)pow(2, bitPosition);
allPacked[byteIndex] += value;
}
}
}
/* Writing the compressed file */
char outFile[MAX_FILENAME];
strcpy(outFile, inputFile);
char *dot = strrchr(outFile, '.');
if (dot) *dot = '\0';
strcat(outFile, "_full_compressed.bps");
fp = fopen(outFile, "wb");
fwrite(&width, sizeof(int), 1, fp);
fwrite(&height, sizeof(int), 1, fp);
fwrite(allPacked, 1, totalPacked, fp);
fclose(fp);
/* Step 3: reconstruct each pixel by OR-ing bits back */
unsigned char *reconstructed = (unsigned char *)calloc(imageSize, 1);
for (int p = 0; p < 8; p++)
{
int base = p * packedPerPlane;
for (int i = 0; i < imageSize; i++)
{
int byteIndex = base + (i / 8);
int bitPosition = 7 - (i % 8);
int value = allPacked[byteIndex];
int divisor = (int)pow(2, bitPosition);
value = value / divisor;
int bit = value % 2;
if (bit == 1)
{
int addValue = (int)pow(2, p);
reconstructed[i] += addValue;
}
}
}
char decFile[MAX_FILENAME];
strcpy(decFile, inputFile);
dot = strrchr(decFile, '.');
if (dot)
{
*dot = '\0';
}
strcat(decFile, "_full_decompressed.bmp");
writeGrayscaleBMP(decFile, reconstructed, width, height, isBottomUp);
long origSize = fh.bfSize;
long compSize = (long)(sizeof(int) * 2) + totalPacked;
int rowPad2 = (4 - (width % 4)) % 4;
//long decSize2 = sizeof(BMPFileHeader) + sizeof(BMPInfoHeader) + 1024 + (long)(width + rowPad2) * height;
long decSize2= getFileSize(decFile);
printResultsBox("COMPRESSION SUMMARY (FULL - LOSSLESS)",
outFile, decFile, origSize, compSize,decSize2);
free(imageData);
for (int p = 0; p < 8; p++)
free(bitPlanes[p]);
free(allPacked);
free(reconstructed);
}
// Public entry point
void processBMPFile(const char *fileName, int operation)
{
/* Compression */
int choice;
printf("\n +-------------------------------------------+\n");
printf(" | BMP COMPRESSION OPTIONS |\n");
printf(" +-------------------------------------------+\n");
printf(" | 1. MSB Bit Plane (Lossy - max savings) |\n");
printf(" | 2. Full Bit Plane (Lossless - no loss) |\n");
printf(" | 3. Both methods (compare side by side) |\n");
printf(" | 4. Quit |\n");
printf(" +-------------------------------------------+\n");
printf(" Enter your choice (1-4): ");
int result = scanf("%d", &choice);
if (result != 1) {
printf("Invalid input.\n");
int ch;
while (1) {
ch = getchar();
if (ch == '\n' || ch == EOF)
{
break;
}
}
return;
}
int ch;
while (1) {
ch = getchar();
if (ch == '\n' || ch == EOF)
{
break;
}
}
switch (choice)
{
case 1:
compressBMPBitPlaneMSB(fileName);
break;
case 2:
compressBMPBitPlaneFull(fileName);
break;
case 3:
compressBMPBitPlaneMSB(fileName);
printf("\n ========================================================\n\n");
compressBMPBitPlaneFull(fileName);
break;
case 4:
printf("Quitting...\n");
return;
default:
printf("Invalid choice.\n");
break;
}
}