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 |
|
|
int psiconv_parse_paint_data_section(const psiconv_buffer buf,int lev, |
27 |
|
|
psiconv_u32 off, int *length, |
28 |
|
|
psiconv_paint_data_section *result) |
29 |
|
|
{ |
30 |
|
|
int res = 0; |
31 |
|
|
int len = 0; |
32 |
frodo |
11 |
psiconv_u32 size,offset,picsize,temp,datasize,pixelnr,datanr; |
33 |
|
|
psiconv_u8 marker; |
34 |
|
|
int i; |
35 |
frodo |
10 |
|
36 |
|
|
psiconv_progress(lev+1,off,"Going to read a paint data section"); |
37 |
|
|
(*result) = malloc(sizeof(**result)); |
38 |
|
|
|
39 |
|
|
psiconv_progress(lev+2,off+len,"Going to read section size"); |
40 |
|
|
size = psiconv_read_u32(buf,lev+2,off+len); |
41 |
|
|
psiconv_debug(lev+2,off+len,"Section size: %08x",size); |
42 |
|
|
len += 4; |
43 |
|
|
|
44 |
|
|
psiconv_progress(lev+2,off+len,"Going to read pixel data offset"); |
45 |
|
|
offset = psiconv_read_u32(buf,lev+2,off+len); |
46 |
|
|
if (size != 0x28) { |
47 |
|
|
psiconv_warn(lev+2,off+len, |
48 |
|
|
"Paint data section data offset has unexpected value"); |
49 |
|
|
psiconv_debug(lev+2,off+len, |
50 |
|
|
"Data offset: read %08x, expected %08x",offset,0x28); |
51 |
|
|
} |
52 |
|
|
len += 4; |
53 |
|
|
|
54 |
|
|
psiconv_progress(lev+2,off+len,"Going to read picture X size"); |
55 |
|
|
(*result)->xsize = psiconv_read_u32(buf,lev+2,off+len); |
56 |
|
|
psiconv_debug(lev+2,off+len,"Picture X size: %08x:",(*result)->xsize); |
57 |
|
|
len += 4; |
58 |
|
|
|
59 |
|
|
psiconv_progress(lev+2,off+len,"Going to read picture Y size"); |
60 |
|
|
(*result)->ysize = psiconv_read_u32(buf,lev+2,off+len); |
61 |
|
|
psiconv_debug(lev+2,off+len,"Picture Y size: %08x:",(*result)->ysize); |
62 |
|
|
len += 4; |
63 |
|
|
|
64 |
|
|
picsize = (*result)->ysize * (*result)->xsize; |
65 |
|
|
|
66 |
frodo |
11 |
psiconv_progress(lev+2,off+len,"Going to read 6 unused longs"); |
67 |
frodo |
10 |
for (i = 0 ; i < 6; i++) { |
68 |
|
|
temp = psiconv_read_u32(buf,lev+2,off+len); |
69 |
|
|
if (temp != 0x00) { |
70 |
|
|
psiconv_warn(lev+2,off+len, |
71 |
|
|
"Paint data section prologue has unknown values"); |
72 |
|
|
psiconv_debug(lev+2,off+len, |
73 |
|
|
"offset %02x: read %08x, expected %08x",i,temp,0x00); |
74 |
|
|
} |
75 |
|
|
len += 4; |
76 |
|
|
} |
77 |
|
|
|
78 |
|
|
(*result)->red = malloc(sizeof(float) * picsize); |
79 |
|
|
(*result)->green = malloc(sizeof(float) * picsize); |
80 |
|
|
(*result)->blue = malloc(sizeof(float) * picsize); |
81 |
|
|
len = offset; |
82 |
|
|
datasize = size - len; |
83 |
|
|
|
84 |
frodo |
11 |
psiconv_progress(lev+2,off+len,"Going to read the pixel data"); |
85 |
frodo |
10 |
pixelnr = 0; |
86 |
|
|
datanr = 0; |
87 |
|
|
while ((datanr < datasize) && (pixelnr < picsize)) { |
88 |
|
|
marker = psiconv_read_u8(buf,lev+3,off+len+datanr); |
89 |
|
|
psiconv_debug(lev+3,off+len+datanr, |
90 |
|
|
"Pixelnr %08x, Datanr %08x: Read marker %02x", |
91 |
|
|
pixelnr,datanr,marker); |
92 |
|
|
datanr ++; |
93 |
|
|
if (marker >= 0x80) { |
94 |
|
|
/* 0x100 - marker bytes of data follow */ |
95 |
|
|
if ((0x100 - marker + datanr > datasize) || |
96 |
|
|
((0x100 - marker) * 4 + pixelnr > picsize)) { |
97 |
|
|
psiconv_warn(lev+3,off+len+datanr,"Corrupted picture data"); |
98 |
|
|
psiconv_debug(lev+3,off+len+datanr, |
99 |
|
|
"Picsize: %08x, Datasize: %08x, Pixelnr: %08x," |
100 |
|
|
"Datanr: %08x, marker: %02x",picsize,datasize,pixelnr, |
101 |
|
|
datanr,marker); |
102 |
|
|
} else { |
103 |
|
|
for (i = 0; i < 0x100-marker; i++) { |
104 |
|
|
temp = psiconv_read_u8(buf,lev+3,off+len+datanr+i); |
105 |
|
|
(*result)->red[pixelnr + i*4] = |
106 |
|
|
(*result)->green[pixelnr + i*4] = |
107 |
|
|
(*result)->blue[pixelnr + i*4] = |
108 |
|
|
(temp & 0x03) * (1.0/3.0); |
109 |
|
|
(*result)->red[pixelnr + i*4 + 1] = |
110 |
|
|
(*result)->green[pixelnr + i*4 +1] = |
111 |
|
|
(*result)->blue[pixelnr + i*4 + 1] = |
112 |
|
|
((temp >> 2) & 0x03) * (1.0/3.0); |
113 |
|
|
(*result)->red[pixelnr + i*4 + 2] = |
114 |
|
|
(*result)->green[pixelnr + i*4 +2] = |
115 |
|
|
(*result)->blue[pixelnr + i*4 + 2] = |
116 |
|
|
((temp >> 4) & 0x03) * (1.0/3.0); |
117 |
|
|
(*result)->red[pixelnr + i*4 + 3] = |
118 |
|
|
(*result)->green[pixelnr + i*4 +3] = |
119 |
|
|
(*result)->blue[pixelnr + i*4 + 3] = |
120 |
|
|
((temp >> 6) & 0x03) * (1.0/3.0); |
121 |
|
|
} |
122 |
|
|
} |
123 |
|
|
pixelnr += (0x100 - marker) * 4; |
124 |
|
|
datanr += (0x100 - marker); |
125 |
|
|
} else { /* marker < 0x080 */ |
126 |
|
|
if ((datanr + 1 > datasize) || |
127 |
|
|
(marker * 4 + pixelnr > picsize)) { |
128 |
|
|
psiconv_warn(lev+3,off+len+datanr,"Corrupted picture data"); |
129 |
|
|
psiconv_debug(lev+3,off+len+datanr, |
130 |
|
|
"Picsize: %08x, Datasize: %08x, Pixelnr: %08x," |
131 |
|
|
"Datanr: %08x, marker: %02x",picsize,datasize,pixelnr, |
132 |
|
|
datanr,marker); |
133 |
|
|
} else { |
134 |
|
|
temp = psiconv_read_u8(buf,lev+3,off+len+datanr); |
135 |
|
|
for (i = 0; i < marker; i++) { |
136 |
|
|
(*result)->red[pixelnr + i*4] = |
137 |
|
|
(*result)->green[pixelnr + i*4] = |
138 |
|
|
(*result)->blue[pixelnr + i*4] = |
139 |
|
|
(temp & 0x03) * (1.0/3.0); |
140 |
|
|
(*result)->red[pixelnr + i*4 + 1] = |
141 |
|
|
(*result)->green[pixelnr + i*4 +1] = |
142 |
|
|
(*result)->blue[pixelnr + i*4 + 1] = |
143 |
|
|
((temp >> 2) & 0x03) * (1.0/3.0); |
144 |
|
|
(*result)->red[pixelnr + i*4 + 2] = |
145 |
|
|
(*result)->green[pixelnr + i*4 +2] = |
146 |
|
|
(*result)->blue[pixelnr + i*4 + 2] = |
147 |
|
|
((temp >> 4) & 0x03) * (1.0/3.0); |
148 |
|
|
(*result)->red[pixelnr + i*4 + 3] = |
149 |
|
|
(*result)->green[pixelnr + i*4 +3] = |
150 |
|
|
(*result)->blue[pixelnr + i*4 + 3] = |
151 |
|
|
((temp >> 6) & 0x03) * (1.0/3.0); |
152 |
|
|
} |
153 |
|
|
} |
154 |
|
|
pixelnr += marker * 4; |
155 |
|
|
datanr += 1; |
156 |
|
|
} |
157 |
|
|
} |
158 |
|
|
len += datanr; |
159 |
|
|
|
160 |
|
|
if (length) |
161 |
|
|
*length = len; |
162 |
|
|
|
163 |
|
|
psiconv_progress(lev+1,off+len-1,"End of paint data section " |
164 |
|
|
"(total length: %08x", len); |
165 |
|
|
|
166 |
|
|
return res; |
167 |
|
|
} |
168 |
|
|
|
169 |
|
|
|