remove usage of libpng with custom save_png

This commit is contained in:
Eduardo Bart 2012-04-28 21:16:22 -03:00
parent 6ed0e099db
commit df0147cf64
8 changed files with 783 additions and 556 deletions

View File

@ -87,7 +87,6 @@ FIND_PACKAGE(OpenAL REQUIRED)
FIND_PACKAGE(VorbisFile REQUIRED) FIND_PACKAGE(VorbisFile REQUIRED)
FIND_PACKAGE(Vorbis REQUIRED) FIND_PACKAGE(Vorbis REQUIRED)
FIND_PACKAGE(Ogg REQUIRED) FIND_PACKAGE(Ogg REQUIRED)
FIND_PACKAGE(PNG REQUIRED)
IF(CMAKE_BUILD_TYPE STREQUAL "Debug" OR CMAKE_BUILD_TYPE STREQUAL "RelWithDebInfo") IF(CMAKE_BUILD_TYPE STREQUAL "Debug" OR CMAKE_BUILD_TYPE STREQUAL "RelWithDebInfo")
MESSAGE(STATUS "Debug information: ON") MESSAGE(STATUS "Debug information: ON")
@ -148,7 +147,6 @@ INCLUDE_DIRECTORIES(
${PHYSFS_INCLUDE_DIR} ${PHYSFS_INCLUDE_DIR}
${GMP_INCLUDE_DIR} ${GMP_INCLUDE_DIR}
${ZLIB_INCLUDE_DIR} ${ZLIB_INCLUDE_DIR}
${PNG_INCLUDE_DIR}
"${CMAKE_CURRENT_LIST_DIR}/.." "${CMAKE_CURRENT_LIST_DIR}/.."
) )
@ -161,7 +159,6 @@ SET(framework_LIBRARIES
${VORBISFILE_LIBRARY} ${VORBIS_LIBRARY} ${OGG_LIBRARY} ${VORBISFILE_LIBRARY} ${VORBIS_LIBRARY} ${OGG_LIBRARY}
${OPENGL_LIBRARIES} ${OPENGL_LIBRARIES}
${OPENAL_LIBRARY} ${OPENAL_LIBRARY}
${PNG_LIBRARY}
${ADDITIONAL_LIBRARIES} ${ADDITIONAL_LIBRARIES}
) )

View File

@ -3,7 +3,7 @@
# PNG_INCLUDE_DIR - the PNG include directory # PNG_INCLUDE_DIR - the PNG include directory
# PNG_LIBRARY - the PNG library # PNG_LIBRARY - the PNG library
FIND_PATH(PNG_INCLUDE_DIR NAMES png.h) FIND_PATH(PNG_INCLUDE_DIR NAMES png.h PATH_SUFFIXES libpng15 libpng)
FIND_LIBRARY(PNG_LIBRARY NAMES libpng15.a libpng.a png15 png) FIND_LIBRARY(PNG_LIBRARY NAMES libpng15.a libpng.a png15 png)
INCLUDE(FindPackageHandleStandardArgs) INCLUDE(FindPackageHandleStandardArgs)
FIND_PACKAGE_HANDLE_STANDARD_ARGS(PNG DEFAULT_MSG PNG_LIBRARY PNG_INCLUDE_DIR) FIND_PACKAGE_HANDLE_STANDARD_ARGS(PNG DEFAULT_MSG PNG_LIBRARY PNG_INCLUDE_DIR)

View File

