Пример #1
0
        public static void Save(byte[] data, int width, int height, Stream outStream, ImageSavedCallbackMethod imageSavedCallback)
        {
            try
            {
                using (var dataStream = new MemoryStream(data))
                {
                    var writer = new BinaryWriter(outStream);

                    // File Type
                    int type = Streams.MakeFourCC('b', 'm', 'p', 'c');
                    writer.Write(type);

                    // Version
                    float version = 1.0f;
                    writer.Write(version);

                    // Meta Data
                    writer.Write(width);
                    writer.Write(height);
                    writer.Write(true);                    // Zip Compressed

                    // Data
                    using (var compressedDataStream = new MemoryStream())
                        using (var compressedStream = new GZipStream(compressedDataStream, CompressionMode.Compress))
                        {
                            dataStream.CopyTo(compressedStream);
                                                #if !WINRT
                            compressedStream.Close();
                                                #endif

                            var compressedData = compressedDataStream.ToArray();
                            writer.Write(compressedData.Length);
                            writer.Write(compressedData);
                        }
                }
            }
            catch (Exception e)
            {
                if (imageSavedCallback != null)
                {
                    imageSavedCallback(false);
                }
            }

            if (imageSavedCallback != null)
            {
                imageSavedCallback(true);
            }
        }
Пример #2
0
        protected void readMetaData(Stream stream, BinaryReader reader)
        {
            // check to make sure is WAV file
            chunkID = reader.ReadInt32();
            if (chunkID != Streams.MakeFourCC('R', 'I', 'F', 'F'))
            {
                Debug.ThrowError("ISoundWAV", "Not a valid WAV file - No RIFF ID");
            }
            chunkSize = reader.ReadInt32();

            riffType = reader.ReadInt32();
            if (riffType != Streams.MakeFourCC('W', 'A', 'V', 'E'))
            {
                Debug.ThrowError("ISoundWAV", "Not a WAV file - No WAVE ID");
            }

            // navigate to 'fmt' chunk
            while (stream.Position <= stream.Length)
            {
                if (stream.Position + sizeof(int) > stream.Length)
                {
                    Debug.ThrowError("ISoundWAV", "No fmt ID");
                }
                formatID = reader.ReadInt32();
                if (formatID == Streams.MakeFourCC('f', 'm', 't', ' '))
                {
                    break;
                }
            }
            formatSize           = reader.ReadInt32();
            formatCode           = reader.ReadInt16();
            channels             = reader.ReadInt16();
            sampleRate           = reader.ReadInt32();
            formatAvgBytesPerSec = reader.ReadInt32();
            formatBlockAlign     = reader.ReadInt16();
            bitDepth             = reader.ReadInt16();

            formatExtraSize = 0;
            if (formatSize == 18)
            {
                // Read any extra values
                formatExtraSize = reader.ReadInt16();
                reader.ReadBytes(formatExtraSize);
            }

            // navigate to 'data' chunk
            while (stream.Position <= stream.Length)
            {
                if (stream.Position + sizeof(int) > stream.Length)
                {
                    Debug.ThrowError("ISoundWAV", "No data ID");
                }
                dataID = reader.ReadInt32();
                if (dataID == Streams.MakeFourCC('d', 'a', 't', 'a'))
                {
                    break;
                }
            }
            dataSize = reader.ReadInt32();

            TotalTime = TimeSpan.FromSeconds(dataSize / formatAvgBytesPerSec);
        }
Пример #3
0
        public static void Save(Stream stream, bool compress, SoftwareModel softwareModel, bool loadColors, bool loadUVs, bool loadNormals)
        {
            var writer = new BinaryWriter(stream);

            // meta data
            writer.Write(Streams.MakeFourCC('R', 'M', 'F', 'T')); // tag
            writer.Write(1.0f);                                   // version
            writer.Write(false);                                  //compress);// TODO: add zip compression

            // frames
            writer.Write(softwareModel.FrameStart);
            writer.Write(softwareModel.FrameEnd);
            writer.Write(softwareModel.FPS);

            // materials
            writer.Write(softwareModel.Materials.Count);
            foreach (var material in softwareModel.Materials)
            {
                writer.Write(material.Name);

                // values1
                writer.Write(material.Values1.Count);
                foreach (var value in material.Values1)
                {
                    writer.Write(value.Key);
                    writer.Write(value.Value);
                }

                // values2
                writer.Write(material.Values2.Count);
                foreach (var value in material.Values2)
                {
                    writer.Write(value.Key);
                    writer.WriteVector(value.Value);
                }

                // values3
                writer.Write(material.Values3.Count);
                foreach (var value in material.Values3)
                {
                    writer.Write(value.Key);
                    writer.WriteVector(value.Value);
                }

                // values4
                writer.Write(material.Values4.Count);
                foreach (var value in material.Values4)
                {
                    writer.Write(value.Key);
                    writer.WriteVector(value.Value);
                }

                // textures
                writer.Write(material.Textures.Count);
                foreach (var texture in material.Textures)
                {
                    writer.Write(texture.Key);
                    writer.Write(texture.Value);
                }
            }

            // meshes
            writer.Write(softwareModel.Meshes.Count);
            foreach (var mesh in softwareModel.Meshes)
            {
                Mesh.Write(writer, softwareModel, mesh, loadColors, loadUVs, loadNormals);
            }

            // actions
            writer.Write(softwareModel.Actions.Count);
            foreach (var action in softwareModel.Actions)
            {
                Action.Write(writer, action);
            }

            // armatures
            writer.Write(softwareModel.Armatures.Count);
            foreach (var armature in softwareModel.Armatures)
            {
                Armature.Write(writer, armature);
            }

            // objects
            writer.Write(softwareModel.Objects.Count);
            foreach (var o in softwareModel.Objects)
            {
                Object.Write(writer, o);
            }
        }
