Repository navigation
Expand file tree
/
Copy pathjunk.cpp
More file actions
271 lines (231 loc) · 7.46 KB
/
Copy pathjunk.cpp
File metadata and controls
271 lines (231 loc) · 7.46 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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#ifndef _MSC_VER
#define fopen_s(pFile,filename,mode) ((*(pFile))=fopen((filename),(mode)))==NULL
#endif
typedef struct s_params
{
bool dryRun;
char *inputFilename;
char *outputFilename;
char *blendFilename;
int canExecute; // equals 7 when true
} params;
params commandParams(char* arguments[], int argCount)
{
char y, *command;
params p;
p.canExecute = 0;
for(int CycleArguments = 1; CycleArguments < argCount; CycleArguments++)
{
y = (arguments[CycleArguments])[0];
if(y == '-')
{
command = arguments[CycleArguments];
++command;
if(strcmp(command, "i") == 0)
{
// Get the port, it will be the next "command"
if(CycleArguments + 1 < argCount){
command = arguments[++CycleArguments];
p.inputFilename = (char*)malloc(sizeof(char) * (strlen(command) + 1));
memcpy(p.inputFilename, command, strlen(command)+1);
p.canExecute |= 0x01;
}
}
else if(strcmp(command, "o") == 0)
{
if(CycleArguments + 1 < argCount){
command = arguments[++CycleArguments];
p.outputFilename = (char*)malloc(sizeof(char) * (strlen(command) + 1));
memcpy(p.outputFilename, command, strlen(command)+1);
p.canExecute |= 0x02;
}
}
else if(strcmp(command, "b") == 0)
{
if(CycleArguments + 1 < argCount){
command = arguments[++CycleArguments];
p.blendFilename = (char*)malloc(sizeof(char) * (strlen(command) + 1));
memcpy(p.blendFilename, command, strlen(command)+1);
p.canExecute |= 0x04;
}
}
else if(strcmp(command, "dry") == 0)
{
p.dryRun = true;
}
}
}
return p;
}
unsigned char *loadFile(char *pszFilename, long *lgSize)
{
FILE *TARGET;
unsigned char *mem, *ptr2;
long h = 0;
fopen_s(&TARGET, pszFilename, "rb");
if(!TARGET)
{
printf("loadFile(): Unable to open file %s\n", pszFilename);
*lgSize = 0;
return 0;
}
fseek(TARGET, 0L, SEEK_END);
h = ftell(TARGET);
*lgSize = h;
fseek(TARGET, 0L, SEEK_SET);
mem = (unsigned char*)malloc(h+1);
if(!mem)
{
printf("loadFile(): Memory not allocated\n");
*lgSize = 0;
return 0;
}
fread(mem, sizeof(char), h, TARGET);
fclose(TARGET);
return mem;
}
unsigned char *readBMP(char *pszFilename, long *lgSize, unsigned int *width, unsigned int *height)
{
FILE *TARGET;
unsigned char *mem;
long h = 0;
unsigned char bitmapFileHeader[14];
unsigned char bitmapInfoHeader[40];
unsigned int rawsize;
fopen_s(&TARGET, pszFilename, "rb");
if(!TARGET)
{
printf("readBMP(): Unable to open file %s\n", pszFilename);
*lgSize = 0;
return 0;
}
fread(&bitmapFileHeader, sizeof(char) * 14, 1, TARGET);
fread(&bitmapInfoHeader, sizeof(char) * 40, 1, TARGET);
if(bitmapInfoHeader[14] != 24)
{
printf("readBMP(): Not a 24-bit BMP File\n");
*lgSize = 0;
return 0;
}
fseek(TARGET, 54, SEEK_SET);
rawsize = bitmapInfoHeader[20] |
(bitmapInfoHeader[21] << 8) |
(bitmapInfoHeader[22] << 16) |
(bitmapInfoHeader[23] << 24);
h = (long) rawsize;
*width = (unsigned int)
(bitmapInfoHeader[4] |
(bitmapInfoHeader[5] << 8) |
(bitmapInfoHeader[6] << 16) |
(bitmapInfoHeader[7] << 24));
*height = (unsigned int)
(bitmapInfoHeader[8] |
(bitmapInfoHeader[9] << 8) |
(bitmapInfoHeader[10] << 16) |
(bitmapInfoHeader[11] << 24));
mem = (unsigned char*)malloc(h+1);
if(mem == NULL)
{
printf("readBMP(): Memory not allocated\n");
*lgSize = 0;
return 0;
}
fread(mem, sizeof(unsigned char), h, TARGET);
*lgSize = h;
fclose(TARGET);
return mem;
}
int main(int argc, char **argv)
{
long fileSize, blendSize, over;
unsigned int w, h, m = 0, blendWidth, blendHeight;
FILE *writeFile;
unsigned char *filecontents = 0;
unsigned char *blendContents = 0;
unsigned char xorChar = 0x3d;
double divisor = 0.90;
params p;
p = commandParams(argv, argc);
if(p.canExecute != 7 && !(p.canExecute == 1 && p.dryRun))
{
printf("Incorrect parameters:\n");
printf("-i inputfile -b blendfile -o outputfile [-dry]\n");
return -1;
}
filecontents = loadFile(p.inputFilename, &fileSize);
if(!filecontents)
{
printf("main(): bad file\n");
return -1;
}
unsigned int sqrtAct = (unsigned int)ceil(sqrt(fileSize + m));
w = h = (sqrtAct + (4 - (sqrtAct % 4)));
if(p.canExecute == 1 && p.dryRun){
printf("Blend bitmap must be exactly %d pixels wide and at least that in height.\n", w);
return 1;
}
blendContents = readBMP(p.blendFilename, &blendSize, &blendWidth, &blendHeight);
if(!blendContents)
{
printf("main(): bad blend file\n");
return -1;
}
fopen_s(&writeFile, p.outputFilename, "wb");
if(!writeFile)
{
printf("main(): bad output file\n");
return -1;
}
unsigned char bmpfileheader[14] = { 'B', 'M', 0,0,0,0, 0,0, 0,0, 54,0,0,0 };
// 0x06-0x09 are reserved and not used by the definition, store the offset of where the file ends
unsigned char bmpinfoheader[40] = { 40,0,0,0, 0,0,0,0, 0,0,0,0, 1,0, 24,0, 0,0,0,0, 0,0,0,0, 0x13,0x0B,0,0, 0x13,0x0B,0,0, 0,0,0,0, 0,0,0,0 };
// size width height plane bit depth rawsize DPI horiz DPI vert palette imp colors
unsigned int rawsize = (w * 3) * h;
unsigned int bmpsize = 54 + rawsize;
bmpfileheader[2] = (unsigned char)(bmpsize);
bmpfileheader[3] = (unsigned char)(bmpsize>>8);
bmpfileheader[4] = (unsigned char)(bmpsize>>16);
bmpfileheader[5] = (unsigned char)(bmpsize>>24);
// Not part of the definition, this tells the "decoder" where the file stops within the image
bmpfileheader[6] = (unsigned char)(fileSize);
bmpfileheader[7] = (unsigned char)(fileSize>>8);
bmpfileheader[8] = (unsigned char)(fileSize>>16);
bmpfileheader[9] = (unsigned char)(fileSize>>24);
bmpinfoheader[4] = (unsigned char)(blendWidth);
bmpinfoheader[5] = (unsigned char)(blendWidth>>8);
bmpinfoheader[6] = (unsigned char)(blendWidth>>16);
bmpinfoheader[7] = (unsigned char)(blendWidth>>24);
bmpinfoheader[8] = (unsigned char)(blendHeight);
bmpinfoheader[9] = (unsigned char)(blendHeight>>8);
bmpinfoheader[10] = (unsigned char)(blendHeight>>16);
bmpinfoheader[11] = (unsigned char)(blendHeight>>24);
bmpinfoheader[20] = (unsigned char)(rawsize);
bmpinfoheader[21] = (unsigned char)(rawsize>>8);
bmpinfoheader[22] = (unsigned char)(rawsize>>16);
bmpinfoheader[23] = (unsigned char)(rawsize>>24);
fwrite(bmpfileheader, 1, 14, writeFile);
fwrite(bmpinfoheader, 1, 40, writeFile);
for(unsigned int x = 0; x < fileSize; x++)
{
unsigned char xc1 = ((filecontents[x] & 0x07) + ((unsigned char)(blendContents[x*3] * divisor))) % 255;
unsigned char xc2 = (((filecontents[x] >> 3) & 0x03) + ((unsigned char)(blendContents[(x*3)+1] * divisor))) % 255;
unsigned char xc3 = (((filecontents[x] >> 5) & 0x07) + ((unsigned char)(blendContents[(x*3)+2] * divisor))) % 255;
fwrite(&xc1, 1, 1, writeFile);
fwrite(&xc2, 1, 1, writeFile);
fwrite(&xc3, 1, 1, writeFile);
}
over = fileSize * 3;
for(unsigned int x = over; x < blendSize; x++)
{
unsigned char xc = blendContents[x] * divisor;
fwrite(&xc, 1, 1, writeFile);
}
fclose(writeFile);
free(blendContents);
free(filecontents);
return 0;
}