2 Copyright (C) 1999-2006 Id Software, Inc. and contributors.
3 For a list of contributors, see the accompanying CONTRIBUTORS file.
5 This file is part of GtkRadiant.
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.
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.
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
22 // by Hydra - hydra@hydras-world.com
24 // HLW = Half-Life-WAD, I don't know if the actual in data in the WAD files
25 // has it's own name, so I'm just calling the individal textures .HLW files :)
27 // Thanks to the guys that made Wally for releasing an example WAD loader.
28 // without it this would not have been possible.
36 typedef unsigned char byte;
38 #include "ifilesystem.h"
44 ============================================================================
48 HalfLife WAD files contain files that look like this:
53 First mip (width * height)
54 Second mip (width * height / 4)
55 Third mip (width * height / 16)
56 Fourth mip (width * height / 64)
58 Palette (Palette size * 3)
61 ============================================================================
64 #define GET_MIP_DATA_SIZE( WIDTH, HEIGHT ) ( sizeof( WAD3_MIP ) + ( WIDTH * HEIGHT ) + ( WIDTH * HEIGHT / 4 ) + ( WIDTH * HEIGHT / 16 ) + ( WIDTH * HEIGHT / 64 ) )
69 unsigned int width, height;
70 unsigned int offsets[4]; // four mip maps stored
71 } WAD3_MIP, *LPWAD3_MIP;
74 =========================================================
78 Hydra: this code isn't bullet proof and probably won't
79 like corrupt WAD files, but it works for now.
81 TODO: make it more robust.
82 =========================================================
91 Image* LoadHLWBuff( byte* buffer ){
93 unsigned long mipdatasize;
99 unsigned char red, green, blue, alphabyte;
101 lpMip = (LPWAD3_MIP)buffer; //!\todo Make endian-safe.
103 mipdatasize = GET_MIP_DATA_SIZE( lpMip->width,lpMip->height );
105 palette = buffer + mipdatasize + 2;
107 buf_p = buffer + lpMip->offsets[0];
109 columns = lpMip->width;
110 rows = lpMip->height;
112 RGBAImage* image = new RGBAImage( columns, rows );
114 for ( row = 0; row < rows; row++ )
116 pixbuf = image->getRGBAPixels() + row * columns * 4;
118 for ( column = 0; column < columns; column++ )
124 red = *( palette + ( palIndex * 3 ) );
125 green = *( palette + ( palIndex * 3 ) + 1 );
126 blue = *( palette + ( palIndex * 3 ) + 2 );
128 // HalfLife engine makes pixels that are BLUE transparent.
129 // So show them that way in the editor.
130 if ( blue == 0xff && red == 0x00 && green == 0x00 ) {
132 blue = 0x00; // don't set the resulting pixel to blue
143 *pixbuf++ = alphabyte;
150 Image* LoadHLW( ArchiveFile& file ){
151 ScopedArchiveBuffer buffer( file );
152 return LoadHLWBuff( buffer.buffer );