D:/Programmation/Cpp/SFML/src/SFML/AdvancedSystem/Compressor.cpp

00001 
00002 //
00003 // SFML - Simple and Fast Multimedia Library
00004 // Copyright (C) 2007 Laurent Gomila (laurent.gom@gmail.com)
00005 //
00006 // This software is provided 'as-is', without any express or implied warranty.
00007 // In no event will the authors be held liable for any damages arising from the use of this software.
00008 //
00009 // Permission is granted to anyone to use this software for any purpose,
00010 // including commercial applications, and to alter it and redistribute it freely,
00011 // subject to the following restrictions:
00012 //
00013 // 1. The origin of this software must not be misrepresented;
00014 //    you must not claim that you wrote the original software.
00015 //    If you use this software in a product, an acknowledgment
00016 //    in the product documentation would be appreciated but is not required.
00017 //
00018 // 2. Altered source versions must be plainly marked as such,
00019 //    and must not be misrepresented as being the original software.
00020 //
00021 // 3. This notice may not be removed or altered from any source distribution.
00022 //
00024 
00026 // Headers
00028 #include <SFML/AdvancedSystem/Compressor.hpp>
00029 #include <SFML/AdvancedSystem/zlib/zlib.h>
00030 #include <iostream>
00031 
00032 
00036 bool sfCompressor::Compress(const char* Source, std::size_t Size, unsigned int Level)
00037 {
00038     // Check input data
00039     if (!Source || !Size)
00040     {
00041         std::cerr << "Failed to uncompress data (invalid parameter)" << std::endl;
00042         return false;
00043     }
00044 
00045     // Make sure the source pointer is not our internal buffer (zlib fails in such case)
00046     if (!myData.empty() && (Source == GetData()))
00047     {
00048         std::cerr << "Failed to compress data (source buffer is the internal buffer)" << std::endl;
00049         return false;
00050     }
00051 
00052     // Clamp the level of compression to the maximum supported by zlib
00053     if (Level > 9)
00054         Level = 9;
00055 
00056     // Get an estimation of the size of the compressed data
00057     uLong CompressedSize = compressBound(static_cast<uLong>(Size));
00058     myData.resize(CompressedSize);
00059 
00060     // Let zlib do the job
00061     int Error = compress2(reinterpret_cast<Byte*>(&myData[0]), &CompressedSize, reinterpret_cast<const Byte*>(Source), static_cast<uLong>(Size), Level);
00062 
00063     // Check errors
00064     if (Error != Z_OK)
00065     {
00066         std::cerr << "Failed to compress data (" << zError(Error) << ")" << std::endl;
00067         return false;
00068     }
00069 
00070     // Resize the internal buffer to the actual size of compressed data
00071     myData.resize(CompressedSize);
00072 
00073     return true;
00074 }
00075 
00076 
00080 bool sfCompressor::Uncompress(const char* Source, std::size_t Size, std::size_t UncompressedSize)
00081 {
00082     // Check input data
00083     if (!Source || !Size || !UncompressedSize)
00084     {
00085         std::cerr << "Failed to uncompress data (invalid parameter)" << std::endl;
00086         return false;
00087     }
00088 
00089     // Make sure the source pointer is not our internal buffer (zlib fails in such case)
00090     if (!myData.empty() && (Source == GetData()))
00091     {
00092         std::cerr << "Failed to uncompress data (source buffer is the internal buffer)" << std::endl;
00093         return false;
00094     }
00095 
00096     // Make sure the destination buffer will be large enough
00097     uLong EstimatedSize = static_cast<uLong>(UncompressedSize);
00098     myData.resize(EstimatedSize);
00099 
00100     // Let zlib do the job
00101     int Error = uncompress(reinterpret_cast<Byte*>(&myData[0]), &EstimatedSize, reinterpret_cast<const Byte*>(Source), static_cast<uLong>(Size));
00102 
00103     // Check errors
00104     if (Error != Z_OK)
00105     {
00106         std::cerr << "Failed to compress data (" << zError(Error) << ")" << std::endl;
00107         return false;
00108     }
00109 
00110     // Resize the internal buffer to the actual size of uncompressed data
00111     myData.resize(EstimatedSize);
00112 
00113     return true;
00114 }
00115 
00116 
00122 const char* sfCompressor::GetData() const
00123 {
00124     return myData.empty() ? NULL : &myData[0];
00125 }
00126 
00127 
00132 std::size_t sfCompressor::GetDataSize() const
00133 {
00134     return myData.size();
00135 }

Generated for SFML by  doxygen 1.5.2