mirror of
https://github.com/AquariaOSE/Aquaria.git
synced 2025-07-02 22:14:37 +00:00
[vfs, #2] Move around some files (cleanup only)
This commit is contained in:
parent
a90f57afb0
commit
1709503344
8 changed files with 523 additions and 660 deletions
483
ExternalLibs/ByteBuffer.h
Normal file
483
ExternalLibs/ByteBuffer.h
Normal file
|
@ -0,0 +1,483 @@
|
|||
#ifndef BYTEBUFFER_H
|
||||
#define BYTEBUFFER_H
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h> // for memcpy
|
||||
#include <stdio.h>
|
||||
#include <string>
|
||||
|
||||
|
||||
// ** compatibility stuff for BBGE .... **
|
||||
|
||||
|
||||
#define BYTEBUFFER_NO_EXCEPTIONS
|
||||
|
||||
// from SDL headers
|
||||
#if defined(__hppa__) || \
|
||||
defined(__m68k__) || defined(mc68000) || defined(_M_M68K) || \
|
||||
(defined(__MIPS__) && defined(__MISPEB__)) || \
|
||||
defined(__ppc__) || defined(__POWERPC__) || defined(_M_PPC) || \
|
||||
defined(__sparc__)
|
||||
#define BB_IS_BIG_ENDIAN 1
|
||||
#endif
|
||||
|
||||
// ****
|
||||
|
||||
|
||||
namespace ByteBufferTools
|
||||
{
|
||||
template<size_t T> inline void convert(char *val)
|
||||
{
|
||||
std::swap(*val, *(val + T - 1));
|
||||
convert<T - 2>(val + 1);
|
||||
}
|
||||
template<> inline void convert<0>(char *) {}
|
||||
template<> inline void convert<1>(char *) {}
|
||||
|
||||
template<typename T> inline void EndianConvert(T *val)
|
||||
{
|
||||
convert<sizeof(T)>((char *)(val));
|
||||
}
|
||||
|
||||
#if BB_IS_BIG_ENDIAN
|
||||
template<typename T> inline void ToLittleEndian(T& val) { EndianConvert<T>(&val); }
|
||||
template<typename T> inline void ToBigEndian(T&) { }
|
||||
#else
|
||||
template<typename T> inline void ToLittleEndian(T&) { }
|
||||
template<typename T> inline void ToBigEndian(T& val) { EndianConvert<T>(&val); }
|
||||
#endif
|
||||
|
||||
template<typename T> void ToLittleEndian(T*); // will generate link error
|
||||
template<typename T> void ToBigEndian(T*); // will generate link error
|
||||
|
||||
};
|
||||
|
||||
#define BB_MAKE_WRITE_OP(T) inline ByteBuffer& operator<<(T val) { append<T>(val); return *this; }
|
||||
#define BB_MAKE_READ_OP(T) inline ByteBuffer& operator>>(T &val) { val = read<T>(); return *this; }
|
||||
|
||||
class ByteBuffer
|
||||
{
|
||||
public:
|
||||
typedef void (*delete_func)(void*);
|
||||
typedef void *(*allocator_func)(size_t);
|
||||
|
||||
enum Mode // for creation with existing pointers
|
||||
{
|
||||
COPY, //- Make a copy of the buffer (default action).
|
||||
REUSE, //- Use the passed-in buffer as is. Requires the pointer
|
||||
// to remain valid over the life of this object.
|
||||
TAKE_OVER, //- Take over the passed-in buffer; it will be deleted on object destruction.
|
||||
};
|
||||
|
||||
#ifdef _MSC_VER
|
||||
typedef __int64 int64;
|
||||
typedef long int32;
|
||||
typedef short int16;
|
||||
typedef char int8;
|
||||
typedef unsigned __int64 uint64;
|
||||
typedef unsigned long uint32;
|
||||
typedef unsigned short uint16;
|
||||
typedef unsigned char uint8;
|
||||
#else
|
||||
typedef long long int64;
|
||||
typedef int int32;
|
||||
typedef short int16;
|
||||
typedef char int8;
|
||||
typedef unsigned long long uint64;
|
||||
typedef unsigned int uint32;
|
||||
typedef unsigned short uint16;
|
||||
typedef unsigned char uint8;
|
||||
#endif
|
||||
|
||||
class Exception
|
||||
{
|
||||
public:
|
||||
Exception(const ByteBuffer *bb, const char *act, uint32 sp = 0)
|
||||
{
|
||||
action = act;
|
||||
rpos = bb->rpos();
|
||||
wpos = bb->wpos();
|
||||
sizeparam = sp;
|
||||
cursize = bb->size();
|
||||
}
|
||||
uint32 rpos, wpos, sizeparam, cursize;
|
||||
const char *action;
|
||||
};
|
||||
|
||||
#ifdef BYTEBUFFER_NO_EXCEPTIONS
|
||||
#define BYTEBUFFER_EXCEPT(bb, desc, sz) { Exception __e(bb, desc, sz); \
|
||||
fprintf(stderr, "Exception in ByteBuffer: '%s', rpos: %u, wpos: %u, cursize: %u, sizeparam: %u", \
|
||||
__e.action, __e.rpos, __e.wpos, __e.cursize, __e.sizeparam); abort(); }
|
||||
#else
|
||||
#define BYTEBUFFER_EXCEPT(bb, desc, sz) throw Exception(bb, desc, sz)
|
||||
#endif
|
||||
|
||||
protected:
|
||||
|
||||
uint8 *_buf; // the ptr to the buffer that holds all the bytes
|
||||
uint32 _rpos, // read position, [0 ... _size]
|
||||
_wpos, // write position, [0 ... _size]
|
||||
_res, // reserved buffer size, [0 ... _size ... _res]
|
||||
_size; // used buffer size
|
||||
delete_func _delfunc;
|
||||
allocator_func _allocfunc;
|
||||
bool _mybuf; // if true, destructor deletes buffer
|
||||
bool _growable; // default true, if false, buffer will not re-allocate more space
|
||||
|
||||
public:
|
||||
|
||||
|
||||
ByteBuffer()
|
||||
: _rpos(0), _wpos(0), _buf(NULL), _size(0), _growable(true), _res(0), _mybuf(false), _delfunc(NULL),
|
||||
_allocfunc(NULL)
|
||||
{
|
||||
}
|
||||
ByteBuffer(uint32 res)
|
||||
: _rpos(0), _wpos(0), _buf(NULL), _size(0), _growable(true), _res(0), _mybuf(false), _delfunc(NULL),
|
||||
_allocfunc(NULL)
|
||||
{
|
||||
_allocate(res);
|
||||
}
|
||||
ByteBuffer(ByteBuffer &buf, Mode mode = COPY, uint32 extra = 0)
|
||||
: _rpos(0), _wpos(0), _buf(NULL), _size(0), _growable(true), _res(0), _mybuf(false), _delfunc(NULL),
|
||||
_allocfunc(NULL)
|
||||
{
|
||||
init(buf, mode, extra);
|
||||
}
|
||||
// del param only used with TAKE_OVER, extra only used with COPY
|
||||
ByteBuffer(void *buf, uint32 size, Mode mode = COPY, delete_func del = NULL, uint32 extra = 0)
|
||||
: _rpos(0), _wpos(0), _size(size), _buf(NULL), _growable(true), _delfunc(del),
|
||||
_mybuf(false), _allocfunc(NULL) // for mode == REUSE
|
||||
{
|
||||
init(buf, size, mode, del, extra);
|
||||
}
|
||||
|
||||
void init(void *buf, uint32 size, Mode mode = COPY, delete_func del = NULL, uint32 extra = 0)
|
||||
{
|
||||
_mybuf = false;
|
||||
switch(mode)
|
||||
{
|
||||
case COPY:
|
||||
_allocate(size + extra);
|
||||
append(buf, size);
|
||||
break;
|
||||
|
||||
case TAKE_OVER:
|
||||
_mybuf = true; // fallthrough
|
||||
case REUSE:
|
||||
_buf = (uint8*)buf;
|
||||
_res = size;
|
||||
_size = size;
|
||||
}
|
||||
}
|
||||
|
||||
void init(ByteBuffer& bb, Mode mode = COPY, uint32 extra = 0)
|
||||
{
|
||||
_allocfunc = bb._allocfunc;
|
||||
|
||||
switch(mode)
|
||||
{
|
||||
case COPY:
|
||||
reserve(bb.size() + extra);
|
||||
append(bb);
|
||||
break;
|
||||
|
||||
case TAKE_OVER:
|
||||
case REUSE:
|
||||
_mybuf = bb._mybuf;
|
||||
_delfunc = bb._delfunc;
|
||||
_buf = bb._buf;
|
||||
_res = bb._res;
|
||||
_size = bb._size;
|
||||
_growable = bb._growable;
|
||||
break;
|
||||
}
|
||||
|
||||
if(mode == TAKE_OVER)
|
||||
{
|
||||
bb._buf = NULL;
|
||||
bb._size = 0;
|
||||
bb._res = 0;
|
||||
}
|
||||
}
|
||||
|
||||
virtual ~ByteBuffer()
|
||||
{
|
||||
clear();
|
||||
}
|
||||
|
||||
void clear(void)
|
||||
{
|
||||
_delete();
|
||||
reset();
|
||||
}
|
||||
|
||||
inline void reset(void)
|
||||
{
|
||||
_rpos = _wpos = _size = 0;
|
||||
}
|
||||
|
||||
void resize(uint32 newsize)
|
||||
{
|
||||
reserve(newsize);
|
||||
_rpos = 0;
|
||||
_wpos = newsize;
|
||||
_size = newsize;
|
||||
}
|
||||
|
||||
void reserve(uint32 newsize)
|
||||
{
|
||||
if(_res < newsize)
|
||||
_allocate(newsize);
|
||||
}
|
||||
|
||||
// ---------------------- Write methods -----------------------
|
||||
|
||||
BB_MAKE_WRITE_OP(char);
|
||||
BB_MAKE_WRITE_OP(uint8);
|
||||
BB_MAKE_WRITE_OP(uint16);
|
||||
BB_MAKE_WRITE_OP(uint32);
|
||||
BB_MAKE_WRITE_OP(uint64);
|
||||
BB_MAKE_WRITE_OP(float);
|
||||
BB_MAKE_WRITE_OP(double);
|
||||
BB_MAKE_WRITE_OP(int);
|
||||
|
||||
ByteBuffer &operator<<(bool value)
|
||||
{
|
||||
append<char>((char)value);
|
||||
return *this;
|
||||
}
|
||||
|
||||
ByteBuffer &operator<<(const char *str)
|
||||
{
|
||||
append((uint8 *)str, str ? strlen(str) : 0);
|
||||
append((uint8)0);
|
||||
return *this;
|
||||
}
|
||||
|
||||
ByteBuffer &operator<<(const std::string &value)
|
||||
{
|
||||
append((uint8 *)value.c_str(), value.length());
|
||||
append((uint8)0);
|
||||
return *this;
|
||||
}
|
||||
|
||||
// -------------------- Read methods --------------------
|
||||
|
||||
BB_MAKE_READ_OP(char);
|
||||
BB_MAKE_READ_OP(uint8);
|
||||
BB_MAKE_READ_OP(uint16);
|
||||
BB_MAKE_READ_OP(uint32);
|
||||
BB_MAKE_READ_OP(uint64);
|
||||
BB_MAKE_READ_OP(float);
|
||||
BB_MAKE_READ_OP(double);
|
||||
BB_MAKE_READ_OP(int);
|
||||
|
||||
ByteBuffer &operator>>(bool &value)
|
||||
{
|
||||
value = read<char>() > 0 ? true : false;
|
||||
return *this;
|
||||
}
|
||||
|
||||
uint8 operator[](uint32 pos)
|
||||
{
|
||||
return read<uint8>(pos);
|
||||
}
|
||||
|
||||
ByteBuffer &operator>>(std::string& value)
|
||||
{
|
||||
value.clear();
|
||||
char c;
|
||||
while(readable() && (c = read<char>()))
|
||||
value += c;
|
||||
return *this;
|
||||
}
|
||||
|
||||
// --------------------------------------------------
|
||||
|
||||
uint32 rpos() const { return _rpos; }
|
||||
uint32 rpos(uint32 rpos)
|
||||
{
|
||||
_rpos = rpos < size() ? rpos : size();
|
||||
return _rpos;
|
||||
}
|
||||
|
||||
uint32 wpos() const { return _wpos; }
|
||||
uint32 wpos(uint32 wpos)
|
||||
{
|
||||
_wpos = wpos < size() ? wpos : size();
|
||||
return _wpos;
|
||||
}
|
||||
|
||||
template <typename T> T read()
|
||||
{
|
||||
T r = read<T>(_rpos);
|
||||
_rpos += sizeof(T);
|
||||
return r;
|
||||
}
|
||||
template <typename T> T read(uint32 pos) const
|
||||
{
|
||||
if(pos + sizeof(T) > size())
|
||||
BYTEBUFFER_EXCEPT(this, "read", sizeof(T));
|
||||
T val = *((T const*)(_buf + pos));
|
||||
ByteBufferTools::ToLittleEndian<T>(val);
|
||||
return val;
|
||||
}
|
||||
|
||||
void read(void *dest, uint32 len)
|
||||
{
|
||||
if (_rpos + len <= size())
|
||||
memcpy(dest, &_buf[_rpos], len);
|
||||
else
|
||||
BYTEBUFFER_EXCEPT(this, "read-into", len);
|
||||
_rpos += len;
|
||||
}
|
||||
|
||||
void skipRead(uint32 len)
|
||||
{
|
||||
_rpos += len;
|
||||
}
|
||||
|
||||
inline const uint8 *contents() const { return _buf; }
|
||||
inline uint8 *contents() { return _buf; }
|
||||
|
||||
inline const void *ptr() const { return _buf; }
|
||||
inline void *ptr() { return _buf; }
|
||||
|
||||
inline uint32 size() const { return _size; }
|
||||
|
||||
inline uint32 bytes() const { return size(); }
|
||||
inline uint32 bits() const { return bytes() * 8; }
|
||||
|
||||
inline uint32 capacity() const { return _res; }
|
||||
|
||||
inline uint32 readable(void) const { return size() - rpos(); }
|
||||
inline uint32 writable(void) const { return size() - wpos(); } // free space left before realloc will occur
|
||||
|
||||
template <typename T> void append(T value)
|
||||
{
|
||||
ByteBufferTools::ToLittleEndian<T>(value);
|
||||
_enlargeIfReq(_wpos + sizeof(T));
|
||||
*((T*)(_buf + _wpos)) = value;
|
||||
_wpos += sizeof(T);
|
||||
if(_size < _wpos)
|
||||
_size = _wpos;
|
||||
}
|
||||
|
||||
void append(const void *src, uint32 bytes)
|
||||
{
|
||||
if (!bytes) return;
|
||||
_enlargeIfReq(_wpos + bytes);
|
||||
memcpy(_buf + _wpos, src, bytes);
|
||||
_wpos += bytes;
|
||||
if(_size < _wpos)
|
||||
_size = _wpos;
|
||||
}
|
||||
void append(const ByteBuffer& buffer)
|
||||
{
|
||||
if(buffer.size())
|
||||
append(buffer.contents(), buffer.size());
|
||||
}
|
||||
|
||||
void put(uint32 pos, const void *src, uint32 bytes)
|
||||
{
|
||||
memcpy(_buf + pos, src, bytes);
|
||||
}
|
||||
|
||||
template <typename T> void put(uint32 pos, T value)
|
||||
{
|
||||
if(pos >= size())
|
||||
BYTEBUFFER_EXCEPT(this, "put", sizeof(T));
|
||||
|
||||
ByteBufferTools::ToLittleEndian<T>(value);
|
||||
*((T*)(_buf + pos)) = value;
|
||||
}
|
||||
|
||||
inline bool growable(void) { return _growable; }
|
||||
inline void growable(bool b) { _growable = b; }
|
||||
|
||||
// dangerous functions
|
||||
|
||||
void _setPtr(void *p)
|
||||
{
|
||||
_buf = (uint8*)p;
|
||||
}
|
||||
|
||||
void _setAllocFunc(allocator_func f)
|
||||
{
|
||||
_allocfunc = f;
|
||||
}
|
||||
|
||||
void _setDelFunc(delete_func f)
|
||||
{
|
||||
_delfunc = f;
|
||||
}
|
||||
|
||||
void _setSize(uint32 s)
|
||||
{
|
||||
_size = s;
|
||||
}
|
||||
|
||||
void _setReserved(uint32 s)
|
||||
{
|
||||
_res = s;
|
||||
}
|
||||
|
||||
protected:
|
||||
|
||||
void _delete(void)
|
||||
{
|
||||
if(_mybuf)
|
||||
{
|
||||
if(_delfunc)
|
||||
_delfunc(_buf);
|
||||
else
|
||||
delete [] _buf;
|
||||
_buf = NULL;
|
||||
_res = 0;
|
||||
}
|
||||
}
|
||||
|
||||
// allocate larger buffer and copy contents. if we own the current buffer, delete old, otherwise, leave it as it is.
|
||||
void _allocate(uint32 s)
|
||||
{
|
||||
if(!_growable && _buf) // only throw if we already have a buf
|
||||
BYTEBUFFER_EXCEPT(this, "_alloc+locked", s);
|
||||
|
||||
// dangerous: It's up to the user to be sure that _allocfunc and _delfunc are matching
|
||||
uint8 *newbuf = (uint8*)(_allocfunc ? _allocfunc(s) : new char[s]);
|
||||
if(_buf)
|
||||
{
|
||||
memcpy(newbuf, _buf, _size);
|
||||
_delete();
|
||||
}
|
||||
_buf = newbuf;
|
||||
_res = s;
|
||||
_mybuf = true;
|
||||
|
||||
if (!_allocfunc)
|
||||
_delfunc = NULL;
|
||||
}
|
||||
|
||||
void _enlargeIfReq(uint32 minSize)
|
||||
{
|
||||
if(_res < minSize)
|
||||
{
|
||||
uint32 a = _res * 2;
|
||||
if(a < minSize) // fallback if doubling the space was not enough
|
||||
a += minSize;
|
||||
_allocate(a);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
};
|
||||
|
||||
|
||||
#undef BB_MAKE_WRITE_OP
|
||||
#undef BB_MAKE_READ_OP
|
||||
#undef BB_IS_BIG_ENDIAN
|
||||
|
||||
|
||||
|
||||
#endif
|
253
ExternalLibs/DeflateCompressor.cpp
Normal file
253
ExternalLibs/DeflateCompressor.cpp
Normal file
|
@ -0,0 +1,253 @@
|
|||
|
||||
#include <zlib.h>
|
||||
|
||||
#include "DeflateCompressor.h"
|
||||
|
||||
// for weird gcc/mingw hackfix below
|
||||
#include <string.h>
|
||||
|
||||
#define PRINTFAIL(s, ...) fprintf(stderr, (s "\n"), __VA_ARGS__)
|
||||
|
||||
|
||||
DeflateCompressor::DeflateCompressor()
|
||||
: _windowBits(-MAX_WBITS), // negative, because we want a raw deflate stream, and not zlib-wrapped
|
||||
_forceCompress(false),
|
||||
_iscompressed(false),
|
||||
_real_size(0)
|
||||
{
|
||||
}
|
||||
|
||||
ZlibCompressor::ZlibCompressor()
|
||||
: DeflateCompressor()
|
||||
{
|
||||
_windowBits = MAX_WBITS; // positive, means we use a zlib-wrapped deflate stream
|
||||
}
|
||||
|
||||
GzipCompressor::GzipCompressor()
|
||||
: DeflateCompressor()
|
||||
{
|
||||
_windowBits = MAX_WBITS + 16; // this makes zlib wrap a minimal gzip header around the stream
|
||||
_forceCompress = true; // we want this for gzip
|
||||
}
|
||||
|
||||
void DeflateCompressor::compress(void* dst, uint32 *dst_size, const void* src, uint32 src_size,
|
||||
uint8 level, int wbits)
|
||||
{
|
||||
z_stream c_stream;
|
||||
|
||||
c_stream.zalloc = (alloc_func)Z_NULL;
|
||||
c_stream.zfree = (free_func)Z_NULL;
|
||||
c_stream.opaque = (voidpf)Z_NULL;
|
||||
|
||||
if (Z_OK != deflateInit2(&c_stream, level, Z_DEFLATED, wbits, 8, Z_DEFAULT_STRATEGY))
|
||||
{
|
||||
*dst_size = 0;
|
||||
return;
|
||||
}
|
||||
|
||||
c_stream.next_out = (Bytef*)dst;
|
||||
c_stream.avail_out = *dst_size;
|
||||
c_stream.next_in = (Bytef*)src;
|
||||
c_stream.avail_in = (uInt)src_size;
|
||||
|
||||
int ret = deflate(&c_stream, Z_FINISH);
|
||||
|
||||
switch(ret)
|
||||
{
|
||||
case Z_STREAM_END:
|
||||
break; // all good
|
||||
case Z_OK:
|
||||
PRINTFAIL("ZLIB: Output buffer not large enough");
|
||||
*dst_size = 0;
|
||||
return;
|
||||
default:
|
||||
PRINTFAIL("ZLIB: Error %d", ret);
|
||||
*dst_size = 0;
|
||||
return;
|
||||
}
|
||||
|
||||
if (Z_OK != deflateEnd(&c_stream))
|
||||
{
|
||||
PRINTFAIL("Can't compress (zlib: deflateEnd)");
|
||||
*dst_size = 0;
|
||||
return;
|
||||
}
|
||||
|
||||
*dst_size = c_stream.total_out;
|
||||
}
|
||||
|
||||
void DeflateCompressor::decompress(void *dst, uint32 *origsize, const void *src, uint32 size, int wbits)
|
||||
{
|
||||
z_stream stream;
|
||||
int err;
|
||||
|
||||
stream.next_in = (Bytef*)src;
|
||||
stream.avail_in = (uInt)size;
|
||||
stream.next_out = (Bytef*)dst;
|
||||
stream.zalloc = Z_NULL;
|
||||
stream.zfree = Z_NULL;
|
||||
stream.opaque = Z_NULL;
|
||||
stream.avail_out = *origsize;
|
||||
stream.total_out = 0;
|
||||
|
||||
err = inflateInit2(&stream, wbits);
|
||||
if (err != Z_OK)
|
||||
{
|
||||
*origsize = 0;
|
||||
return;
|
||||
}
|
||||
|
||||
err = inflate(&stream, Z_FINISH);
|
||||
if (err != Z_STREAM_END)
|
||||
{
|
||||
inflateEnd(&stream);
|
||||
*origsize = 0;
|
||||
return;
|
||||
}
|
||||
*origsize = (uint32)stream.total_out;
|
||||
|
||||
err = inflateEnd(&stream);
|
||||
if(err != Z_OK)
|
||||
*origsize = 0;
|
||||
}
|
||||
|
||||
|
||||
void DeflateCompressor::Compress(uint8 level)
|
||||
{
|
||||
if(!_forceCompress && (!level || _iscompressed || (!size())))
|
||||
return;
|
||||
|
||||
char *buf;
|
||||
|
||||
|
||||
uint32 oldsize = size();
|
||||
uint32 newsize = compressBound(oldsize) + 30; // for optional gzip header
|
||||
|
||||
buf = new char[newsize];
|
||||
|
||||
compress((void*)buf, &newsize, (void*)contents(), oldsize, level, _windowBits);
|
||||
if(!newsize || (!_forceCompress && newsize > oldsize)) // only allow more data if compression is forced (which is the case for gzip)
|
||||
{
|
||||
delete [] buf;
|
||||
return;
|
||||
}
|
||||
|
||||
resize(newsize);
|
||||
rpos(0);
|
||||
wpos(0);
|
||||
append(buf,newsize);
|
||||
delete [] buf;
|
||||
|
||||
_iscompressed = true;
|
||||
|
||||
_real_size = oldsize;
|
||||
}
|
||||
|
||||
void DeflateCompressor::Decompress(void)
|
||||
{
|
||||
if( (!_iscompressed) || (!size()))
|
||||
return;
|
||||
|
||||
if(!_real_size)
|
||||
{
|
||||
if(decompressBlockwise() == Z_OK)
|
||||
_iscompressed = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
uint32 rs = (uint32)_real_size;
|
||||
uint32 origsize = rs;
|
||||
uint8 *target = new uint8[rs];
|
||||
wpos(0);
|
||||
rpos(0);
|
||||
decompress((void*)target, &origsize, (const void*)contents(), size(), _windowBits);
|
||||
if(origsize != rs)
|
||||
{
|
||||
PRINTFAIL("DeflateCompressor: Inflate error! cursize=%u origsize=%u realsize=%u",size(),origsize,rs);
|
||||
delete [] target;
|
||||
return;
|
||||
}
|
||||
clear();
|
||||
append(target, origsize);
|
||||
delete [] target;
|
||||
_real_size = 0;
|
||||
_iscompressed = false;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
#define CHUNK 16384
|
||||
|
||||
int DeflateCompressor::decompressBlockwise()
|
||||
{
|
||||
int ret;
|
||||
unsigned have;
|
||||
z_stream strm;
|
||||
unsigned char out[CHUNK];
|
||||
|
||||
/* allocate inflate state */
|
||||
strm.zalloc = Z_NULL;
|
||||
strm.zfree = Z_NULL;
|
||||
strm.opaque = Z_NULL;
|
||||
strm.avail_in = 0;
|
||||
strm.next_in = Z_NULL;
|
||||
ret = inflateInit2(&strm, _windowBits);
|
||||
if (ret != Z_OK)
|
||||
return ret;
|
||||
|
||||
ByteBuffer bb;
|
||||
|
||||
strm.avail_in = size();
|
||||
strm.next_in = contents();
|
||||
|
||||
/* decompress until deflate stream ends or end of file */
|
||||
do {
|
||||
/* run inflate() on input until output buffer not full */
|
||||
do {
|
||||
strm.avail_out = CHUNK;
|
||||
strm.next_out = out;
|
||||
ret = inflate(&strm, Z_NO_FLUSH);
|
||||
switch (ret) {
|
||||
case Z_NEED_DICT:
|
||||
case Z_STREAM_ERROR:
|
||||
case Z_BUF_ERROR:
|
||||
ret = Z_DATA_ERROR; /* and fall through */
|
||||
case Z_DATA_ERROR:
|
||||
case Z_MEM_ERROR:
|
||||
(void)inflateEnd(&strm);
|
||||
return ret;
|
||||
}
|
||||
have = CHUNK - strm.avail_out;
|
||||
bb.append(out, have);
|
||||
} while (strm.avail_out == 0);
|
||||
/* done when inflate() says it's done */
|
||||
} while (ret != Z_STREAM_END);
|
||||
|
||||
/* clean up and return */
|
||||
(void)inflateEnd(&strm);
|
||||
|
||||
if (ret != Z_STREAM_END)
|
||||
return Z_DATA_ERROR;
|
||||
|
||||
// exchange pointer
|
||||
clear();
|
||||
init(bb, TAKE_OVER);
|
||||
|
||||
return Z_OK;
|
||||
}
|
||||
|
||||
void GzipCompressor::Decompress(void)
|
||||
{
|
||||
uint32 t = 0;
|
||||
rpos(size() - sizeof(uint32)); // according to RFC 1952, input size are the last 4 bytes at the end of the file, in little endian
|
||||
*this >> t;
|
||||
_real_size = t;
|
||||
|
||||
// !! NOTE: this fixes a gcc/mingw bug where _real_size would be set incorrectly
|
||||
#if __GNUC__
|
||||
char xx[20];
|
||||
sprintf(xx, "%u", t);
|
||||
#endif
|
||||
|
||||
DeflateCompressor::Decompress(); // will set rpos back anyway
|
||||
}
|
59
ExternalLibs/DeflateCompressor.h
Normal file
59
ExternalLibs/DeflateCompressor.h
Normal file
|
@ -0,0 +1,59 @@
|
|||
#ifndef DEFLATE_COMPRESSOR_H
|
||||
#define DEFLATE_COMPRESSOR_H
|
||||
|
||||
#include "ByteBuffer.h"
|
||||
|
||||
// implements a raw deflate stream, not zlib wrapped, and not checksummed.
|
||||
class DeflateCompressor : public ByteBuffer
|
||||
{
|
||||
public:
|
||||
DeflateCompressor();
|
||||
virtual ~DeflateCompressor() {}
|
||||
virtual void Compress(uint8 level = 1);
|
||||
virtual void Decompress(void);
|
||||
|
||||
bool Compressed(void) const { return _iscompressed; }
|
||||
void Compressed(bool b) { _iscompressed = b; }
|
||||
void SetForceCompression(bool f) { _forceCompress = f; }
|
||||
uint32 RealSize(void) const { return _iscompressed ? _real_size : size(); }
|
||||
void RealSize(uint32 realsize) { _real_size = realsize; }
|
||||
void clear(void) // not required to be strictly virtual; be careful not to mess up static types!
|
||||
{
|
||||
ByteBuffer::clear();
|
||||
_real_size = 0;
|
||||
_iscompressed = false;
|
||||
}
|
||||
|
||||
protected:
|
||||
int _windowBits; // read zlib docs to know what this means
|
||||
unsigned int _real_size;
|
||||
bool _forceCompress;
|
||||
bool _iscompressed;
|
||||
|
||||
private:
|
||||
static void decompress(void *dst, uint32 *origsize, const void *src, uint32 size, int wbits);
|
||||
static void compress(void* dst, uint32 *dst_size, const void* src, uint32 src_size,
|
||||
uint8 level, int wbits);
|
||||
|
||||
int decompressBlockwise();
|
||||
};
|
||||
|
||||
// implements deflate stream, zlib wrapped
|
||||
class ZlibCompressor : public DeflateCompressor
|
||||
{
|
||||
public:
|
||||
ZlibCompressor();
|
||||
virtual ~ZlibCompressor() {}
|
||||
};
|
||||
|
||||
// the output produced by this stream contains a minimal gzip header,
|
||||
// and can be directly written to a .gz file.
|
||||
class GzipCompressor : public DeflateCompressor
|
||||
{
|
||||
public:
|
||||
GzipCompressor();
|
||||
virtual ~GzipCompressor() {}
|
||||
virtual void Decompress(void);
|
||||
};
|
||||
|
||||
#endif
|
|
@ -7,9 +7,8 @@
|
|||
|
||||
//STL headers
|
||||
#include <string>
|
||||
#include <utility>
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include "FileAPI.h"
|
||||
#include "ByteBuffer.h"
|
||||
using namespace std;
|
||||
|
||||
//OpenGL headers
|
||||
|
@ -22,33 +21,10 @@ using namespace std;
|
|||
|
||||
#include "GL/gl.h"
|
||||
|
||||
#include "SDL_endian.h"
|
||||
|
||||
//glFont header
|
||||
#include "glfont2.h"
|
||||
using namespace glfont;
|
||||
|
||||
static int read_int(ifstream &input)
|
||||
{
|
||||
int buffer;
|
||||
|
||||
input.read((char *)&buffer, 4);
|
||||
return SDL_SwapLE32(buffer);
|
||||
}
|
||||
|
||||
static float read_float(ifstream &input)
|
||||
{
|
||||
union
|
||||
{
|
||||
int i;
|
||||
float f;
|
||||
} buffer;
|
||||
|
||||
input.read((char *)&buffer.i, 4);
|
||||
buffer.i = SDL_SwapLE32(buffer.i);
|
||||
return buffer.f;
|
||||
}
|
||||
|
||||
//*******************************************************************
|
||||
//GLFont Class Implementation
|
||||
//*******************************************************************
|
||||
|
@ -71,27 +47,44 @@ GLFont::~GLFont ()
|
|||
//*******************************************************************
|
||||
bool GLFont::Create (const char *file_name, int tex, bool loadTexture)
|
||||
{
|
||||
ifstream input;
|
||||
int num_chars, num_tex_bytes;
|
||||
char *tex_bytes;
|
||||
|
||||
//Destroy the old font if there was one, just to be safe
|
||||
Destroy();
|
||||
|
||||
|
||||
#ifdef BBGE_BUILD_VFS
|
||||
//Open input file
|
||||
input.open(file_name, ios::in | ios::binary);
|
||||
if (!input)
|
||||
ttvfs::VFSFile *vf = vfs.GetFile(file_name);
|
||||
if (!vf)
|
||||
return false;
|
||||
ByteBuffer bb((void*)vf->getBuf(), vf->size(), ByteBuffer::TAKE_OVER);
|
||||
vf->dropBuf(false);
|
||||
#else
|
||||
VFILE *fh = vfopen(file_name, "rb");
|
||||
if (!fh)
|
||||
return false;
|
||||
vfseek(fh, 0, SEEK_END);
|
||||
long int sz = vftell(fh);
|
||||
vfseek(fh, 0, SEEK_SET);
|
||||
ByteBuffer bb(sz);
|
||||
bb.resize(sz);
|
||||
vfread(bb.contents(), 1, sz, fh);
|
||||
vfclose(fh);
|
||||
#endif
|
||||
|
||||
int dummy;
|
||||
|
||||
// Read the header from file
|
||||
header.tex = tex;
|
||||
input.seekg(4, ios::cur); // skip tex field
|
||||
header.tex_width = read_int(input);
|
||||
header.tex_height = read_int(input);
|
||||
header.start_char = read_int(input);
|
||||
header.end_char = read_int(input);
|
||||
input.seekg(4, ios::cur); // skip chars field
|
||||
|
||||
bb >> dummy; // skip tex field
|
||||
bb >> header.tex_width;
|
||||
bb >> header.tex_height;
|
||||
bb >> header.start_char;
|
||||
bb >> header.end_char;
|
||||
bb >> dummy; // skip chars field
|
||||
|
||||
//Allocate space for character array
|
||||
num_chars = header.end_char - header.start_char + 1;
|
||||
if ((header.chars = new GLFontChar[num_chars]) == NULL)
|
||||
|
@ -100,19 +93,19 @@ bool GLFont::Create (const char *file_name, int tex, bool loadTexture)
|
|||
//Read character array
|
||||
for (int i = 0; i < num_chars; i++)
|
||||
{
|
||||
header.chars[i].dx = read_float(input);
|
||||
header.chars[i].dy = read_float(input);
|
||||
header.chars[i].tx1 = read_float(input);
|
||||
header.chars[i].ty1 = read_float(input);
|
||||
header.chars[i].tx2 = read_float(input);
|
||||
header.chars[i].ty2 = read_float(input);
|
||||
bb >> header.chars[i].dx;
|
||||
bb >> header.chars[i].dy;
|
||||
bb >> header.chars[i].tx1;
|
||||
bb >> header.chars[i].ty1;
|
||||
bb >> header.chars[i].tx2;
|
||||
bb >> header.chars[i].ty2;
|
||||
}
|
||||
|
||||
//Read texture pixel data
|
||||
num_tex_bytes = header.tex_width * header.tex_height * 2;
|
||||
tex_bytes = new char[num_tex_bytes];
|
||||
input.read(tex_bytes, num_tex_bytes);
|
||||
|
||||
//input.read(tex_bytes, num_tex_bytes);
|
||||
bb.read(tex_bytes, num_tex_bytes);
|
||||
|
||||
//Build2DMipmaps(3, header.tex_width, header.tex_height, GL_UNSIGNED_BYTE, tex_bytes, 1);
|
||||
|
||||
|
@ -147,9 +140,6 @@ bool GLFont::Create (const char *file_name, int tex, bool loadTexture)
|
|||
//Free texture pixels memory
|
||||
delete[] tex_bytes;
|
||||
|
||||
//Close input file
|
||||
input.close();
|
||||
|
||||
//Return successfully
|
||||
return true;
|
||||
}
|
||||
|
|
|
@ -97,9 +97,11 @@ extern int APIENTRY pngLoadRawF(FILE *file, pngRawInfo *rawinfo);
|
|||
|
||||
extern int APIENTRY pngLoad(const char *filename, int mipmap, int trans, pngInfo *info);
|
||||
extern int APIENTRY pngLoadF(FILE *file, int mipmap, int trans, pngInfo *info);
|
||||
extern int APIENTRY pngLoadMem(const char *mem, int size, int mipmap, int trans, pngInfo *info);
|
||||
|
||||
extern unsigned int APIENTRY pngBind(const char *filename, int mipmap, int trans, pngInfo *info, int wrapst, int minfilter, int magfilter);
|
||||
extern unsigned int APIENTRY pngBindF(FILE *file, int mipmap, int trans, pngInfo *info, int wrapst, int minfilter, int magfilter);
|
||||
extern unsigned int APIENTRY pngBindMem(const char *mem, int size, int mipmap, int trans, pngInfo *info, int wrapst, int minfilter, int magfilter);
|
||||
|
||||
extern void APIENTRY pngSetStencil(unsigned char red, unsigned char green, unsigned char blue);
|
||||
extern void APIENTRY pngSetAlphaCallback(unsigned char (*callback)(unsigned char red, unsigned char green, unsigned char blue));
|
||||
|
|
|
@ -1,29 +1,29 @@
|
|||
/*
|
||||
* PNG loader library for OpenGL v1.45 (10/07/00)
|
||||
* by Ben Wyatt ben@wyatt100.freeserve.co.uk
|
||||
* Using LibPNG 1.0.2 and ZLib 1.1.3
|
||||
*
|
||||
* This software is provided 'as-is', without any express or implied warranty.
|
||||
* In no event will the author be held liable for any damages arising from the
|
||||
* use of this software.
|
||||
*
|
||||
* Permission is hereby granted to use, copy, modify, and distribute this
|
||||
* source code, or portions hereof, for any purpose, without fee, subject to
|
||||
* the following restrictions:
|
||||
*
|
||||
* 1. The origin of this source code must not be misrepresented. You must not
|
||||
* claim that you wrote the original software. If you use this software in
|
||||
* a product, an acknowledgment in the product documentation would be
|
||||
* appreciated but is not required.
|
||||
* 2. Altered versions must be plainly marked as such and must not be
|
||||
* misrepresented as being the original source.
|
||||
* 3. This notice must not be removed or altered from any source distribution.
|
||||
*/
|
||||
* PNG loader library for OpenGL v1.45 (10/07/00)
|
||||
* by Ben Wyatt ben@wyatt100.freeserve.co.uk
|
||||
* Using LibPNG 1.0.2 and ZLib 1.1.3
|
||||
*
|
||||
* This software is provided 'as-is', without any express or implied warranty.
|
||||
* In no event will the author be held liable for any damages arising from the
|
||||
* use of this software.
|
||||
*
|
||||
* Permission is hereby granted to use, copy, modify, and distribute this
|
||||
* source code, or portions hereof, for any purpose, without fee, subject to
|
||||
* the following restrictions:
|
||||
*
|
||||
* 1. The origin of this source code must not be misrepresented. You must not
|
||||
* claim that you wrote the original software. If you use this software in
|
||||
* a product, an acknowledgment in the product documentation would be
|
||||
* appreciated but is not required.
|
||||
* 2. Altered versions must be plainly marked as such and must not be
|
||||
* misrepresented as being the original source.
|
||||
* 3. This notice must not be removed or altered from any source distribution.
|
||||
*/
|
||||
|
||||
|
||||
#ifdef _WIN32 /* Stupid Windows needs to include windows.h before gl.h */
|
||||
#undef FAR
|
||||
#include <windows.h>
|
||||
#undef FAR
|
||||
#include <windows.h>
|
||||
#endif
|
||||
|
||||
#include "../glpng.h"
|
||||
|
@ -55,17 +55,17 @@ static int PalettedTextures = -1;
|
|||
static GLint MaxTextureSize = 0;
|
||||
|
||||
/* screenGamma = displayGamma/viewingGamma
|
||||
* displayGamma = CRT has gamma of ~2.2
|
||||
* viewingGamma depends on platform. PC is 1.0, Mac is 1.45, SGI defaults
|
||||
* to 1.7, but this can be checked and changed w/ /usr/sbin/gamma command.
|
||||
* If the environment variable VIEWING_GAMMA is set, adjust gamma per this value.
|
||||
*/
|
||||
* displayGamma = CRT has gamma of ~2.2
|
||||
* viewingGamma depends on platform. PC is 1.0, Mac is 1.45, SGI defaults
|
||||
* to 1.7, but this can be checked and changed w/ /usr/sbin/gamma command.
|
||||
* If the environment variable VIEWING_GAMMA is set, adjust gamma per this value.
|
||||
*/
|
||||
#ifdef _MAC
|
||||
static double screenGamma = 2.2 / 1.45;
|
||||
static double screenGamma = 2.2 / 1.45;
|
||||
#elif SGI
|
||||
static double screenGamma = 2.2 / 1.7;
|
||||
static double screenGamma = 2.2 / 1.7;
|
||||
#else /* PC/default */
|
||||
static double screenGamma = 2.2 / 1.0;
|
||||
static double screenGamma = 2.2 / 1.0;
|
||||
#endif
|
||||
|
||||
static char gammaExplicit = 0; /*if */
|
||||
|
@ -259,8 +259,8 @@ int APIENTRY pngLoadRawF(FILE *fp, pngRawInfo *pinfo) {
|
|||
png_infop info;
|
||||
png_infop endinfo;
|
||||
png_bytep data;
|
||||
png_bytep *row_p;
|
||||
double fileGamma;
|
||||
png_bytep *row_p;
|
||||
double fileGamma;
|
||||
|
||||
png_uint_32 width, height;
|
||||
int depth, color;
|
||||
|
@ -365,8 +365,8 @@ int APIENTRY pngLoadF(FILE *fp, int mipmap, int trans, pngInfo *pinfo) {
|
|||
png_infop info;
|
||||
png_infop endinfo;
|
||||
png_bytep data, data2;
|
||||
png_bytep *row_p;
|
||||
double fileGamma;
|
||||
png_bytep *row_p;
|
||||
double fileGamma;
|
||||
|
||||
png_uint_32 width, height, rw, rh;
|
||||
int depth, color;
|
||||
|
@ -402,20 +402,20 @@ int APIENTRY pngLoadF(FILE *fp, int mipmap, int trans, pngInfo *pinfo) {
|
|||
if (MaxTextureSize == 0)
|
||||
glGetIntegerv(GL_MAX_TEXTURE_SIZE, &MaxTextureSize);
|
||||
|
||||
#ifdef SUPPORTS_PALETTE_EXT
|
||||
#ifdef _WIN32
|
||||
if (PalettedTextures == -1)
|
||||
PalettedTextures = ExtSupported("GL_EXT_paletted_texture") && (strstr((const char *) glGetString(GL_VERSION), "1.1.0 3Dfx Beta") == NULL);
|
||||
#ifdef SUPPORTS_PALETTE_EXT
|
||||
#ifdef _WIN32
|
||||
if (PalettedTextures == -1)
|
||||
PalettedTextures = ExtSupported("GL_EXT_paletted_texture") && (strstr((const char *) glGetString(GL_VERSION), "1.1.0 3Dfx Beta") == NULL);
|
||||
|
||||
if (PalettedTextures) {
|
||||
if (glColorTableEXT == NULL) {
|
||||
glColorTableEXT = (PFNGLCOLORTABLEEXTPROC) wglGetProcAddress("glColorTableEXT");
|
||||
if (glColorTableEXT == NULL)
|
||||
PalettedTextures = 0;
|
||||
}
|
||||
if (PalettedTextures) {
|
||||
if (glColorTableEXT == NULL) {
|
||||
glColorTableEXT = (PFNGLCOLORTABLEEXTPROC) wglGetProcAddress("glColorTableEXT");
|
||||
if (glColorTableEXT == NULL)
|
||||
PalettedTextures = 0;
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
|
||||
if (PalettedTextures == -1)
|
||||
PalettedTextures = 0;
|
||||
|
@ -461,10 +461,10 @@ int APIENTRY pngLoadF(FILE *fp, int mipmap, int trans, pngInfo *pinfo) {
|
|||
|
||||
data2 = (png_bytep) malloc(rw*rh*channels);
|
||||
|
||||
/* Doesn't work on certain sizes */
|
||||
/* if (gluScaleImage(glformat, width, height, GL_UNSIGNED_BYTE, data, rw, rh, GL_UNSIGNED_BYTE, data2) != 0)
|
||||
return 0;
|
||||
*/
|
||||
/* Doesn't work on certain sizes */
|
||||
/* if (gluScaleImage(glformat, width, height, GL_UNSIGNED_BYTE, data, rw, rh, GL_UNSIGNED_BYTE, data2) != 0)
|
||||
return 0;
|
||||
*/
|
||||
Resize(channels, data, width, height, data2, rw, rh);
|
||||
|
||||
width = rw, height = rh;
|
||||
|
@ -478,7 +478,7 @@ int APIENTRY pngLoadF(FILE *fp, int mipmap, int trans, pngInfo *pinfo) {
|
|||
glPixelStorei(GL_PACK_ALIGNMENT, 1);
|
||||
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
|
||||
|
||||
#ifdef SUPPORTS_PALETTE_EXT
|
||||
#ifdef SUPPORTS_PALETTE_EXT
|
||||
if (PalettedTextures && mipmap >= 0 && trans == PNG_SOLID && color == PNG_COLOR_TYPE_PALETTE) {
|
||||
png_colorp pal;
|
||||
int cols;
|
||||
|
@ -502,12 +502,12 @@ int APIENTRY pngLoadF(FILE *fp, int mipmap, int trans, pngInfo *pinfo) {
|
|||
glTexImage2D(GL_TEXTURE_2D, mipmap, intf, width, height, 0, GL_COLOR_INDEX, GL_UNSIGNED_BYTE, data);
|
||||
}
|
||||
else
|
||||
#endif
|
||||
if (trans == PNG_SOLID || trans == PNG_ALPHA || trans == PNG_LUMINANCEALPHA || color == PNG_COLOR_TYPE_RGB_ALPHA || color == PNG_COLOR_TYPE_GRAY_ALPHA) {
|
||||
GLenum glformat;
|
||||
GLint glcomponent;
|
||||
#endif
|
||||
if (trans == PNG_SOLID || trans == PNG_ALPHA || trans == PNG_LUMINANCEALPHA || color == PNG_COLOR_TYPE_RGB_ALPHA || color == PNG_COLOR_TYPE_GRAY_ALPHA) {
|
||||
GLenum glformat;
|
||||
GLint glcomponent;
|
||||
|
||||
switch (color) {
|
||||
switch (color) {
|
||||
case PNG_COLOR_TYPE_GRAY:
|
||||
case PNG_COLOR_TYPE_RGB:
|
||||
case PNG_COLOR_TYPE_PALETTE:
|
||||
|
@ -526,48 +526,48 @@ int APIENTRY pngLoadF(FILE *fp, int mipmap, int trans, pngInfo *pinfo) {
|
|||
default:
|
||||
/*puts("glformat not set");*/
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (trans == PNG_LUMINANCEALPHA)
|
||||
glformat = GL_LUMINANCE_ALPHA;
|
||||
|
||||
if (mipmap == PNG_BUILDMIPMAPS)
|
||||
Build2DMipmaps(glcomponent, width, height, glformat, data, 1);
|
||||
else if (mipmap == PNG_SIMPLEMIPMAPS)
|
||||
Build2DMipmaps(glcomponent, width, height, glformat, data, 0);
|
||||
else
|
||||
glTexImage2D(GL_TEXTURE_2D, mipmap, glcomponent, width, height, 0, glformat, GL_UNSIGNED_BYTE, data);
|
||||
}
|
||||
else {
|
||||
png_bytep p, endp, q;
|
||||
int r, g, b, a;
|
||||
|
||||
if (trans == PNG_LUMINANCEALPHA)
|
||||
glformat = GL_LUMINANCE_ALPHA;
|
||||
p = data, endp = p+width*height*3;
|
||||
q = data2 = (png_bytep) malloc(sizeof(png_byte)*width*height*4);
|
||||
|
||||
if (mipmap == PNG_BUILDMIPMAPS)
|
||||
Build2DMipmaps(glcomponent, width, height, glformat, data, 1);
|
||||
else if (mipmap == PNG_SIMPLEMIPMAPS)
|
||||
Build2DMipmaps(glcomponent, width, height, glformat, data, 0);
|
||||
else
|
||||
glTexImage2D(GL_TEXTURE_2D, mipmap, glcomponent, width, height, 0, glformat, GL_UNSIGNED_BYTE, data);
|
||||
}
|
||||
else {
|
||||
png_bytep p, endp, q;
|
||||
int r, g, b, a;
|
||||
if (pinfo != NULL) pinfo->Alpha = 8;
|
||||
|
||||
p = data, endp = p+width*height*3;
|
||||
q = data2 = (png_bytep) malloc(sizeof(png_byte)*width*height*4);
|
||||
#define FORSTART \
|
||||
do { \
|
||||
r = *p++; /*red */ \
|
||||
g = *p++; /*green*/ \
|
||||
b = *p++; /*blue */ \
|
||||
*q++ = r; \
|
||||
*q++ = g; \
|
||||
*q++ = b;
|
||||
|
||||
if (pinfo != NULL) pinfo->Alpha = 8;
|
||||
#define FOREND \
|
||||
q++; \
|
||||
} while (p != endp);
|
||||
|
||||
#define FORSTART \
|
||||
do { \
|
||||
r = *p++; /*red */ \
|
||||
g = *p++; /*green*/ \
|
||||
b = *p++; /*blue */ \
|
||||
*q++ = r; \
|
||||
*q++ = g; \
|
||||
*q++ = b;
|
||||
#define ALPHA *q
|
||||
|
||||
#define FOREND \
|
||||
q++; \
|
||||
} while (p != endp);
|
||||
|
||||
#define ALPHA *q
|
||||
|
||||
switch (trans) {
|
||||
switch (trans) {
|
||||
case PNG_CALLBACKT:
|
||||
FORSTART
|
||||
ALPHA = AlphaCallback((unsigned char) r, (unsigned char) g, (unsigned char) b);
|
||||
FOREND
|
||||
break;
|
||||
break;
|
||||
|
||||
case PNG_STENCIL:
|
||||
FORSTART
|
||||
|
@ -576,76 +576,76 @@ int APIENTRY pngLoadF(FILE *fp, int mipmap, int trans, pngInfo *pinfo) {
|
|||
else
|
||||
ALPHA = 255;
|
||||
FOREND
|
||||
break;
|
||||
break;
|
||||
|
||||
case PNG_BLEND1:
|
||||
FORSTART
|
||||
a = r+g+b;
|
||||
if (a > 255) ALPHA = 255; else ALPHA = a;
|
||||
if (a > 255) ALPHA = 255; else ALPHA = a;
|
||||
FOREND
|
||||
break;
|
||||
break;
|
||||
|
||||
case PNG_BLEND2:
|
||||
FORSTART
|
||||
a = r+g+b;
|
||||
if (a > 255*2) ALPHA = 255; else ALPHA = a/2;
|
||||
if (a > 255*2) ALPHA = 255; else ALPHA = a/2;
|
||||
FOREND
|
||||
break;
|
||||
break;
|
||||
|
||||
case PNG_BLEND3:
|
||||
FORSTART
|
||||
ALPHA = (r+g+b)/3;
|
||||
FOREND
|
||||
break;
|
||||
break;
|
||||
|
||||
case PNG_BLEND4:
|
||||
FORSTART
|
||||
a = r*r+g*g+b*b;
|
||||
if (a > 255) ALPHA = 255; else ALPHA = a;
|
||||
if (a > 255) ALPHA = 255; else ALPHA = a;
|
||||
FOREND
|
||||
break;
|
||||
break;
|
||||
|
||||
case PNG_BLEND5:
|
||||
FORSTART
|
||||
a = r*r+g*g+b*b;
|
||||
if (a > 255*2) ALPHA = 255; else ALPHA = a/2;
|
||||
if (a > 255*2) ALPHA = 255; else ALPHA = a/2;
|
||||
FOREND
|
||||
break;
|
||||
break;
|
||||
|
||||
case PNG_BLEND6:
|
||||
FORSTART
|
||||
a = r*r+g*g+b*b;
|
||||
if (a > 255*3) ALPHA = 255; else ALPHA = a/3;
|
||||
if (a > 255*3) ALPHA = 255; else ALPHA = a/3;
|
||||
FOREND
|
||||
break;
|
||||
break;
|
||||
|
||||
case PNG_BLEND7:
|
||||
FORSTART
|
||||
a = r*r+g*g+b*b;
|
||||
if (a > 255*255) ALPHA = 255; else ALPHA = (int) sqrt(a);
|
||||
if (a > 255*255) ALPHA = 255; else ALPHA = (int) sqrt(a);
|
||||
FOREND
|
||||
break;
|
||||
break;
|
||||
}
|
||||
|
||||
#undef FORSTART
|
||||
#undef FOREND
|
||||
#undef ALPHA
|
||||
|
||||
if (mipmap == PNG_BUILDMIPMAPS)
|
||||
Build2DMipmaps(4, width, height, GL_RGBA, data2, 1);
|
||||
else if (mipmap == PNG_SIMPLEMIPMAPS)
|
||||
Build2DMipmaps(4, width, height, GL_RGBA, data2, 0);
|
||||
else
|
||||
glTexImage2D(GL_TEXTURE_2D, mipmap, 4, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data2);
|
||||
|
||||
free(data2);
|
||||
}
|
||||
|
||||
#undef FORSTART
|
||||
#undef FOREND
|
||||
#undef ALPHA
|
||||
|
||||
if (mipmap == PNG_BUILDMIPMAPS)
|
||||
Build2DMipmaps(4, width, height, GL_RGBA, data2, 1);
|
||||
else if (mipmap == PNG_SIMPLEMIPMAPS)
|
||||
Build2DMipmaps(4, width, height, GL_RGBA, data2, 0);
|
||||
else
|
||||
glTexImage2D(GL_TEXTURE_2D, mipmap, 4, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data2);
|
||||
|
||||
free(data2);
|
||||
}
|
||||
|
||||
glPixelStorei(GL_PACK_ALIGNMENT, pack);
|
||||
glPixelStorei(GL_UNPACK_ALIGNMENT, unpack);
|
||||
glPixelStorei(GL_PACK_ALIGNMENT, pack);
|
||||
glPixelStorei(GL_UNPACK_ALIGNMENT, unpack);
|
||||
} /* OpenGL end */
|
||||
|
||||
png_read_end(png, endinfo);
|
||||
png_read_end(png, endinfo);
|
||||
png_destroy_read_struct(&png, &info, &endinfo);
|
||||
|
||||
free(data);
|
||||
|
@ -684,6 +684,14 @@ unsigned int APIENTRY pngBindF(FILE *file, int mipmap, int trans, pngInfo *info,
|
|||
return 0;
|
||||
}
|
||||
|
||||
unsigned int APIENTRY pngBindMem(const char *mem, int size, int mipmap, int trans, pngInfo *info, int wrapst, int minfilter, int magfilter) {
|
||||
unsigned int id = SetParams(wrapst, magfilter, minfilter);
|
||||
|
||||
if (id != 0 && pngLoadMem(mem, size, mipmap, trans, info))
|
||||
return id;
|
||||
return 0;
|
||||
}
|
||||
|
||||
void APIENTRY pngSetStencil(unsigned char red, unsigned char green, unsigned char blue) {
|
||||
StencilRed = red, StencilGreen = green, StencilBlue = blue;
|
||||
}
|
||||
|
@ -710,3 +718,329 @@ void APIENTRY pngSetStandardOrientation(int standardorientation) {
|
|||
StandardOrientation = standardorientation;
|
||||
}
|
||||
|
||||
|
||||
|
||||
// -- added memory read functions --
|
||||
|
||||
/*pointer to a new input function that takes as its
|
||||
arguments a pointer to a png_struct, a pointer to
|
||||
a location where input data can be stored, and a 32-bit
|
||||
unsigned int that is the number of bytes to be read.
|
||||
To exit and output any fatal error messages the new write
|
||||
function should call png_error(png_ptr, "Error msg"). */
|
||||
|
||||
typedef struct glpng_memread_struct
|
||||
{
|
||||
png_bytep mem;
|
||||
png_size_t rpos;
|
||||
} glpng_memread;
|
||||
|
||||
void glpng_read_mem(png_structp png, png_bytep dst, png_size_t size)
|
||||
{
|
||||
glpng_memread *mr = (glpng_memread*)png_get_io_ptr(png);
|
||||
memcpy(dst, mr->mem + mr->rpos, size);
|
||||
mr->rpos += size;
|
||||
}
|
||||
|
||||
int APIENTRY pngLoadMem(const char *mem, int size, int mipmap, int trans, pngInfo *pinfo) {
|
||||
GLint pack, unpack;
|
||||
unsigned char header[8];
|
||||
png_structp png;
|
||||
png_infop info;
|
||||
png_infop endinfo;
|
||||
png_bytep data, data2;
|
||||
png_bytep *row_p;
|
||||
double fileGamma;
|
||||
|
||||
png_uint_32 width, height, rw, rh;
|
||||
int depth, color;
|
||||
|
||||
png_uint_32 i;
|
||||
glpng_memread memread;
|
||||
|
||||
if(size < 8)
|
||||
return 0; // error
|
||||
|
||||
memcpy(header, mem, 8);
|
||||
|
||||
if (!png_check_sig(header, 8))
|
||||
return 0;
|
||||
|
||||
png = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
|
||||
info = png_create_info_struct(png);
|
||||
endinfo = png_create_info_struct(png);
|
||||
|
||||
// DH: added following lines
|
||||
if (setjmp(png_jmpbuf(png)))
|
||||
{
|
||||
png_destroy_read_struct(&png, &info, &endinfo);
|
||||
return 0;
|
||||
}
|
||||
// ~DH
|
||||
|
||||
memread.rpos = 0;
|
||||
memread.mem = ((png_bytep)mem) + 8;
|
||||
png_set_read_fn(png, &memread, glpng_read_mem);
|
||||
png_set_sig_bytes(png, 8);
|
||||
png_read_info(png, info);
|
||||
png_get_IHDR(png, info, &width, &height, &depth, &color, NULL, NULL, NULL);
|
||||
|
||||
if (pinfo != NULL) {
|
||||
pinfo->Width = width;
|
||||
pinfo->Height = height;
|
||||
pinfo->Depth = depth;
|
||||
}
|
||||
|
||||
if (MaxTextureSize == 0)
|
||||
glGetIntegerv(GL_MAX_TEXTURE_SIZE, &MaxTextureSize);
|
||||
|
||||
#ifdef SUPPORTS_PALETTE_EXT
|
||||
#ifdef _WIN32
|
||||
if (PalettedTextures == -1)
|
||||
PalettedTextures = ExtSupported("GL_EXT_paletted_texture") && (strstr((const char *) glGetString(GL_VERSION), "1.1.0 3Dfx Beta") == NULL);
|
||||
|
||||
if (PalettedTextures) {
|
||||
if (glColorTableEXT == NULL) {
|
||||
glColorTableEXT = (PFNGLCOLORTABLEEXTPROC) wglGetProcAddress("glColorTableEXT");
|
||||
if (glColorTableEXT == NULL)
|
||||
PalettedTextures = 0;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
|
||||
if (PalettedTextures == -1)
|
||||
PalettedTextures = 0;
|
||||
|
||||
if (color == PNG_COLOR_TYPE_GRAY || color == PNG_COLOR_TYPE_GRAY_ALPHA)
|
||||
png_set_gray_to_rgb(png);
|
||||
|
||||
if (color&PNG_COLOR_MASK_ALPHA && trans != PNG_ALPHA) {
|
||||
png_set_strip_alpha(png);
|
||||
color &= ~PNG_COLOR_MASK_ALPHA;
|
||||
}
|
||||
|
||||
if (!(PalettedTextures && mipmap >= 0 && trans == PNG_SOLID))
|
||||
if (color == PNG_COLOR_TYPE_PALETTE)
|
||||
png_set_expand(png);
|
||||
|
||||
/*--GAMMA--*/
|
||||
checkForGammaEnv();
|
||||
if (png_get_gAMA(png, info, &fileGamma))
|
||||
png_set_gamma(png, screenGamma, fileGamma);
|
||||
else
|
||||
png_set_gamma(png, screenGamma, 1.0/2.2);
|
||||
|
||||
png_read_update_info(png, info);
|
||||
|
||||
data = (png_bytep) malloc(png_get_rowbytes(png, info)*height);
|
||||
row_p = (png_bytep *) malloc(sizeof(png_bytep)*height);
|
||||
|
||||
for (i = 0; i < height; i++) {
|
||||
if (StandardOrientation)
|
||||
row_p[height - 1 - i] = &data[png_get_rowbytes(png, info)*i];
|
||||
else
|
||||
row_p[i] = &data[png_get_rowbytes(png, info)*i];
|
||||
}
|
||||
|
||||
png_read_image(png, row_p);
|
||||
free(row_p);
|
||||
|
||||
rw = SafeSize(width), rh = SafeSize(height);
|
||||
|
||||
if (rw != width || rh != height) {
|
||||
const int channels = png_get_rowbytes(png, info)/width;
|
||||
|
||||
data2 = (png_bytep) malloc(rw*rh*channels);
|
||||
|
||||
/* Doesn't work on certain sizes */
|
||||
/* if (gluScaleImage(glformat, width, height, GL_UNSIGNED_BYTE, data, rw, rh, GL_UNSIGNED_BYTE, data2) != 0)
|
||||
return 0;
|
||||
*/
|
||||
Resize(channels, data, width, height, data2, rw, rh);
|
||||
|
||||
width = rw, height = rh;
|
||||
free(data);
|
||||
data = data2;
|
||||
}
|
||||
|
||||
{ /* OpenGL stuff */
|
||||
glGetIntegerv(GL_PACK_ALIGNMENT, &pack);
|
||||
glGetIntegerv(GL_UNPACK_ALIGNMENT, &unpack);
|
||||
glPixelStorei(GL_PACK_ALIGNMENT, 1);
|
||||
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
|
||||
|
||||
#ifdef SUPPORTS_PALETTE_EXT
|
||||
if (PalettedTextures && mipmap >= 0 && trans == PNG_SOLID && color == PNG_COLOR_TYPE_PALETTE) {
|
||||
png_colorp pal;
|
||||
int cols;
|
||||
GLint intf;
|
||||
|
||||
if (pinfo != NULL) pinfo->Alpha = 0;
|
||||
png_get_PLTE(png, info, &pal, &cols);
|
||||
|
||||
switch (cols) {
|
||||
case 1<<1: intf = GL_COLOR_INDEX1_EXT; break;
|
||||
case 1<<2: intf = GL_COLOR_INDEX2_EXT; break;
|
||||
case 1<<4: intf = GL_COLOR_INDEX4_EXT; break;
|
||||
case 1<<8: intf = GL_COLOR_INDEX8_EXT; break;
|
||||
case 1<<12: intf = GL_COLOR_INDEX12_EXT; break;
|
||||
case 1<<16: intf = GL_COLOR_INDEX16_EXT; break;
|
||||
default:
|
||||
/*printf("Warning: Colour depth %i not recognised\n", cols);*/
|
||||
return 0;
|
||||
}
|
||||
glColorTableEXT(GL_TEXTURE_2D, GL_RGB8, cols, GL_RGB, GL_UNSIGNED_BYTE, pal);
|
||||
glTexImage2D(GL_TEXTURE_2D, mipmap, intf, width, height, 0, GL_COLOR_INDEX, GL_UNSIGNED_BYTE, data);
|
||||
}
|
||||
else
|
||||
#endif
|
||||
if (trans == PNG_SOLID || trans == PNG_ALPHA || trans == PNG_LUMINANCEALPHA || color == PNG_COLOR_TYPE_RGB_ALPHA || color == PNG_COLOR_TYPE_GRAY_ALPHA) {
|
||||
GLenum glformat;
|
||||
GLint glcomponent;
|
||||
|
||||
switch (color) {
|
||||
case PNG_COLOR_TYPE_GRAY:
|
||||
case PNG_COLOR_TYPE_RGB:
|
||||
case PNG_COLOR_TYPE_PALETTE:
|
||||
glformat = GL_RGB;
|
||||
glcomponent = 3;
|
||||
if (pinfo != NULL) pinfo->Alpha = 0;
|
||||
break;
|
||||
|
||||
case PNG_COLOR_TYPE_GRAY_ALPHA:
|
||||
case PNG_COLOR_TYPE_RGB_ALPHA:
|
||||
glformat = GL_RGBA;
|
||||
glcomponent = 4;
|
||||
if (pinfo != NULL) pinfo->Alpha = 8;
|
||||
break;
|
||||
|
||||
default:
|
||||
/*puts("glformat not set");*/
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (trans == PNG_LUMINANCEALPHA)
|
||||
glformat = GL_LUMINANCE_ALPHA;
|
||||
|
||||
if (mipmap == PNG_BUILDMIPMAPS)
|
||||
Build2DMipmaps(glcomponent, width, height, glformat, data, 1);
|
||||
else if (mipmap == PNG_SIMPLEMIPMAPS)
|
||||
Build2DMipmaps(glcomponent, width, height, glformat, data, 0);
|
||||
else
|
||||
glTexImage2D(GL_TEXTURE_2D, mipmap, glcomponent, width, height, 0, glformat, GL_UNSIGNED_BYTE, data);
|
||||
}
|
||||
else {
|
||||
png_bytep p, endp, q;
|
||||
int r, g, b, a;
|
||||
|
||||
p = data, endp = p+width*height*3;
|
||||
q = data2 = (png_bytep) malloc(sizeof(png_byte)*width*height*4);
|
||||
|
||||
if (pinfo != NULL) pinfo->Alpha = 8;
|
||||
|
||||
#define FORSTART \
|
||||
do { \
|
||||
r = *p++; /*red */ \
|
||||
g = *p++; /*green*/ \
|
||||
b = *p++; /*blue */ \
|
||||
*q++ = r; \
|
||||
*q++ = g; \
|
||||
*q++ = b;
|
||||
|
||||
#define FOREND \
|
||||
q++; \
|
||||
} while (p != endp);
|
||||
|
||||
#define ALPHA *q
|
||||
|
||||
switch (trans) {
|
||||
case PNG_CALLBACKT:
|
||||
FORSTART
|
||||
ALPHA = AlphaCallback((unsigned char) r, (unsigned char) g, (unsigned char) b);
|
||||
FOREND
|
||||
break;
|
||||
|
||||
case PNG_STENCIL:
|
||||
FORSTART
|
||||
if (r == StencilRed && g == StencilGreen && b == StencilBlue)
|
||||
ALPHA = 0;
|
||||
else
|
||||
ALPHA = 255;
|
||||
FOREND
|
||||
break;
|
||||
|
||||
case PNG_BLEND1:
|
||||
FORSTART
|
||||
a = r+g+b;
|
||||
if (a > 255) ALPHA = 255; else ALPHA = a;
|
||||
FOREND
|
||||
break;
|
||||
|
||||
case PNG_BLEND2:
|
||||
FORSTART
|
||||
a = r+g+b;
|
||||
if (a > 255*2) ALPHA = 255; else ALPHA = a/2;
|
||||
FOREND
|
||||
break;
|
||||
|
||||
case PNG_BLEND3:
|
||||
FORSTART
|
||||
ALPHA = (r+g+b)/3;
|
||||
FOREND
|
||||
break;
|
||||
|
||||
case PNG_BLEND4:
|
||||
FORSTART
|
||||
a = r*r+g*g+b*b;
|
||||
if (a > 255) ALPHA = 255; else ALPHA = a;
|
||||
FOREND
|
||||
break;
|
||||
|
||||
case PNG_BLEND5:
|
||||
FORSTART
|
||||
a = r*r+g*g+b*b;
|
||||
if (a > 255*2) ALPHA = 255; else ALPHA = a/2;
|
||||
FOREND
|
||||
break;
|
||||
|
||||
case PNG_BLEND6:
|
||||
FORSTART
|
||||
a = r*r+g*g+b*b;
|
||||
if (a > 255*3) ALPHA = 255; else ALPHA = a/3;
|
||||
FOREND
|
||||
break;
|
||||
|
||||
case PNG_BLEND7:
|
||||
FORSTART
|
||||
a = r*r+g*g+b*b;
|
||||
if (a > 255*255) ALPHA = 255; else ALPHA = (int) sqrt(a);
|
||||
FOREND
|
||||
break;
|
||||
}
|
||||
|
||||
#undef FORSTART
|
||||
#undef FOREND
|
||||
#undef ALPHA
|
||||
|
||||
if (mipmap == PNG_BUILDMIPMAPS)
|
||||
Build2DMipmaps(4, width, height, GL_RGBA, data2, 1);
|
||||
else if (mipmap == PNG_SIMPLEMIPMAPS)
|
||||
Build2DMipmaps(4, width, height, GL_RGBA, data2, 0);
|
||||
else
|
||||
glTexImage2D(GL_TEXTURE_2D, mipmap, 4, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data2);
|
||||
|
||||
free(data2);
|
||||
}
|
||||
|
||||
glPixelStorei(GL_PACK_ALIGNMENT, pack);
|
||||
glPixelStorei(GL_UNPACK_ALIGNMENT, unpack);
|
||||
} /* OpenGL end */
|
||||
|
||||
png_read_end(png, endinfo);
|
||||
png_destroy_read_struct(&png, &info, &endinfo);
|
||||
|
||||
free(data);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue