forked from difcareer/010templates
-
Notifications
You must be signed in to change notification settings - Fork 1
/
PNG12Template.bt
executable file
·404 lines (359 loc) · 10.7 KB
/
PNG12Template.bt
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
//--------------------------------------
//--- 010 Editor v2.0.2 Binary Template
//
// File: PNG12Template.bt
// Author: RCS
// Revision: 0.3
// Purpose: Use templates on PNG images
// Changes: 0.2 - Allow duplicate chunks
// 0.3 - Made chunk names case sensitive
//--------------------------------------
typedef struct {
WORD btPngSignature[4] <format=hex>;
} PNG_SIGNATURE;
typedef struct {
DWORD btChunkLen;
SetForeColor(cYellow);
SetBackColor(cBlue);
CHAR btChunkType[4];
SetBackColor(cWhite);
} PNG_CHUNK_HEADER;
typedef enum <byte> pngColorSpaceType {
GrayScale = 0,
TrueColor = 2,
Indexed = 3,
AlphaGrayScale = 4,
AlphaTrueColor = 6
} PNG_COLOR_SPACE_TYPE;
// Compression Methods
typedef enum <byte> pngCompressionMethod {
Deflate = 0
} PNG_COMPR_METHOD;
// Filter Methods
typedef enum <byte> pngFilterMethod {
AdaptiveFiltering = 0
} PNG_FILTER_METHOD;
// Interlace Methods
typedef enum <byte> pngInterlaceMethod {
NoInterlace = 0,
Adam7Interlace = 1
} PNG_INTERLACE_METHOD;
// IHDR data
typedef struct {
UINT width;
UINT height;
BYTE bit_depth;
PNG_COLOR_SPACE_TYPE color_type;
PNG_COMPR_METHOD compr_method;
PNG_FILTER_METHOD filter_method;
PNG_INTERLACE_METHOD interlace_method;
} IHDR_CHUNK_DATA;
typedef struct {
BYTE btRed <format=hex>;
BYTE btGreen <format=hex>;
BYTE btBlue <format=hex>;
} PNG_PALETTE_PIXEL;
typedef struct {
uint x;
uint y;
} PNG_POINT;
typedef struct {
PNG_POINT white;
PNG_POINT red;
PNG_POINT green;
PNG_POINT blue;
} PNG_CHRM_CHUNK_DATA;
typedef enum <byte> {
Perceptual = 0,
RelativeColorimetric = 1,
Saturation = 2,
AbsoluteColorimetric = 3
} PNG_SRGB_CHUNK_DATA;
typedef struct {
string profile_name;
unsigned byte red;
} PNG_ICCP_CHUNK_DATA;
//---------------------------------------------
// PNG is big endian
BigEndian();
// Start PNG file
struct PngFile {
// Check the PNG signature
// (89h 50h 4Eh 47h 0Dh 0Ah 1Ah 0Ah)
SetForeColor(cRed);
PNG_SIGNATURE sig;
if (sig.btPngSignature[0] != 0x8950 ||
sig.btPngSignature[1] != 0x4E47 ||
sig.btPngSignature[2] != 0x0D0A ||
sig.btPngSignature[3] != 0x1A0A) {
Warning( "File is not a PNG image. Template stopped." );
return -1;
}
// Chunks naming rules
// Ancillary bit: bit 5 of first byte
// Private bit: bit 5 of second byte
// Reserved bit: bit 5 of third byte
// Safe-to-copy bit: bit 5 of fourth byte
SetForeColor(cGreen);
PNG_CHUNK_HEADER ihdrChunkHdr;
if (Strncmp(ihdrChunkHdr.btChunkType, "IHDR", 4) != 0)
{
Warning( "PNG File does not start with IHDR chunk." );
return -2;
}
SetForeColor(cBlack);
IHDR_CHUNK_DATA ihdrChunkData;
SetForeColor(cBlue);
DWORD ihdrCrc <format=hex>;
while (!FEof())
{
// Chunks naming rules
// Ancillary bit: bit 5 of first byte
// Private bit: bit 5 of second byte
// Reserved bit: bit 5 of third byte
// Safe-to-copy bit: bit 5 of fourth byte
SetForeColor(cGreen);
PNG_CHUNK_HEADER chunkHdr;
if (Strncmp(chunkHdr.btChunkType, "PLTE", 4) == 0)
{
SetForeColor(cBlack);
struct PNG_CHUNK_PLTE
{
PNG_PALETTE_PIXEL plteChunkData[chunkHdr.btChunkLen/3];
SetForeColor(cBlue);
DWORD plteCrc <format=hex>;
} chunk_plte;
}
else if (Strncmp(chunkHdr.btChunkType, "tRNS", 4) == 0)
{
SetForeColor(cBlack);
struct PNG_CHUNK_TRNS
{
BYTE trnsChunkData[chunkHdr.btChunkLen] <format=hex>;
SetForeColor(cBlue);
DWORD trnsCrc <format=hex>;
} chunk_trns;
}
else if (Strncmp(chunkHdr.btChunkType, "IDAT", 4) == 0)
{
SetForeColor(cWhite);
SetBackColor(cBlack);
struct PNG_CHUNK_IDAT
{
BYTE idatChunkData[chunkHdr.btChunkLen] <format=hex>;
SetForeColor(cBlack);
SetBackColor(cWhite);
SetForeColor(cBlue);
DWORD idatCrc <format=hex>;
} chunk_idat;
}
else if (Strncmp(chunkHdr.btChunkType, "gAMA", 4) == 0)
{
SetForeColor(cBlack);
struct PNG_CHUNK_GAMA
{
BYTE gamaChunkData[chunkHdr.btChunkLen];
SetForeColor(cBlue);
DWORD gamaCrc <format=hex>;
} chunk_gama;
}
else if (Strncmp(chunkHdr.btChunkType, "cHRM", 4) == 0)
{
SetForeColor(cBlack);
struct PNG_CHUNK_CHRM
{
PNG_CHRM_CHUNK_DATA chrmChunkData;
SetForeColor(cBlue);
DWORD chrmCrc <format=hex>;
} chunk_chrm;
}
else if (Strncmp(chunkHdr.btChunkType, "sRGB", 4) == 0)
{
SetForeColor(cBlack);
struct PNG_CHUNK_SRGB
{
PNG_SRGB_CHUNK_DATA srgbChunkData;
SetForeColor(cBlue);
DWORD srgbCrc <format=hex>;
} chunk_srgb;
}
else if (Strncmp(chunkHdr.btChunkType, "iEXt", 4) == 0)
{
SetForeColor(cBlack);
struct PNG_CHUNK_IEXT
{
string iextIdChunkData;
byte iextCompressionFlag;
PNG_COMPR_METHOD iextComprMethod;
string iextLanguageTag;
string iextTranslatedKeyword;
char iextValChunkData[chunkHdr.btChunkLen -
Strlen(iextIdChunkData) -1 -
Strlen(iextLanguageTag) -1 -
Strlen(iextTranslatedKeyword) -1 -
2];
SetForeColor(cBlue);
DWORD iextCrc <format=hex>;
} chunk_iext;
}
else if (Strncmp(chunkHdr.btChunkType, "tEXt", 4) == 0)
{
SetForeColor(cBlack);
struct PNG_CHUNK_TEXT
{
string textIdChunkData;
char textValChunkData[chunkHdr.btChunkLen - Strlen(textIdChunkData) -1];
SetForeColor(cBlue);
DWORD textCrc <format=hex>;
} chunk_text;
}
else if (Strncmp(chunkHdr.btChunkType, "zEXt", 4) == 0)
{
SetForeColor(cBlack);
struct PNG_CHUNK_ZEXT
{
string zextIdChunkData;
PNG_COMPR_METHOD comprMethod;
char zextValChunkData[chunkHdr.btChunkLen - Strlen(zextIdChunkData) -2];
SetForeColor(cBlue);
DWORD zextCrc <format=hex>;
} chunk_text;
}
else if (Strncmp(chunkHdr.btChunkType, "tIME", 4) == 0)
{
SetForeColor(cBlack);
struct PNG_CHUNK_TIME
{
short timeYear <format=decimal>;
byte timeMonth <format=decimal>;
byte timeDay <format=decimal>;
byte timeHour <format=decimal>;
byte timeMin <format=decimal>;
byte timeSec <format=decimal>;
SetForeColor(cBlue);
DWORD zextCrc <format=hex>;
} chunk_time;
}
else if (Strncmp(chunkHdr.btChunkType, "bKGD", 4) == 0)
{
SetForeColor(cBlack);
struct PNG_CHUNK_BKGD
{
switch (ihdrChunkData.color_type)
{
case 3: // Indexed
unsigned byte bgColorPaletteIndex <format=hex>;
break;
case 0: // Grayscale
case 4: // Grayscale with alpha
unsigned short bgGrayscalePixelValue <format=hex>;
break;
case 2: // TrueColor
case 6: // TrueColor with alpha
unsigned short bgColorPixelRed <format=hex>;
unsigned short bgColorPixelGreen <format=hex>;
unsigned short bgColorPixelBlue <format=hex>;
break;
default:
Warning( "Unknown Color Model Type for background color chunk." );
return -4;
}
SetForeColor(cBlue);
DWORD zextCrc <format=hex>;
} chunk_bkgd;
}
else if (Strncmp(chunkHdr.btChunkType, "pHYs", 4) == 0)
{
SetForeColor(cBlack);
struct PNG_CHUNK_PHYS
{
uint physPixelPerUnitX;
uint physPixelPerUnitY;
enum <byte> {
UnkownUnit = 0,
Meter = 1
} physUnitSpec;
SetForeColor(cBlue);
DWORD zextCrc <format=hex>;
} chunk_phys;
}
else if (Strncmp(chunkHdr.btChunkType, "sBIT", 4) == 0)
{
SetForeColor(cBlack);
struct PNG_CHUNK_SBIT
{
switch (ihdrChunkData.color_type)
{
case 3: // Indexed
byte sbitRed;
byte sbitGreen;
byte sbitBlue;
break;
case 0: // Grayscale
byte sbitGraySource;
break;
case 4: // Grayscale with alpha
byte sbitGrayAlphaSource;
byte sbitGrayAlphaSourceAlpha;
break;
case 2: // TrueColor
byte sbitColorRed;
byte sbitColorGreen;
byte sbitColorBlue;
break;
case 6: // TrueColor with alpha
byte sbitColorAlphaRed;
byte sbitColorAlphaGreen;
byte sbitColorAlphaBlue;
byte sbitColorAlphaAlpha;
break;
default:
Warning( "Unknown Color Model Type for background color chunk." );
return -4;
}
SetForeColor(cBlue);
DWORD sbitCrc <format=hex>;
} chunk_sbit;
}
else if (Strncmp(chunkHdr.btChunkType, "sPLT", 4) == 0)
{
SetForeColor(cBlue);
struct PNG_CHUNK_SPLT
{
string paletteName;
byte sampleDepth;
byte spltData[chunkHdr.btChunkLen - Strlen(paletteName) -2];
SetForeColor(cBlue);
DWORD spltCrc <format=hex>;
} chunk_splt;
}
else if (Strncmp(chunkHdr.btChunkType, "hIST", 4) == 0)
{
SetForeColor(cBlue);
struct PNG_CHUNK_HIST
{
DWORD iendCrc <format=hex>;
SetForeColor(cBlue);
DWORD histCrc <format=hex>;
} chunk_hist;
}
else if (Strncmp(chunkHdr.btChunkType, "IEND", 4) == 0)
{
SetForeColor(cBlue);
struct PNG_CHUNK_IEND
{
DWORD iendCrc <format=hex>;
} chunk_iend;
}
else
{
SetForeColor(cBlack);
struct PNG_CHUNK_UNKNOWN
{
BYTE genChunkData[chunkHdr.btChunkLen];
SetForeColor(cBlue);
DWORD genCrc <format=hex>;
} chunk_unknown;
}
}
} myPngFile;