XMLNodePtr XMLDoc::Parse( Stream& source ) { uint32_t size = source.GetSize(); mXMLSrc.resize(size + 1, 0); source.Read(&mXMLSrc[0], size); mXMLSrc[size] = '\0'; mDocument.parse<0>(&mXMLSrc[0]); mRoot = std::make_shared<XMLNode>(mDocument.first_node()); return mRoot; }
static int64 sLZ4Compress(Stream& out, Stream& in, int64 size, Gate<int64, int64> progress, bool co) { LZ4CompressStream outs(out); #ifdef _MULTITHREADED if(co) outs.Co(); #endif sCompressStreamCopy_(outs, in, progress, in, size); outs.Close(); if(!out.IsError() && !outs.IsError()) return out.GetSize(); return -1; }
static int64 sZstdDecompress(Stream& out, Stream& in, int64 size, Gate<int64, int64> progress, bool co) { ZstdDecompressStream ins(in); #ifdef _MULTITHREADED if(co) ins.Co(); #endif sCompressStreamCopy_(out, ins, progress, in, size); ins.Close(); if(!out.IsError() && !ins.IsError()) return out.GetSize(); return -1; }
int32 Chunk::FindSubChunkOffset(std::string name) { // Reverse the name name = std::string(name.rbegin(), name.rend()); if (name.size() != 4) return -1; Stream* stream = GetStream(); uint32 matched = 0; while (stream->GetPos() < stream->GetSize()) { char b = stream->Read<char>(); if (b != name[matched]) matched = 0; else ++matched; if (matched == 4) return stream->GetPos() - 4; } return -1; }
static bool ras_decode(Bitmap& target, Stream& stream) { // reset stream stream.Seek(0,Stream::START); // streamsize - headersize int size = stream.GetSize() - 32; if ( size < 1 ) return false; // read header uint32 magic = ReadBigEndian<uint32>(stream); uint32 width = ReadBigEndian<uint32>(stream); uint32 height = ReadBigEndian<uint32>(stream); uint32 bits = ReadBigEndian<uint32>(stream); /* uint32 length = */ ReadBigEndian<uint32>(stream); uint32 type = ReadBigEndian<uint32>(stream); uint32 maptype = ReadBigEndian<uint32>(stream); uint32 maplength = ReadBigEndian<uint32>(stream); // verify header if ( magic != 0x59a66a95 ) return false; if ( bits != 1 && bits != 8 && bits != 24 && bits != 32 ) return false; if ( type != 0 && type != 1 && type != 2 && type != 3 ) return false; // create bitmap switch ( bits ) { case 1: target = Bitmap(width,height,PIXELFORMAT_INTENSITY8); break; case 8: target = Bitmap(width,height,PIXELFORMAT_PALETTE8(NULL)); break; case 24: target = Bitmap(width,height,PIXELFORMAT_RGB888); break; case 32: target = Bitmap(width,height,PIXELFORMAT_ARGB8888); break; default: return false; } // read colormap switch ( maptype ) { // "none" - no palette case 0: { stream.Seek(maplength,Stream::CURRENT); break; } // "equal rgb" -mode palette case 1: { if ( bits != 8 || maplength > 768 ) return false; const PixelFormat& pxf = target.GetPixelFormat(); Color32* palette = pxf.GetPalette(); int count = static_cast<int>(maplength / 3); int i; for ( i=0; i<count; ++i ) palette[i].r = ReadBigEndian<uint8>(stream); for ( i=0; i<count; ++i ) palette[i].g = ReadBigEndian<uint8>(stream); for ( i=0; i<count; ++i ) palette[i].b = ReadBigEndian<uint8>(stream); for ( i=0; i<count; ++i ) palette[i].a = 0xff; break; } // "raw" -mode palette // TODO: this code has not been tested // (couldn't find files to test with or specification) // this is my best guess how the "raw" mode works! case 2: { if ( bits != 8 || maplength > 768 ) return false; const PixelFormat& pxf = target.GetPixelFormat(); Color32* palette = pxf.GetPalette(); int count = static_cast<int>(maplength / 3); for ( int i=0; i<count; ++i ) { palette[i].r = ReadBigEndian<uint8>(stream); palette[i].g = ReadBigEndian<uint8>(stream); palette[i].b = ReadBigEndian<uint8>(stream); palette[i].a = 0xff; } } // unknown fileformat default: return false; } // decode buffer int buffersize = stream.GetSize() - stream.GetOffset(); uint8* buffer = new uint8[buffersize]; stream.Read(buffer,buffersize); // read image uint8* image = target.GetImage(); int numpixel = width * height; if ( numpixel < 1 ) return false; switch ( bits ) { case 1: { switch ( type ) { case 0: case 1: case 3: { int ycount = static_cast<int>(height); while ( ycount-- ) { int bitcount = -1; uint8 v = 0; int xcount = static_cast<int>(width); while ( xcount-- ) { if ( bitcount < 0 ) { bitcount = 7; v = *buffer++; } *image++ = (v >> (bitcount--)) & 1 ? 0 : 255; } } break; } case 2: { // TODO: RLE decoder // TODO: special handling of 1 bit stream break; } } break; } case 8: { switch ( type ) { case 0: case 1: case 3: { memcpy(image,buffer,numpixel); break; } case 2: { // TODO: RLE decoder // TODO: RGB/BGR fix // TODO: test rasrle() rasrle(image,buffer,numpixel); break; } } break; } case 24: { switch ( type ) { case 0: case 1: { memcpy(image,buffer,numpixel*3); break; } case 2: { // TODO: RLE decoder // TODO: RGB/BGR fix // TODO: test rasrle() rasrle(image,buffer,numpixel*3); break; } case 3: { while ( numpixel-- ) { image[0] = buffer[2]; image[1] = buffer[1]; image[2] = buffer[0]; buffer += 3; image += 3; } break; } } break; } case 32: { switch ( type ) { case 0: case 1: { while ( numpixel-- ) { image[0] = buffer[1]; image[1] = buffer[2]; image[2] = buffer[3]; image[3] = buffer[0]; buffer += 4; image += 4; } break; } case 2: { // TODO: RLE decoder // TODO: RGB/BGR fix // TODO: test rasrle() rasrle(image,buffer,numpixel*4); break; } case 3: { while ( numpixel-- ) { image[0] = buffer[3]; image[1] = buffer[2]; image[2] = buffer[1]; image[3] = buffer[0]; buffer += 4; image += 4; } break; } } break; } } // free decode buffer delete[] buffer; return true; }