CXBTFFrame appendContent(CXBTFWriter &writer, int width, int height, unsigned char *data, unsigned int size, unsigned int format, bool hasAlpha, unsigned int flags) { CXBTFFrame frame; #ifdef USE_LZO_PACKING lzo_uint packedSize = size; if ((flags & FLAGS_USE_LZO) == FLAGS_USE_LZO) { // grab a temporary buffer for unpacking into packedSize = size + size / 16 + 64 + 3; // see simple.c in lzo unsigned char *packed = new unsigned char[packedSize]; unsigned char *working = new unsigned char[LZO1X_999_MEM_COMPRESS]; if (packed && working) { if (lzo1x_999_compress(data, size, packed, &packedSize, working) != LZO_E_OK || packedSize > size) { // compression failed, or compressed size is bigger than uncompressed, so store as uncompressed packedSize = size; writer.AppendContent(data, size); } else { // success lzo_uint optimSize = size; if (lzo1x_optimize(packed, packedSize, data, &optimSize, NULL) != LZO_E_OK || optimSize != size) { //optimisation failed packedSize = size; writer.AppendContent(data, size); } else { // success writer.AppendContent(packed, packedSize); } } delete[] working; delete[] packed; } } else #else unsigned int packedSize = size; #endif { writer.AppendContent(data, size); } frame.SetPackedSize(packedSize); frame.SetUnpackedSize(size); frame.SetWidth(width); frame.SetHeight(height); frame.SetFormat(hasAlpha ? format : format | XB_FMT_OPAQUE); frame.SetDuration(0); return frame; }
CXBTFFrame appendContent(CXBTFWriter &writer, int width, int height, unsigned char *data, unsigned int size, unsigned int format, unsigned int flags) { CXBTFFrame frame; lzo_uint compressedSize = size; if ((flags & FLAGS_USE_LZO) == FLAGS_USE_LZO) { // grab a temporary buffer for unpacking into squish::u8 *compressed = new squish::u8[size + size / 16 + 64 + 3]; // see simple.c in lzo squish::u8 *working = new squish::u8[LZO1X_999_MEM_COMPRESS]; if (compressed && working) { if (lzo1x_999_compress(data, size, compressed, (lzo_uint*)&compressedSize, working) != LZO_E_OK || compressedSize > size) { // compression failed, or compressed size is bigger than uncompressed, so store as uncompressed compressedSize = size; writer.AppendContent(data, size); } else { // success lzo_uint optimSize = size; lzo1x_optimize(compressed, compressedSize, data, &optimSize, NULL); writer.AppendContent(compressed, compressedSize); } delete[] working; delete[] compressed; } } else { writer.AppendContent(data, size); } frame.SetPackedSize(compressedSize); frame.SetUnpackedSize(size); frame.SetWidth(width); frame.SetHeight(height); frame.SetFormat(format); frame.SetDuration(0); return frame; }
bool CXBTFReader::Open(const std::string& path) { if (path.empty()) return false; m_path = path; #ifdef TARGET_WINDOWS std::wstring strPathW; g_charsetConverter.utf8ToW(CSpecialProtocol::TranslatePath(m_path), strPathW, false); m_file = _wfopen(strPathW.c_str(), L"rb"); #else m_file = fopen(m_path.c_str(), "rb"); #endif if (m_file == nullptr) return false; // read the magic word char magic[4]; if (!ReadString(m_file, magic, sizeof(magic))) return false; if (strncmp(XBTF_MAGIC.c_str(), magic, sizeof(magic)) != 0) return false; // read the version char version[1]; if (!ReadString(m_file, version, sizeof(version))) return false; if (strncmp(XBTF_VERSION.c_str(), version, sizeof(version)) != 0) return false; unsigned int nofFiles; if (!ReadUInt32(m_file, nofFiles)) return false; for (uint32_t i = 0; i < nofFiles; i++) { CXBTFFile xbtfFile; uint32_t u32; uint64_t u64; char path[CXBTFFile::MaximumPathLength]; memset(path, 0, sizeof(path)); if (!ReadString(m_file, path, sizeof(path))) return false; xbtfFile.SetPath(path); if (!ReadUInt32(m_file, u32)) return false; xbtfFile.SetLoop(u32); unsigned int nofFrames; if (!ReadUInt32(m_file, nofFrames)) return false; for (uint32_t j = 0; j < nofFrames; j++) { CXBTFFrame frame; if (!ReadUInt32(m_file, u32)) return false; frame.SetWidth(u32); if (!ReadUInt32(m_file, u32)) return false; frame.SetHeight(u32); if (!ReadUInt32(m_file, u32)) return false; frame.SetFormat(u32); if (!ReadUInt64(m_file, u64)) return false; frame.SetPackedSize(u64); if (!ReadUInt64(m_file, u64)) return false; frame.SetUnpackedSize(u64); if (!ReadUInt32(m_file, u32)) return false; frame.SetDuration(u32); if (!ReadUInt64(m_file, u64)) return false; frame.SetOffset(u64); xbtfFile.GetFrames().push_back(frame); } AddFile(xbtfFile); } // Sanity check uint64_t pos = static_cast<uint64_t>(ftell(m_file)); if (pos != GetHeaderSize()) return false; return true; }
bool CXBTFReader::Open(const std::string& fileName) { m_fileName = fileName; #ifdef TARGET_WINDOWS std::wstring strPathW; g_charsetConverter.utf8ToW(CSpecialProtocol::TranslatePath(m_fileName), strPathW, false); m_file = _wfopen(strPathW.c_str(), L"rb"); #else m_file = fopen(m_fileName.c_str(), "rb"); #endif if (m_file == NULL) { return false; } char magic[4]; READ_STR(magic, 4, m_file); if (strncmp(magic, XBTF_MAGIC, sizeof(magic)) != 0) { return false; } char version[1]; READ_STR(version, 1, m_file); if (strncmp(version, XBTF_VERSION, sizeof(version)) != 0) { return false; } unsigned int nofFiles; READ_U32(nofFiles, m_file); for (unsigned int i = 0; i < nofFiles; i++) { CXBTFFile file; unsigned int u32; uint64_t u64; READ_STR(file.GetPath(), 256, m_file); READ_U32(u32, m_file); file.SetLoop(u32); unsigned int nofFrames; READ_U32(nofFrames, m_file); for (unsigned int j = 0; j < nofFrames; j++) { CXBTFFrame frame; READ_U32(u32, m_file); frame.SetWidth(u32); READ_U32(u32, m_file); frame.SetHeight(u32); READ_U32(u32, m_file); frame.SetFormat(u32); READ_U64(u64, m_file); frame.SetPackedSize(u64); READ_U64(u64, m_file); frame.SetUnpackedSize(u64); READ_U32(u32, m_file); frame.SetDuration(u32); READ_U64(u64, m_file); frame.SetOffset(u64); file.GetFrames().push_back(frame); } m_xbtf.GetFiles().push_back(file); m_filesMap[file.GetPath()] = file; } // Sanity check int64_t pos = ftell(m_file); if (pos != (int64_t)m_xbtf.GetHeaderSize()) { printf("Expected header size (%" PRId64") != actual size (%" PRId64")\n", m_xbtf.GetHeaderSize(), pos); return false; } return true; }