2 Copyright (C) 2001-2006, William Joseph.
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
24 #include "ifilesystem.h"
27 #include "stream/stringstream.h"
28 #include "bytestreamutils.h"
34 typedef unsigned char byte;
44 void LoadPCXPalette( const char *filename, byte palette[768] ){
46 int length = vfsLoadFile( filename, (void **)&buffer );
51 const pcx_header_t* pcx = reinterpret_cast<const pcx_header_t*>( buffer );
53 if ( pcx->manufacturer != 0x0a
56 || pcx->bits_per_pixel != 8 ) {
60 memcpy( palette, buffer + length - 768, 768 );
62 vfsFreeFile( buffer );
65 const int WAL_NAME_LENGTH = 32;
66 const int WAL_MIPMAP_COUNT = 4;
69 char name[WAL_NAME_LENGTH];
70 unsigned width, height;
71 unsigned offsets[WAL_MIPMAP_COUNT]; // four mip maps stored
72 char animname[WAL_NAME_LENGTH]; // next frame in animation chain
78 const int M8_NAME_LENGTH = 32;
79 const int M8_MIPMAP_COUNT = 16;
83 char name[M8_NAME_LENGTH];
84 unsigned width[M8_MIPMAP_COUNT], height[M8_MIPMAP_COUNT]; // width and height of each mipmap
85 unsigned offsets[M8_MIPMAP_COUNT]; // 16 mip maps stored
86 char animname[M8_NAME_LENGTH]; // next frame in animation chain
87 byte palette[768]; // palette stored in m8
93 Image* LoadMipTex( byte* buffer, byte TypeofTex ){
94 int w, h, offset, flags, contents, value;
98 PointerInputStream inputStream( buffer );
100 if ( TypeofTex == HERETIC2_M8 ) {
101 inputStream.seek( 4 + M8_NAME_LENGTH ); // version, name
102 w = istream_read_int32_le( inputStream );
103 inputStream.seek( 4 * ( M8_MIPMAP_COUNT - 1 ) ); // remaining widths
104 h = istream_read_int32_le( inputStream );
105 inputStream.seek( 4 * ( M8_MIPMAP_COUNT - 1 ) ); // remaining heights
106 offset = istream_read_int32_le( inputStream );
107 inputStream.seek( 4 * ( M8_MIPMAP_COUNT - 1 ) ); // remaining offsets
108 inputStream.seek( M8_NAME_LENGTH ); // animname
109 inputStream.read( palette, 768 );
110 flags = istream_read_int32_le( inputStream );
111 contents = istream_read_int32_le( inputStream );
112 value = istream_read_int32_le( inputStream );
113 source = buffer + offset;
117 LoadPCXPalette( "pics/colormap.pcx", palette );
119 inputStream.seek( WAL_NAME_LENGTH ); // name
120 w = istream_read_int32_le( inputStream );
121 h = istream_read_int32_le( inputStream );
122 offset = istream_read_int32_le( inputStream );
123 inputStream.seek( 4 * ( WAL_MIPMAP_COUNT - 1 ) ); // remaining offsets
124 inputStream.seek( WAL_NAME_LENGTH ); // animname
125 flags = istream_read_int32_le( inputStream );
126 contents = istream_read_int32_le( inputStream );
127 value = istream_read_int32_le( inputStream );
128 source = buffer + offset;
131 RGBAImageFlags* image = new RGBAImageFlags( w, h, flags, contents, value );
133 byte* dest = image->getRGBAPixels();
134 byte* end = source + ( w * h );
135 for (; source != end; ++source, dest += 4 )
137 *( dest + 0 ) = palette[*source * 3 + 0];
138 *( dest + 1 ) = palette[*source * 3 + 1];
139 *( dest + 2 ) = palette[*source * 3 + 2];
146 Image* LoadWal( ArchiveFile& file ){
147 ScopedArchiveBuffer buffer( file );
148 return LoadMipTex( buffer.buffer, QUAKE2_WAL );
151 Image* LoadM8( ArchiveFile& file ){
152 ScopedArchiveBuffer buffer( file );
153 return LoadMipTex( buffer.buffer, HERETIC2_M8 );