const_byteptr decodeArray(Codec const& codec, Uint* xs, unsigned n, const_byteptr p, const_byteptr end) { if (codec.k_max_bytes * n + p > end) for (unsigned i = 0; i < n; ++i) p = codec.decode(xs[i], p, end); else for (unsigned i = 0; i < n; ++i) p = codec.decode(xs[i], p); return p; }
void test() { for(int i=0;i<Codec::factoryCount();i++) { Codec::Factory * pFactory = Codec::factory( i ); // generate random data byte data[ CODEC_DATA_SIZE ]; for(int i=0;i<sizeof(data);i++) data[i] = (i & 0xff); //rand() & 0xff; // create the codec Codec * pCodec = pFactory->create(); Test( pCodec != NULL ); byte encoded[ CODEC_DATA_SIZE * 3 ]; int nBytes = pCodec->encode( data, sizeof(data), encoded, sizeof(encoded), Codec::CL_NORMAL ); Test( nBytes >= 0 ); delete pCodec; printf("Codec %s, %d bytes encoded into %d bytes - %d%% Compression\n", pFactory->name(), CODEC_DATA_SIZE, nBytes, ((nBytes * 100) / CODEC_DATA_SIZE) ); pCodec = pFactory->create(); Test( pCodec != NULL ); int nDecodedBytes = pCodec->decode( encoded, nBytes, data, sizeof(data) ); Test( nDecodedBytes == CODEC_DATA_SIZE ); for(int j=0;j<sizeof(data);j++) Test( data[j] == (j & 0xff) ); delete pCodec; } }
int main(void) { Codec codec; vector<string> strs = {"", "vn"}; string encoded_string = codec.encode(strs); vector<string> strs2 = codec.decode(encoded_string); assert(strs.size() == strs2.size()); for (size_t i = 0; i < strs.size(); i++) assert(strs[i] == strs2[i]); cout << "\nPassed All\n"; return 0; }
//----------------------------------------------------------------------------- Image & Image::load(DataStreamPtr& stream, const String& type ) { if( m_pBuffer && m_bAutoDelete ) { OGRE_FREE(m_pBuffer, MEMCATEGORY_GENERAL); m_pBuffer = NULL; } Codec * pCodec = 0; if (!type.empty()) { // use named codec pCodec = Codec::getCodec(type); } else { // derive from magic number // read the first 32 bytes or file size, if less size_t magicLen = std::min(stream->size(), (size_t)32); char magicBuf[32]; stream->read(magicBuf, magicLen); // return to start stream->seek(0); pCodec = Codec::getCodec(magicBuf, magicLen); } if( !pCodec ) OGRE_EXCEPT( Exception::ERR_INVALIDPARAMS, "Unable to load image - unable to identify codec. Check file extension " "and file format.", "Image::load" ); Codec::DecodeResult res = pCodec->decode(stream); ImageCodec::ImageData* pData = static_cast<ImageCodec::ImageData*>(res.second.getPointer()); m_uWidth = pData->width; m_uHeight = pData->height; m_uDepth = pData->depth; m_uSize = pData->size; m_uNumMipmaps = pData->num_mipmaps; m_uFlags = pData->flags; // Get the format and compute the pixel size m_eFormat = pData->format; m_ucPixelSize = static_cast<uchar>(PixelUtil::getNumElemBytes( m_eFormat )); // Just use internal buffer of returned memory stream m_pBuffer = res.first->getPtr(); // Make sure stream does not delete res.first->setFreeOnClose(false); return *this; }
//----------------------------------------------------------------------------- Image & Image::load(DataStreamPtr& stream, const String& type ) { freeMemory(); Codec * pCodec = 0; if (!type.empty()) { // use named codec pCodec = Codec::getCodec(type); } else { // derive from magic number // read the first 32 bytes or file size, if less size_t magicLen = std::min(stream->size(), (size_t)32); char magicBuf[32]; stream->read(magicBuf, magicLen); // return to start stream->seek(0); pCodec = Codec::getCodec(magicBuf, magicLen); if( !pCodec ) OGRE_EXCEPT( Exception::ERR_INVALIDPARAMS, "Unable to load image: Image format is unknown. Unable to identify codec. " "Check it or specify format explicitly.", "Image::load" ); } Codec::DecodeResult res = pCodec->decode(stream); ImageCodec::ImageData* pData = static_cast<ImageCodec::ImageData*>(res.second.getPointer()); mWidth = pData->width; mHeight = pData->height; mDepth = pData->depth; mBufSize = pData->size; mNumMipmaps = pData->num_mipmaps; mFlags = pData->flags; // Get the format and compute the pixel size mFormat = pData->format; mPixelSize = static_cast<uchar>(PixelUtil::getNumElemBytes( mFormat )); // Just use internal buffer of returned memory stream mBuffer = res.first->getPtr(); // Make sure stream does not delete res.first->setFreeOnClose(false); // make sure we delete mAutoDelete = true; return *this; }
//----------------------------------------------------------------------------- Image & Image::load(const String& strFileName, const String& group) { OgreGuard( "Image::load" ); freeData(); String strExt; size_t pos = strFileName.find_last_of("."); if( pos == String::npos ) OGRE_EXCEPT( Exception::ERR_INVALIDPARAMS, "Unable to load image file '" + strFileName + "' - invalid extension.", "Image::load" ); while( pos != strFileName.length() - 1 ) strExt += strFileName[++pos]; Codec * pCodec = Codec::getCodec(strExt); if( !pCodec ) OGRE_EXCEPT( Exception::ERR_INVALIDPARAMS, "Unable to load image file '" + strFileName + "' - invalid extension.", "Image::load" ); DataStreamPtr encoded = ResourceGroupManager::getSingleton().openResource(strFileName, group); Codec::DecodeResult res = pCodec->decode(encoded); ImageCodec::ImageData* pData = static_cast<ImageCodec::ImageData*>(res.second.getPointer()); // Get the format and compute the pixel size m_uWidth = pData->width; m_uHeight = pData->height; m_uDepth = pData->depth; m_uSize = pData->size; m_eFormat = pData->format; m_uNumMipmaps = pData->num_mipmaps; m_ucPixelSize = PixelUtil::getNumElemBytes( m_eFormat ); m_uFlags = pData->flags; // re-use the decoded buffer m_pBuffer = res.first->getPtr(); // ensure we don't delete when stream is closed res.first->setFreeOnClose(false); OgreUnguardRet( *this ); }
// Your Codec object will be instantiated and called as such: // Codec codec; // codec.decode(codec.encode(strs)); // int main() { Codec codec; vector<string> strs; strs.push_back("hello"); strs.push_back(""); strs.push_back("gaobo"); string coded_str = codec.encode(strs); cout << "coded str: " << coded_str << endl; vector<string> decoded_strs = codec.decode(coded_str); for (int i = 0; i < decoded_strs.size(); i++) { cout << decoded_strs[i] << endl; } return 0; }
//----------------------------------------------------------------------------- Image & Image::load(DataStreamPtr& stream, const String& type ) { OgreGuard( "Image::load" ); freeData(); String strType = type; Codec * pCodec = Codec::getCodec(strType); if( !pCodec ) OGRE_EXCEPT( Exception::ERR_INVALIDPARAMS, "Unable to load image - invalid extension.", "Image::load" ); Codec::DecodeResult res = pCodec->decode(stream); ImageCodec::ImageData* pData = static_cast<ImageCodec::ImageData*>(res.second.getPointer()); m_uWidth = pData->width; m_uHeight = pData->height; m_uDepth = pData->depth; m_uSize = pData->size; m_uNumMipmaps = pData->num_mipmaps; m_uFlags = pData->flags; // Get the format and compute the pixel size m_eFormat = pData->format; m_ucPixelSize = PixelUtil::getNumElemBytes( m_eFormat ); // Just use internal buffer of returned memory stream m_pBuffer = res.first->getPtr(); // Make sure stream does not delete res.first->setFreeOnClose(false); OgreUnguardRet( *this ); }