Пример #4
0
        private void init(string filename, Stream stream, string contentDirectory, Dictionary <string, Type> materialTypes, List <MaterialFieldBinder> value1BinderTypes, List <MaterialFieldBinder> value2BinderTypes, List <MaterialFieldBinder> value3BinderTypes, List <MaterialFieldBinder> value4BinderTypes, List <MaterialFieldBinder> textureBinderTypes, Dictionary <string, string> fileExtOverrides, int classicInstanceCount, Loader.LoadedCallbackMethod loadedCallback)
        {
            try
            {
                var reader = new BinaryReader(stream);

                // meta data
                if (reader.ReadInt32() != Streams.MakeFourCC('R', 'M', 'F', 'T'))
                {
                    Debug.ThrowError("Error", "Not a ReignModel file: " + filename);
                }
                float version = reader.ReadSingle();
                if (version != 1.0f)
                {
                    Debug.ThrowError("Error", "Unsuported model version: " + version.ToString());
                }
                bool compressed = reader.ReadBoolean();

                // frames
                FrameStart = reader.ReadSingle();
                FrameEnd   = reader.ReadSingle();
                FrameCount = FrameEnd - FrameStart;
                FPS        = reader.ReadSingle();

                // materials
                int materialCount = reader.ReadInt32();
                Materials = new MaterialI[materialCount];
                Textures  = new List <ITexture2D>();
                for (int i = 0; i != materialCount; ++i)
                {
                    string name = reader.ReadString();

                    // create material
                    bool pass = false;
                    foreach (var materialType in (Dictionary <string, Type>)materialTypes)
                    {
                        if (materialType.Key == name)
                        {
                            Materials[i]      = (MaterialI)Activator.CreateInstance(materialType.Value);
                            Materials[i].Name = name;
                            pass = true;
                            break;
                        }
                    }
                    if (!pass)
                    {
                        Debug.ThrowError("Model", "Failed to find a valid material type for: " + name);
                    }
                    var material = Materials[i];

                    // values1
                    var values1    = new Dictionary <string, float>();
                    int valueCount = reader.ReadInt32();
                    for (int i2 = 0; i2 != valueCount; ++i2)
                    {
                        values1.Add(reader.ReadString(), reader.ReadSingle());
                    }
                    bindTypes(material, values1, value1BinderTypes, contentDirectory, fileExtOverrides, handleFoundValueBinder);

                    // values2
                    var values2 = new Dictionary <string, Vector2>();
                    valueCount = reader.ReadInt32();
                    for (int i2 = 0; i2 != valueCount; ++i2)
                    {
                        values2.Add(reader.ReadString(), reader.ReadVector2());
                    }
                    bindTypes(material, values2, value2BinderTypes, contentDirectory, fileExtOverrides, handleFoundValueBinder);

                    // values3
                    var values3 = new Dictionary <string, Vector3>();
                    valueCount = reader.ReadInt32();
                    for (int i2 = 0; i2 != valueCount; ++i2)
                    {
                        values3.Add(reader.ReadString(), reader.ReadVector3());
                    }
                    bindTypes(material, values3, value3BinderTypes, contentDirectory, fileExtOverrides, handleFoundValueBinder);

                    // values4
                    var values4 = new Dictionary <string, Vector4>();
                    valueCount = reader.ReadInt32();
                    for (int i2 = 0; i2 != valueCount; ++i2)
                    {
                        values4.Add(reader.ReadString(), reader.ReadVector4());
                    }
                    bindTypes(material, values4, value4BinderTypes, contentDirectory, fileExtOverrides, handleFoundValueBinder);

                    // textures
                    var textures     = new Dictionary <string, string>();
                    int textureCount = reader.ReadInt32();
                    for (int i2 = 0; i2 != textureCount; ++i2)
                    {
                        textures.Add(reader.ReadString(), reader.ReadString());
                    }
                    bindTypes(material, textures, textureBinderTypes, contentDirectory, fileExtOverrides, handleFoundTextureBinder);
                }

                // meshes
                int meshCount = reader.ReadInt32();
                Meshes = new Mesh[meshCount];
                for (int i = 0; i != meshCount; ++i)
                {
                    Meshes[i] = new Mesh(reader, this, classicInstanceCount);
                }

                // actions
                int actionCount = reader.ReadInt32();
                Actions = new Action[actionCount];
                for (int i = 0; i != actionCount; ++i)
                {
                    Actions[i] = new Action(reader);
                }

                // armatures
                int armatureCount = reader.ReadInt32();
                Armatures = new Armature[armatureCount];
                for (int i = 0; i != armatureCount; ++i)
                {
                    Armatures[i] = new Armature(reader);
                }

                // objects
                int objectCount = reader.ReadInt32();
                Objects = new Object[objectCount];
                for (int i = 0; i != objectCount; ++i)
                {
                    string type = reader.ReadString();
                    if (type == "MESH")
                    {
                        Objects[i] = new ObjectMesh(reader, this);
                    }
                    else if (type == "ARMATURE")
                    {
                        Objects[i] = new ObjectArmature(reader, this);
                    }
                    else
                    {
                        Debug.ThrowError("Mesh", "Unsuported Object type: " + type);
                    }
                }

                // link objects
                foreach (var o in Objects)
                {
                    o.linkObjects(Objects);
                }
            }
            catch (Exception e)
            {
                FailedToLoad = true;
                Loader.AddLoadableException(e);
                Dispose();
                if (loadedCallback != null)
                {
                    loadedCallback(this, false);
                }
                return;
            }

            if (Textures.Count == 0)
            {
                Loaded = true;
                if (loadedCallback != null)
                {
                    loadedCallback(this, true);
                }
            }
            else
            {
                new LoadWaiter(Textures.ToArray(),
                               delegate(object sender, bool succeeded)
                {
                    if (succeeded)
                    {
                        Loaded = true;
                        if (loadedCallback != null)
                        {
                            loadedCallback(this, true);
                        }
                    }
                    else
                    {
                        FailedToLoad = true;
                        Dispose();
                        if (loadedCallback != null)
                        {
                            loadedCallback(this, false);
                        }
                    }
                });
            }
        }
