/
scc_char.c
132 lines (119 loc) · 3.21 KB
/
scc_char.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
/* ScummC
* Copyright (C) 2006 Alban Bedel
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/
/**
* @file scc_char.c
* @ingroup scumm
* @brief SCUMM charset parser
*/
#include "config.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <inttypes.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include "scc_util.h"
#include "scc_fd.h"
#include "scc_char.h"
scc_charmap_t* scc_parse_charmap(scc_fd_t* fd, unsigned size) {
uint32_t size2,unk,bpp,height,num_char;
uint8_t pal[15],byte,mask,bits,step;
off_t font_ptr;
uint32_t* char_off,x,i;
scc_charmap_t* chmap;
scc_char_t *ch;
if(size < 25 ) {
scc_log(LOG_ERR,"CHAR block is too small.\n");
return NULL;
}
// read the charset header
size2 = scc_fd_r32le(fd);
if(size2 != size-15)
scc_log(LOG_WARN,"Warning size2 is invalid: %d != %d\n",size2,size-15);
unk = scc_fd_r16le(fd);
if(scc_fd_read(fd,pal,15) != 15) {
scc_log(LOG_ERR,"Failed to read CHAR palette.\n");
return NULL;
}
font_ptr = scc_fd_pos(fd);
bpp = scc_fd_r8(fd);
switch(bpp) {
case 1:
mask = 0x1;
break;
case 2:
mask = 0x3;
break;
case 4:
mask = 0xF;
break;
default:
scc_log(LOG_ERR,"Invalid CHAR bpp: %d\n",bpp);
return NULL;
}
height = scc_fd_r8(fd);
if(!height) {
scc_log(LOG_ERR,"Charset have 0 height ??\n");
return NULL;
}
num_char = scc_fd_r16le(fd);
if(!num_char) {
scc_log(LOG_ERR,"Charset have no char ???\n");
return NULL;
}
if(size < 25 + 4*num_char) {
scc_log(LOG_ERR,"Charset is too small.\n");
return NULL;
}
// read the offset table
char_off = malloc(num_char*4);
for(i = 0 ; i < num_char ; i++)
char_off[i] = scc_fd_r32le(fd);
chmap = calloc(1,sizeof(scc_charmap_t));
chmap->bpp = bpp;
chmap->height = height;
// read the chars
scc_log(LOG_V,"Reading %d chars\n",num_char);
for(i = 0 ; i < num_char ; i++) {
if(!char_off[i]) continue;
scc_fd_seek(fd,font_ptr + char_off[i],SEEK_SET);
ch = &chmap->chars[i];
ch->w = scc_fd_r8(fd);
ch->h = scc_fd_r8(fd);
ch->x = scc_fd_r8(fd);
ch->y = scc_fd_r8(fd);
if(ch->w + ch->x > chmap->width) chmap->width = ch->w + ch->x;
if(!ch->w || !ch->h) continue;
ch->data = malloc(ch->w*ch->h);
bits = 0;
step = 8/bpp;
x = 0;
while(x < ch->w*ch->h) {
if(!bits) {
byte = scc_fd_r8(fd);
bits = 8;
}
ch->data[x] = (byte >> (8-bpp)) & mask;
byte <<= bpp;
bits -= bpp;
x++;
}
chmap->max_char = i;
}
return chmap;
}