[20] | 1 | /* GFX2mo5 - libraw2mo5.c |
---|
| 2 | * CloudStrife - 20080921 |
---|
[35] | 3 | * PulkoMandy - 20101221, 20130213 |
---|
[20] | 4 | * Diffusé sous licence libre CeCILL v2 |
---|
[35] | 5 | * Voir LICENCE |
---|
[20] | 6 | */ |
---|
| 7 | |
---|
[35] | 8 | #include <stdbool.h> |
---|
[49] | 9 | #include <stdint.h> |
---|
[20] | 10 | #include <stdio.h> |
---|
| 11 | #include <stdlib.h> |
---|
| 12 | #include <string.h> |
---|
| 13 | |
---|
[49] | 14 | unsigned char *raw2mo5(unsigned char *input, int height, int fixup, bool to) |
---|
[20] | 15 | { |
---|
| 16 | unsigned char *tmpBuffer; |
---|
| 17 | int x,y; |
---|
[35] | 18 | int previous = 0; |
---|
[49] | 19 | bool lfo = false; |
---|
| 20 | uint8_t val; |
---|
[68] | 21 | static const int width = 320; |
---|
[20] | 22 | |
---|
| 23 | tmpBuffer = (unsigned char*)calloc(0x4000,1); |
---|
| 24 | if (tmpBuffer == NULL) |
---|
| 25 | { |
---|
| 26 | printf("Allocation tmpBuffer raté\n"); |
---|
| 27 | exit(4); |
---|
| 28 | } |
---|
| 29 | |
---|
[41] | 30 | for (y = 0; y < height; y++) |
---|
[20] | 31 | for (x = 0; x < 320; x+=8) { |
---|
| 32 | int fore = 255; |
---|
| 33 | int back = 255; |
---|
| 34 | int pix; |
---|
[35] | 35 | bool oldlfo = lfo; |
---|
| 36 | |
---|
[20] | 37 | for(pix = 0; pix < 8; pix++) { |
---|
| 38 | int nc = input[y*width+x+pix]; |
---|
| 39 | if (nc > 15) printf("Color over limit!\n"); |
---|
| 40 | if (back == nc) { |
---|
| 41 | // Pixel is in backcolor, set FORME to 0 |
---|
[35] | 42 | lfo = false; |
---|
[20] | 43 | } else if (fore == nc) { |
---|
| 44 | // Pixel is in forecolor, set FORME to 1 |
---|
| 45 | tmpBuffer[(y*320+x)/8] |= 0x80>>pix; |
---|
[35] | 46 | lfo = true; |
---|
[20] | 47 | } else if (back==255) { |
---|
| 48 | // Pixel is in unknown color, back is free : allocate backcolor |
---|
| 49 | back = nc; |
---|
[35] | 50 | lfo = false; |
---|
[20] | 51 | } else if (fore == 255) { |
---|
| 52 | // Pixel is unknown color, back is allocated : allocate front and set FORME |
---|
| 53 | fore = nc; |
---|
| 54 | tmpBuffer[(y*320+x)/8] |= 0x80>>pix; |
---|
[35] | 55 | lfo = true; |
---|
[20] | 56 | } else { |
---|
| 57 | printf("Color clash at %d %d : %d %d %d\n",x+pix,y,fore, back, |
---|
| 58 | input[y*width+x+pix]); |
---|
| 59 | } |
---|
| 60 | } |
---|
| 61 | |
---|
[41] | 62 | if (fore == 255) fore = fixup; |
---|
[35] | 63 | if (fore != 255) { |
---|
| 64 | previous &= 0xF; |
---|
| 65 | previous |= fore << 4; |
---|
| 66 | } |
---|
| 67 | if (back != 255) { |
---|
| 68 | previous &= 0xF0; |
---|
| 69 | previous |= back; |
---|
| 70 | } |
---|
| 71 | |
---|
| 72 | // Make sure the last pixel of this GPL and the first of the next GPL |
---|
| 73 | // are both FORME or both FOND, else we get an ugly glitch on the |
---|
| 74 | // EFGJ033 Gate Array MO5! |
---|
[49] | 75 | val = tmpBuffer[(y*320+x)/8]; |
---|
| 76 | if(fixup > 0 && ((oldlfo == !(val & 0x80) && val != 0) || val == 0xFF)) |
---|
[35] | 77 | { |
---|
[49] | 78 | previous = 7 | (previous << 4); |
---|
[35] | 79 | tmpBuffer[(y*320+x)/8] ^= 0xFF; |
---|
| 80 | |
---|
| 81 | lfo = !lfo; |
---|
| 82 | } |
---|
| 83 | |
---|
[49] | 84 | // TO8 mode |
---|
| 85 | if(to) |
---|
| 86 | { |
---|
| 87 | previous = (previous & 0x7) | ((previous & 0xF0) >> 1) | ((previous & 0x8) << 4); |
---|
| 88 | previous ^= 0xC0; |
---|
| 89 | } |
---|
| 90 | |
---|
[35] | 91 | tmpBuffer[0x2000+(y*320+x)/8] = previous; |
---|
[20] | 92 | } |
---|
| 93 | |
---|
| 94 | return tmpBuffer; |
---|
| 95 | } |
---|
[68] | 96 | |
---|
| 97 | |
---|
| 98 | unsigned char *raw2bm16(unsigned char *input, int height) |
---|
| 99 | { |
---|
| 100 | unsigned char *tmpBuffer; |
---|
| 101 | int x,y; |
---|
| 102 | int p1 = 0; |
---|
| 103 | int p2 = 0x2000; |
---|
| 104 | static const int width = 160; |
---|
| 105 | |
---|
| 106 | tmpBuffer = (unsigned char*)calloc(0x4000,1); |
---|
| 107 | if (tmpBuffer == NULL) |
---|
| 108 | { |
---|
| 109 | printf("Allocation tmpBuffer raté\n"); |
---|
| 110 | exit(4); |
---|
| 111 | } |
---|
| 112 | |
---|
| 113 | for (y = 0; y < height; y++) |
---|
| 114 | for (x = 0; x < width; x+=4) { |
---|
| 115 | int pix; |
---|
| 116 | |
---|
| 117 | for(pix = 0; pix < 4; pix++) { |
---|
| 118 | int nc = input[y*width+x+pix]; |
---|
| 119 | if (nc > 15) printf("Color over limit!\n"); |
---|
| 120 | |
---|
| 121 | switch(pix) { |
---|
| 122 | case 0: |
---|
| 123 | tmpBuffer[p1] = nc << 4; |
---|
| 124 | break; |
---|
| 125 | case 1: |
---|
| 126 | tmpBuffer[p1++] |= nc; |
---|
| 127 | break; |
---|
| 128 | case 2: |
---|
| 129 | tmpBuffer[p2] = nc << 4; |
---|
| 130 | break; |
---|
| 131 | case 3: |
---|
| 132 | tmpBuffer[p2++] |= nc; |
---|
| 133 | break; |
---|
| 134 | } |
---|
| 135 | } |
---|
| 136 | } |
---|
| 137 | |
---|
| 138 | return tmpBuffer; |
---|
| 139 | } |
---|