-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathencTest.java
More file actions
552 lines (331 loc) · 13.7 KB
/
Copy pathencTest.java
File metadata and controls
552 lines (331 loc) · 13.7 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
package CMPRTest;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferByte;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.Arrays;
import java.util.OptionalInt;
import javax.imageio.ImageIO;
public class encTest {
static RandomAccessFile r;
static RandomAccessFile wr;
public static int dx;
public static int dy;
static BufferedImage in;
static BufferedImage mip;
public static int lod;
private static short[] cc85 = // convert 8-bit color to 5-bit color
{
0x00,0x00,0x00,0x00, 0x00,0x01,0x01,0x01, 0x01,0x01,0x01,0x01, 0x01,0x02,0x02,0x02,
0x02,0x02,0x02,0x02, 0x02,0x03,0x03,0x03, 0x03,0x03,0x03,0x03, 0x03,0x04,0x04,0x04,
0x04,0x04,0x04,0x04, 0x04,0x04,0x05,0x05, 0x05,0x05,0x05,0x05, 0x05,0x05,0x06,0x06,
0x06,0x06,0x06,0x06, 0x06,0x06,0x07,0x07, 0x07,0x07,0x07,0x07, 0x07,0x07,0x08,0x08,
0x08,0x08,0x08,0x08, 0x08,0x08,0x09,0x09, 0x09,0x09,0x09,0x09, 0x09,0x09,0x09,0x0a,
0x0a,0x0a,0x0a,0x0a, 0x0a,0x0a,0x0a,0x0b, 0x0b,0x0b,0x0b,0x0b, 0x0b,0x0b,0x0b,0x0c,
0x0c,0x0c,0x0c,0x0c, 0x0c,0x0c,0x0c,0x0d, 0x0d,0x0d,0x0d,0x0d, 0x0d,0x0d,0x0d,0x0d,
0x0e,0x0e,0x0e,0x0e, 0x0e,0x0e,0x0e,0x0e, 0x0f,0x0f,0x0f,0x0f, 0x0f,0x0f,0x0f,0x0f,
0x10,0x10,0x10,0x10, 0x10,0x10,0x10,0x10, 0x11,0x11,0x11,0x11, 0x11,0x11,0x11,0x11,
0x12,0x12,0x12,0x12, 0x12,0x12,0x12,0x12, 0x12,0x13,0x13,0x13, 0x13,0x13,0x13,0x13,
0x13,0x14,0x14,0x14, 0x14,0x14,0x14,0x14, 0x14,0x15,0x15,0x15, 0x15,0x15,0x15,0x15,
0x15,0x16,0x16,0x16, 0x16,0x16,0x16,0x16, 0x16,0x16,0x17,0x17, 0x17,0x17,0x17,0x17,
0x17,0x17,0x18,0x18, 0x18,0x18,0x18,0x18, 0x18,0x18,0x19,0x19, 0x19,0x19,0x19,0x19,
0x19,0x19,0x1a,0x1a, 0x1a,0x1a,0x1a,0x1a, 0x1a,0x1a,0x1b,0x1b, 0x1b,0x1b,0x1b,0x1b,
0x1b,0x1b,0x1b,0x1c, 0x1c,0x1c,0x1c,0x1c, 0x1c,0x1c,0x1c,0x1d, 0x1d,0x1d,0x1d,0x1d,
0x1d,0x1d,0x1d,0x1e, 0x1e,0x1e,0x1e,0x1e, 0x1e,0x1e,0x1e,0x1f, 0x1f,0x1f,0x1f,0x1f
};
static short[] cc86 = {
0x00,0x00,0x00,0x01, 0x01,0x01,0x01,0x02, 0x02,0x02,0x02,0x03, 0x03,0x03,0x03,0x04,
0x04,0x04,0x04,0x05, 0x05,0x05,0x05,0x06, 0x06,0x06,0x06,0x07, 0x07,0x07,0x07,0x08,
0x08,0x08,0x08,0x09, 0x09,0x09,0x09,0x0a, 0x0a,0x0a,0x0a,0x0b, 0x0b,0x0b,0x0b,0x0c,
0x0c,0x0c,0x0c,0x0d, 0x0d,0x0d,0x0d,0x0e, 0x0e,0x0e,0x0e,0x0f, 0x0f,0x0f,0x0f,0x10,
0x10,0x10,0x10,0x11, 0x11,0x11,0x11,0x12, 0x12,0x12,0x12,0x13, 0x13,0x13,0x13,0x14,
0x14,0x14,0x14,0x15, 0x15,0x15,0x15,0x15, 0x16,0x16,0x16,0x16, 0x17,0x17,0x17,0x17,
0x18,0x18,0x18,0x18, 0x19,0x19,0x19,0x19, 0x1a,0x1a,0x1a,0x1a, 0x1b,0x1b,0x1b,0x1b,
0x1c,0x1c,0x1c,0x1c, 0x1d,0x1d,0x1d,0x1d, 0x1e,0x1e,0x1e,0x1e, 0x1f,0x1f,0x1f,0x1f,
0x20,0x20,0x20,0x20, 0x21,0x21,0x21,0x21, 0x22,0x22,0x22,0x22, 0x23,0x23,0x23,0x23,
0x24,0x24,0x24,0x24, 0x25,0x25,0x25,0x25, 0x26,0x26,0x26,0x26, 0x27,0x27,0x27,0x27,
0x28,0x28,0x28,0x28, 0x29,0x29,0x29,0x29, 0x2a,0x2a,0x2a,0x2a, 0x2a,0x2b,0x2b,0x2b,
0x2b,0x2c,0x2c,0x2c, 0x2c,0x2d,0x2d,0x2d, 0x2d,0x2e,0x2e,0x2e, 0x2e,0x2f,0x2f,0x2f,
0x2f,0x30,0x30,0x30, 0x30,0x31,0x31,0x31, 0x31,0x32,0x32,0x32, 0x32,0x33,0x33,0x33,
0x33,0x34,0x34,0x34, 0x34,0x35,0x35,0x35, 0x35,0x36,0x36,0x36, 0x36,0x37,0x37,0x37,
0x37,0x38,0x38,0x38, 0x38,0x39,0x39,0x39, 0x39,0x3a,0x3a,0x3a, 0x3a,0x3b,0x3b,0x3b,
0x3b,0x3c,0x3c,0x3c, 0x3c,0x3d,0x3d,0x3d, 0x3d,0x3e,0x3e,0x3e, 0x3e,0x3f,0x3f,0x3f
};
private static short[] cc83 = // convert 8-bit color to 3-bit color
{
0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x01, 0x01,0x01,0x01,0x01, 0x01,0x01,0x01,0x01, 0x01,0x01,0x01,0x01,
0x01,0x01,0x01,0x01, 0x01,0x01,0x01,0x01, 0x01,0x01,0x01,0x01, 0x01,0x01,0x01,0x01,
0x01,0x01,0x01,0x01, 0x01,0x01,0x01,0x02, 0x02,0x02,0x02,0x02, 0x02,0x02,0x02,0x02,
0x02,0x02,0x02,0x02, 0x02,0x02,0x02,0x02, 0x02,0x02,0x02,0x02, 0x02,0x02,0x02,0x02,
0x02,0x02,0x02,0x02, 0x02,0x02,0x02,0x02, 0x02,0x02,0x02,0x02, 0x03,0x03,0x03,0x03,
0x03,0x03,0x03,0x03, 0x03,0x03,0x03,0x03, 0x03,0x03,0x03,0x03, 0x03,0x03,0x03,0x03,
0x03,0x03,0x03,0x03, 0x03,0x03,0x03,0x03, 0x03,0x03,0x03,0x03, 0x03,0x03,0x03,0x03,
0x04,0x04,0x04,0x04, 0x04,0x04,0x04,0x04, 0x04,0x04,0x04,0x04, 0x04,0x04,0x04,0x04,
0x04,0x04,0x04,0x04, 0x04,0x04,0x04,0x04, 0x04,0x04,0x04,0x04, 0x04,0x04,0x04,0x04,
0x04,0x04,0x04,0x04, 0x05,0x05,0x05,0x05, 0x05,0x05,0x05,0x05, 0x05,0x05,0x05,0x05,
0x05,0x05,0x05,0x05, 0x05,0x05,0x05,0x05, 0x05,0x05,0x05,0x05, 0x05,0x05,0x05,0x05,
0x05,0x05,0x05,0x05, 0x05,0x05,0x05,0x05, 0x05,0x06,0x06,0x06, 0x06,0x06,0x06,0x06,
0x06,0x06,0x06,0x06, 0x06,0x06,0x06,0x06, 0x06,0x06,0x06,0x06, 0x06,0x06,0x06,0x06,
0x06,0x06,0x06,0x06, 0x06,0x06,0x06,0x06, 0x06,0x06,0x06,0x06, 0x06,0x07,0x07,0x07,
0x07,0x07,0x07,0x07, 0x07,0x07,0x07,0x07, 0x07,0x07,0x07,0x07, 0x07,0x07,0x07,0x07
};
private static short[] cc84 = // convert 8-bit color to 4-bit color
{
0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x01,0x01,0x01, 0x01,0x01,0x01,0x01,
0x01,0x01,0x01,0x01, 0x01,0x01,0x01,0x01, 0x01,0x01,0x02,0x02, 0x02,0x02,0x02,0x02,
0x02,0x02,0x02,0x02, 0x02,0x02,0x02,0x02, 0x02,0x02,0x02,0x03, 0x03,0x03,0x03,0x03,
0x03,0x03,0x03,0x03, 0x03,0x03,0x03,0x03, 0x03,0x03,0x03,0x03, 0x04,0x04,0x04,0x04,
0x04,0x04,0x04,0x04, 0x04,0x04,0x04,0x04, 0x04,0x04,0x04,0x04, 0x04,0x05,0x05,0x05,
0x05,0x05,0x05,0x05, 0x05,0x05,0x05,0x05, 0x05,0x05,0x05,0x05, 0x05,0x05,0x06,0x06,
0x06,0x06,0x06,0x06, 0x06,0x06,0x06,0x06, 0x06,0x06,0x06,0x06, 0x06,0x06,0x06,0x07,
0x07,0x07,0x07,0x07, 0x07,0x07,0x07,0x07, 0x07,0x07,0x07,0x07, 0x07,0x07,0x07,0x07,
0x08,0x08,0x08,0x08, 0x08,0x08,0x08,0x08, 0x08,0x08,0x08,0x08, 0x08,0x08,0x08,0x08,
0x08,0x09,0x09,0x09, 0x09,0x09,0x09,0x09, 0x09,0x09,0x09,0x09, 0x09,0x09,0x09,0x09,
0x09,0x09,0x0a,0x0a, 0x0a,0x0a,0x0a,0x0a, 0x0a,0x0a,0x0a,0x0a, 0x0a,0x0a,0x0a,0x0a,
0x0a,0x0a,0x0a,0x0b, 0x0b,0x0b,0x0b,0x0b, 0x0b,0x0b,0x0b,0x0b, 0x0b,0x0b,0x0b,0x0b,
0x0b,0x0b,0x0b,0x0b, 0x0c,0x0c,0x0c,0x0c, 0x0c,0x0c,0x0c,0x0c, 0x0c,0x0c,0x0c,0x0c,
0x0c,0x0c,0x0c,0x0c, 0x0c,0x0d,0x0d,0x0d, 0x0d,0x0d,0x0d,0x0d, 0x0d,0x0d,0x0d,0x0d,
0x0d,0x0d,0x0d,0x0d, 0x0d,0x0d,0x0e,0x0e, 0x0e,0x0e,0x0e,0x0e, 0x0e,0x0e,0x0e,0x0e,
0x0e,0x0e,0x0e,0x0e, 0x0e,0x0e,0x0e,0x0f, 0x0f,0x0f,0x0f,0x0f, 0x0f,0x0f,0x0f,0x0f
};
final static short[] cc38 = // convert 3-bit color to 8-bit color
{
0x00,0x24,0x49,0x6d, 0x92,0xb6,0xdb,0xff
};
final static short[] cc48 = // convert 4-bit color to 8-bit color
{
0x00,0x11,0x22,0x33, 0x44,0x55,0x66,0x77, 0x88,0x99,0xaa,0xbb, 0xcc,0xdd,0xee,0xff
};
public static void begin(File i, File o,long pos,boolean wd) throws IOException {
// o.delete();
r = new RandomAccessFile(i, "r");
in = ImageIO.read(i);
int h = in.getHeight();
int w = in.getWidth();
int lod = 1; //cntMips(h,w); //don't know about mips yet
wr = new RandomAccessFile(o, "rw");
wr.seek(pos);
for(int j = 0; j < lod; ++j) {
int ww = (int)Math.round(w/(Math.pow(2,j)));
int hh = (int)Math.round(h/(Math.pow(2,j)));
Image img = in.getScaledInstance(ww, hh, Image.SCALE_REPLICATE);
mip = itbi(img);
// encRGB8(ww,hh);
encCMPR(ww,hh);
}
if(wd) {
wr.seek(0x10);
wr.writeShort((short)w);
wr.writeShort((short)h);
}
}
public static void encRGB8(int w, int h) throws IOException {
int y = 0;
int x =0;
while(y<h) {
int[] texel = new int[16];
mip.getRGB(x, y, 4, 4, texel, 0, 4);
for(int i = 0; i < texel.length ; ++i)
wr.writeShort((texel[i]>>16)&0xFFFF); //AR
for(int i = 0; i < texel.length ; ++i)
wr.writeShort((texel[i])&0xFFFF); //GB
//System.exit(0);
//System.out.println("X: "+X+" Y: "+Y+" dx: "+dx+" dy: "+dy);
x+= 4;
if(x >= w) {
x = 0;
y += 4;
}
}}
public static void enc5a3(int w, int h) throws IOException {
int y = 0;
int x =0;
while(y<h) {
int[] texel = new int[16];
mip.getRGB(x, y, 4, 4, texel, 0, 4);
for(int i = 0; i < texel.length ; ++i)
wr.writeShort( ((texel[i] >> 24) & 0xFF) == 0xFF ? a888toRGB(texel[i], false) : a888toRGB(texel[i], true) );
//System.out.println("X: "+X+" Y: "+Y+" dx: "+dx+" dy: "+dy);
x+= 4;
if(x >= w) {
x = 0;
y += 4;
}
}}
public static int a888toRGB(int c, boolean alpha) {
int a;
int r;
int g;
int b;
int ret =0;
if(!alpha) {
a= 0x1;
r = cc85[(c>>16)&0xff];
g = cc85[(c>>8)&0xff];
b = cc85[c&0xff];
ret = (a<<15) | r <<10 | g<<5 | b;
}
else {
a= cc83[(c>>24)&0xff];
r = cc84[(c>>16)&0xff];
g = cc84[(c>>8)&0xff];
b = cc84[c&0xff];
ret = (0x0 << 15)&0x7 | a<< 12 | r <<8 | g<<4 | b;
}
System.out.println(Integer.toBinaryString(ret));
//System.exit(0);
return ret;
}
public static void encCMPR(int w, int h) throws IOException {
/* byte[] pix = ((DataBufferByte) in.getRaster().getDataBuffer()).getData();
int pixl = ((pix[0] & 255) << 24 )|(( pix[1] & 255) << 16) | (pix[2] & 255) <<8 | pix[3] & 255;
*/
int x = 0;
int y = 0;
int dx = 0;
int dy = 0;
int dyHack = 0;
while( y< (h-4)) {
if( dx>= 8 ) {
if(dyHack == x)
dy=4;
else dy = 0;
dx=0;
}
int[] texel = new int[16];
long CMPRTexel = ARGB2CMPR(texel,dx,dy,x,y);
wr.writeLong(CMPRTexel);
//System.out.println("x: "+x+" y: "+y+" dx: "+dx+" dy: "+dy);
dyHack = x;
if(dx == 4 && dy == 4) { x+=8;}
dx+=4;
if(x>w-4) {
x= 0 ;
y+=8;
}
}
return;
}
private static long ARGB2CMPR(int[] texel, int pdx, int pdy, int x, int y){
try {
mip.getRGB(x+pdx, y+pdy, 4, 4, texel, 0, 4);
} catch(ArrayIndexOutOfBoundsException e) {
return 0;
}
int[] pal = getPalette(texel);
int ind32 = getIndicies(texel, pal);
int c0c1 = to565(pal);
return (((long)c0c1) << 32) | (ind32 & 0xffffffffL);
}
public static int[] getPalette(int[] pix) {
float[] pal = new float[4];
int[] pald = new int[4];
//extremes (c0,c1)
pal[0] = Arrays.stream(pix).max().getAsInt();
pal[1] = Arrays.stream(pix).min().getAsInt();
//avgs (c2, c3)
pal[2] = ((2*pal[0]+pal[1])/3);
pal[3] = ((pal[0]+2*pal[1])/3);
for(int i = 0; i < 4; ++i)
pald[i] = Math.round(pal[i]);
// }
//int writeExtremes = to565(new int[] {ex1,ex2});
return pald;
}
public static int getIndicies(int[] texel, int[] pal) {
int[] ind = new int[16];
int ind32 = 0;
for(int i = 0 ; i <texel.length; ++i)
ind[i] = closest(texel[i],pal);
//System.exit(0);
for(int i = 0; i < ind.length; ++i)
ind32 += ((ind[i]& 3) << 30-(2*i)) ;
return ind32;
}
private static int to565(int[] te) {
boolean alpha = false;
int[] t565 = new int[te.length];
int ret565 = 0;
for(int i = 0; i < te.length; ++i) {
if( ((te[i]>>24) &0xFF) !=0xFF )
alpha = true;
t565[i] =
cc85[ te[i] >> 16 & 0xff ] << 11
| cc86[ te[i] >> 8 & 0xff ] << 5
| cc85[ te[i] & 0xff ];
}
int big=0;
int smol =0;
if(t565[0] >= t565[1]) {
big = t565[0];
smol = t565[1];
} else {big = t565[1]; smol = t565[0]; }
// ret565 = (big & 0xFFFF) << 16 | (smol & 0xFFFF);
if(alpha)
ret565 = (smol & 0xFFFF) << 16 | (big+0x1 & 0xFFFF);
else
ret565 = (big & 0xFFFF) << 16 | (smol & 0xFFFF);
return ret565;
}
public static void p(String txt) {
System.out.println(txt);
}
public static void pint(int txt) {
System.out.println(Integer.toHexString(txt)+" ");
}
public static void pb(int txt) {
System.out.println(Integer.toBinaryString(txt));
}
public static int closest(int find, int[] values) {
int closest = Integer.MAX_VALUE;
int temp= 0 ;
int temp1 = 0;
int ci = 0;
if( ((find>>24) &0xFF) !=0xFF )
return 3;
for(int i = 0; i<values.length; ++i) {
temp = Math.abs(find - values[i]);
if(temp < closest) {
closest = temp;
ci = i;
}
}
return ci;
}
public static BufferedImage itbi(Image im) {
BufferedImage bi = new BufferedImage
(im.getWidth(null),im.getHeight(null),BufferedImage.TYPE_INT_ARGB);
Graphics bg = bi.getGraphics();
bg.drawImage(im, 0, 0, null);
bg.dispose();
return bi;
}
public static int cntMips(int h, int w) {
int hl = 0;
int wl = 0;
int l = 0;
int temp = h;
while(temp>4) {
temp = temp/2;
++hl;
}
temp = w;
while(temp>4) {
temp = temp/2;
++wl;
}
if(hl>wl)
l = wl;
else l = hl;
System.out.println("lod is "+l);
return l;
}
}