]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - plugins/imageq2/wal32.cpp
Merge branch 'transfilterfix' into 'master'
[xonotic/netradiant.git] / plugins / imageq2 / wal32.cpp
1 /*
2    Copyright (C) 2001-2006, William Joseph.
3    All Rights Reserved.
4
5    This file is part of GtkRadiant.
6
7    GtkRadiant is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11
12    GtkRadiant is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with GtkRadiant; if not, write to the Free Software
19    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
20  */
21
22 #include "wal32.h"
23
24 #include "ifilesystem.h"
25
26 #include "bytestreamutils.h"
27 #include "imagelib.h"
28
29 typedef unsigned char byte;
30
31 const int M32_NAME_LENGTH = 128;
32 const int M32_MIPMAP_COUNT = 16;
33
34 typedef struct m32_header_t {
35     int version;
36     char name[M32_NAME_LENGTH];
37     char altname[M32_NAME_LENGTH];                      // texture substitution
38     char animname[M32_NAME_LENGTH];                     // next frame in animation chain
39     char damagename[M32_NAME_LENGTH];                   // image that should be shown when damaged
40     unsigned width[M32_MIPMAP_COUNT], height[M32_MIPMAP_COUNT];
41     unsigned offsets[M32_MIPMAP_COUNT];
42     int flags;
43     int contents;
44     int value;
45     float scale_x, scale_y;
46     int mip_scale;
47
48     // detail texturing info
49     char dt_name[M32_NAME_LENGTH];              // detailed texture name
50     float dt_scale_x, dt_scale_y;
51     float dt_u, dt_v;
52     float dt_alpha;
53     int dt_src_blend_mode, dt_dst_blend_mode;
54
55     int unused[20];                         // future expansion to maintain compatibility with h2
56 } m32_header_t;
57
58
59 Image *LoadM32Buff(byte *buffer)
60 {
61     PointerInputStream inputStream(buffer);
62
63     inputStream.seek(4 // version
64                      + M32_NAME_LENGTH // name
65                      + M32_NAME_LENGTH // altname
66                      + M32_NAME_LENGTH // animname
67                      + M32_NAME_LENGTH); // damagename
68     int w = istream_read_uint32_le(inputStream);
69     inputStream.seek(4 * (M32_MIPMAP_COUNT - 1)); // remaining widths
70     int h = istream_read_uint32_le(inputStream);
71     inputStream.seek(4 * (M32_MIPMAP_COUNT - 1)); // remaining heights
72     int offset = istream_read_uint32_le(inputStream);
73     inputStream.seek(4 * (M32_MIPMAP_COUNT - 1)); // remaining offsets
74     int flags = istream_read_uint32_le(inputStream);
75     int contents = istream_read_uint32_le(inputStream);
76     int value = istream_read_uint32_le(inputStream);
77
78     RGBAImageFlags *image = new RGBAImageFlags(w, h, flags, contents, value);
79
80     const byte *source = buffer + offset;
81     std::copy(source, source + (w * h * 4), image->getRGBAPixels());
82
83     return image;
84 }
85
86 Image *LoadM32(ArchiveFile &file)
87 {
88     ScopedArchiveBuffer buffer(file);
89     return LoadM32Buff(buffer.buffer);
90 }