]> icculus.org git repositories - divverent/netradiant.git/blob - plugins/imageq2/wal.cpp
NOW I do it right: #woxblox#
[divverent/netradiant.git] / plugins / imageq2 / wal.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 "wal.h"
23
24 #include "ifilesystem.h"
25
26 #include "os/path.h"
27 #include "stream/stringstream.h"
28 #include "bytestreamutils.h"
29 #include "imagelib.h"
30
31 #define QUAKE2_WAL   0
32 #define HERETIC2_M8  1
33
34 typedef unsigned char byte;
35
36 struct pcx_header_t
37 {
38   char manufacturer;
39   char version;
40   char encoding;
41   char bits_per_pixel;
42 };
43
44 void LoadPCXPalette(const char *filename, byte palette[768])
45 {
46   byte* buffer;
47         int length = vfsLoadFile (filename, (void **)&buffer);
48   if(buffer == 0)
49                 return;
50
51   const pcx_header_t* pcx = reinterpret_cast<const pcx_header_t*>(buffer);
52
53         if (pcx->manufacturer != 0x0a
54                 || pcx->version != 5
55                 || pcx->encoding != 1
56                 || pcx->bits_per_pixel != 8)
57     return;
58
59         memcpy(palette, buffer + length - 768, 768);
60
61   vfsFreeFile(buffer);
62 }
63
64 const int WAL_NAME_LENGTH = 32;
65 const int WAL_MIPMAP_COUNT = 4;
66 struct wal_header_t
67 {
68         char      name[WAL_NAME_LENGTH];
69         unsigned  width, height;
70         unsigned  offsets[WAL_MIPMAP_COUNT];      // four mip maps stored
71         char      animname[WAL_NAME_LENGTH];    // next frame in animation chain
72         int       flags;
73         int       contents;
74         int       value;
75 };
76
77 const int M8_NAME_LENGTH = 32;
78 const int M8_MIPMAP_COUNT = 16;
79 struct m8_header_t
80 {
81   int         version;
82   char        name[M8_NAME_LENGTH];
83   unsigned    width[M8_MIPMAP_COUNT], height[M8_MIPMAP_COUNT];  // width and height of each mipmap
84   unsigned    offsets[M8_MIPMAP_COUNT];            // 16 mip maps stored
85   char        animname[M8_NAME_LENGTH];           // next frame in animation chain
86   byte        palette[768];           // palette stored in m8
87   int         flags;
88   int         contents;
89   int         value;
90 };
91
92 Image* LoadMipTex(byte* buffer, byte TypeofTex)
93 {
94   int w, h, offset, flags, contents, value;
95   byte palette[768];
96   byte* source;
97
98   PointerInputStream inputStream(buffer);
99
100   if ( TypeofTex == HERETIC2_M8 )
101   {
102     inputStream.seek(4 + M8_NAME_LENGTH); // version, name
103     w = istream_read_int32_le(inputStream);
104     inputStream.seek(4 * (M8_MIPMAP_COUNT - 1)); // remaining widths
105     h = istream_read_int32_le(inputStream);
106     inputStream.seek(4 * (M8_MIPMAP_COUNT - 1)); // remaining heights
107     offset = istream_read_int32_le(inputStream);
108     inputStream.seek(4 * (M8_MIPMAP_COUNT - 1)); // remaining offsets
109     inputStream.seek(M8_NAME_LENGTH); // animname
110     inputStream.read(palette, 768);
111     flags = istream_read_int32_le(inputStream);
112     contents = istream_read_int32_le(inputStream);
113     value = istream_read_int32_le(inputStream);
114     source = buffer + offset;
115   }
116   else
117   {
118     LoadPCXPalette("pics/colormap.pcx", palette);
119
120     inputStream.seek(WAL_NAME_LENGTH); // name
121     w = istream_read_int32_le(inputStream);
122     h = istream_read_int32_le(inputStream);
123     offset = istream_read_int32_le(inputStream);
124     inputStream.seek(4 * (WAL_MIPMAP_COUNT - 1)); // remaining offsets
125     inputStream.seek(WAL_NAME_LENGTH); // animname
126     flags = istream_read_int32_le(inputStream);
127     contents = istream_read_int32_le(inputStream);
128     value = istream_read_int32_le(inputStream);
129     source = buffer + offset;
130   }
131
132   RGBAImageFlags* image = new RGBAImageFlags(w, h, flags, contents, value);
133
134   byte* dest = image->getRGBAPixels();
135   byte* end = source + (w * h);
136   for(; source != end; ++source, dest += 4)
137   {
138     *(dest + 0) = palette[*source * 3 + 0];
139     *(dest + 1) = palette[*source * 3 + 1];
140     *(dest + 2) = palette[*source * 3 + 2];
141     *(dest + 3) = 255;
142   }
143
144   return image;
145 }
146
147 Image* LoadWal(ArchiveFile& file)
148 {
149   ScopedArchiveBuffer buffer(file);
150   return LoadMipTex( buffer.buffer, QUAKE2_WAL );
151 }
152
153 Image* LoadM8(ArchiveFile& file)
154 {
155   ScopedArchiveBuffer buffer(file);
156   return LoadMipTex( buffer.buffer, HERETIC2_M8 );
157 }