Пример #5
0
        protected override void init(Stream stream, bool flip, Loader.LoadedCallbackMethod loadedCallback)
        {
            try
            {
                ImageType     = ImageTypes.BMPC;
                ImageFormat   = ImageFormats.BMPC;
                SurfaceFormat = SurfaceFormats.RGBAx8;

                using (var reader = new BinaryReader(stream))
                {
                    // File Type
                    int type = reader.ReadInt32();
                    if (type != Streams.MakeFourCC('b', 'm', 'p', 'c'))
                    {
                        throw new Exception("Not a .bmpc file");
                    }

                    // Version
                    float version = reader.ReadSingle();
                    if (version != 1.0f)
                    {
                        throw new Exception("Unsuported .bmpc version");
                    }

                    // Meta Data
                    Size = new Size2(reader.ReadInt32(), reader.ReadInt32());
                    bool zipCompressed = reader.ReadBoolean();

                    // Data
                    using (var decompressedDataStream = new MemoryStream())
                    {
                        int dataLength = reader.ReadInt32();
                        int dataRead   = 0;
                        do
                        {
                            int read = 1024;
                            if ((dataRead + read) > dataLength)
                            {
                                read -= (int)((dataRead + read) - dataLength);
                            }

                            var data = reader.ReadBytes(read);
                            decompressedDataStream.Write(data, 0, data.Length);

                            dataRead += read;
                        } while (dataRead < dataLength);
                        decompressedDataStream.Position = 0;

                                                #if NaCl || SILVERLIGHT
                        using (var zip = new GZipInputStream(decompressedDataStream))
                            using (var dataStream = new MemoryStream())
                            {
                                var buffer = new byte[4096];
                                int read   = 0;
                                do
                                {
                                    read = zip.Read(buffer, 0, buffer.Length);
                                    dataStream.Write(buffer, 0, buffer.Length);
                                } while (read > 0);

                                Mipmaps    = new Mipmap[1];
                                Mipmaps[0] = new Mipmap(dataStream.GetBuffer(), Size.Width, Size.Height, 1, 4);
                                if (flip)
                                {
                                    Mipmaps[0].FlipVertical();
                                }
                            }
                                                #else
                        using (var decompressedStream = new GZipStream(decompressedDataStream, CompressionMode.Decompress))
                            using (var dataStream = new MemoryStream())
                            {
                                decompressedStream.CopyTo(dataStream);
                                Mipmaps    = new Mipmap[1];
                                Mipmaps[0] = new Mipmap(dataStream.ToArray(), Size.Width, Size.Height, 1, 4);
                                if (flip)
                                {
                                    Mipmaps[0].FlipVertical();
                                }
                            }
                                                #endif
                    }
                }
            }
            catch (Exception e)
            {
                FailedToLoad = true;
                Loader.AddLoadableException(e);
                if (loadedCallback != null)
                {
                    loadedCallback(this, false);
                }
                return;
            }

            Loaded = true;
            Loader.AddLoadable(this);
            if (loadedCallback != null)
            {
                loadedCallback(this, true);
            }
        }