xash3d-fwgs/utils/mdldec/texture.c

167 lines
3.6 KiB
C
Raw Normal View History

2020-03-04 05:08:14 +01:00
/*
texture.c - texture writer
Copyright (C) 2020 Andrey Akhmichin
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "const.h"
#include "crtlib.h"
#include "studio.h"
#include "img_bmp.h"
#include "img_tga.h"
2020-03-04 05:08:14 +01:00
#include "mdldec.h"
#include "texture.h"
/*
============
WriteBMP
============
*/
static void WriteBMP( FILE *fp, mstudiotexture_t *texture )
2020-03-04 05:08:14 +01:00
{
int i;
2020-03-04 05:08:14 +01:00
const byte *p;
2023-10-19 08:20:36 +02:00
byte *palette, *pic;
2020-03-04 05:08:14 +01:00
rgba_t rgba_palette[256];
bmp_t bmp_hdr = {0,};
bmp_hdr.id[0] = 'B';
bmp_hdr.id[1] = 'M';
bmp_hdr.width = texture->width;
bmp_hdr.height = texture->height;
bmp_hdr.planes = 1;
bmp_hdr.bitsPerPixel = 8;
bmp_hdr.bitmapDataSize = bmp_hdr.width * bmp_hdr.height;
2020-03-04 05:08:14 +01:00
bmp_hdr.colors = 256;
bmp_hdr.fileSize = sizeof( bmp_hdr ) + bmp_hdr.bitmapDataSize + sizeof( rgba_palette );
2020-03-04 05:08:14 +01:00
bmp_hdr.bitmapDataOffset = sizeof( bmp_hdr ) + sizeof( rgba_palette );
bmp_hdr.bitmapHeaderSize = BI_SIZE;
pic = (byte *)texture_hdr + texture->index;
palette = pic + bmp_hdr.bitmapDataSize;
2020-03-04 05:08:14 +01:00
fwrite( &bmp_hdr, sizeof( bmp_hdr ), 1, fp );
p = palette;
for( i = 0; i < (int)bmp_hdr.colors; i++ )
{
rgba_palette[i][2] = *p++;
rgba_palette[i][1] = *p++;
rgba_palette[i][0] = *p++;
rgba_palette[i][3] = 0;
}
fwrite( rgba_palette, sizeof( rgba_palette ), 1, fp );
p = pic;
p += ( bmp_hdr.height - 1 ) * bmp_hdr.width;
for( i = 0; i < bmp_hdr.height; i++ )
{
2023-10-19 08:20:36 +02:00
fwrite( p, bmp_hdr.width, 1, fp );
2020-03-04 05:08:14 +01:00
p -= bmp_hdr.width;
}
}
/*
============
WriteTGA
============
*/
static void WriteTGA( FILE *fp, mstudiotexture_t *texture )
{
int i;
const byte *p;
byte *palette, *pic;
rgb_t rgb_palette[256];
tga_t tga_hdr = {0,};
tga_hdr.colormap_type = tga_hdr.image_type = 1;
tga_hdr.colormap_length = 256;
tga_hdr.colormap_size = 24;
tga_hdr.pixel_size = 8;
tga_hdr.width = texture->width;
tga_hdr.height = texture->height;
2020-03-04 05:08:14 +01:00
pic = (byte *)texture_hdr + texture->index;
palette = pic + tga_hdr.width * tga_hdr.height;
2020-03-04 05:08:14 +01:00
fwrite( &tga_hdr, sizeof( tga_hdr ), 1, fp );
p = palette;
for( i = 0; i < (int)tga_hdr.colormap_length; i++ )
{
rgb_palette[i][2] = *p++;
rgb_palette[i][1] = *p++;
rgb_palette[i][0] = *p++;
}
fwrite( rgb_palette, sizeof( rgb_palette ), 1, fp );
p = pic;
p += ( tga_hdr.height - 1 ) * tga_hdr.width;
for( i = 0; i < tga_hdr.height; i++ )
{
fwrite( p, tga_hdr.width, 1, fp );
p -= tga_hdr.width;
}
2020-03-04 05:08:14 +01:00
}
/*
============
WriteTextures
============
*/
void WriteTextures( void )
{
int i, len;
FILE *fp;
2023-10-19 08:20:36 +02:00
mstudiotexture_t *texture = (mstudiotexture_t *)( (byte *)texture_hdr + texture_hdr->textureindex );
char filename[MAX_SYSPATH];
2020-03-04 05:08:14 +01:00
2023-10-19 08:20:36 +02:00
for( i = 0; i < texture_hdr->numtextures; ++i, ++texture )
{
len = Q_snprintf( filename, MAX_SYSPATH, "%s%s", destdir, texture->name );
if( len == -1 )
{
fprintf( stderr, "ERROR: Destination path is too long. Can't write %s\n", texture->name );
continue;
}
fp = fopen( filename, "wb" );
if( !fp )
{
fprintf( stderr, "ERROR: Can't write texture file %s\n", filename );
continue;
}
if( !Q_stricmp( COM_FileExtension( texture->name ), "tga" ))
WriteTGA( fp, texture );
else
WriteBMP( fp, texture );
fclose( fp );
printf( "Texture: %s\n", filename );
}
2020-03-04 05:08:14 +01:00
}