-
Notifications
You must be signed in to change notification settings - Fork 0
/
tm2alphafixer.cpp
84 lines (65 loc) · 1.77 KB
/
tm2alphafixer.cpp
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
/*
This file is a part of the
Amagami SS PS2 Translation Project
You may feel free to use this code if you want so.
We provide no guarantee that this code is working,
useful or anything else. Use it at your own risk.
*/
#include "tm2alphafixer.h"
AlphaFixer::AlphaFixer() : Module("AlphaFixer")
{
}
int AlphaFixer::fixalpha(QString inputPath, int mode)
{
FILE *infile;
char buf[4];
unsigned int clut[256];
unsigned int temp;
int pos, i;
infile = fopen(inputPath.toAscii(), "rb+");
if(infile == NULL)
{
printf("\nFailed to open TM2 image file.\n");
return 1;
}
fread(buf, 4, 1, infile);
if(memcmp(buf, "TIM2", 4)!= 0)
{
printf("\nThis is not a TM2 image\n");
return 1;
}
fseek(infile, 0, SEEK_END);
pos = ftell(infile) - 1024;
fseek(infile, pos, SEEK_SET);
fread(clut, 1024, 1, infile);
if(mode == 1) //from 0-255 to 0-128
{
printf("\nDownsampling TM2 image...\n");
for(i = 0; i < 256; i++)
{
temp = (((clut[i] & 0xFF000000) >> 24) + 1) >> 1;
clut[i] = (clut[i] & 0x00FFFFFF) | (temp << 24);
}
}
else if(mode == 2) //from 0-128 to 0-255
{
printf("\nUpsampling TM2 image...\n");
for(i = 0; i < 256; i++)
{
temp = (((clut[i] & 0xFF000000) >> 24) + 0) << 1;
if (temp > 255)
{
temp = 255;
}
clut[i] = (clut[i] & 0x00FFFFFF) | (temp << 24);
}
}
else
{
fatalExit("Mode is set wrongly.");
}
fseek(infile, pos, SEEK_SET);
fwrite(clut, 1024, 1, infile);
fclose(infile);
return 0;
}