1 | /* GFX2mo5 - libraw2mo5.c |
---|
2 | * CloudStrife - 20080921 |
---|
3 | * PulkoMandy - 20101221, 20130213 |
---|
4 | * Diffusé sous licence libre CeCILL v2 |
---|
5 | * Voir LICENCE |
---|
6 | */ |
---|
7 | |
---|
8 | #include <stdbool.h> |
---|
9 | #include <stdio.h> |
---|
10 | #include <stdlib.h> |
---|
11 | #include <string.h> |
---|
12 | |
---|
13 | unsigned char *raw2mo5(unsigned char *input, bool fixup) |
---|
14 | { |
---|
15 | unsigned char *tmpBuffer; |
---|
16 | int x,y; |
---|
17 | int previous = 0; |
---|
18 | |
---|
19 | tmpBuffer = (unsigned char*)calloc(0x4000,1); |
---|
20 | if (tmpBuffer == NULL) |
---|
21 | { |
---|
22 | printf("Allocation tmpBuffer raté\n"); |
---|
23 | exit(4); |
---|
24 | } |
---|
25 | #define width 320 |
---|
26 | |
---|
27 | bool lfo = false; |
---|
28 | for (y = 0; y < 200; y++) |
---|
29 | for (x = 0; x < 320; x+=8) { |
---|
30 | int fore = 255; |
---|
31 | int back = 255; |
---|
32 | int pix; |
---|
33 | bool oldlfo = lfo; |
---|
34 | |
---|
35 | for(pix = 0; pix < 8; pix++) { |
---|
36 | int nc = input[y*width+x+pix]; |
---|
37 | if (nc > 15) printf("Color over limit!\n"); |
---|
38 | if (back == nc) { |
---|
39 | // Pixel is in backcolor, set FORME to 0 |
---|
40 | lfo = false; |
---|
41 | } else if (fore == nc) { |
---|
42 | // Pixel is in forecolor, set FORME to 1 |
---|
43 | tmpBuffer[(y*320+x)/8] |= 0x80>>pix; |
---|
44 | lfo = true; |
---|
45 | } else if (back==255) { |
---|
46 | // Pixel is in unknown color, back is free : allocate backcolor |
---|
47 | back = nc; |
---|
48 | lfo = false; |
---|
49 | } else if (fore == 255) { |
---|
50 | // Pixel is unknown color, back is allocated : allocate front and set FORME |
---|
51 | fore = nc; |
---|
52 | tmpBuffer[(y*320+x)/8] |= 0x80>>pix; |
---|
53 | lfo = true; |
---|
54 | } else { |
---|
55 | printf("Color clash at %d %d : %d %d %d\n",x+pix,y,fore, back, |
---|
56 | input[y*width+x+pix]); |
---|
57 | } |
---|
58 | } |
---|
59 | |
---|
60 | if (fore == 255) fore = 7; |
---|
61 | if (fore != 255) { |
---|
62 | previous &= 0xF; |
---|
63 | previous |= fore << 4; |
---|
64 | } |
---|
65 | if (back != 255) { |
---|
66 | previous &= 0xF0; |
---|
67 | previous |= back; |
---|
68 | } |
---|
69 | |
---|
70 | // Make sure the last pixel of this GPL and the first of the next GPL |
---|
71 | // are both FORME or both FOND, else we get an ugly glitch on the |
---|
72 | // EFGJ033 Gate Array MO5! |
---|
73 | if(fixup && oldlfo == !(tmpBuffer[(y*320+x)/8] & 0x80)) |
---|
74 | { |
---|
75 | previous = (previous >> 4) | (previous << 4); |
---|
76 | tmpBuffer[(y*320+x)/8] ^= 0xFF; |
---|
77 | |
---|
78 | lfo = !lfo; |
---|
79 | } |
---|
80 | |
---|
81 | tmpBuffer[0x2000+(y*320+x)/8] = previous; |
---|
82 | } |
---|
83 | |
---|
84 | return tmpBuffer; |
---|
85 | } |
---|