This repository has been archived by the owner on Oct 19, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
whodunit.c
83 lines (60 loc) · 1.83 KB
/
whodunit.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
/**
* Copies a BMP piece by piece, just because.
*/
#include <stdio.h>
#include <stdlib.h>
#include "bmp.h"
int main(int argc, char *argv[])
{
if (argc != 3) {
fprintf(stderr, "Usage: ./copy infile outfile\n");
return 1;
}
char *infile = argv[1];
char *outfile = argv[2];
FILE *inptr = fopen(infile, "r");
if (inptr == NULL) {
fprintf(stderr, "Could not open %s.\n", infile);
return 2;
}
FILE *outptr = fopen(outfile, "w");
if (outptr == NULL) {
fclose(inptr);
fprintf(stderr, "Could not create %s.\n", outfile);
return 3;
}
BITMAPFILEHEADER bf;
fread(&bf, sizeof(BITMAPFILEHEADER), 1, inptr);
BITMAPINFOHEADER bi;
fread(&bi, sizeof(BITMAPINFOHEADER), 1, inptr);
if (bf.bfType != 0x4d42 || bf.bfOffBits != 54 || bi.biSize != 40 ||
bi.biBitCount != 24 || bi.biCompression != 0) {
fclose(outptr);
fclose(inptr);
fprintf(stderr, "Unsupported file format.\n");
return 4;
}
fwrite(&bf, sizeof(BITMAPFILEHEADER), 1, outptr);
fwrite(&bi, sizeof(BITMAPINFOHEADER), 1, outptr);
int padding = (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
for (int i = 0, biHeight = abs(bi.biHeight); i < biHeight; i++) {
for (int j = 0; j < bi.biWidth; j++) {
RGBTRIPLE triple;
fread(&triple, sizeof(RGBTRIPLE), 1, inptr);
// Makes image copy green
if (triple.rgbtRed == 0xff)
{
triple.rgbtRed = 0x00;
triple.rgbtGreen = 0xff;
}
fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);
}
fseek(inptr, padding, SEEK_CUR);
for (int k = 0; k < padding; k++) {
fputc(0x00, outptr);
}
}
fclose(inptr);
fclose(outptr);
return 0;
}