forked from catmirrors/xlvns
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lf2.c
127 lines (110 loc) · 2.71 KB
/
lf2.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
/*
* LEAF Visual Novel System For X
* (c) Copyright 1999,2000 Go Watanabe mailto:[email protected]
* All rights reserverd.
*
* ORIGINAL LVNS (c) Copyright 1996-1999 LEAF/AQUAPLUS Inc.
*
* $Id: lf2.c,v 1.8 2000/05/10 17:32:23 go Exp $
*
*/
/*
* $Id: lf2.c,v 1.8 2000/05/10 17:32:23 go Exp $
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "lvnsimage.h"
#include "leafpack.h"
LvnsImage *
lvnsimage_load_lf2(const u_char *data, LvnsImage *over)
{
LvnsImage *img;
int width, height, xoffset, yoffset;
size_t size;
/* check magic number 'LEAFCODE' */
if (strncmp(data, "LEAF256\0", 8)){
fprintf(stderr, "This file isn't LF2 format.\n");
return NULL;
}
xoffset = (data[9] << 8) | data[8];
yoffset = (data[11] << 8) | data[10];
width = ((data[13] << 8) | data[12]) + xoffset;
height = ((data[15] << 8) | data[14]) + yoffset;
if (over) {
img = over;
img->width = width;
img->height = height;
img->xoffset = xoffset;
img->yoffset = yoffset;
img->rwidth = width - xoffset;
img->rheight = height - yoffset;
} else {
img = lvnsimage_new(width, height, xoffset, yoffset);
}
size = img->rwidth * img->rheight;
img->transparent = data[0x12];
img->palette_num = data[0x16];
/* read palette */
{
int i;
const u_char *p = data + 0x18;
for (i=0; i<img->palette_num;i++) {
img->palette[i][2] = *p++;
img->palette[i][1] = *p++;
img->palette[i][0] = *p++;
}
}
{
int ring[0x1000];
int i, j;
int c, m;
int flag;
int upper, lower;
int pos, len;
int d;
int idx;
int p;
p = 0x18 + img->palette_num * 3;
/* initialize ring buffer */
for (i = 0; i < 0xfff; i++){
ring[i] = 0;
}
/* extract data */
for (i = 0, c = 0, m = 0xfee; i < size;){
/* flag bits, which indicates data or location */
if (--c < 0) {
flag = data[p++];
flag ^= 0xff;
c = 7;
}
if(flag & 0x80){
/* data */
d = data[p++];
d ^= 0xff;
ring[m++] = d;
idx = (i % img->rwidth) + img->rwidth * (img->rheight - i / img->rwidth - 1);
img->data[idx] = d;
i++;
m &= 0xfff;
} else {
/* copy from ring buffer */
upper = data[p++];
upper ^= 0xff;
lower = data[p++];
lower ^= 0xff;
len = (upper & 0x0f) + 3;
pos = (upper >> 4) + (lower << 4);
for(j = 0; j < len && i < size; j++){
idx = (i % img->rwidth) + img->rwidth * (img->rheight - i / img->rwidth - 1);
ring[m++] = img->data[idx] = ring[pos++];
i++;
m &= 0xfff;
pos &= 0xfff;
}
}
flag = flag << 1;
}
}
return img;
}