@ -1,217 +1,223 @@
/* /*
* Copyright (c) 2010-2012 OTClient <https://github.com/edubart/otclient> * Copyright (c) 2010-2012 OTClient <https://github.com/edubart/otclient>
* *
* Permission is hereby granted, free of charge, to any person obtaining a copy * Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal * of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights * in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is * copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions: * furnished to do so, subject to the following conditions:
* *
* The above copyright notice and this permission notice shall be included in * The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software. * all copies or substantial portions of the Software.
* *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE. * THE SOFTWARE.
*/ */
#include "resourcemanager.h" #include "resourcemanager.h"
#include "filestream.h" #include "filestream.h"
#include <framework/application.h> #include <framework/application.h>
#include <framework/luascript/luainterface.h> #include <framework/luascript/luainterface.h>
#include <physfs.h> #include <physfs.h>
ResourceManager g_resources; ResourceManager g_resources;
void ResourceManager::init(const char *argv0) void ResourceManager::init(const char *argv0)
{ {
PHYSFS_init(argv0); PHYSFS_init(argv0);
} }
void ResourceManager::terminate() void ResourceManager::terminate()
{ {
PHYSFS_deinit(); PHYSFS_deinit();
} }
bool ResourceManager::setupWriteDir(const std::string& appWriteDirName) bool ResourceManager::setupWriteDir(const std::string& appWriteDirName)
{ {
std::string userDir = PHYSFS_getUserDir(); std::string userDir = PHYSFS_getUserDir();
std::string dirName = Fw::mkstr(".", appWriteDirName); std::string dirName = Fw::mkstr(".", appWriteDirName);
std::string writeDir = userDir + dirName; std::string writeDir = userDir + dirName;
if(!PHYSFS_setWriteDir(writeDir.c_str())) { if(!PHYSFS_setWriteDir(writeDir.c_str())) {
if(!PHYSFS_setWriteDir(userDir.c_str())) if(!PHYSFS_setWriteDir(userDir.c_str()))
return false; return false;
if(!PHYSFS_mkdir(dirName.c_str())) { if(!PHYSFS_mkdir(dirName.c_str())) {
PHYSFS_setWriteDir(NULL); PHYSFS_setWriteDir(NULL);
return false; return false;
} }
if(!PHYSFS_setWriteDir(writeDir.c_str())) if(!PHYSFS_setWriteDir(writeDir.c_str()))
return false; return false;
} }
addToSearchPath(writeDir); addToSearchPath(writeDir);
return true; return true;
} }
bool ResourceManager::addToSearchPath(const std::string& path, bool insertInFront) bool ResourceManager::addToSearchPath(const std::string& path, bool insertInFront)
{ {
if(!PHYSFS_addToSearchPath(path.c_str(), insertInFront ? 0 : 1)) if(!PHYSFS_addToSearchPath(path.c_str(), insertInFront ? 0 : 1))
return false; return false;
return true; return true;
} }
void ResourceManager::searchAndAddPackages(const std::string& packagesDir, const std::string& packageExt, bool append) void ResourceManager::searchAndAddPackages(const std::string& packagesDir, const std::string& packageExt, bool append)
{ {
auto files = listDirectoryFiles(resolvePath(packagesDir)); auto files = listDirectoryFiles(resolvePath(packagesDir));
for(const std::string& file : files) { for(const std::string& file : files) {
if(boost::ends_with(file, packageExt)) if(boost::ends_with(file, packageExt))
addToSearchPath(packagesDir + "/" + file, !append); addToSearchPath(packagesDir + "/" + file, !append);
} }
} }
bool ResourceManager::fileExists(const std::string& fileName) bool ResourceManager::fileExists(const std::string& fileName)
{ {
return (PHYSFS_exists(resolvePath(fileName).c_str()) && !PHYSFS_isDirectory(resolvePath(fileName).c_str())); return (PHYSFS_exists(resolvePath(fileName).c_str()) && !PHYSFS_isDirectory(resolvePath(fileName).c_str()));
} }
bool ResourceManager::directoryExists(const std::string& directoryName) bool ResourceManager::directoryExists(const std::string& directoryName)
{ {
return (PHYSFS_isDirectory(resolvePath(directoryName).c_str())); return (PHYSFS_isDirectory(resolvePath(directoryName).c_str()));
} }
void ResourceManager::loadFile(const std::string& fileName, std::iostream& out) void ResourceManager::loadFile(const std::string& fileName, std::iostream& out)
{ {
std::string fullPath = resolvePath(fileName); std::string fullPath = resolvePath(fileName);
out.clear(std::ios::goodbit); out.clear(std::ios::goodbit);
PHYSFS_file* file = PHYSFS_openRead(fullPath.c_str()); PHYSFS_file* file = PHYSFS_openRead(fullPath.c_str());
if(!file) { if(!file) {
out.clear(std::ios::failbit); out.clear(std::ios::failbit);
Fw::throwException("failed to load file '", fullPath.c_str(), "': ", PHYSFS_getLastError()); Fw::throwException("failed to load file '", fullPath.c_str(), "': ", PHYSFS_getLastError());
} else { } else {
int fileSize = PHYSFS_fileLength(file); int fileSize = PHYSFS_fileLength(file);
if(fileSize > 0) { if(fileSize > 0) {
std::vector<char> buffer(fileSize); std::vector<char> buffer(fileSize);
PHYSFS_read(file, (void*)&buffer[0], 1, fileSize); PHYSFS_read(file, (void*)&buffer[0], 1, fileSize);
out.write(&buffer[0], fileSize); out.write(&buffer[0], fileSize);
} else } else
out.clear(std::ios::eofbit); out.clear(std::ios::eofbit);
PHYSFS_close(file); PHYSFS_close(file);
out.seekg(0, std::ios::beg); out.seekg(0, std::ios::beg);
} }
} }
std::string ResourceManager::loadFile(const std::string& fileName) std::string ResourceManager::loadFile(const std::string& fileName)
{ {
std::stringstream fin; std::stringstream fin;
loadFile(fileName, fin); loadFile(fileName, fin);
return fin.str(); return fin.str();
} }
bool ResourceManager::saveFile(const std::string& fileName, const uchar* data, uint size) bool ResourceManager::saveFile(const std::string& fileName, const uchar* data, uint size)
{ {
PHYSFS_file* file = PHYSFS_openWrite(fileName.c_str()); PHYSFS_file* file = PHYSFS_openWrite(fileName.c_str());
if(!file) if(!file)
{ {
logError(PHYSFS_getLastError()); logError(PHYSFS_getLastError());
return false; return false;
} }
PHYSFS_write(file, (void*)data, size, 1); PHYSFS_write(file, (void*)data, size, 1);
PHYSFS_close(file); PHYSFS_close(file);
return true; return true;
} }
bool ResourceManager::saveFile(const std::string& fileName, std::iostream& in) bool ResourceManager::saveFile(const std::string& fileName, std::iostream& in)
{ {
std::streampos oldPos = in.tellg(); std::streampos oldPos = in.tellg();
in.seekg(0, std::ios::end); in.seekg(0, std::ios::end);
std::streampos size = in.tellg(); std::streampos size = in.tellg();
in.seekg(0, std::ios::beg); in.seekg(0, std::ios::beg);
std::vector<char> buffer(size); std::vector<char> buffer(size);
in.read(&buffer[0], size); in.read(&buffer[0], size);
bool ret = saveFile(fileName, (const uchar*)&buffer[0], size); bool ret = saveFile(fileName, (const uchar*)&buffer[0], size);
in.seekg(oldPos, std::ios::beg); in.seekg(oldPos, std::ios::beg);
return ret; return ret;
} }
bool ResourceManager::saveFile(const std::string& fileName, const std::string& data) bool ResourceManager::saveFile(const std::string& fileName, const std::string& data)
{ {
return saveFile(fileName, (const uchar*)data.c_str(), data.size()); return saveFile(fileName, (const uchar*)data.c_str(), data.size());
} }
FileStreamPtr ResourceManager::openFile(const std::string& fileName) FileStreamPtr ResourceManager::openFile(const std::string& fileName)
{ {
std::string fullPath = resolvePath(fileName); std::string fullPath = resolvePath(fileName);
PHYSFS_File* file = PHYSFS_openRead(fullPath.c_str()); PHYSFS_File* file = PHYSFS_openRead(fullPath.c_str());
if(!file) { if(!file) {
logTraceError("unable to open file '", fullPath, "': ", PHYSFS_getLastError()); logTraceError("unable to open file '", fullPath, "': ", PHYSFS_getLastError());
return nullptr; return nullptr;
} }
return FileStreamPtr(new FileStream(fullPath, file)); return FileStreamPtr(new FileStream(fullPath, file));
} }
FileStreamPtr ResourceManager::appendFile(const std::string& fileName) FileStreamPtr ResourceManager::appendFile(const std::string& fileName)
{ {
PHYSFS_File* file = PHYSFS_openAppend(fileName.c_str()); PHYSFS_File* file = PHYSFS_openAppend(fileName.c_str());
if(!file) { if(!file) {
logTraceError("failed to append file '", fileName, "': ", PHYSFS_getLastError()); logTraceError("failed to append file '", fileName, "': ", PHYSFS_getLastError());
return nullptr; return nullptr;
} }
return FileStreamPtr(new FileStream(fileName, file)); return FileStreamPtr(new FileStream(fileName, file));
} }
FileStreamPtr ResourceManager::createFile(const std::string& fileName) FileStreamPtr ResourceManager::createFile(const std::string& fileName)
{ {
PHYSFS_File* file = PHYSFS_openWrite(fileName.c_str()); PHYSFS_File* file = PHYSFS_openWrite(fileName.c_str());
if(!file) { if(!file) {
logTraceError("failed to create file '", fileName, "': ", PHYSFS_getLastError()); logTraceError("failed to create file '", fileName, "': ", PHYSFS_getLastError());
return nullptr; return nullptr;
} }
return FileStreamPtr(new FileStream(fileName, file)); return FileStreamPtr(new FileStream(fileName, file));
} }
bool ResourceManager::deleteFile(const std::string& fileName) bool ResourceManager::deleteFile(const std::string& fileName)
{ {
return PHYSFS_delete(resolvePath(fileName).c_str()) != 0; return PHYSFS_delete(resolvePath(fileName).c_str()) != 0;
} }
std::list<std::string> ResourceManager::listDirectoryFiles(const std::string& directoryPath)
{ bool ResourceManager::makeDir(const std::string directory)
std::list<std::string> files; {
auto rc = PHYSFS_enumerateFiles(resolvePath(directoryPath).c_str()); return PHYSFS_mkdir(directory.c_str());
}
for(int i = 0; rc[i] != NULL; i++)
files.push_back(rc[i]); std::list<std::string> ResourceManager::listDirectoryFiles(const std::string& directoryPath)
{
PHYSFS_freeList(rc); std::list<std::string> files;
return files; auto rc = PHYSFS_enumerateFiles(resolvePath(directoryPath).c_str());
}
for(int i = 0; rc[i] != NULL; i++)
std::string ResourceManager::resolvePath(const std::string& path) files.push_back(rc[i]);
{
std::string fullPath; PHYSFS_freeList(rc);
if(boost::starts_with(path, "/")) return files;
fullPath = path; }
else {
std::string scriptPath = "/" + g_lua.getCurrentSourcePath(); std::string ResourceManager::resolvePath(const std::string& path)
if(!scriptPath.empty()) {
fullPath += scriptPath + "/"; std::string fullPath;
fullPath += path; if(boost::starts_with(path, "/"))
} fullPath = path;
if(!(boost::starts_with(fullPath, "/"))) else {
logTraceWarning("the following file path is not fully resolved: ", path); std::string scriptPath = "/" + g_lua.getCurrentSourcePath();
boost::replace_all(fullPath, "//", "/"); if(!scriptPath.empty())
return fullPath; fullPath += scriptPath + "/";
} fullPath += path;
}
std::string ResourceManager::getBaseDir() if(!(boost::starts_with(fullPath, "/")))
{ logTraceWarning("the following file path is not fully resolved: ", path);
return PHYSFS_getBaseDir(); boost::replace_all(fullPath, "//", "/");
} return fullPath;
}
std::string ResourceManager::getBaseDir()
{
return PHYSFS_getBaseDir();
}

