91 lines
2.6 KiB
C
91 lines
2.6 KiB
C
/*
|
|
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);
|
|
|
|
/* 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] = 'p';
|
|
filename[strlen(filename)-2] = 'a';
|
|
filename[strlen(filename)-1] = 'l';
|
|
fprintf(stdout, "writing %s\n", filename);
|
|
fTGA = fopen(filename, "w");
|
|
fprintf(fTGA, "JASC-PAL\r\n");
|
|
fprintf(fTGA, "0100\r\n");
|
|
fprintf(fTGA, "256\r\n");
|
|
for (int i = 0; i < QPALSIZE; i+=3)
|
|
fprintf(fTGA, "%d %d %d\r\n", pal_buff[i], pal_buff[i+1], pal_buff[i+2]);
|
|
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;
|
|
}
|