merge pcx.c, tga.c and wad.c. util seemed the most logical place, but LoadPCX

had to be modified to get away from vid.h (better designe now IMO anyway)
This commit is contained in:
Bill Currie 2001-05-30 20:56:53 +00:00
parent 80be659b4a
commit 940598f240
12 changed files with 13 additions and 626 deletions

View file

@ -49,6 +49,6 @@ typedef struct
pcx_t *EncodePCX (byte * data, int width, int height,
int rowbytes, byte * palette, qboolean flip, int *length);
struct tex_s *LoadPCX (VFile *f, int convert); // tex is from Hunk_TempAlloc
struct tex_s *LoadPCX (VFile *f, int convert, byte *pal); // tex is from Hunk_TempAlloc
#endif // __pcx_h

View file

@ -13,9 +13,9 @@ libQFutil_la_SOURCES = \
checksum.c cmd.c console.c con_print.c crc.c cvar.c hash.c \
info.c link.c math.S \
mathlib.c \
mdfour.c msg.c plugin.c qargs.c qendian.c qfplist.c quakefs.c \
quakeio.c sizebuf.c sys.c sys_error.c va.c ver_check.c zone.c \
$(fnmatch_SRC)
mdfour.c msg.c pcx.c plugin.c qargs.c qendian.c qfplist.c quakefs.c \
quakeio.c sizebuf.c sys.c sys_error.c tga.c va.c ver_check.c wad.c \
zone.c $(fnmatch_SRC)
LIBLIST = libQFutil.la @LIBRARY_SEARCH_PATH@

View file

