-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmpp2bmp.c
194 lines (178 loc) · 5.26 KB
/
mpp2bmp.c
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
/* -------------------------------------------------------------------
MPP to BMP file converter.
by Zerkman / Sector One
------------------------------------------------------------------- */
/* Copyright (c) 2012-2025 Francois Galea <fgalea at free.fr>
* This program is free software. It comes without any warranty, to
* the extent permitted by applicable law. You can redistribute it
* and/or modify it under the terms of the Do What The Fuck You Want
* To Public License, Version 2, as published by Sam Hocevar. See
* the COPYING file or http://www.wtfpl.net/ for more details. */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "pixbuf.h"
typedef struct {
int id;
int ncolors;
int nfixed;
int border0;
int x0;
int (*xinc)(int);
int xdelta;
int width;
int height;
} Mode;
static int xinc0(int c) { return ((c==15)?88:((c==31)?12:((c==37)?100:4))); }
static int xinc1(int c) { return (((c)&1)?16:4); }
static int xinc2(int c) { return 8; }
static int xinc3(int c) { return ((c==15)?112:((c==31)?12:((c==37)?100:4))); }
static const Mode modes[4] = {
{ 0, 54, 0, 1, 33, xinc0, 148, 320, 199 },
{ 1, 48, 0, 1, 9, xinc1, 160, 320, 199 },
{ 2, 56, 0, 1, 5, xinc2, 128, 320, 199 },
{ 3, 54, 6, 0, 69, xinc3, 160, 416, 273 },
};
static int read32(const void *ptr) {
const unsigned char *p = ptr;
return (p[0]<<24) | (p[1]<<16) | (p[2]<<8) | p[3];
}
PixBuf *decode_mpp(const Mode *mode, int ste, int extra, FILE *in) {
int bits = 3 * (3 + ste + extra);
int palette_size = (mode->ncolors-mode->nfixed)*mode->height;
int packed_palette_size = ((bits*(mode->ncolors-mode->nfixed-mode->border0*2)*mode->height+15)/8)&-2;
int extra_palette_bytes = palette_size*sizeof(Pixel) - packed_palette_size;
int image_size = mode->width/2*mode->height;
Pixel *pal = malloc(palette_size*sizeof(Pixel));
unsigned char *img = malloc(image_size);
unsigned char *p = ((unsigned char*)pal)+extra_palette_bytes;
int bitbuf = 0, bbnbits = 0;
int i;
fread(p, packed_palette_size, 1, in);
fread(img, image_size, 1, in);
for (i = 0; i < palette_size; i++) {
int x = i % mode->ncolors;
if (mode->border0 && (x == 0 || x == ((mode->ncolors-1) & -16))) {
pal[i].rgb = 0;
continue;
}
while (bbnbits < bits) {
bitbuf = (bitbuf << 8) | *p++;
bbnbits += 8;
}
bbnbits -= bits;
unsigned short c = (bitbuf >> bbnbits) & ((1<<bits)-1);
Pixel p;
p.cp.a = 0;
switch (bits) {
case 9:
p.cp.r = (c>>6);
p.cp.g = (c>>3)&7;
p.cp.b = c&7;
break;
case 12:
case 15:
p.cp.r = ((c>>7)&0xe) | ((c>>11)&1);
p.cp.g = ((c>>3)&0xe) | ((c>>7)&1);
p.cp.b = ((c<<1)&0xe) | ((c>>3)&1);
if (bits == 15) {
p.cp.r = (p.cp.r << 1) | ((c>>14)&1);
p.cp.g = (p.cp.g << 1) | ((c>>13)&1);
p.cp.b = (p.cp.b << 1) | ((c>>12)&1);
}
break;
default:
abort();
}
p.cp.r <<= 8 - (bits/3);
p.cp.g <<= 8 - (bits/3);
p.cp.b <<= 8 - (bits/3);
pal[i] = p;
}
PixBuf *pix = pixbuf_new(mode->width, mode->height);
memset(pix->array, 0, mode->width * mode->height * sizeof(Pixel));
int k=0, l=0;
int x, y;
unsigned short b0=0, b1=0, b2=0, b3=0;
Pixel palette[16];
memset(palette, 0, sizeof(Pixel));
for (y=0; y<mode->height; ++y) {
Pixel *ppal = pal + y * (mode->ncolors-mode->nfixed);
int nextx = mode->x0;
int nextc = 0;
for (i=mode->nfixed; i<16; ++i)
palette[i] = *ppal++;
for (x=0; x<mode->width; ++x) {
if (x==nextx) {
palette[nextc&0xf] = *ppal++;
nextx += mode->xinc(nextc);
++nextc;
}
if ((x&0xf) == 0) {
b0 = (img[k+0]<<8) | (img[k+1]);
b1 = (img[k+2]<<8) | (img[k+3]);
b2 = (img[k+4]<<8) | (img[k+5]);
b3 = (img[k+6]<<8) | (img[k+7]);
k += 8;
}
i = ((b3>>12)&8) | ((b2>>13)&4) | ((b1>>14)&2) | ((b0>>15)&1);
pix->array[l++] = palette[i];
b0 <<= 1;
b1 <<= 1;
b2 <<= 1;
b3 <<= 1;
}
}
free(pal);
free(img);
return pix;
}
int main(int argc, char **argv)
{
if (argc < 2) {
fprintf(stderr, "usage: %s file.mpp [output.bmp]\n", argv[0]);
return 1;
}
const char *filename = argv[1];
unsigned char header[12];
FILE *in = fopen(filename, "rb");
if (!in) {
perror(filename);
return 1;
}
fread(header, 12, 1, in);
const Mode *mode = &modes[header[3]];
int doubl = (header[4]&4) >> 2;
int extra = (header[4]&2) >> 1;
int ste = header[4]&1;
int skip = read32(header+8);
if (skip)
fseek(in, skip, SEEK_CUR);
PixBuf *pix = decode_mpp(mode, ste, extra, in);
if (doubl) {
PixBuf *pix2 = decode_mpp(mode, ste, extra, in);
int k;
for (k = 0; k < mode->width*mode->height; ++k) {
Pixel a = pix->array[k], b = pix2->array[k];
a.cp.r = ((int)a.cp.r + b.cp.r)/2;
a.cp.g = ((int)a.cp.g + b.cp.g)/2;
a.cp.b = ((int)a.cp.b + b.cp.b)/2;
pix->array[k] = a;
}
pixbuf_delete(pix2);
}
fclose(in);
if (argc > 2)
pixbuf_export_bmp(pix, argv[2]);
else {
char buf[256];
char *dot = strrchr(argv[1], '.');
int len;
len = dot ? (dot-argv[1]) : strlen(argv[1]);
strncpy(buf, argv[1], len);
strcpy(buf+len, ".bmp");
pixbuf_export_bmp(pix, buf);
}
pixbuf_delete(pix);
return 0;
}