/[public]/psiconv/trunk/lib/psiconv/parse_image.c
ViewVC logotype

Contents of /psiconv/trunk/lib/psiconv/parse_image.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 10 - (show annotations)
Mon Oct 11 15:06:57 1999 UTC (20 years, 10 months ago) by frodo
File MIME type: text/plain
File size: 6486 byte(s)
(Frodo) First stap at image parsing. Won't compile yet - too bad

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

frodo@frodo.looijaard.name
ViewVC Help
Powered by ViewVC 1.1.26