summaryrefslogtreecommitdiff
path: root/ffx-convert.cpp
blob: 0fa68187367a769fedf637b9b92ea0183b782435 (plain)
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
#include <unistd.h>
#include "generic.h"
#include "Main.h"
#include "Image.h"
#include "Input.h"
#include "Output.h"

#define IMG_SX 256
#define IMG_SY 256

#define BLOC_SX 16
#define BLOC_SY 8

CODE_BEGINS

Color LookUp(char i) {
    return Color(i << 4, i << 4, i << 4, 255);
}

int transform(int x, int y) {
    int numero_bloc_x = x / BLOC_SX;
    int numero_bloc_y = y / BLOC_SY;
    
    int numero_bloc = numero_bloc_y * (IMG_SX / BLOC_SX) + numero_bloc_x;
    
    int bx = x % BLOC_SX;
    int by = y % BLOC_SY;
    
    return numero_bloc * BLOC_SX * BLOC_SY + by * BLOC_SX + bx;    
}

virtual int startup() throw (GeneralException) {
    int c;
    
    while ((c = getopt(argc, argv, "")) != EOF) {
	switch (c) {
	default:
	    printm(M_ERROR, "Unknow option: %c\n", c);
	    throw Exit(-1);
	}
    }
    
    if ((argc - optind) != 2) {
	printm(M_ERROR, "Need two arguments\n");
	throw Exit(-1);
    }
    
    Input * map = new Input(argv[optind]);
    Output * tga = new Output(argv[optind + 1]);
    Image * img = new Image(IMG_SX, IMG_SY);
    
    map->seek(0x140);
    
    char buffer[65536];
    
    Byte b;
    
    for (int i = 0; i < 32768; i++) {
	int j = i << 1;
	map->read(&b, 1);
	buffer[j] = b & 0x0f;
	buffer[j + 1] = (b & 0xf0) >> 4;
    }
    
    img->Fill();

    for (int y = 0; y < IMG_SX; y++) {
	for (int x = 0; x < IMG_SY; x++) {
	    img->SetPixel(x, y, LookUp(buffer[transform(x, y)]));
	}
    }

    img->Prepare(FORMAT_TGA_BASIC);
    
    copy(img, tga);
    
    return 0;
}

CODE_ENDS