View File

@ -50,6 +50,7 @@ public:
FileStreamPtr createFile(const std::string& fileName); FileStreamPtr createFile(const std::string& fileName);
bool deleteFile(const std::string& fileName); bool deleteFile(const std::string& fileName);
bool makeDir(const std::string directory);
std::list<std::string> listDirectoryFiles(const std::string& directoryPath = ""); std::list<std::string> listDirectoryFiles(const std::string& directoryPath = "");
std::string resolvePath(const std::string& path); std::string resolvePath(const std::string& path);

View File

@ -557,7 +557,7 @@ int load_apng(std::stringstream& file, struct apng_data *apng)
unsigned char * pImg2; unsigned char * pImg2;
unsigned char * pDst1; unsigned char * pDst1;
unsigned char * pDst2; unsigned char * pDst2;
unsigned int * frames_delay; unsigned short* frames_delay;
file.read((char*)sig, 8); file.read((char*)sig, 8);
if(!file.eof() && memcmp(sig, png_sign, 8) == 0) { if(!file.eof() && memcmp(sig, png_sign, 8) == 0) {
@ -677,7 +677,7 @@ int load_apng(std::stringstream& file, struct apng_data *apng)
frames = read32(file); frames = read32(file);
if(frames_delay) if(frames_delay)
free(frames_delay); free(frames_delay);
frames_delay = (unsigned int*)malloc(frames*sizeof(int)); frames_delay = (unsigned short*)malloc(frames*sizeof(int));
loops = read32(file); loops = read32(file);
crc = read32(file); crc = read32(file);
if (pOut1) if (pOut1)
@ -862,6 +862,279 @@ int load_apng(std::stringstream& file, struct apng_data *apng)
return 0; return 0;
} }
void write_chunk(std::ostream& f, const char* name, unsigned char* data, unsigned int length)
{
unsigned int crc = crc32(0, Z_NULL, 0);
unsigned int len = swap32(length);
f.write((char*)&len, 4);
f.write(name, 4);
crc = crc32(crc, (const Bytef*)name, 4);
if(data != NULL && length > 0) {
f.write((char*)data, length);
crc = crc32(crc, data, length);
}
crc = swap32(crc);
f.write((char*)&crc, 4);
}
void write_IDATs(std::ostream& f, unsigned char* data, unsigned int length, unsigned int idat_size)
{
unsigned int z_cmf = data[0];
if((z_cmf & 0x0f) == 8 && (z_cmf & 0xf0) <= 0x70) {
if(length >= 2) {
unsigned int z_cinfo = z_cmf >> 4;
unsigned int half_z_window_size = 1 << (z_cinfo + 7);
while(idat_size <= half_z_window_size && half_z_window_size >= 256) {
z_cinfo--;
half_z_window_size >>= 1;
}
z_cmf = (z_cmf & 0x0f) | (z_cinfo << 4);
if(data[0] != (unsigned char)z_cmf) {
data[0] = (unsigned char)z_cmf;
data[1] &= 0xe0;
data[1] += (unsigned char)(0x1f - ((z_cmf << 8) + data[1]) % 0x1f);
}
}
}
while(length > 0) {
unsigned int ds = length;
if(ds > PNG_ZBUF_SIZE)
ds = PNG_ZBUF_SIZE;
write_chunk(f, "IDAT", data, ds);
data += ds;
length -= ds;
}
}
void save_png(std::stringstream& f, int width, int height, int channels, unsigned char *pixels)
{
unsigned int bpp = 4;
unsigned char coltype = 0;
if(channels == 3)
coltype = 2;
else if (channels == 2)
coltype = 4;
else if (channels == 4)
coltype = 6;
struct IHDR {
unsigned int mWidth;
unsigned int mHeight;
unsigned char mDepth;
unsigned char mColorType;
unsigned char mCompression;
unsigned char mFilterMethod;
unsigned char mInterlaceMethod;
} ihdr = { swap32(width), swap32(height), 8, coltype, 0, 0, 0 };
z_stream zstream1;
z_stream zstream2;
unsigned int i, j;
unsigned int rowbytes = width * bpp;
unsigned int idat_size = (rowbytes + 1) * height;
unsigned int zbuf_size = idat_size + ((idat_size + 7) >> 3) + ((idat_size + 63) >> 6) + 11;
unsigned char* row_buf = (unsigned char*)malloc(rowbytes + 1);
unsigned char* sub_row = (unsigned char*)malloc(rowbytes + 1);
unsigned char* up_row = (unsigned char*)malloc(rowbytes + 1);
unsigned char* avg_row = (unsigned char*)malloc(rowbytes + 1);
unsigned char* paeth_row = (unsigned char*)malloc(rowbytes + 1);
unsigned char* zbuf1 = (unsigned char*)malloc(zbuf_size);
unsigned char* zbuf2 = (unsigned char*)malloc(zbuf_size);
if(!row_buf || !sub_row || !up_row || !avg_row || !paeth_row || !zbuf1 || !zbuf2)
return;
row_buf[0] = 0;
sub_row[0] = 1;
up_row[0] = 2;
avg_row[0] = 3;
paeth_row[0] = 4;
zstream1.data_type = Z_BINARY;
zstream1.zalloc = Z_NULL;
zstream1.zfree = Z_NULL;
zstream1.opaque = Z_NULL;
deflateInit2(&zstream1, Z_BEST_COMPRESSION, 8, 15, 8, Z_DEFAULT_STRATEGY);
zstream2.data_type = Z_BINARY;
zstream2.zalloc = Z_NULL;
zstream2.zfree = Z_NULL;
zstream2.opaque = Z_NULL;
deflateInit2(&zstream2, Z_BEST_COMPRESSION, 8, 15, 8, Z_FILTERED);
int a, b, c, pa, pb, pc, p, v;
unsigned char* prev;
unsigned char* row;
f.write((char*)png_sign, 8);
write_chunk(f, "IHDR", (unsigned char*)(&ihdr), 13);
if(palsize > 0)
write_chunk(f, "PLTE", (unsigned char*)(&pal), palsize * 3);
if(trnssize > 0)
write_chunk(f, "tRNS", trns, trnssize);
zstream1.next_out = zbuf1;
zstream1.avail_out = zbuf_size;
zstream2.next_out = zbuf2;
zstream2.avail_out = zbuf_size;
prev = NULL;
row = pixels;
for(j = 0; j < (unsigned int)height; j++) {
unsigned char* out;
unsigned int sum = 0;
unsigned char* best_row = row_buf;
unsigned int mins = ((unsigned int)(-1)) >> 1;
out = row_buf + 1;
for(i = 0; i < rowbytes; i++) {
v = out[i] = row[i];
sum += (v < 128) ? v : 256 - v;
}
mins = sum;
sum = 0;
out = sub_row + 1;
for(i = 0; i < bpp; i++) {
v = out[i] = row[i];
sum += (v < 128) ? v : 256 - v;
}
for(i = bpp; i < rowbytes; i++) {
v = out[i] = row[i] - row[i - bpp];
sum += (v < 128) ? v : 256 - v;
if(sum > mins) break;
}
if(sum < mins) {
mins = sum;
best_row = sub_row;
}
if(prev) {
sum = 0;
out = up_row + 1;
for(i = 0; i < rowbytes; i++) {
v = out[i] = row[i] - prev[i];
sum += (v < 128) ? v : 256 - v;
if(sum > mins) break;
}
if(sum < mins) {
mins = sum;
best_row = up_row;
}
sum = 0;
out = avg_row + 1;
for(i = 0; i < bpp; i++) {
v = out[i] = row[i] - prev[i] / 2;
sum += (v < 128) ? v : 256 - v;
}
for(i = bpp; i < rowbytes; i++) {
v = out[i] = row[i] - (prev[i] + row[i - bpp]) / 2;
sum += (v < 128) ? v : 256 - v;
if(sum > mins) break;
}
if(sum < mins) {
mins = sum;
best_row = avg_row;
}
sum = 0;
out = paeth_row + 1;
for(i = 0; i < bpp; i++) {
v = out[i] = row[i] - prev[i];
sum += (v < 128) ? v : 256 - v;
}
for(i = bpp; i < rowbytes; i++) {
a = row[i - bpp];
b = prev[i];
c = prev[i - bpp];
p = b - c;
pc = a - c;
pa = abs(p);
pb = abs(pc);
pc = abs(p + pc);
p = (pa <= pb && pa <= pc) ? a : (pb <= pc) ? b : c;
v = out[i] = row[i] - p;
sum += (v < 128) ? v : 256 - v;
if(sum > mins) break;
}
if(sum < mins) {
best_row = paeth_row;
}
}
zstream1.next_in = row_buf;
zstream1.avail_in = rowbytes + 1;
deflate(&zstream1, Z_NO_FLUSH);
zstream2.next_in = best_row;
zstream2.avail_in = rowbytes + 1;
deflate(&zstream2, Z_NO_FLUSH);
prev = row;
row += rowbytes;
}
deflate(&zstream1, Z_FINISH);
deflate(&zstream2, Z_FINISH);
if(zstream1.total_out <= zstream2.total_out)
write_IDATs(f, zbuf1, zstream1.total_out, idat_size);
else
write_IDATs(f, zbuf2, zstream2.total_out, idat_size);
deflateReset(&zstream1);
zstream1.data_type = Z_BINARY;
deflateReset(&zstream2);
zstream2.data_type = Z_BINARY;
write_chunk(f, "IEND", 0, 0);
deflateEnd(&zstream1);
deflateEnd(&zstream2);
free(zbuf1);
free(zbuf2);
free(row_buf);
free(sub_row);
free(up_row);
free(avg_row);
free(paeth_row);
}
void free_apng(struct apng_data *apng) void free_apng(struct apng_data *apng)
{ {
if(apng->pdata) if(apng->pdata)

View File

@ -35,11 +35,12 @@ struct apng_data {
unsigned char coltype; unsigned char coltype;
unsigned int num_frames; unsigned int num_frames;
unsigned int num_plays; unsigned int num_plays;
unsigned int *frames_delay; // each frame delay in ms unsigned short *frames_delay; // each frame delay in ms
}; };
// returns -1 on error, 0 on success // returns -1 on error, 0 on success
int load_apng(std::stringstream& file, struct apng_data *apng); int load_apng(std::stringstream& file, struct apng_data *apng);
void save_png(std::stringstream& file, int width, int height, int channels, unsigned char *pixels);
void free_apng(struct apng_data *apng); void free_apng(struct apng_data *apng);
#endif #endif

View File

@ -1,282 +1,238 @@
/* /*
* Copyright (c) 2010-2012 OTClient <https://github.com/edubart/otclient> * Copyright (c) 2010-2012 OTClient <https://github.com/edubart/otclient>
* *
* Permission is hereby granted, free of charge, to any person obtaining a copy * Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal * of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights * in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is * copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions: * furnished to do so, subject to the following conditions:
* *
* The above copyright notice and this permission notice shall be included in * The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software. * all copies or substantial portions of the Software.
* *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE. * THE SOFTWARE.
*/ */
#include "spritemanager.h" #include "spritemanager.h"
#include <framework/core/resourcemanager.h> #include <framework/core/resourcemanager.h>
#include <framework/core/eventdispatcher.h> #include <framework/core/eventdispatcher.h>
#include <framework/core/filestream.h> #include <framework/core/filestream.h>
#include <framework/graphics/graphics.h> #include <framework/graphics/graphics.h>
#include <physfs.h> #include <framework/thirdparty/apngloader.h>
#include <physfs.h>
SpriteManager g_sprites;
SpriteManager g_sprites;
SpriteManager::SpriteManager()
{ SpriteManager::SpriteManager()
m_spritesCount = 0; {
m_signature = 0; m_spritesCount = 0;
} m_signature = 0;
}
bool SpriteManager::load(const std::string& file)
{ bool SpriteManager::load(const std::string& file)
try { {
m_spritesFile = g_resources.openFile(file); try {
if(!m_spritesFile) m_spritesFile = g_resources.openFile(file);
return false; if(!m_spritesFile)
return false;
// cache file buffer to avoid lags from hard drive
m_spritesFile->cache(); // cache file buffer to avoid lags from hard drive
m_spritesFile->cache();
m_signature = m_spritesFile->getU32();
m_spritesCount = m_spritesFile->getU16(); m_signature = m_spritesFile->getU32();
m_sprites.resize(m_spritesCount); m_spritesCount = m_spritesFile->getU16();
m_loaded = true; m_sprites.resize(m_spritesCount);
return true; m_loaded = true;
} catch(Exception& e) { return true;
logError("Failed to load sprites from '", file, "': ", e.what()); } catch(Exception& e) {
return false; logError("Failed to load sprites from '", file, "': ", e.what());
} return false;
} }
}
void SpriteManager::unload()
{ void SpriteManager::unload()
m_sprites.clear(); {
m_spritesCount = 0; m_sprites.clear();
m_signature = 0; m_spritesCount = 0;
} m_signature = 0;
}
void SpriteManager::preloadSprites()
{ void SpriteManager::preloadSprites()
// preload every 50 sprites, periodically {
const int burst = 50; // preload every 50 sprites, periodically
const int interval = 10; const int burst = 50;
auto preload = [this](int start) { const int interval = 10;
for(int i=start;i<start+burst && i<=m_spritesCount;++i) { auto preload = [this](int start) {
loadSpriteTexture(i); for(int i=start;i<start+burst && i<=m_spritesCount;++i) {
} loadSpriteTexture(i);
}; }
};
for(int i=1;i<=m_spritesCount;i+=burst)
g_eventDispatcher.scheduleEvent(std::bind(preload, i), (i/burst) * interval); for(int i=1;i<=m_spritesCount;i+=burst)
} g_eventDispatcher.scheduleEvent(std::bind(preload, i), (i/burst) * interval);
}
TexturePtr SpriteManager::loadSpriteTexture(int id)
{ TexturePtr SpriteManager::loadSpriteTexture(int id)
m_spritesFile->seek(((id-1) * 4) + 6); {
m_spritesFile->seek(((id-1) * 4) + 6);
uint32 spriteAddress = m_spritesFile->getU32();
uint32 spriteAddress = m_spritesFile->getU32();
// no sprite? return an empty texture
if(spriteAddress == 0) // no sprite? return an empty texture
return g_graphics.getEmptyTexture(); if(spriteAddress == 0)
return g_graphics.getEmptyTexture();
m_spritesFile->seek(spriteAddress);
m_spritesFile->seek(spriteAddress);
// skip color key
m_spritesFile->getU8(); // skip color key
m_spritesFile->getU8(); m_spritesFile->getU8();
m_spritesFile->getU8(); m_spritesFile->getU8();
m_spritesFile->getU8();
uint16 pixelDataSize = m_spritesFile->getU16();
uint16 pixelDataSize = m_spritesFile->getU16();
static std::vector<uint8> pixels(SPRITE_SIZE);
int writePos = 0; static std::vector<uint8> pixels(SPRITE_SIZE);
int read = 0; int writePos = 0;
int read = 0;
// decompress pixels
while(read < pixelDataSize) { // decompress pixels
uint16 transparentPixels = m_spritesFile->getU16(); while(read < pixelDataSize) {
uint16 coloredPixels = m_spritesFile->getU16(); uint16 transparentPixels = m_spritesFile->getU16();
uint16 coloredPixels = m_spritesFile->getU16();
if(writePos + transparentPixels*4 + coloredPixels*3 >= SPRITE_SIZE)
return g_graphics.getEmptyTexture(); if(writePos + transparentPixels*4 + coloredPixels*3 >= SPRITE_SIZE)
return g_graphics.getEmptyTexture();
for(int i = 0; i < transparentPixels; i++) {
pixels[writePos + 0] = 0x00; for(int i = 0; i < transparentPixels; i++) {
pixels[writePos + 1] = 0x00; pixels[writePos + 0] = 0x00;
pixels[writePos + 2] = 0x00; pixels[writePos + 1] = 0x00;
pixels[writePos + 3] = 0x00; pixels[writePos + 2] = 0x00;
writePos += 4; pixels[writePos + 3] = 0x00;
} writePos += 4;
}
for(int i = 0; i < coloredPixels; i++) {
pixels[writePos + 0] = m_spritesFile->getU8(); for(int i = 0; i < coloredPixels; i++) {
pixels[writePos + 1] = m_spritesFile->getU8(); pixels[writePos + 0] = m_spritesFile->getU8();
pixels[writePos + 2] = m_spritesFile->getU8(); pixels[writePos + 1] = m_spritesFile->getU8();
pixels[writePos + 3] = 0xFF; pixels[writePos + 2] = m_spritesFile->getU8();
pixels[writePos + 3] = 0xFF;
writePos += 4;
} writePos += 4;
}
read += 4 + (3 * coloredPixels);
} read += 4 + (3 * coloredPixels);
}
// fill remaining pixels with alpha
while(writePos < SPRITE_SIZE) { // fill remaining pixels with alpha
pixels[writePos + 0] = 0x00; while(writePos < SPRITE_SIZE) {
pixels[writePos + 1] = 0x00; pixels[writePos + 0] = 0x00;
pixels[writePos + 2] = 0x00; pixels[writePos + 1] = 0x00;
pixels[writePos + 3] = 0x00; pixels[writePos + 2] = 0x00;
writePos += 4; pixels[writePos + 3] = 0x00;
} writePos += 4;
}
TexturePtr spriteTex(new Texture(32, 32, 4, &pixels[0]));
spriteTex->setSmooth(true); TexturePtr spriteTex(new Texture(32, 32, 4, &pixels[0]));
spriteTex->setSmooth(true);
if(g_graphics.canUseMipmaps())
spriteTex->generateSoftwareMipmaps(pixels); if(g_graphics.canUseMipmaps())
spriteTex->generateSoftwareMipmaps(pixels);
return spriteTex;
} return spriteTex;
}
TexturePtr& SpriteManager::getSpriteTexture(int id)
{ TexturePtr& SpriteManager::getSpriteTexture(int id)
if(id == 0) {
return g_graphics.getEmptyTexture(); if(id == 0)
return g_graphics.getEmptyTexture();
assert(id > 0 && id <= m_spritesCount);
assert(id > 0 && id <= m_spritesCount);
// load sprites on demand
TexturePtr& sprite = m_sprites[id-1]; // load sprites on demand
if(!sprite) TexturePtr& sprite = m_sprites[id-1];
sprite = loadSpriteTexture(id); if(!sprite)
sprite = loadSpriteTexture(id);
//TODO: release unused sprites textures after X seconds
// to avoid massive texture allocations //TODO: release unused sprites textures after X seconds
return sprite; // to avoid massive texture allocations
return sprite;
} }
bool SpriteManager::exportSprites() bool SpriteManager::exportSprites()
{ {
g_resources.makeDir("sprites");
std::stringstream ss;
for(volatile int i = 1; i <= m_spritesCount; i++) { for(volatile int i = 1; i <= m_spritesCount; i++) {
m_spritesFile->seek(((i-1) * 4) + 6); m_spritesFile->seek(((i-1) * 4) + 6);
uint32 spriteAddress = m_spritesFile->getU32(); uint32 spriteAddress = m_spritesFile->getU32();
if(spriteAddress == 0) if(spriteAddress == 0)
continue; continue;
m_spritesFile->seek(spriteAddress); m_spritesFile->seek(spriteAddress);
// skip color key // skip color key
m_spritesFile->getU8(); m_spritesFile->getU8();
m_spritesFile->getU8(); m_spritesFile->getU8();
m_spritesFile->getU8(); m_spritesFile->getU8();
uint16 pixelDataSize = m_spritesFile->getU16(); uint16 pixelDataSize = m_spritesFile->getU16();
uchar pixels[SPRITE_SIZE]; uchar pixels[SPRITE_SIZE];
int writePos = 0; int writePos = 0;
int read = 0; int read = 0;
// decompress pixels // decompress pixels
while(read < pixelDataSize) { while(read < pixelDataSize) {
uint16 transparentPixels = m_spritesFile->getU16(); uint16 transparentPixels = m_spritesFile->getU16();
uint16 coloredPixels = m_spritesFile->getU16(); uint16 coloredPixels = m_spritesFile->getU16();
if(writePos + transparentPixels*4 + coloredPixels*3 >= SPRITE_SIZE) if(writePos + transparentPixels*4 + coloredPixels*3 >= SPRITE_SIZE)
return false; // skip the whole process or just ignore this sprite? return false; // skip the whole process or just ignore this sprite?
for(int i = 0; i < transparentPixels; i++) { for(int i = 0; i < transparentPixels; i++) {
pixels[writePos + 0] = 0x00; pixels[writePos + 0] = 0x00;
pixels[writePos + 1] = 0x00; pixels[writePos + 1] = 0x00;
pixels[writePos + 2] = 0x00; pixels[writePos + 2] = 0x00;
pixels[writePos + 3] = 0x00; pixels[writePos + 3] = 0x00;
writePos += 4; writePos += 4;
} }
for(int i = 0; i < coloredPixels; i++) { for(int i = 0; i < coloredPixels; i++) {
pixels[writePos + 0] = m_spritesFile->getU8(); pixels[writePos + 0] = m_spritesFile->getU8();
pixels[writePos + 1] = m_spritesFile->getU8(); pixels[writePos + 1] = m_spritesFile->getU8();
pixels[writePos + 2] = m_spritesFile->getU8(); pixels[writePos + 2] = m_spritesFile->getU8();
pixels[writePos + 3] = 0xFF; pixels[writePos + 3] = 0xFF;
writePos += 4; writePos += 4;
} }
read += 4 + (3 * coloredPixels); read += 4 + (3 * coloredPixels);
} }
// fill remaining pixels with alpha // fill remaining pixels with alpha
while(writePos < SPRITE_SIZE) { while(writePos < SPRITE_SIZE) {
pixels[writePos + 0] = 0x00; pixels[writePos + 0] = 0x00;
pixels[writePos + 1] = 0x00; pixels[writePos + 1] = 0x00;
pixels[writePos + 2] = 0x00; pixels[writePos + 2] = 0x00;
pixels[writePos + 3] = 0x00; pixels[writePos + 3] = 0x00;
writePos += 4; writePos += 4;
} }
// We should get the OTClient and export to that folder... // We should get the OTClient and export to that folder...
std::string fileName = Fw::formatString("%s/otclient/sprites/%d.png", PHYSFS_getUserDir(), i); std::string fileName = Fw::formatString("sprites/%d.png", i);
FILE *pFile = fopen(fileName.c_str(), "wb"); ss.str("");
save_png(ss, SPRITE_WIDTH, SPRITE_HEIGHT, SPRITE_CHANNELS, pixels);
if(!pFile) g_resources.saveFile(fileName, ss);
return false;
png_structp pPng = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
if(!pPng) {
fclose(pFile);
return false;
}
png_infop pPngInfo = png_create_info_struct(pPng);
if(!pPngInfo) {
fclose(pFile);
png_destroy_write_struct(&pPng, NULL);
return false;
}
if(setjmp(png_jmpbuf(pPng))) {
fclose(pFile);
png_destroy_write_struct(&pPng, &pPngInfo);
return false;
}
png_init_io(pPng, pFile);
png_set_compression_level(pPng, PNG_COMPRESSION);
int bitDepthPerChannel = SPRITE_CHANNELS/4*8;
int colorType = PNG_COLOR_TYPE_RGB_ALPHA;
png_set_IHDR(pPng, pPngInfo, SPRITE_WIDTH, SPRITE_HEIGHT, bitDepthPerChannel, colorType,
PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
png_write_info(pPng, pPngInfo);
if(bitDepthPerChannel == 16) // Reverse endian order (PNG is Big Endian)
png_set_swap(pPng);
int bytesPerRow = SPRITE_WIDTH*SPRITE_CHANNELS;
uint8* pImgData = pixels;
for(int row=0; row < SPRITE_HEIGHT; row++) { // Write non-interlaced buffer
png_write_row(pPng, pImgData);
pImgData += bytesPerRow;
}
png_write_end(pPng, NULL);
png_destroy_write_struct(&pPng, &pPngInfo);
fclose(pFile);
} }
return true; return true;
} }

View File

@ -1,74 +1,67 @@
/* /*
* Copyright (c) 2010-2012 OTClient <https://github.com/edubart/otclient> * Copyright (c) 2010-2012 OTClient <https://github.com/edubart/otclient>
* *
* Permission is hereby granted, free of charge, to any person obtaining a copy * Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal * of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights * in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is * copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions: * furnished to do so, subject to the following conditions:
* *
* The above copyright notice and this permission notice shall be included in * The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software. * all copies or substantial portions of the Software.
* *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE. * THE SOFTWARE.
*/ */
#ifndef SPRITEMANAGER_H #ifndef SPRITEMANAGER_H
#define SPRITEMANAGER_H #define SPRITEMANAGER_H
#include <framework/core/declarations.h> #include <framework/core/declarations.h>
#include <framework/graphics/texture.h> #include <framework/graphics/texture.h>
#include <png.h> #include <png.h>
#define SPRITE_WIDTH 32 class SpriteManager
#define SPRITE_HEIGHT 32 {
#define SPRITE_CHANNELS 4 enum {
#define SPRITE_SIZE SPRITE_WIDTH*SPRITE_HEIGHT*SPRITE_CHANNELS SPRITE_WIDTH=32,
SPRITE_HEIGHT=32,
SPRITE_CHANNELS=4,
SPRITE_SIZE=SPRITE_WIDTH*SPRITE_HEIGHT*SPRITE_CHANNELS
};
/* public:
Compression levels: SpriteManager();
None = 0
Fastest = 1
Best = 9
*/
#define PNG_COMPRESSION 6 bool load(const std::string& file);
void unload();
class SpriteManager void preloadSprites();
{
public: uint32 getSignature() { return m_signature; }
SpriteManager();
bool load(const std::string& file);
void unload();
void preloadSprites();
uint32 getSignature() { return m_signature; }
int getSpritesCount() { return m_spritesCount; } int getSpritesCount() { return m_spritesCount; }
bool exportSprites(); bool exportSprites();
TexturePtr& getSpriteTexture(int id); TexturePtr& getSpriteTexture(int id);
bool isLoaded() { return m_loaded; } bool isLoaded() { return m_loaded; }
private: private:
TexturePtr loadSpriteTexture(int id); TexturePtr loadSpriteTexture(int id);
Boolean<false> m_loaded; Boolean<false> m_loaded;
uint32 m_signature; uint32 m_signature;
int m_spritesCount; int m_spritesCount;
std::vector<TexturePtr> m_sprites; std::vector<TexturePtr> m_sprites;
TexturePtr m_transparentSprite; TexturePtr m_transparentSprite;
FileStreamPtr m_spritesFile; FileStreamPtr m_spritesFile;
}; };
extern SpriteManager g_sprites; extern SpriteManager g_sprites;
#endif #endif