@ -42,11 +42,10 @@
#include "QF/qtypes.h"
#include "QF/texture.h"
#include "QF/vfs.h"
#include "QF/vid.h"
#include "QF/zone.h"
tex_t *
LoadPCX (VFile *f, int convert)
LoadPCX (VFile *f, int convert, byte *pal)
{
pcx_t *pcx;
int pcx_mark;
@ -94,7 +93,7 @@ LoadPCX (VFile *f, int convert)
if (convert) {
tex->palette = 0;
} else {
tex->palette = vid_basepal;
tex->palette = pal;
}
pix = tex->data;

View file

@ -42,7 +42,7 @@ noinst_LIBRARIES= libqfnet.a
common_ASM= sys_ia32.S worlda.S
common_SOURCES= game.c host_skin.c skin.c wad.c world.c com.c $(common_ASM)
common_SOURCES= game.c host_skin.c skin.c world.c com.c $(common_ASM)
common_ldflags= -export-dynamic
@ -73,7 +73,7 @@ client_LIB_DEPS= libqfnet.a $(qf_client_LIBS)
client_SOURCES= cl_cam.c cl_cmd.c cl_demo.c cl_input.c cl_main.c cl_screen.c \
cl_parse.c cl_tent.c \
console.c keys.c sbar.c \
r_view.c locs.c pcx.c tga.c
r_view.c locs.c
server_SOURCES= host.c host_cmd.c pr_cmds.c sv_cvar.c sv_main.c \
sv_move.c sv_phys.c sv_progs.c sv_user.c

View file

@ -170,7 +170,7 @@ Skin_Cache (skin_t *skin)
return NULL;
}
}
tex = LoadPCX (file, 0);
tex = LoadPCX (file, 0, vid_basepal);
Qclose (file);
if (!tex || tex->width > 320 || tex->height > 200) {

View file

@ -1,162 +0,0 @@
/*
wad.c
(description)
Copyright (C) 1996-1997 Id Software, Inc.
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 2
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.
You should have received a copy of the GNU General Public License
along with this program; if not, write to:
Free Software Foundation, Inc.
59 Temple Place - Suite 330
Boston, MA 02111-1307, USA
$Id$
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include "QF/qendian.h"
#include "QF/sys.h"
#include "QF/wad.h"
#include "QF/vfs.h"
int wad_numlumps;
lumpinfo_t *wad_lumps;
byte *wad_base;
void SwapPic (qpic_t *pic);
/*
W_CleanupName
Lowercases name and pads with spaces and a terminating 0 to the length of
lumpinfo_t->name. Used so lumpname lookups can proceed rapidly by
comparing 4 chars at a time Space padding is so names can be printed
nicely in tables. Can safely be performed in place.
*/
void
W_CleanupName (char *in, char *out)
{
int i;
int c;
for (i = 0; i < 16; i++) {
c = in[i];
if (!c)
break;
if (c >= 'A' && c <= 'Z')
c += ('a' - 'A');
out[i] = c;
}
for (; i < 16; i++)
out[i] = 0;
}
void
W_LoadWadFile (char *filename)
{
lumpinfo_t *lump_p;
wadinfo_t *header;
unsigned int i;
int infotableofs;
wad_base = COM_LoadHunkFile (filename);
if (!wad_base)
Sys_Error ("W_LoadWadFile: couldn't load %s", filename);
header = (wadinfo_t *) wad_base;
if (header->identification[0] != 'W'
|| header->identification[1] != 'A'
|| header->identification[2] != 'D'
|| header->identification[3] != '2')
Sys_Error ("Wad file %s doesn't have WAD2 id\n", filename);
wad_numlumps = LittleLong (header->numlumps);
infotableofs = LittleLong (header->infotableofs);
wad_lumps = (lumpinfo_t *) (wad_base + infotableofs);
for (i = 0, lump_p = wad_lumps; i < wad_numlumps; i++, lump_p++) {
lump_p->filepos = LittleLong (lump_p->filepos);
lump_p->size = LittleLong (lump_p->size);
W_CleanupName (lump_p->name, lump_p->name);
if (lump_p->type == TYP_QPIC)
SwapPic ((qpic_t *) (wad_base + lump_p->filepos));
}
}
lumpinfo_t *
W_GetLumpinfo (char *name)
{
int i;
lumpinfo_t *lump_p;
char clean[16];
W_CleanupName (name, clean);
for (lump_p = wad_lumps, i = 0; i < wad_numlumps; i++, lump_p++) {
if (!strcmp (clean, lump_p->name))
return lump_p;
}
Sys_Error ("W_GetLumpinfo: %s not found", name);
return NULL;
}
void *
W_GetLumpName (char *name)
{
lumpinfo_t *lump;
lump = W_GetLumpinfo (name);
return (void *) (wad_base + lump->filepos);
}
void *
W_GetLumpNum (int num)
{
lumpinfo_t *lump;
if (num < 0 || num > wad_numlumps)
Sys_Error ("W_GetLumpNum: bad number: %i", num);
lump = wad_lumps + num;
return (void *) (wad_base + lump->filepos);
}
/*
automatic byte swapping
*/
void
SwapPic (qpic_t *pic)
{
pic->width = LittleLong (pic->width);
pic->height = LittleLong (pic->height);
}

View file

@ -99,9 +99,9 @@ client_LIB_DEPS= libqfnet.a $(qf_client_LIBS)
client_SOURCES= cl_cam.c cl_cmd.c cl_cvar.c cl_demo.c cl_ents.c cl_input.c \
cl_main.c cl_misc.c cl_ngraph.c cl_parse.c cl_screen.c cl_pred.c \
cl_skin.c cl_slist.c cl_tent.c \
console.c keys.c locs.c pcx.c \
r_view.c sbar.c skin.c teamplay.c tga.c \
wad.c $(syscl_SRC)
console.c keys.c locs.c \
r_view.c sbar.c skin.c teamplay.c \
$(syscl_SRC)
# Software-rendering clients
#

View file

@ -1,206 +0,0 @@
/*
pcx.c
pcx image handling
Copyright (C) 1996-1997 Id Software, Inc.
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 2
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.
You should have received a copy of the GNU General Public License
along with this program; if not, write to:
Free Software Foundation, Inc.
59 Temple Place - Suite 330
Boston, MA 02111-1307, USA
$Id$
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#ifdef HAVE_STRING_H
# include <string.h>
#endif
#ifdef HAVE_STRINGS_H
# include <strings.h>
#endif
#include "QF/console.h"
#include "QF/pcx.h"
#include "QF/qendian.h"
#include "QF/qtypes.h"
#include "QF/texture.h"
#include "QF/vfs.h"
#include "QF/vid.h"
#include "QF/zone.h"
tex_t *
LoadPCX (VFile *f, int convert)
{
pcx_t *pcx;
int pcx_mark;
byte *palette;
byte *pix;
byte *dataByte;
int x, y;
int runLength = 1;
int count;
tex_t *tex;
//
// parse the PCX file
//
pcx_mark = Hunk_LowMark ();
pcx = Hunk_AllocName (com_filesize, "PCX");
Qread (f, pcx, com_filesize);
pcx->xmax = LittleShort (pcx->xmax);
pcx->xmin = LittleShort (pcx->xmin);
pcx->ymax = LittleShort (pcx->ymax);
pcx->ymin = LittleShort (pcx->ymin);
pcx->hres = LittleShort (pcx->hres);
pcx->vres = LittleShort (pcx->vres);
pcx->bytes_per_line = LittleShort (pcx->bytes_per_line);
pcx->palette_type = LittleShort (pcx->palette_type);
if (pcx->manufacturer != 0x0a
|| pcx->version != 5
|| pcx->encoding != 1
|| pcx->bits_per_pixel != 8 || pcx->xmax >= 320 || pcx->ymax >= 256) {
Con_Printf ("Bad pcx file\n");
return 0;
}
palette = ((byte*)pcx) + com_filesize - 768;
dataByte = (byte*)&pcx[1];
count = (pcx->xmax + 1) * (pcx->ymax + 1);
if (convert)
count *= 4;
tex = Hunk_TempAlloc (sizeof (tex_t) + count);
tex->width = pcx->xmax + 1;
tex->height = pcx->ymax + 1;
if (convert) {
tex->palette = 0;
} else {
tex->palette = vid_basepal;
}
pix = tex->data;
for (y = 0; y < tex->height; y++) {
for (x = 0; x < tex->width;) {
runLength = 1;
if (dataByte >= palette)
break;
if ((*dataByte & 0xC0) == 0xC0) {
runLength = *dataByte++ & 0x3F;
if (dataByte >= palette)
break;
}
if (convert) {
while (count && runLength > 0) {
pix[0] = palette[*dataByte * 3];
pix[1] = palette[*dataByte * 3 + 1];
pix[2] = palette[*dataByte * 3 + 2];
pix[3] = 255;
pix += 4;
count -= 4;
runLength--;
x++;
}
} else {
while (count && runLength > 0) {
*pix++ = *dataByte;
count--;
runLength--;
x++;
}
}
if (runLength)
break;
dataByte++;
}
if (runLength)
break;
}
Hunk_FreeToLowMark (pcx_mark);
if (count || runLength) {
Con_Printf ("PCX was malformed. You should delete it.\n");
return 0;
}
return tex;
}
pcx_t *
EncodePCX (byte * data, int width, int height,
int rowbytes, byte * palette, qboolean flip, int *length)
{
int i, j;
pcx_t *pcx;
byte *pack;
if (!(pcx = Hunk_TempAlloc (width * height * 2 + 1000))) {
Con_Printf ("EncodePCX: not enough memory\n");
return 0;
}
pcx->manufacturer = 0x0a; // PCX id
pcx->version = 5; // 256 color
pcx->encoding = 1; // uncompressed
pcx->bits_per_pixel = 8; // 256 color
pcx->xmin = 0;
pcx->ymin = 0;
pcx->xmax = LittleShort ((short) (width - 1));
pcx->ymax = LittleShort ((short) (height - 1));
pcx->hres = LittleShort ((short) width);
pcx->vres = LittleShort ((short) height);
memset (pcx->palette, 0, sizeof (pcx->palette));
pcx->color_planes = 1; // chunky image
pcx->bytes_per_line = LittleShort ((short) width);
pcx->palette_type = LittleShort (2); // not a grey scale
memset (pcx->filler, 0, sizeof (pcx->filler));
// pack the image
pack = (byte*)&pcx[1];
if (flip)
data += rowbytes * (height - 1);
for (i = 0; i < height; i++) {
for (j = 0; j < width; j++) {
if ((*data & 0xc0) != 0xc0)
*pack++ = *data++;
else {
*pack++ = 0xc1;
*pack++ = *data++;
}
}
data += rowbytes - width;
if (flip)
data -= rowbytes * 2;
}
// write the palette
*pack++ = 0x0c; // palette ID byte
for (i = 0; i < 768; i++)
*pack++ = *palette++;
// write output file
*length = pack - (byte *) pcx;
return pcx;
}

View file

@ -171,7 +171,7 @@ Skin_Cache (skin_t *skin)
return NULL;
}
}
tex = LoadPCX (file, 0);
tex = LoadPCX (file, 0, vid_basepal);
Qclose (file);
if (!tex || tex->width > 320 || tex->height > 200) {

View file

@ -1,244 +0,0 @@
/*
tga.c
targa image handling
Copyright (C) 1996-1997 Id Software, Inc.
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 2
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.
You should have received a copy of the GNU General Public License
along with this program; if not, write to:
Free Software Foundation, Inc.
59 Temple Place - Suite 330
Boston, MA 02111-1307, USA
$Id$
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#ifdef HAVE_STRING_H
# include <string.h>
#endif
#ifdef HAVE_STRINGS_H
# include <strings.h>
#endif
#include <stdlib.h>
#include "QF/qendian.h"
#include "QF/sys.h"
#include "QF/tga.h"
#include "QF/vfs.h"
static int
fgetLittleShort (VFile *f)
{
byte b1, b2;
b1 = Qgetc (f);
b2 = Qgetc (f);
return (short) (b1 + b2 * 256);
}
/*
static int
fgetLittleLong (VFile *f)
{
byte b1, b2, b3, b4;
b1 = Qgetc(f);
b2 = Qgetc(f);
b3 = Qgetc(f);
b4 = Qgetc(f);
return b1 + (b2<<8) + (b3<<16) + (b4<<24);
}
*/
byte *
LoadTGA (VFile *fin)
{
int columns, rows, numPixels;
byte *pixbuf;
int row, column;
unsigned char red = 0, green = 0, blue = 0, alphabyte = 0;
TargaHeader targa_header;
byte *targa_rgba;
targa_header.id_length = Qgetc (fin);
targa_header.colormap_type = Qgetc (fin);
targa_header.image_type = Qgetc (fin);
targa_header.colormap_index = fgetLittleShort (fin);
targa_header.colormap_length = fgetLittleShort (fin);
targa_header.colormap_size = Qgetc (fin);
targa_header.x_origin = fgetLittleShort (fin);
targa_header.y_origin = fgetLittleShort (fin);
targa_header.width = fgetLittleShort (fin);
targa_header.height = fgetLittleShort (fin);
targa_header.pixel_size = Qgetc (fin);
targa_header.attributes = Qgetc (fin);
if (targa_header.image_type != 2 && targa_header.image_type != 10)
Sys_Error ("LoadTGA: Only type 2 and 10 targa RGB images supported\n");
if (targa_header.colormap_type != 0
|| (targa_header.pixel_size != 32 && targa_header.pixel_size != 24))
Sys_Error
("Texture_LoadTGA: Only 32 or 24 bit images supported (no colormaps)\n");
columns = targa_header.width;
rows = targa_header.height;
numPixels = columns * rows;
targa_rgba = malloc (numPixels * 4);
if (targa_header.id_length != 0)
Qseek (fin, targa_header.id_length, SEEK_CUR); // skip TARGA image
// comment
if (targa_header.image_type == 2) { // Uncompressed, RGB images
for (row = rows - 1; row >= 0; row--) {
pixbuf = targa_rgba + row * columns * 4;
for (column = 0; column < columns; column++) {
switch (targa_header.pixel_size) {
case 24:
blue = Qgetc (fin);
green = Qgetc (fin);
red = Qgetc (fin);
*pixbuf++ = red;
*pixbuf++ = green;
*pixbuf++ = blue;
*pixbuf++ = 255;
break;
case 32:
blue = Qgetc (fin);
green = Qgetc (fin);
red = Qgetc (fin);
alphabyte = Qgetc (fin);
*pixbuf++ = red;
*pixbuf++ = green;
*pixbuf++ = blue;
*pixbuf++ = alphabyte;
break;
}
}
}
} else if (targa_header.image_type == 10) { // Runlength encoded RGB
// images
unsigned char packetHeader, packetSize, j;
for (row = rows - 1; row >= 0; row--) {
pixbuf = targa_rgba + row * columns * 4;
for (column = 0; column < columns;) {
packetHeader = Qgetc (fin);
packetSize = 1 + (packetHeader & 0x7f);
if (packetHeader & 0x80) { // run-length packet
switch (targa_header.pixel_size) {
case 24:
blue = Qgetc (fin);
green = Qgetc (fin);
red = Qgetc (fin);
alphabyte = 255;
break;
case 32:
blue = Qgetc (fin);
green = Qgetc (fin);
red = Qgetc (fin);
alphabyte = Qgetc (fin);
break;
}
for (j = 0; j < packetSize; j++) {
*pixbuf++ = red;
*pixbuf++ = green;
*pixbuf++ = blue;
*pixbuf++ = alphabyte;
column++;
if (column == columns) { // run spans across rows
column = 0;
if (row > 0)
row--;
else
goto breakOut;
pixbuf = targa_rgba + row * columns * 4;
}
}
} else { // non run-length packet
for (j = 0; j < packetSize; j++) {
switch (targa_header.pixel_size) {
case 24:
blue = Qgetc (fin);
green = Qgetc (fin);
red = Qgetc (fin);
*pixbuf++ = red;
*pixbuf++ = green;
*pixbuf++ = blue;
*pixbuf++ = 255;
break;
case 32:
blue = Qgetc (fin);
green = Qgetc (fin);
red = Qgetc (fin);
alphabyte = Qgetc (fin);
*pixbuf++ = red;
*pixbuf++ = green;
*pixbuf++ = blue;
*pixbuf++ = alphabyte;
break;
}
column++;
if (column == columns) { // pixel packet run spans
// across rows
column = 0;
if (row > 0)
row--;
else
goto breakOut;
pixbuf = targa_rgba + row * columns * 4;
}
}
}
}
breakOut:;
}
}
Qclose (fin);
return targa_rgba;
}
void
WriteTGAfile (const char *tganame, byte *data, int width, int height)
{
TargaHeader header;
memset (&header, 0, sizeof (header));
header.image_type = 2; // uncompressed type
header.width = LittleShort (width);
header.height = LittleShort (height);
header.pixel_size = 24;
COM_WriteBuffers (tganame, 2, &header, sizeof (header), data,
width * height * 3);
}