Zlib moved into separate module.

This commit is contained in:
Miroslav Bendík 2012-09-18 10:43:34 +02:00
parent 6133aff2f3
commit 28e9450573
5 changed files with 122 additions and 48 deletions

@ -57,12 +57,14 @@ set(mapper_HDRS
PixelAttributes.h PixelAttributes.h
PlayerAttributes.h PlayerAttributes.h
TileGenerator.h TileGenerator.h
ZlibDecompressor.h
) )
set(mapper_SRCS set(mapper_SRCS
PixelAttributes.cpp PixelAttributes.cpp
PlayerAttributes.cpp PlayerAttributes.cpp
TileGenerator.cpp TileGenerator.cpp
ZlibDecompressor.cpp
mapper.cpp mapper.cpp
) )

@ -13,10 +13,10 @@
#include <gdfontmb.h> #include <gdfontmb.h>
#include <iostream> #include <iostream>
#include <sstream> #include <sstream>
#include <zlib.h>
#include "config.h" #include "config.h"
#include "PlayerAttributes.h" #include "PlayerAttributes.h"
#include "TileGenerator.h" #include "TileGenerator.h"
#include "ZlibDecompressor.h"
#include "colors.h" #include "colors.h"
using namespace std; using namespace std;
@ -70,40 +70,6 @@ static inline int readBlockContent(const unsigned char *mapData, int version, in
} }
} }
static inline std::string zlibDecompress(const unsigned char *data, std::size_t size, std::size_t *processed)
{
string buffer;
const size_t BUFSIZE = 128 * 1024;
uint8_t temp_buffer[BUFSIZE];
z_stream strm;
strm.zalloc = Z_NULL;
strm.zfree = Z_NULL;
strm.opaque = Z_NULL;
strm.next_in = Z_NULL;
strm.avail_in = size;
if (inflateInit(&strm) != Z_OK) {
throw DecompressError();
}
strm.next_in = const_cast<unsigned char *>(data);
int ret = 0;
do {
strm.avail_out = BUFSIZE;
strm.next_out = temp_buffer;
ret = inflate(&strm, Z_NO_FLUSH);
buffer += string(reinterpret_cast<char *>(temp_buffer), BUFSIZE - strm.avail_out);
} while (ret == Z_OK);
if (ret != Z_STREAM_END) {
throw DecompressError();
}
*processed = strm.next_in - data;
(void)inflateEnd(&strm);
return buffer;
}
static inline int colorSafeBounds(int color) static inline int colorSafeBounds(int color)
{ {
if (color > 255) { if (color > 255) {
@ -371,11 +337,12 @@ void TileGenerator::renderMap()
else { else {
dataOffset = 2; dataOffset = 2;
} }
size_t processed;
string mapData = zlibDecompress(data + dataOffset, length - dataOffset, &processed); ZlibDecompressor decompressor(data, length);
dataOffset += processed; decompressor.setSeekPos(dataOffset);
string mapMetadata = zlibDecompress(data + dataOffset, length - dataOffset, &processed); ZlibDecompressor::string mapData = decompressor.decompress();
dataOffset += processed; ZlibDecompressor::string mapMetadata = decompressor.decompress();
dataOffset = decompressor.seekPos();
// Skip unused data // Skip unused data
if (version <= 21) { if (version <= 21) {
@ -455,11 +422,11 @@ void TileGenerator::renderMap()
} }
} }
inline void TileGenerator::renderMapBlock(const std::string &mapBlock, const BlockPos &pos, int version) inline void TileGenerator::renderMapBlock(const unsigned_string &mapBlock, const BlockPos &pos, int version)
{ {
int xBegin = (pos.x - m_xMin) * 16; int xBegin = (pos.x - m_xMin) * 16;
int zBegin = (m_zMax - pos.z) * 16; int zBegin = (m_zMax - pos.z) * 16;
const unsigned char *mapData = reinterpret_cast<const unsigned char *>(mapBlock.c_str()); const unsigned char *mapData = mapBlock.c_str();
for (int z = 0; z < 16; ++z) { for (int z = 0; z < 16; ++z) {
int imageY = getImageY(zBegin + 15 - z); int imageY = getImageY(zBegin + 15 - z);
for (int x = 0; x < 16; ++x) { for (int x = 0; x < 16; ++x) {
@ -608,7 +575,7 @@ std::map<int, TileGenerator::BlockList> TileGenerator::getBlocksOnZ(int zPos, sq
const unsigned char *data = reinterpret_cast<const unsigned char *>(sqlite3_column_blob(statement, 1)); const unsigned char *data = reinterpret_cast<const unsigned char *>(sqlite3_column_blob(statement, 1));
int size = sqlite3_column_bytes(statement, 1); int size = sqlite3_column_bytes(statement, 1);
BlockPos pos = decodeBlockPos(blocknum); BlockPos pos = decodeBlockPos(blocknum);
blocks[pos.x].push_back(Block(pos, std::basic_string<unsigned char>(data, size))); blocks[pos.x].push_back(Block(pos, unsigned_string(data, size)));
} }
else { else {
break; break;

@ -62,9 +62,6 @@ class DbError {
class ColorError { class ColorError {
}; };
class DecompressError {
};
class VersionError { class VersionError {
}; };
@ -72,8 +69,9 @@ class VersionError {
class TileGenerator class TileGenerator
{ {
private: private:
typedef std::basic_string<unsigned char> unsigned_string;
typedef std::map<std::string, Color> ColorMap; typedef std::map<std::string, Color> ColorMap;
typedef std::pair<BlockPos, std::basic_string<unsigned char> > Block; typedef std::pair<BlockPos, unsigned_string> Block;
typedef std::list<Block> BlockList; typedef std::list<Block> BlockList;
public: public:
@ -98,7 +96,7 @@ private:
void renderMap(); void renderMap();
std::list<int> getZValueList() const; std::list<int> getZValueList() const;
std::map<int, BlockList> getBlocksOnZ(int zPos, sqlite3_stmt *statement) const; std::map<int, BlockList> getBlocksOnZ(int zPos, sqlite3_stmt *statement) const;
void renderMapBlock(const std::string &mapBlock, const BlockPos &pos, int version); void renderMapBlock(const unsigned_string &mapBlock, const BlockPos &pos, int version);
void renderShading(int zPos); void renderShading(int zPos);
void renderScale(); void renderScale();
void renderOrigin(); void renderOrigin();

70
ZlibDecompressor.cpp Normal file

@ -0,0 +1,70 @@
/*
* =====================================================================
* Version: 1.0
* Created: 18.09.2012 10:20:47
* Author: Miroslav Bendík
* Company: LinuxOS.sk
* =====================================================================
*/
#include <zlib.h>
#include "ZlibDecompressor.h"
ZlibDecompressor::ZlibDecompressor(const unsigned char *data, std::size_t size):
m_data(data),
m_seekPos(0),
m_size(size)
{
}
ZlibDecompressor::~ZlibDecompressor()
{
}
void ZlibDecompressor::setSeekPos(std::size_t seekPos)
{
m_seekPos = seekPos;
}
std::size_t ZlibDecompressor::seekPos() const
{
return m_seekPos;
}
ZlibDecompressor::string ZlibDecompressor::decompress()
{
const unsigned char *data = m_data + m_seekPos;
const std::size_t size = m_size - m_seekPos;
string buffer;
const size_t BUFSIZE = 128 * 1024;
uint8_t temp_buffer[BUFSIZE];
z_stream strm;
strm.zalloc = Z_NULL;
strm.zfree = Z_NULL;
strm.opaque = Z_NULL;
strm.next_in = Z_NULL;
strm.avail_in = size;
if (inflateInit(&strm) != Z_OK) {
throw DecompressError();
}
strm.next_in = const_cast<unsigned char *>(data);
int ret = 0;
do {
strm.avail_out = BUFSIZE;
strm.next_out = temp_buffer;
ret = inflate(&strm, Z_NO_FLUSH);
buffer += string(reinterpret_cast<unsigned char *>(temp_buffer), BUFSIZE - strm.avail_out);
} while (ret == Z_OK);
if (ret != Z_STREAM_END) {
throw DecompressError();
}
m_seekPos += strm.next_in - data;
(void)inflateEnd(&strm);
return buffer;
}

37
ZlibDecompressor.h Normal file

@ -0,0 +1,37 @@
/*
* =====================================================================
* Version: 1.0
* Created: 18.09.2012 10:20:51
* Author: Miroslav Bendík
* Company: LinuxOS.sk
* =====================================================================
*/
#ifndef ZLIBDECOMPRESSOR_H_ZQL1PN8Q
#define ZLIBDECOMPRESSOR_H_ZQL1PN8Q
#include <cstdlib>
#include <string>
class ZlibDecompressor
{
public:
typedef std::basic_string<unsigned char> string;
class DecompressError {
};
ZlibDecompressor(const unsigned char *data, std::size_t size);
~ZlibDecompressor();
void setSeekPos(std::size_t seekPos);
std::size_t seekPos() const;
string decompress();
private:
const unsigned char *m_data;
std::size_t m_seekPos;
std::size_t m_size;
}; /* ----- end of class ZlibDecompressor ----- */
#endif /* end of include guard: ZLIBDECOMPRESSOR_H_ZQL1PN8Q */