idtech2-tools/pal2tga.c

107 lines
3.1 KiB
C
Raw Permalink Normal View History

2021-08-24 22:20:01 +00:00
/*
PAL 2 TGA SOURCECODE
The MIT License (MIT)
Copyright (c) 2016-2019 Marco "eukara" Hladik <marco at icculus.org>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#define TGAHEADER 18
#define QPALSIZE 768
#define T2PVERSION "1.1"
void process_pal2tga(char *filename)
{
FILE *fLMP;
unsigned char pal_buff[QPALSIZE];
FILE *fTGA;
unsigned char tga_buff[QPALSIZE + TGAHEADER];
short pal_loop, tga_loop;
struct stat pal_st;
fLMP = fopen(filename, "rb");
if (!fLMP) {
fprintf(stderr, "couldn't find %s\n", filename);
return;
}
/* There are no other types of palette lumps. Sorry */
stat(filename, &pal_st);
if (pal_st.st_size != QPALSIZE) {
fprintf(stderr, "invalid palette lump %s, skipping\n", filename);
return;
}
fread(pal_buff, 1, QPALSIZE, fLMP);
fclose(fLMP);
memset(tga_buff, 0, 18);
tga_buff[2] = 2; /* Uncompressed TARGA */
tga_buff[12] = 16; /* Width */
tga_buff[14] = 16; /* Height */
tga_buff[16] = 24; /* Color depth */
/* TARGAs are flipped in an odd way,
* so we gotta do some sorting magic (vertical flip) */
for (tga_loop = 15; tga_loop >= 0; tga_loop--) {
for (pal_loop = 0; pal_loop < 16; pal_loop++) {
tga_buff[18 + (tga_loop * 48) + (pal_loop * 3) + 0] =
pal_buff[((15 - tga_loop) * 48) + (pal_loop * 3) + 2];
tga_buff[18 + (tga_loop * 48) + (pal_loop * 3) + 1] =
pal_buff[((15 - tga_loop) * 48) + (pal_loop * 3) + 1];
tga_buff[18 + (tga_loop * 48) + (pal_loop * 3) + 2] =
pal_buff[((15 - tga_loop) * 48) + (pal_loop * 3) + 0];
}
}
/* FIXME: We assume too much!
* Save the output to FILENAME.tga
* This is ugly when the input name has no .lmp extension.
* But who's going to try that. ...right? */
filename[strlen(filename)-3] = 't';
filename[strlen(filename)-2] = 'g';
filename[strlen(filename)-1] = 'a';
fprintf(stdout, "writing %s\n", filename);
fTGA = fopen(filename, "w+b");
fwrite(tga_buff, 1, QPALSIZE + TGAHEADER, fTGA);
fclose(fTGA);
}
int main(int argc, char *argv[])
{
int c;
if (argc <= 1) {
fprintf(stderr, "usage: pal2tga [file.lmp ...]\n");
return 1;
}
for (c = 1; c < argc; c++)
process_pal2tga(argv[c]);
return 0;
}