2006-03-20 16:43:15 +00:00
|
|
|
/*
|
2014-06-01 17:55:23 +00:00
|
|
|
* Portions of this file are copyright Rebirth contributors and licensed as
|
|
|
|
* described in COPYING.txt.
|
|
|
|
* Portions of this file are copyright Parallax Software and licensed
|
|
|
|
* according to the Parallax license below.
|
|
|
|
* See COPYING.txt for license details.
|
|
|
|
|
2006-03-20 16:43:15 +00:00
|
|
|
THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX
|
|
|
|
SOFTWARE CORPORATION ("PARALLAX"). PARALLAX, IN DISTRIBUTING THE CODE TO
|
|
|
|
END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A
|
|
|
|
ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS
|
|
|
|
IN USING, DISPLAYING, AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS
|
|
|
|
SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE
|
|
|
|
FREE PURPOSES. IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE
|
|
|
|
CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES. THE END-USER UNDERSTANDS
|
2010-07-27 11:41:43 +00:00
|
|
|
AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE.
|
2006-03-20 16:43:15 +00:00
|
|
|
COPYRIGHT 1993-1998 PARALLAX SOFTWARE CORPORATION. ALL RIGHTS RESERVED.
|
|
|
|
*/
|
|
|
|
/*
|
|
|
|
*
|
2008-01-23 17:25:09 +00:00
|
|
|
* Routines to read/write pcx images.
|
2006-03-20 16:43:15 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "gr.h"
|
2010-07-27 11:41:43 +00:00
|
|
|
#include "grdef.h"
|
2006-03-20 16:43:15 +00:00
|
|
|
#include "u_mem.h"
|
|
|
|
#include "pcx.h"
|
2008-01-23 17:25:09 +00:00
|
|
|
#include "physfsx.h"
|
2013-03-03 01:03:33 +00:00
|
|
|
#include "palette.h"
|
2008-01-23 17:25:09 +00:00
|
|
|
|
2013-11-30 21:54:17 +00:00
|
|
|
#include "dxxsconf.h"
|
2013-12-21 05:12:38 +00:00
|
|
|
#include "compiler-lengthof.h"
|
2015-01-20 02:46:42 +00:00
|
|
|
#include "compiler-range_for.h"
|
|
|
|
#include "partial_range.h"
|
2013-11-30 21:54:17 +00:00
|
|
|
|
2015-12-13 18:00:49 +00:00
|
|
|
namespace dcx {
|
2015-12-13 18:00:48 +00:00
|
|
|
|
2016-01-09 16:38:14 +00:00
|
|
|
static int pcx_encode_byte(ubyte byt, ubyte cnt, PHYSFS_File *fid);
|
|
|
|
static int pcx_encode_line(const uint8_t *inBuff, uint_fast32_t inLen, PHYSFS_File *fp);
|
2006-03-20 16:43:15 +00:00
|
|
|
|
|
|
|
/* PCX Header data type */
|
2013-12-22 22:03:07 +00:00
|
|
|
struct PCXHeader
|
|
|
|
{
|
2010-07-27 11:41:43 +00:00
|
|
|
ubyte Manufacturer;
|
|
|
|
ubyte Version;
|
|
|
|
ubyte Encoding;
|
|
|
|
ubyte BitsPerPixel;
|
|
|
|
short Xmin;
|
|
|
|
short Ymin;
|
|
|
|
short Xmax;
|
|
|
|
short Ymax;
|
|
|
|
short Hdpi;
|
|
|
|
short Vdpi;
|
|
|
|
ubyte ColorMap[16][3];
|
|
|
|
ubyte Reserved;
|
|
|
|
ubyte Nplanes;
|
|
|
|
short BytesPerLine;
|
|
|
|
ubyte filler[60];
|
2013-12-22 22:03:07 +00:00
|
|
|
} __pack__;
|
2010-07-27 11:41:43 +00:00
|
|
|
|
|
|
|
#define PCXHEADER_SIZE 128
|
2006-03-20 16:43:15 +00:00
|
|
|
|
2008-01-13 00:58:49 +00:00
|
|
|
/*
|
2016-01-09 16:38:14 +00:00
|
|
|
* reads n PCXHeader structs from a PHYSFS_File
|
2008-01-13 00:58:49 +00:00
|
|
|
*/
|
2016-01-09 16:38:14 +00:00
|
|
|
static int PCXHeader_read_n(PCXHeader *ph, int n, PHYSFS_File *fp)
|
2008-01-13 00:58:49 +00:00
|
|
|
{
|
|
|
|
int i;
|
2010-07-27 11:41:43 +00:00
|
|
|
|
2008-01-13 00:58:49 +00:00
|
|
|
for (i = 0; i < n; i++) {
|
2011-06-01 07:59:51 +00:00
|
|
|
ph->Manufacturer = PHYSFSX_readByte(fp);
|
|
|
|
ph->Version = PHYSFSX_readByte(fp);
|
|
|
|
ph->Encoding = PHYSFSX_readByte(fp);
|
|
|
|
ph->BitsPerPixel = PHYSFSX_readByte(fp);
|
|
|
|
ph->Xmin = PHYSFSX_readShort(fp);
|
|
|
|
ph->Ymin = PHYSFSX_readShort(fp);
|
|
|
|
ph->Xmax = PHYSFSX_readShort(fp);
|
|
|
|
ph->Ymax = PHYSFSX_readShort(fp);
|
|
|
|
ph->Hdpi = PHYSFSX_readShort(fp);
|
|
|
|
ph->Vdpi = PHYSFSX_readShort(fp);
|
2013-03-03 01:03:33 +00:00
|
|
|
PHYSFS_read(fp, &ph->ColorMap, 16*3, 1);
|
2011-06-01 07:59:51 +00:00
|
|
|
ph->Reserved = PHYSFSX_readByte(fp);
|
|
|
|
ph->Nplanes = PHYSFSX_readByte(fp);
|
|
|
|
ph->BytesPerLine = PHYSFSX_readShort(fp);
|
|
|
|
PHYSFS_read(fp, &ph->filler, 60, 1);
|
2008-01-13 00:58:49 +00:00
|
|
|
}
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
2015-12-13 18:00:48 +00:00
|
|
|
}
|
|
|
|
|
2013-03-03 01:03:33 +00:00
|
|
|
#if defined(DXX_BUILD_DESCENT_I)
|
2015-12-13 18:00:49 +00:00
|
|
|
namespace dsx {
|
2015-12-13 18:00:48 +00:00
|
|
|
|
2013-01-06 21:03:57 +00:00
|
|
|
int bald_guy_load(const char * filename, grs_bitmap * bmp,int bitmap_type ,palette_array_t &palette )
|
2008-07-15 12:50:27 +00:00
|
|
|
{
|
|
|
|
PCXHeader header;
|
2013-07-14 23:05:27 +00:00
|
|
|
int i, count, fsize;
|
2014-12-02 03:24:38 +00:00
|
|
|
ubyte data, c, xor_value;
|
2013-07-14 23:05:27 +00:00
|
|
|
unsigned int row, xsize;
|
|
|
|
unsigned int col, ysize;
|
2008-07-15 12:50:27 +00:00
|
|
|
|
2015-01-17 18:31:42 +00:00
|
|
|
auto PCXfile = PHYSFSX_openReadBuffered(filename);
|
2008-07-15 12:50:27 +00:00
|
|
|
if ( !PCXfile )
|
|
|
|
return PCX_ERROR_OPENING;
|
|
|
|
|
2011-06-01 07:59:51 +00:00
|
|
|
PHYSFSX_fseek(PCXfile, -1, SEEK_END);
|
|
|
|
fsize = PHYSFS_tell(PCXfile);
|
|
|
|
PHYSFS_read(PCXfile, &xor_value, 1, 1);
|
2008-07-15 12:50:27 +00:00
|
|
|
xor_value--;
|
2011-06-01 07:59:51 +00:00
|
|
|
PHYSFSX_fseek(PCXfile, 0, SEEK_SET);
|
2008-07-15 12:50:27 +00:00
|
|
|
|
2015-01-28 03:42:53 +00:00
|
|
|
RAIIdmem<uint8_t[]> bguy_data, bguy_data1;
|
|
|
|
MALLOC(bguy_data, uint8_t[], fsize);
|
|
|
|
MALLOC(bguy_data1, uint8_t[], fsize);
|
2008-07-15 12:50:27 +00:00
|
|
|
|
2011-06-01 07:59:51 +00:00
|
|
|
PHYSFS_read(PCXfile, bguy_data1, 1, fsize);
|
2008-07-15 12:50:27 +00:00
|
|
|
|
|
|
|
for (i = 0; i < fsize; i++) {
|
|
|
|
c = bguy_data1[fsize - i - 1] ^ xor_value;
|
|
|
|
bguy_data[i] = c;
|
|
|
|
xor_value--;
|
|
|
|
}
|
2015-01-17 18:31:42 +00:00
|
|
|
PCXfile.reset();
|
2015-02-17 03:52:59 +00:00
|
|
|
auto p = bguy_data.get();
|
2008-07-15 12:50:27 +00:00
|
|
|
memcpy( &header, p, sizeof(PCXHeader) );
|
|
|
|
p += sizeof(PCXHeader);
|
|
|
|
|
|
|
|
// Is it a 256 color PCX file?
|
|
|
|
if ((header.Manufacturer != 10)||(header.Encoding != 1)||(header.Nplanes != 1)||(header.BitsPerPixel != 8)||(header.Version != 5)) {
|
|
|
|
return PCX_ERROR_WRONG_VERSION;
|
|
|
|
}
|
|
|
|
header.Xmin= INTEL_SHORT(header.Xmin);
|
|
|
|
header.Xmax = INTEL_SHORT(header.Xmax);
|
|
|
|
header.Ymin = INTEL_SHORT(header.Ymin);
|
|
|
|
header.Ymax = INTEL_SHORT(header.Ymax);
|
|
|
|
|
|
|
|
// Find the size of the image
|
2008-12-21 08:54:03 +00:00
|
|
|
xsize = header.Xmax - header.Xmin + 1;
|
|
|
|
ysize = header.Ymax - header.Ymin + 1;
|
2008-07-15 12:50:27 +00:00
|
|
|
|
|
|
|
if ( bmp->bm_data == NULL ) {
|
2014-07-04 03:45:35 +00:00
|
|
|
*bmp = {};
|
2014-12-02 03:24:38 +00:00
|
|
|
MALLOC(bmp->bm_mdata, unsigned char, xsize * ysize );
|
2008-07-15 12:50:27 +00:00
|
|
|
if ( bmp->bm_data == NULL ) {
|
|
|
|
return PCX_ERROR_MEMORY;
|
|
|
|
}
|
|
|
|
bmp->bm_w = bmp->bm_rowsize = xsize;
|
|
|
|
bmp->bm_h = ysize;
|
2015-07-25 23:10:47 +00:00
|
|
|
bmp->set_type(bitmap_type);
|
2008-07-15 12:50:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for (row=0; row< ysize ; row++) {
|
2014-12-02 03:24:38 +00:00
|
|
|
auto pixdata = &bmp->get_bitmap_data()[bmp->bm_rowsize*row];
|
2008-07-15 12:50:27 +00:00
|
|
|
for (col=0; col< xsize ; ) {
|
|
|
|
data = *p;
|
|
|
|
p++;
|
|
|
|
if ((data & 0xC0) == 0xC0) {
|
|
|
|
count = data & 0x3F;
|
|
|
|
data = *p;
|
|
|
|
p++;
|
|
|
|
memset( pixdata, data, count );
|
|
|
|
pixdata += count;
|
|
|
|
col += count;
|
|
|
|
} else {
|
|
|
|
*pixdata++ = data;
|
|
|
|
col++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Read the extended palette at the end of PCX file
|
|
|
|
// Read in a character which should be 12 to be extended palette file
|
|
|
|
|
|
|
|
p++;
|
2013-12-17 22:14:43 +00:00
|
|
|
copy_diminish_palette(palette, p);
|
2008-07-15 12:50:27 +00:00
|
|
|
return PCX_ERROR_NONE;
|
|
|
|
}
|
2015-12-13 18:00:48 +00:00
|
|
|
|
|
|
|
}
|
2013-03-03 01:03:33 +00:00
|
|
|
#endif
|
|
|
|
|
2015-12-13 18:00:49 +00:00
|
|
|
namespace dcx {
|
2015-12-13 18:00:48 +00:00
|
|
|
|
2013-03-03 01:03:33 +00:00
|
|
|
struct PCX_PHYSFS_file
|
|
|
|
{
|
2015-01-17 18:31:42 +00:00
|
|
|
RAIIPHYSFS_File PCXfile;
|
2013-03-03 01:03:33 +00:00
|
|
|
};
|
|
|
|
|
2014-12-02 03:35:01 +00:00
|
|
|
static int pcx_read_bitmap_file(struct PCX_PHYSFS_file *const pcxphysfs, grs_bitmap &bmp,int bitmap_type ,palette_array_t &palette);
|
2008-07-15 12:50:27 +00:00
|
|
|
|
2014-12-02 03:35:01 +00:00
|
|
|
int pcx_read_bitmap(const char * filename, grs_bitmap &bmp, int bitmap_type, palette_array_t &palette )
|
2013-03-03 01:03:33 +00:00
|
|
|
{
|
|
|
|
int result;
|
2015-01-17 18:31:42 +00:00
|
|
|
PCX_PHYSFS_file pcxphysfs{PHYSFSX_openReadBuffered(filename)};
|
2013-03-03 01:03:33 +00:00
|
|
|
if (!pcxphysfs.PCXfile)
|
|
|
|
return PCX_ERROR_OPENING;
|
|
|
|
result = pcx_read_bitmap_file(&pcxphysfs, bmp, bitmap_type, palette);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int PCX_PHYSFS_read(struct PCX_PHYSFS_file *pcxphysfs, ubyte *data, unsigned size)
|
|
|
|
{
|
|
|
|
return PHYSFS_read(pcxphysfs->PCXfile, data, size, sizeof(*data));
|
|
|
|
}
|
|
|
|
|
2014-12-02 03:35:01 +00:00
|
|
|
static int pcx_read_bitmap_file(struct PCX_PHYSFS_file *const pcxphysfs, grs_bitmap &bmp, int bitmap_type, palette_array_t &palette)
|
2006-03-20 16:43:15 +00:00
|
|
|
{
|
|
|
|
PCXHeader header;
|
|
|
|
int i, row, col, count, xsize, ysize;
|
2014-12-02 03:24:38 +00:00
|
|
|
ubyte data;
|
2006-03-20 16:43:15 +00:00
|
|
|
|
|
|
|
// read 128 char PCX header
|
2013-03-03 01:03:33 +00:00
|
|
|
if (PCXHeader_read_n( &header, 1, pcxphysfs->PCXfile )!=1) {
|
2006-03-20 16:43:15 +00:00
|
|
|
return PCX_ERROR_NO_HEADER;
|
|
|
|
}
|
2010-07-27 11:41:43 +00:00
|
|
|
|
2006-03-20 16:43:15 +00:00
|
|
|
// Is it a 256 color PCX file?
|
|
|
|
if ((header.Manufacturer != 10)||(header.Encoding != 1)||(header.Nplanes != 1)||(header.BitsPerPixel != 8)||(header.Version != 5)) {
|
|
|
|
return PCX_ERROR_WRONG_VERSION;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Find the size of the image
|
|
|
|
xsize = header.Xmax - header.Xmin + 1;
|
|
|
|
ysize = header.Ymax - header.Ymin + 1;
|
|
|
|
|
|
|
|
if ( bitmap_type == BM_LINEAR ) {
|
2014-12-02 03:35:01 +00:00
|
|
|
if ( bmp.bm_data == NULL ) {
|
|
|
|
gr_init_bitmap_alloc(bmp, bitmap_type, 0, 0, xsize, ysize, xsize);
|
2006-03-20 16:43:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-25 23:10:47 +00:00
|
|
|
if (bmp.get_type() == BM_LINEAR)
|
|
|
|
{
|
2006-03-20 16:43:15 +00:00
|
|
|
for (row=0; row< ysize ; row++) {
|
2014-12-02 03:35:01 +00:00
|
|
|
auto pixdata = &bmp.get_bitmap_data()[bmp.bm_rowsize*row];
|
2006-03-20 16:43:15 +00:00
|
|
|
for (col=0; col< xsize ; ) {
|
2013-03-03 01:03:33 +00:00
|
|
|
if (PCX_PHYSFS_read(pcxphysfs, &data, 1) != 1) {
|
2006-03-20 16:43:15 +00:00
|
|
|
return PCX_ERROR_READING;
|
|
|
|
}
|
|
|
|
if ((data & 0xC0) == 0xC0) {
|
|
|
|
count = data & 0x3F;
|
2013-03-03 01:03:33 +00:00
|
|
|
if (PCX_PHYSFS_read(pcxphysfs, &data, 1) != 1) {
|
2006-03-20 16:43:15 +00:00
|
|
|
return PCX_ERROR_READING;
|
|
|
|
}
|
|
|
|
memset( pixdata, data, count );
|
|
|
|
pixdata += count;
|
|
|
|
col += count;
|
|
|
|
} else {
|
|
|
|
*pixdata++ = data;
|
|
|
|
col++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for (row=0; row< ysize ; row++) {
|
|
|
|
for (col=0; col< xsize ; ) {
|
2013-03-03 01:03:33 +00:00
|
|
|
if (PCX_PHYSFS_read(pcxphysfs, &data, 1) != 1) {
|
2006-03-20 16:43:15 +00:00
|
|
|
return PCX_ERROR_READING;
|
|
|
|
}
|
|
|
|
if ((data & 0xC0) == 0xC0) {
|
|
|
|
count = data & 0x3F;
|
2013-03-03 01:03:33 +00:00
|
|
|
if (PCX_PHYSFS_read(pcxphysfs, &data, 1) != 1) {
|
2006-03-20 16:43:15 +00:00
|
|
|
return PCX_ERROR_READING;
|
|
|
|
}
|
|
|
|
for (i=0;i<count;i++)
|
2014-12-02 03:35:01 +00:00
|
|
|
gr_bm_pixel(bmp, col+i, row, data );
|
2006-03-20 16:43:15 +00:00
|
|
|
col += count;
|
|
|
|
} else {
|
2014-12-02 03:35:01 +00:00
|
|
|
gr_bm_pixel(bmp, col, row, data );
|
2006-03-20 16:43:15 +00:00
|
|
|
col++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read the extended palette at the end of PCX file
|
|
|
|
// Read in a character which should be 12 to be extended palette file
|
2013-03-03 01:03:33 +00:00
|
|
|
if (PCX_PHYSFS_read(pcxphysfs, &data, 1) == 1) {
|
2006-03-20 16:43:15 +00:00
|
|
|
if ( data == 12 ) {
|
2013-01-06 21:11:53 +00:00
|
|
|
if (PCX_PHYSFS_read(pcxphysfs, reinterpret_cast<ubyte *>(&palette[0]), palette.size() * sizeof(palette[0])) != 1) {
|
2006-03-20 16:43:15 +00:00
|
|
|
return PCX_ERROR_READING;
|
|
|
|
}
|
2013-12-17 22:14:43 +00:00
|
|
|
diminish_palette(palette);
|
2006-03-20 16:43:15 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return PCX_ERROR_NO_PALETTE;
|
|
|
|
}
|
|
|
|
return PCX_ERROR_NONE;
|
|
|
|
}
|
|
|
|
|
2013-01-06 21:03:57 +00:00
|
|
|
int pcx_write_bitmap(const char * filename, grs_bitmap * bmp, palette_array_t &palette )
|
2006-03-20 16:43:15 +00:00
|
|
|
{
|
|
|
|
int retval;
|
|
|
|
ubyte data;
|
2014-07-04 03:46:40 +00:00
|
|
|
PCXHeader header{};
|
2006-03-20 16:43:15 +00:00
|
|
|
|
|
|
|
header.Manufacturer = 10;
|
|
|
|
header.Encoding = 1;
|
|
|
|
header.Nplanes = 1;
|
|
|
|
header.BitsPerPixel = 8;
|
|
|
|
header.Version = 5;
|
2008-01-23 17:25:09 +00:00
|
|
|
header.Xmax = bmp->bm_w-1;
|
|
|
|
header.Ymax = bmp->bm_h-1;
|
|
|
|
header.BytesPerLine = bmp->bm_w;
|
2006-03-20 16:43:15 +00:00
|
|
|
|
2015-01-17 18:31:42 +00:00
|
|
|
auto PCXfile = PHYSFSX_openWriteBuffered(filename);
|
2006-03-20 16:43:15 +00:00
|
|
|
if ( !PCXfile )
|
|
|
|
return PCX_ERROR_OPENING;
|
|
|
|
|
2008-01-23 17:25:09 +00:00
|
|
|
if (PHYSFS_write(PCXfile, &header, PCXHEADER_SIZE, 1) != 1)
|
|
|
|
{
|
2006-03-20 16:43:15 +00:00
|
|
|
return PCX_ERROR_WRITING;
|
|
|
|
}
|
|
|
|
|
2014-09-20 23:47:27 +00:00
|
|
|
for (uint_fast32_t i=0; i<bmp->bm_h; i++ ) {
|
2014-12-02 03:24:38 +00:00
|
|
|
if (!pcx_encode_line( &bmp->get_bitmap_data()[bmp->bm_rowsize*i], bmp->bm_w, PCXfile )) {
|
2006-03-20 16:43:15 +00:00
|
|
|
return PCX_ERROR_WRITING;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-01-23 17:25:09 +00:00
|
|
|
// Mark an extended palette
|
2006-03-20 16:43:15 +00:00
|
|
|
data = 12;
|
2008-01-23 17:25:09 +00:00
|
|
|
if (PHYSFS_write(PCXfile, &data, 1, 1) != 1)
|
|
|
|
{
|
2006-03-20 16:43:15 +00:00
|
|
|
return PCX_ERROR_WRITING;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write the extended palette
|
2014-09-20 23:47:27 +00:00
|
|
|
for (uint_fast32_t i=0; i < palette.size(); i++ )
|
2013-01-06 21:11:53 +00:00
|
|
|
{
|
|
|
|
palette[i].r <<= 2;
|
|
|
|
palette[i].g <<= 2;
|
|
|
|
palette[i].b <<= 2;
|
|
|
|
}
|
2006-03-20 16:43:15 +00:00
|
|
|
|
2013-01-06 21:11:53 +00:00
|
|
|
retval = PHYSFS_write(PCXfile, &palette[0], sizeof(palette), 1);
|
2008-01-23 17:25:09 +00:00
|
|
|
|
2013-12-17 22:14:43 +00:00
|
|
|
diminish_palette(palette);
|
2006-03-20 16:43:15 +00:00
|
|
|
|
|
|
|
if (retval !=1) {
|
|
|
|
return PCX_ERROR_WRITING;
|
|
|
|
}
|
|
|
|
return PCX_ERROR_NONE;
|
|
|
|
}
|
|
|
|
|
2008-01-23 17:25:09 +00:00
|
|
|
// returns number of bytes written into outBuff, 0 if failed
|
2016-01-09 16:38:14 +00:00
|
|
|
int pcx_encode_line(const uint8_t *inBuff, uint_fast32_t inLen, PHYSFS_File *fp)
|
2008-01-23 17:25:09 +00:00
|
|
|
{
|
2015-01-20 02:46:42 +00:00
|
|
|
ubyte last;
|
|
|
|
int i;
|
2006-03-20 16:43:15 +00:00
|
|
|
register int total;
|
|
|
|
register ubyte runCount; // max single runlength is 63
|
|
|
|
total = 0;
|
2008-01-23 17:25:09 +00:00
|
|
|
last = *(inBuff);
|
2006-03-20 16:43:15 +00:00
|
|
|
runCount = 1;
|
|
|
|
|
2015-02-05 03:03:49 +00:00
|
|
|
range_for (const auto ub, unchecked_partial_range(inBuff, 1u, inLen))
|
2015-01-20 02:46:42 +00:00
|
|
|
{
|
2012-11-18 18:21:50 +00:00
|
|
|
if (ub == last) {
|
2006-03-20 16:43:15 +00:00
|
|
|
runCount++; // it encodes
|
|
|
|
if (runCount == 63) {
|
|
|
|
if (!(i=pcx_encode_byte(last, runCount, fp)))
|
|
|
|
return(0);
|
|
|
|
total += i;
|
|
|
|
runCount = 0;
|
|
|
|
}
|
|
|
|
} else { // this != last
|
|
|
|
if (runCount) {
|
|
|
|
if (!(i=pcx_encode_byte(last, runCount, fp)))
|
|
|
|
return(0);
|
|
|
|
total += i;
|
|
|
|
}
|
2012-11-18 18:21:50 +00:00
|
|
|
last = ub;
|
2006-03-20 16:43:15 +00:00
|
|
|
runCount = 1;
|
|
|
|
}
|
2008-01-23 17:25:09 +00:00
|
|
|
}
|
2006-03-20 16:43:15 +00:00
|
|
|
|
|
|
|
if (runCount) { // finish up
|
|
|
|
if (!(i=pcx_encode_byte(last, runCount, fp)))
|
|
|
|
return 0;
|
|
|
|
return total + i;
|
|
|
|
}
|
|
|
|
return total;
|
|
|
|
}
|
|
|
|
|
2015-01-29 04:27:36 +00:00
|
|
|
static inline int PHYSFSX_putc(PHYSFS_File *file, uint8_t ch)
|
|
|
|
{
|
|
|
|
if (PHYSFS_write(file, &ch, 1, 1) < 1)
|
|
|
|
return -1;
|
|
|
|
else
|
|
|
|
return ch;
|
|
|
|
}
|
|
|
|
|
2008-01-23 17:25:09 +00:00
|
|
|
// subroutine for writing an encoded byte pair
|
2006-03-20 16:43:15 +00:00
|
|
|
// returns count of bytes written, 0 if error
|
2016-01-09 16:38:14 +00:00
|
|
|
int pcx_encode_byte(ubyte byt, ubyte cnt, PHYSFS_File *fid)
|
2006-03-20 16:43:15 +00:00
|
|
|
{
|
|
|
|
if (cnt) {
|
|
|
|
if ( (cnt==1) && (0xc0 != (0xc0 & byt)) ) {
|
2008-01-23 17:25:09 +00:00
|
|
|
if(EOF == PHYSFSX_putc(fid, (int)byt))
|
2006-03-20 16:43:15 +00:00
|
|
|
return 0; // disk write error (probably full)
|
|
|
|
return 1;
|
|
|
|
} else {
|
2008-01-23 17:25:09 +00:00
|
|
|
if(EOF == PHYSFSX_putc(fid, (int)0xC0 | cnt))
|
2006-03-20 16:43:15 +00:00
|
|
|
return 0; // disk write error
|
2008-01-23 17:25:09 +00:00
|
|
|
if(EOF == PHYSFSX_putc(fid, (int)byt))
|
2006-03-20 16:43:15 +00:00
|
|
|
return 0; // disk write error
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
//text for error messges
|
2012-07-22 23:17:52 +00:00
|
|
|
static const char pcx_error_messages[] = {
|
2006-03-20 16:43:15 +00:00
|
|
|
"No error.\0"
|
|
|
|
"Error opening file.\0"
|
|
|
|
"Couldn't read PCX header.\0"
|
|
|
|
"Unsupported PCX version.\0"
|
|
|
|
"Error reading data.\0"
|
|
|
|
"Couldn't find palette information.\0"
|
|
|
|
"Error writing data.\0"
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
//function to return pointer to error message
|
2012-07-22 23:17:52 +00:00
|
|
|
const char *pcx_errormsg(int error_number)
|
2006-03-20 16:43:15 +00:00
|
|
|
{
|
2012-07-22 23:17:52 +00:00
|
|
|
const char *p = pcx_error_messages;
|
2006-03-20 16:43:15 +00:00
|
|
|
|
|
|
|
while (error_number--) {
|
|
|
|
|
2013-11-30 21:54:17 +00:00
|
|
|
if (p == pcx_error_messages + lengthof(pcx_error_messages)) return NULL;
|
2006-03-20 16:43:15 +00:00
|
|
|
|
|
|
|
p += strlen(p)+1;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return p;
|
|
|
|
}
|
2015-12-13 18:00:48 +00:00
|
|
|
|
|
|
|
}
|