1 |
frodo |
10 |
/* |
2 |
|
|
parse_image.c - Part of psiconv, a PSION 5 file formats converter |
3 |
|
|
Copyright (c) 1999 Frodo Looijaard <frodol@dds.nl> |
4 |
|
|
|
5 |
|
|
This program is free software; you can redistribute it and/or modify |
6 |
|
|
it under the terms of the GNU General Public License as published by |
7 |
|
|
the Free Software Foundation; either version 2 of the License, or |
8 |
|
|
(at your option) any later version. |
9 |
|
|
|
10 |
|
|
This program is distributed in the hope that it will be useful, |
11 |
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 |
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13 |
|
|
GNU General Public License for more details. |
14 |
|
|
|
15 |
|
|
You should have received a copy of the GNU General Public License |
16 |
|
|
along with this program; if not, write to the Free Software |
17 |
|
|
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
18 |
|
|
*/ |
19 |
|
|
|
20 |
|
|
#include "config.h" |
21 |
|
|
#include <stdlib.h> |
22 |
|
|
|
23 |
|
|
#include "data.h" |
24 |
|
|
#include "parse_routines.h" |
25 |
|
|
|
26 |
frodo |
12 |
int psiconv_parse_mbm_jumptable_section(const psiconv_buffer buf,int lev, |
27 |
|
|
psiconv_u32 off, int *length, |
28 |
|
|
psiconv_mbm_jumptable_section *result) |
29 |
|
|
{ |
30 |
|
|
int res = 0; |
31 |
|
|
int len = 0; |
32 |
|
|
psiconv_u32 listlen,temp; |
33 |
|
|
int i; |
34 |
|
|
|
35 |
|
|
psiconv_progress(lev+1,off+len,"Going to read the mbm jumptable section"); |
36 |
|
|
(*result) = psiconv_list_new(sizeof(psiconv_u32)); |
37 |
|
|
|
38 |
|
|
psiconv_progress(lev+2,off+len,"Going to read the list length"); |
39 |
|
|
listlen = psiconv_read_u32(buf,lev+2,off+len); |
40 |
|
|
psiconv_debug(lev+2,off+len,"List length: %08x",listlen); |
41 |
|
|
len += 4; |
42 |
|
|
|
43 |
|
|
psiconv_progress(lev+2,off+len,"Going to read the list"); |
44 |
|
|
for (i = 0; i < listlen; i++) { |
45 |
|
|
temp = psiconv_read_u32(buf,lev+2,off+len); |
46 |
|
|
psiconv_list_add(*result,&temp); |
47 |
|
|
psiconv_debug(lev+3,off+len,"Offset: %08x",temp); |
48 |
|
|
len += 4; |
49 |
|
|
} |
50 |
|
|
|
51 |
|
|
if (length) |
52 |
|
|
*length = len; |
53 |
|
|
|
54 |
|
|
psiconv_progress(lev+1,off+len-1,"End of mbm jumptable section " |
55 |
frodo |
13 |
"(total length: %08x)", len); |
56 |
frodo |
12 |
|
57 |
|
|
return res; |
58 |
|
|
} |
59 |
|
|
|
60 |
frodo |
10 |
int psiconv_parse_paint_data_section(const psiconv_buffer buf,int lev, |
61 |
|
|
psiconv_u32 off, int *length, |
62 |
|
|
psiconv_paint_data_section *result) |
63 |
|
|
{ |
64 |
|
|
int res = 0; |
65 |
|
|
int len = 0; |
66 |
frodo |
12 |
int read_err = 0; |
67 |
frodo |
13 |
psiconv_u32 size,offset,picsize,temp,datasize,pixelnr,datanr,linelen; |
68 |
frodo |
11 |
psiconv_u8 marker; |
69 |
|
|
int i; |
70 |
frodo |
10 |
|
71 |
|
|
psiconv_progress(lev+1,off,"Going to read a paint data section"); |
72 |
|
|
(*result) = malloc(sizeof(**result)); |
73 |
|
|
|
74 |
|
|
psiconv_progress(lev+2,off+len,"Going to read section size"); |
75 |
|
|
size = psiconv_read_u32(buf,lev+2,off+len); |
76 |
|
|
psiconv_debug(lev+2,off+len,"Section size: %08x",size); |
77 |
|
|
len += 4; |
78 |
|
|
|
79 |
|
|
psiconv_progress(lev+2,off+len,"Going to read pixel data offset"); |
80 |
|
|
offset = psiconv_read_u32(buf,lev+2,off+len); |
81 |
frodo |
13 |
if (offset != 0x28) { |
82 |
frodo |
10 |
psiconv_warn(lev+2,off+len, |
83 |
|
|
"Paint data section data offset has unexpected value"); |
84 |
|
|
psiconv_debug(lev+2,off+len, |
85 |
|
|
"Data offset: read %08x, expected %08x",offset,0x28); |
86 |
frodo |
12 |
res = -1; |
87 |
frodo |
10 |
} |
88 |
|
|
len += 4; |
89 |
|
|
|
90 |
|
|
psiconv_progress(lev+2,off+len,"Going to read picture X size"); |
91 |
|
|
(*result)->xsize = psiconv_read_u32(buf,lev+2,off+len); |
92 |
|
|
psiconv_debug(lev+2,off+len,"Picture X size: %08x:",(*result)->xsize); |
93 |
|
|
len += 4; |
94 |
|
|
|
95 |
|
|
psiconv_progress(lev+2,off+len,"Going to read picture Y size"); |
96 |
|
|
(*result)->ysize = psiconv_read_u32(buf,lev+2,off+len); |
97 |
|
|
psiconv_debug(lev+2,off+len,"Picture Y size: %08x:",(*result)->ysize); |
98 |
|
|
len += 4; |
99 |
|
|
|
100 |
frodo |
13 |
picsize = (*result)->ysize * (*result)->xsize * 2; |
101 |
|
|
linelen = (*result)->xsize; |
102 |
frodo |
10 |
|
103 |
frodo |
11 |
psiconv_progress(lev+2,off+len,"Going to read 6 unused longs"); |
104 |
frodo |
10 |
for (i = 0 ; i < 6; i++) { |
105 |
|
|
temp = psiconv_read_u32(buf,lev+2,off+len); |
106 |
|
|
if (temp != 0x00) { |
107 |
|
|
psiconv_warn(lev+2,off+len, |
108 |
|
|
"Paint data section prologue has unknown values"); |
109 |
|
|
psiconv_debug(lev+2,off+len, |
110 |
|
|
"offset %02x: read %08x, expected %08x",i,temp,0x00); |
111 |
frodo |
12 |
res = -1; |
112 |
frodo |
10 |
} |
113 |
|
|
len += 4; |
114 |
|
|
} |
115 |
|
|
|
116 |
|
|
(*result)->red = malloc(sizeof(float) * picsize); |
117 |
|
|
(*result)->green = malloc(sizeof(float) * picsize); |
118 |
|
|
(*result)->blue = malloc(sizeof(float) * picsize); |
119 |
|
|
len = offset; |
120 |
|
|
datasize = size - len; |
121 |
|
|
|
122 |
frodo |
11 |
psiconv_progress(lev+2,off+len,"Going to read the pixel data"); |
123 |
frodo |
10 |
pixelnr = 0; |
124 |
|
|
datanr = 0; |
125 |
|
|
while ((datanr < datasize) && (pixelnr < picsize)) { |
126 |
|
|
marker = psiconv_read_u8(buf,lev+3,off+len+datanr); |
127 |
|
|
psiconv_debug(lev+3,off+len+datanr, |
128 |
|
|
"Pixelnr %08x, Datanr %08x: Read marker %02x", |
129 |
|
|
pixelnr,datanr,marker); |
130 |
|
|
datanr ++; |
131 |
|
|
if (marker >= 0x80) { |
132 |
|
|
/* 0x100 - marker bytes of data follow */ |
133 |
|
|
if ((0x100 - marker + datanr > datasize) || |
134 |
|
|
((0x100 - marker) * 4 + pixelnr > picsize)) { |
135 |
|
|
psiconv_warn(lev+3,off+len+datanr,"Corrupted picture data"); |
136 |
|
|
psiconv_debug(lev+3,off+len+datanr, |
137 |
|
|
"Picsize: %08x, Datasize: %08x, Pixelnr: %08x," |
138 |
|
|
"Datanr: %08x, marker: %02x",picsize,datasize,pixelnr, |
139 |
|
|
datanr,marker); |
140 |
frodo |
12 |
res = -1; |
141 |
|
|
read_err = 1; |
142 |
frodo |
10 |
} else { |
143 |
|
|
for (i = 0; i < 0x100-marker; i++) { |
144 |
|
|
temp = psiconv_read_u8(buf,lev+3,off+len+datanr+i); |
145 |
|
|
(*result)->red[pixelnr + i*4] = |
146 |
|
|
(*result)->green[pixelnr + i*4] = |
147 |
|
|
(*result)->blue[pixelnr + i*4] = |
148 |
|
|
(temp & 0x03) * (1.0/3.0); |
149 |
|
|
(*result)->red[pixelnr + i*4 + 1] = |
150 |
|
|
(*result)->green[pixelnr + i*4 +1] = |
151 |
|
|
(*result)->blue[pixelnr + i*4 + 1] = |
152 |
|
|
((temp >> 2) & 0x03) * (1.0/3.0); |
153 |
|
|
(*result)->red[pixelnr + i*4 + 2] = |
154 |
|
|
(*result)->green[pixelnr + i*4 +2] = |
155 |
|
|
(*result)->blue[pixelnr + i*4 + 2] = |
156 |
|
|
((temp >> 4) & 0x03) * (1.0/3.0); |
157 |
|
|
(*result)->red[pixelnr + i*4 + 3] = |
158 |
|
|
(*result)->green[pixelnr + i*4 +3] = |
159 |
|
|
(*result)->blue[pixelnr + i*4 + 3] = |
160 |
|
|
((temp >> 6) & 0x03) * (1.0/3.0); |
161 |
|
|
} |
162 |
|
|
} |
163 |
|
|
pixelnr += (0x100 - marker) * 4; |
164 |
|
|
datanr += (0x100 - marker); |
165 |
|
|
} else { /* marker < 0x080 */ |
166 |
|
|
if ((datanr + 1 > datasize) || |
167 |
|
|
(marker * 4 + pixelnr > picsize)) { |
168 |
|
|
psiconv_warn(lev+3,off+len+datanr,"Corrupted picture data"); |
169 |
|
|
psiconv_debug(lev+3,off+len+datanr, |
170 |
|
|
"Picsize: %08x, Datasize: %08x, Pixelnr: %08x," |
171 |
|
|
"Datanr: %08x, marker: %02x",picsize,datasize,pixelnr, |
172 |
|
|
datanr,marker); |
173 |
frodo |
12 |
res = -1; |
174 |
|
|
read_err = 1; |
175 |
frodo |
10 |
} else { |
176 |
|
|
temp = psiconv_read_u8(buf,lev+3,off+len+datanr); |
177 |
frodo |
13 |
for (i = 0; i < marker; i++) { |
178 |
frodo |
10 |
(*result)->red[pixelnr + i*4] = |
179 |
|
|
(*result)->green[pixelnr + i*4] = |
180 |
|
|
(*result)->blue[pixelnr + i*4] = |
181 |
|
|
(temp & 0x03) * (1.0/3.0); |
182 |
|
|
(*result)->red[pixelnr + i*4 + 1] = |
183 |
|
|
(*result)->green[pixelnr + i*4 +1] = |
184 |
|
|
(*result)->blue[pixelnr + i*4 + 1] = |
185 |
|
|
((temp >> 2) & 0x03) * (1.0/3.0); |
186 |
|
|
(*result)->red[pixelnr + i*4 + 2] = |
187 |
|
|
(*result)->green[pixelnr + i*4 +2] = |
188 |
|
|
(*result)->blue[pixelnr + i*4 + 2] = |
189 |
|
|
((temp >> 4) & 0x03) * (1.0/3.0); |
190 |
|
|
(*result)->red[pixelnr + i*4 + 3] = |
191 |
|
|
(*result)->green[pixelnr + i*4 +3] = |
192 |
|
|
(*result)->blue[pixelnr + i*4 + 3] = |
193 |
|
|
((temp >> 6) & 0x03) * (1.0/3.0); |
194 |
|
|
} |
195 |
|
|
} |
196 |
|
|
pixelnr += marker * 4; |
197 |
|
|
datanr += 1; |
198 |
|
|
} |
199 |
|
|
} |
200 |
frodo |
12 |
if (!read_err && ((datanr != datasize) || (pixelnr != picsize))) { |
201 |
|
|
psiconv_warn(lev+2,off+len,"Corrupted picture data!"); |
202 |
|
|
psiconv_debug(lev+3,off+len+datanr, |
203 |
|
|
"Picsize: %08x, Datasize: %08x, Pixelnr: %08x," |
204 |
|
|
"Datanr: %08x",picsize,datasize,pixelnr,datanr); |
205 |
|
|
res = -1; |
206 |
|
|
} |
207 |
|
|
|
208 |
frodo |
10 |
len += datanr; |
209 |
|
|
|
210 |
|
|
if (length) |
211 |
|
|
*length = len; |
212 |
|
|
|
213 |
|
|
psiconv_progress(lev+1,off+len-1,"End of paint data section " |
214 |
frodo |
13 |
"(total length: %08x)", len); |
215 |
frodo |
10 |
|
216 |
|
|
return res; |
217 |
|
|
} |
218 |
|
|
|
219 |
|
|
|