Quake 1 LMP manipulation tools
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

110 lines
3.2 KiB

3 months ago
/*
TGA 2 PAL 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>
#define TGAHEADER 18
#define QPALSIZE 768
void process_tga2pal(char *filename)
{
FILE *fTGA;
unsigned char tga_header[TGAHEADER];
unsigned char tga_buff[QPALSIZE];
FILE *fLMP;
unsigned char pal_buff[QPALSIZE];
short loop_pal, loop_tga;
fTGA = fopen(filename, "rb");
if (!fTGA) {
fprintf(stderr, "couldn't find %s\n", filename);
return;
}
/* Put the TARGA header into the buffer for validation */
fread(tga_header, 1, TGAHEADER, fTGA);
/* only allow uncompressed, 24bit TARGAs */
if (tga_header[2] != 2) {
fprintf(stderr, "%s should be an uncompressed, RGB image\n", filename);
return;
}
if (tga_header[16] != 24) {
fprintf(stderr, "%s is not 24 bit in depth\n", filename);
return;
}
if (tga_header[12] != 16 || tga_header[14] != 16) {
fprintf(stderr, "%s is not a 16x16 image\n", filename);
return;
}
/* Skip to after the TARGA HEADER... and then read the buffer */
fseek(fTGA, 18, SEEK_SET);
fread(tga_buff, 1, QPALSIZE, fTGA);
fclose(fTGA);
/* TARGAs are flipped in an odd way,
* so we gotta do the sorting dance */
for(loop_tga = 15; loop_tga >= 0; loop_tga--) {
for(loop_pal = 0; loop_pal < 16; loop_pal++) {
pal_buff[((15 - loop_tga) * 48) + (loop_pal * 3) + 0] =
tga_buff[(loop_tga * 48) + (loop_pal * 3) + 2];
pal_buff[((15 - loop_tga) * 48) + (loop_pal * 3) + 1] =
tga_buff[(loop_tga * 48) + (loop_pal * 3) + 1];
pal_buff[((15 - loop_tga) * 48) + (loop_pal * 3) + 2] =
tga_buff[(loop_tga * 48) + (loop_pal * 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] = 'l';
filename[strlen(filename)-2] = 'm';
filename[strlen(filename)-1] = 'p';
fprintf(stdout, "writing %s\n", filename);
fLMP = fopen(filename, "w+b");
fwrite(pal_buff, 1, QPALSIZE, fLMP);
fclose(fLMP);
}
int main(int argc, char *argv[])
{
int c;
if (argc <= 1) {
fprintf(stderr, "usage: tga2pal [file.tga ...]\n");
return 1;
}
for (c = 1; c < argc; c++)
process_tga2pal(argv[c]);
return 0;
}