-
Notifications
You must be signed in to change notification settings - Fork 0
/
bmp_to_prime.c
156 lines (130 loc) · 10 KB
/
bmp_to_prime.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
#include <stdio.h>
#include <stdlib.h>
#include <gmp.h>
#include "bmp.h"
int main(int argc, char *argv[])
{
// ensure proper usage
if (argc != 3)
{
fprintf(stderr, "Usage: %s infile outfile\n", argv[0]);
return 1;
}
// remember filenames
char *infile = argv[1];
char *outfile = argv[2];
// open input file
FILE *inptr = fopen(infile, "r");
if (inptr == NULL)
{
fprintf(stderr, "Could not open %s.\n", infile);
return 2;
}
// read infile's BITMAPFILEHEADER
BITMAPFILEHEADER bf;
fread(&bf, sizeof(BITMAPFILEHEADER), 1, inptr);
// read infile's BITMAPINFOHEADER
BITMAPINFOHEADER bi;
fread(&bi, sizeof(BITMAPINFOHEADER), 1, inptr);
// ensure infile is (likely) a 24-bit uncompressed BMP 4.0
if (bf.bfType != 0x4d42 || bf.bfOffBits != 54 || bi.biSize != 40 || bi.biBitCount != 24 || bi.biCompression != 0)
{
fclose(inptr);
fprintf(stderr, "Unsupported file format.\n");
return 3;
}
// determine padding for scanlines
int padding = (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
int bmp_size = bi.biWidth * abs(bi.biHeight);
char *bmp_numbers = (char *)malloc(bmp_size + 1);
if (bmp_numbers == NULL)
{
fclose(inptr);
fprintf(stderr, "Problem while allocating memory.\n");
return 4;
}
bmp_numbers[bmp_size] = '\0';
// iterate over infile's scanlines
for (int i = 0, biHeight = abs(bi.biHeight); i < biHeight; i++)
{
// iterate over pixels in scanline
for (int j = 0; j < bi.biWidth; j++)
{
// temporary storage
RGBTRIPLE triple;
// read RGB triple from infile
fread(&triple, sizeof(RGBTRIPLE), 1, inptr);
// calculate luminance
// https://en.wikipedia.org/wiki/Relative_luminance
double L = 0.2126 * triple.rgbtRed + 0.7152 * triple.rgbtGreen + 0.0722 * triple.rgbtBlue;
// Write the character
bmp_numbers[i * bi.biWidth + j] = L < 200 ? '8' : '1';
}
// skip over padding, if any
fseek(inptr, padding, SEEK_CUR);
}
// close infile
fclose(inptr);
char *bmp_topdown;
if (bi.biHeight > 0)
{
bmp_topdown = (char *)malloc(bmp_size + 1);
if (bmp_topdown == NULL)
{
free(bmp_numbers);
fprintf(stderr, "Problem while allocating memory.\n");
return 5;
}
bmp_topdown[bmp_size] = '\0';
// iterate over character lines top-down
for (int i = 0, biHeight = abs(bi.biHeight); i < biHeight; i++)
{
// iterate over characters in lines
for (int j = 0; j < bi.biWidth; j++)
{
bmp_topdown[i * bi.biWidth + j] = bmp_numbers[(abs(bi.biHeight) - i - 1) * bi.biWidth + j];
}
}
free(bmp_numbers);
}
else
{
bmp_topdown = bmp_numbers;
}
int valid;
// create an integer for characters
mpz_t noprime;
valid = mpz_init_set_str(noprime, bmp_topdown, 10);
free(bmp_topdown);
if (valid == -1)
{
mpz_clear(noprime);
fprintf(stderr, "String is not a valid number.\n");
return 6;
}
// create an integer for the prime
mpz_t next_prime;
mpz_init(next_prime);
mpz_nextprime(next_prime, noprime);
mpz_clear(noprime);
FILE *outptr = fopen(outfile, "w");
if (outptr == NULL)
{
mpz_clear(next_prime);
fprintf(stderr, "Could not open %s.\n", outfile);
return 7;
}
char base64_part1[] = "d09GRgABAAAAABCcAA0AAAAAIDQAAQAAAAAAAAAAAAAAAAAAAAAAAAAAAABGRlRNAAAQgAAAABwAAAAccQlHoEdERUYAAB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OmzZg1Z96CRUuWrVi1Zt2GTVu27di1Z9+BQ0cyjmWdyDl15tyFS1eu5d24defeg0dPnkNB+PX98+MtlfwF1iQXxQAAAAAAAf//AAJ4nGNgZGBg4AFiMSBmYmAEwiQgZgHzGAAH1ACRAAAAAQAAAADUGBYRAAAAAM5niUAAAAAAzomoTg==";
valid = gmp_fprintf(outptr, "<!DOCTYPE html>\n<html lang=\"en\">\n\n<head>\n <meta charset=\"utf-8\">\n <title>%s</title>\n <meta name=\"author\" content=\"\">\n <meta name=\"description\" content=\"\">\n <style>\n @font-face {\n font-family: 'square';\n src: url(data:application/x-font-woff;charset=utf-8;base64,%s%s) format('woff');\n }\n\n #prime {\n font-family: 'square';\n font-size: 9px;\n }\n </style>\n <script>\n (function(window, document, undefined) {\n function addNewlines(str, lngth) {\n var result = '';\n while (str.length > 0) {\n result += str.substring(0, lngth) + '<br>';\n str = str.substring(lngth);\n }\n return result;\n }\n var prime = '%Zd';\n window.onload = init;\n\n function init() {\n var element = document.getElementById('prime');\n element.innerHTML = addNewlines(prime, %d) + '<br> is a %d digit number and a prime.';\n }\n\n })(window, document, undefined);\n </script>\n</head>\n\n<body>\n <div id=\"prime\"></div>\n</body>\n</html>\n", outfile, base64_part1, base64_part2, next_prime, bi.biWidth, mpz_sizeinbase(next_prime, 10));
// close outfile
fclose(outptr);
mpz_clear(next_prime);
if (valid == -1)
{
fprintf(stderr, "Can not write to file %s.\n", outfile);
return 8;
}
printf("%d characters written to file.\n", valid);
return 0;
}