]> git.draconx.ca Git - upkg.git/blob - src/engine/texture.gob
3db176e045982b31de94268bed9031a7f63cf889
[upkg.git] / src / engine / texture.gob
1 %alltop{
2 /*
3  *  upkg: tool for manipulating Unreal Tournament packages.
4  *  Copyright © 2009-2011 Nick Bowler
5  *
6  *  This program is free software: you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation, either version 3 of the License, or
9  *  (at your option) any later version.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19 %}
20
21 %{
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <uobject/loadable.h>
26 #include <uobject/exportable.h>
27 #include <engine/palette.h>
28 #include "pack.h"
29 #include "pcx.h"
30 %}
31
32 %h{
33 #include <uobject/uobject.h>
34
35 struct engine_texture_data {
36         unsigned long width, height;
37
38         unsigned long datalen;
39         unsigned char data[];
40 };
41
42 %}
43
44 %{
45 struct engine_texture_data *decode_mipmap(UObject *uo)
46 {
47         struct upkg_file *f = uo->pkg_file;
48         struct engine_texture_data *data;
49         size_t rc, pos = 0, buflen;
50         unsigned long end_offset;
51         unsigned char buf[32];
52         long datalen;
53
54         buflen = upkg_export_read(f, buf, sizeof buf);
55         if (uo->pkg->version >= 63) {
56                 /*
57                  * There's an offset to the end of the image data here; not
58                  * clear why it's useful since it's implied by the very next
59                  * field anyway.  However, we will nevertheless validate this
60                  * property below.
61                  */
62                 if (pos + 4 > buflen)
63                         return NULL;
64                 end_offset = unpack_32_le(buf);
65                 pos += 4;
66         }
67
68         rc = upkg_decode_index(&datalen, buf+pos, buflen-pos);
69         if (rc == 0 || datalen < 0)
70                 return NULL;
71         pos += rc;
72
73         data = malloc(sizeof *data + datalen);
74         if (!data)
75                 return NULL;
76         data->datalen = datalen;
77
78         /* Rewind to the start of the image data, and slurp it in. */
79         if (upkg_export_seek(f, -(long)(buflen-pos), SEEK_CUR) != 0)
80                 goto err_free;
81         rc = upkg_export_read(f, data->data, data->datalen);
82         if (rc != data->datalen)
83                 goto err_free;
84
85         /* At this point, the current file offset should match the one recorded
86          * above. */
87         if (uo->pkg->version >= 63 && end_offset != (f->base + f->offset))
88                 goto err_free;
89
90         /* Read in the remaining fields */
91         buflen = upkg_export_read(f, buf, 10);
92         if (buflen < 10)
93                 goto err_free;
94
95         data->width  = unpack_32_le(buf+0);
96         data->height = unpack_32_le(buf+4);
97         /* Last 2 bytes seem to be simply the base-2 log of width/height for
98          * whatever reason.  Ignore them for now. */
99
100         return data;
101 err_free:
102         free(data);
103         return NULL;
104 }
105 %}
106
107 class Engine:Texture from U:Object (dynamic)
108         (interface U:Object:Exportable)
109         (interface U:Object:Loadable)
110 {
111         private unsigned USize;
112         private unsigned UClamp;
113         private unsigned UBits;
114         private unsigned VSize;
115         private unsigned VClamp;
116         private unsigned VBits;
117
118         private Engine:Palette *Palette;
119
120         private struct engine_texture_data **mipmap_data;
121         private unsigned char mipmap_count;
122
123         interface U:Object:Loadable
124         private int load(U:Object *uo)
125         {
126                 struct engine_texture_data **data;
127                 Self *self = SELF(uo);
128
129                 if (upkg_export_seek(uo->pkg_file, 0, SEEK_SET) != 0)
130                         return -1;
131
132                 data = malloc(self->_priv->mipmap_count * sizeof *data);
133                 if (!data)
134                         return -1;
135
136                 for (int i = 0; i < self->_priv->mipmap_count; i++) {
137                         data[i] = decode_mipmap(uo);
138                         if (!data[i]) {
139                                 /* Unwind the allocations. */
140                                 for (; i >= 0; i--)
141                                         free(data[i]);
142                                 free(data);
143                                 return -1;
144                         }
145                 }
146
147                 self->_priv->mipmap_data = data;
148                 return 0;
149         }
150
151         interface U:Object:Loadable
152         private void unload(U:Object *uo)
153         {
154                 Self *self = SELF(uo);
155
156                 for (int i = 0; i < self->_priv->mipmap_count; i++)
157                         free(self->_priv->mipmap_data[i]);
158                 free(self->_priv->mipmap_data);
159                 self->_priv->mipmap_data = NULL;
160         }
161
162         interface U:Object:Exportable
163         private int export_name(U:Object *uo, char *buf, size_t n)
164         {
165                 return snprintf(buf, n, "%s.pcx", uo->pkg_file->name);
166         }
167
168         interface U:Object:Exportable
169         private int export(U:Object *uo, FILE *f)
170         {
171                 Self *self = SELF(uo);
172                 struct engine_texture_data *data;
173                 struct pcx_head head;
174
175                 if (!self->_priv->mipmap_data || !self->_priv->Palette)
176                         return -1;
177                 data = self->_priv->mipmap_data[0];
178
179                 head.width  = data->width;
180                 head.height = data->height;
181
182                 if (pcx_init_header(&head) != 0)
183                         return -1;
184                 if (fwrite(head.encoded, sizeof head.encoded, 1, f) != 1)
185                         return -1;
186
187                 for (unsigned i = 0; i < head.height; i++) {
188                         if (pcx_write_scanline(&head, data->data+i*data->width,
189                                                f) != 0) {
190                                 return -1;
191                         }
192                 }
193
194                 if (pcx_write_palette(self->_priv->Palette, f) != 0)
195                         return -1;
196
197                 return 0;
198         }
199
200         override (U:Object) int deserialize(U:Object *uo)
201         {
202                 struct upkg_file *f = uo->pkg_file;
203                 Self *self = SELF(uo);
204
205                 PARENT_HANDLER(uo);
206
207                 if (upkg_export_read(f, &self->_priv->mipmap_count, 1) != 1)
208                         return -1;
209                 if (self->_priv->mipmap_count == 0)
210                         return -1;
211
212                 f->base += 1;
213                 f->len  -= 1;
214                 upkg_export_seek(f, 0, SEEK_SET);
215
216                 return 0;
217         }
218
219         property UINT USize
220                 ( nick = "USize"
221                 , blurb = "Width of the texture."
222                 , minimum = 0
223                 , maximum = 2048
224                 , default_value = 0
225                 , link
226                 );
227         property UINT UClamp
228                 ( nick = "UClamp"
229                 , blurb = "???"
230                 , minimum = 0
231                 , maximum = 2048
232                 , default_value = 0
233                 , link
234                 );
235         property UINT UBits
236                 ( nick = "UBits"
237                 , blurb = "???"
238                 , minimum = 0
239                 , maximum = 64
240                 , default_value = 0
241                 , link
242                 );
243         property UINT VSize
244                 ( nick = "VSize"
245                 , blurb = "Height of the texture."
246                 , minimum = 0
247                 , maximum = 2048
248                 , default_value = 0
249                 , link
250                 );
251         property UINT VClamp
252                 ( nick = "VClamp"
253                 , blurb = "???"
254                 , minimum = 0
255                 , maximum = 2048
256                 , default_value = 0
257                 , link
258                 );
259         property UINT VBits
260                 ( nick = "VBits"
261                 , blurb = "???"
262                 , minimum = 0
263                 , maximum = 64
264                 , default_value = 0
265                 , link
266                 );
267
268         property OBJECT Palette
269                 ( nick = "Palette"
270                 , blurb = "Reference to the texture's palette."
271                 , object_type = Engine:Palette
272                 , link
273                 );
274 }