]> de.git.xonotic.org Git - xonotic/netradiant.git/blobdiff - plugins/imageq2/wal.cpp
netradiant: strip 16-bit png to 8-bit, fix #153
[xonotic/netradiant.git] / plugins / imageq2 / wal.cpp
index 162a0667587d5752949c0e95294eb0fac0043610..5994e5088c6663334c78680ffb8e4fef431676d8 100644 (file)
 #include "bytestreamutils.h"
 #include "imagelib.h"
 
-const int QUAKE2_WAL = 0;
-const int HERETIC2_M8 = 1;
+const int QUAKE2_WAL   = 0;
+const int HERETIC2_M8  = 1;
 
 typedef unsigned char byte;
 
-struct pcx_header_t {
-    char manufacturer;
-    char version;
-    char encoding;
-    char bits_per_pixel;
+struct pcx_header_t
+{
+       char manufacturer;
+       char version;
+       char encoding;
+       char bits_per_pixel;
 };
 
-void LoadPCXPalette(const char *filename, byte palette[768])
-{
-    byte *buffer;
-    int length = vfsLoadFile(filename, (void **) &buffer);
-    if (buffer == 0) {
-        return;
-    }
+void LoadPCXPalette( const char *filename, byte palette[768] ){
+       byte* buffer;
+       int length = vfsLoadFile( filename, (void **)&buffer );
+       if ( buffer == 0 ) {
+               return;
+       }
 
-    const pcx_header_t *pcx = reinterpret_cast<const pcx_header_t *>( buffer );
+       const pcx_header_t* pcx = reinterpret_cast<const pcx_header_t*>( buffer );
 
-    if (pcx->manufacturer != 0x0a
-        || pcx->version != 5
-        || pcx->encoding != 1
-        || pcx->bits_per_pixel != 8) {
-        return;
-    }
+       if ( pcx->manufacturer != 0x0a
+                || pcx->version != 5
+                || pcx->encoding != 1
+                || pcx->bits_per_pixel != 8 ) {
+               return;
+       }
 
-    memcpy(palette, buffer + length - 768, 768);
+       memcpy( palette, buffer + length - 768, 768 );
 
-    vfsFreeFile(buffer);
+       vfsFreeFile( buffer );
 }
 
 const int WAL_NAME_LENGTH = 32;
 const int WAL_MIPMAP_COUNT = 4;
-struct wal_header_t {
-    char name[WAL_NAME_LENGTH];
-    unsigned width, height;
-    unsigned offsets[WAL_MIPMAP_COUNT];       // four mip maps stored
-    char animname[WAL_NAME_LENGTH];         // next frame in animation chain
-    int flags;
-    int contents;
-    int value;
+struct wal_header_t
+{
+       char name[WAL_NAME_LENGTH];
+       unsigned width, height;
+       unsigned offsets[WAL_MIPMAP_COUNT];       // four mip maps stored
+       char animname[WAL_NAME_LENGTH];         // next frame in animation chain
+       int flags;
+       int contents;
+       int value;
 };
 
 const int M8_NAME_LENGTH = 32;
 const int M8_MIPMAP_COUNT = 16;
-struct m8_header_t {
-    int version;
-    char name[M8_NAME_LENGTH];
-    unsigned width[M8_MIPMAP_COUNT], height[M8_MIPMAP_COUNT];   // width and height of each mipmap
-    unsigned offsets[M8_MIPMAP_COUNT];             // 16 mip maps stored
-    char animname[M8_NAME_LENGTH];                // next frame in animation chain
-    byte palette[768];                // palette stored in m8
-    int flags;
-    int contents;
-    int value;
+struct m8_header_t
+{
+       int version;
+       char name[M8_NAME_LENGTH];
+       unsigned width[M8_MIPMAP_COUNT], height[M8_MIPMAP_COUNT];   // width and height of each mipmap
+       unsigned offsets[M8_MIPMAP_COUNT];             // 16 mip maps stored
+       char animname[M8_NAME_LENGTH];                // next frame in animation chain
+       byte palette[768];                // palette stored in m8
+       int flags;
+       int contents;
+       int value;
 };
 
-Image *LoadMipTex(byte *buffer, byte TypeofTex)
-{
-    int w, h, offset, flags, contents, value;
-    byte palette[768];
-    byte *source;
-
-    PointerInputStream inputStream(buffer);
-
-    if (TypeofTex == HERETIC2_M8) {
-        inputStream.seek(4 + M8_NAME_LENGTH); // version, name
-        w = istream_read_int32_le(inputStream);
-        inputStream.seek(4 * (M8_MIPMAP_COUNT - 1)); // remaining widths
-        h = istream_read_int32_le(inputStream);
-        inputStream.seek(4 * (M8_MIPMAP_COUNT - 1)); // remaining heights
-        offset = istream_read_int32_le(inputStream);
-        inputStream.seek(4 * (M8_MIPMAP_COUNT - 1)); // remaining offsets
-        inputStream.seek(M8_NAME_LENGTH); // animname
-        inputStream.read(palette, 768);
-        flags = istream_read_int32_le(inputStream);
-        contents = istream_read_int32_le(inputStream);
-        value = istream_read_int32_le(inputStream);
-        source = buffer + offset;
-    } else {
-        LoadPCXPalette("pics/colormap.pcx", palette);
-
-        inputStream.seek(WAL_NAME_LENGTH); // name
-        w = istream_read_int32_le(inputStream);
-        h = istream_read_int32_le(inputStream);
-        offset = istream_read_int32_le(inputStream);
-        inputStream.seek(4 * (WAL_MIPMAP_COUNT - 1)); // remaining offsets
-        inputStream.seek(WAL_NAME_LENGTH); // animname
-        flags = istream_read_int32_le(inputStream);
-        contents = istream_read_int32_le(inputStream);
-        value = istream_read_int32_le(inputStream);
-        source = buffer + offset;
-    }
-
-    RGBAImageFlags *image = new RGBAImageFlags(w, h, flags, contents, value);
-
-    byte *dest = image->getRGBAPixels();
-    byte *end = source + (w * h);
-    for (; source != end; ++source, dest += 4) {
-        *(dest + 0) = palette[*source * 3 + 0];
-        *(dest + 1) = palette[*source * 3 + 1];
-        *(dest + 2) = palette[*source * 3 + 2];
-        *(dest + 3) = 255;
-    }
-
-    return image;
+Image* LoadMipTex( byte* buffer, byte TypeofTex ){
+       int w, h, offset, flags, contents, value;
+       byte palette[768];
+       byte* source;
+
+       PointerInputStream inputStream( buffer );
+
+       if ( TypeofTex == HERETIC2_M8 ) {
+               inputStream.seek( 4 + M8_NAME_LENGTH ); // version, name
+               w = istream_read_int32_le( inputStream );
+               inputStream.seek( 4 * ( M8_MIPMAP_COUNT - 1 ) ); // remaining widths
+               h = istream_read_int32_le( inputStream );
+               inputStream.seek( 4 * ( M8_MIPMAP_COUNT - 1 ) ); // remaining heights
+               offset = istream_read_int32_le( inputStream );
+               inputStream.seek( 4 * ( M8_MIPMAP_COUNT - 1 ) ); // remaining offsets
+               inputStream.seek( M8_NAME_LENGTH ); // animname
+               inputStream.read( palette, 768 );
+               flags = istream_read_int32_le( inputStream );
+               contents = istream_read_int32_le( inputStream );
+               value = istream_read_int32_le( inputStream );
+               source = buffer + offset;
+       }
+       else
+       {
+               LoadPCXPalette( "pics/colormap.pcx", palette );
+
+               inputStream.seek( WAL_NAME_LENGTH ); // name
+               w = istream_read_int32_le( inputStream );
+               h = istream_read_int32_le( inputStream );
+               offset = istream_read_int32_le( inputStream );
+               inputStream.seek( 4 * ( WAL_MIPMAP_COUNT - 1 ) ); // remaining offsets
+               inputStream.seek( WAL_NAME_LENGTH ); // animname
+               flags = istream_read_int32_le( inputStream );
+               contents = istream_read_int32_le( inputStream );
+               value = istream_read_int32_le( inputStream );
+               source = buffer + offset;
+       }
+
+       RGBAImageFlags* image = new RGBAImageFlags( w, h, flags, contents, value );
+
+       byte* dest = image->getRGBAPixels();
+       byte* end = source + ( w * h );
+       for (; source != end; ++source, dest += 4 )
+       {
+               *( dest + 0 ) = palette[*source * 3 + 0];
+               *( dest + 1 ) = palette[*source * 3 + 1];
+               *( dest + 2 ) = palette[*source * 3 + 2];
+               *( dest + 3 ) = 255;
+       }
+
+       return image;
 }
 
-Image *LoadWal(ArchiveFile &file)
-{
-    ScopedArchiveBuffer buffer(file);
-    return LoadMipTex(buffer.buffer, QUAKE2_WAL);
+Image* LoadWal( ArchiveFile& file ){
+       ScopedArchiveBuffer buffer( file );
+       return LoadMipTex( buffer.buffer, QUAKE2_WAL );
 }
 
-Image *LoadM8(ArchiveFile &file)
-{
-    ScopedArchiveBuffer buffer(file);
-    return LoadMipTex(buffer.buffer, HERETIC2_M8);
+Image* LoadM8( ArchiveFile& file ){
+       ScopedArchiveBuffer buffer( file );
+       return LoadMipTex( buffer.buffer, HERETIC2_M8 );
 }