bool POVMS_Object::WriteHeader(POVMSStream *stream, int& objectsize)
{
	POVMSInt datasize = 0;
	POVMSInt maxsize = 16;

	objectsize = 0;

	if(stream == NULL)
		return false;

	objectsize = POVMSStream_Size(&data);
	if(objectsize == 0)
		return false;

	datasize += POVMSStream_WriteString("POVRAYMS", stream, &maxsize);             // header       8 byte
	datasize += POVMSStream_WriteInt(0x0351, stream + datasize, &maxsize);         // version      4 byte
	datasize += POVMSStream_WriteInt(objectsize, stream + datasize, &maxsize);     // object size  4 byte

	if((datasize != 16) || (maxsize != 0))
		return false;

	return true;
}
Beispiel #2
0
void POVMS_Object::Write(OutputStream& stream, bool append, bool compress)
{
    POVMSType encoding = kPOVMSRawStreamEncoding;
    POVMSStream headerstream[16];
    POVMSStream *objectstream = nullptr;
    POVMSStream *compressedstream = nullptr;
    int maxheadersize = 0;
    int maxobjectsize = 0;
    int objectsize = 0;
    int datasize = 0;

    try
    {
        objectsize = POVMSStream_Size(&data);
        if(objectsize == 0)
            throw POV_EXCEPTION_CODE(pov_base::kNullPointerErr);

        if(append == false)
        {
            datasize = 0;
            maxheadersize = 12; // header

            datasize += POVMSStream_WriteString("POVRAYMS", headerstream + datasize, &maxheadersize);              // header       8 byte
            datasize += POVMSStream_WriteInt(0x0370, headerstream + datasize, &maxheadersize);                     // version      4 byte

            if(!stream.write((void *)headerstream, datasize))
                throw POV_EXCEPTION_CODE(pov_base::kFileDataErr);
        }

        objectstream = new POVMSStream[objectsize];
        maxobjectsize = objectsize;
        (void)POVMSStream_Write(&data, objectstream, &maxobjectsize);

#ifdef POVMS_COMPRESSION_ENABLED
        if(compress == true)
        {
            compressedstream = new POVMSStream[objectsize * 2];

            // compress stream data
            uLongf destlen = (uLongf)(objectsize * 2);
            if(compress2((Bytef *)(compressedstream), &destlen, (Bytef *)(objectstream), (uLongf)(objectsize), Z_BEST_COMPRESSION) != Z_OK)
                throw POV_EXCEPTION_CODE(pov_base::kCannotHandleDataErr);

            datasize = 0;
            maxheadersize = 12; // header

            datasize += POVMSStream_WriteInt((POVMSInt)(destlen), headerstream + datasize, &maxheadersize);        // data size    4 byte
            datasize += POVMSStream_WriteType(kPOVMSGZipStreamEncoding, headerstream + datasize, &maxheadersize);  // encoding     4 byte
            datasize += POVMSStream_WriteInt(objectsize, headerstream + datasize, &maxheadersize);                 // object size  4 byte

            if(!stream.write((void *)headerstream, datasize))
                throw POV_EXCEPTION_CODE(pov_base::kFileDataErr);

            if(!stream.write((void *)compressedstream, (int)(destlen)))                                            // data         x byte
                throw POV_EXCEPTION_CODE(pov_base::kFileDataErr);
        }
        else
#endif
        {
            datasize = 0;
            maxheadersize = 8; // header

            datasize += POVMSStream_WriteInt(objectsize, headerstream + datasize, &maxheadersize);                 // object size  4 byte
            datasize += POVMSStream_WriteType(kPOVMSRawStreamEncoding, headerstream + datasize, &maxheadersize);   // encoding     4 byte

            if(!stream.write((void *)headerstream, datasize))
                throw POV_EXCEPTION_CODE(pov_base::kFileDataErr);

            if(!stream.write((void *)objectstream, objectsize))                                                    // object       x byte
                throw POV_EXCEPTION_CODE(pov_base::kFileDataErr);
        }

        if (objectstream != nullptr)
            delete[] objectstream;
        objectstream = nullptr;

        if (compressedstream != nullptr)
            delete[] compressedstream;
        compressedstream = nullptr;
    }
    catch(...)
    {
        if (objectstream != nullptr)
            delete[] objectstream;

        if (compressedstream != nullptr)
            delete[] compressedstream;

        throw;
    }
}