Exemple #1
0
        /// <summary>
        /// Removes the specified texture from the array.
        /// </summary>
        /// <param name="array">The array.</param>
        /// <param name="request">The request.</param>
        /// <exception cref="TexLibraryException">You can't remove a texture from a texture cube.</exception>
        private void Remove(TexImage array, ArrayElementRemovalRequest request)
        {
            Log.Info("Removing texture at rank " + request.Indice + " from the texture array ...");

            if (array.Dimension == TexImage.TextureDimension.TextureCube)
            {
                Log.Error("You can't remove a texture from a texture cube.");
                throw new TextureToolsException("You can't remove a texture from a texture cube.");
            }

            int subImageCount = array.SubImageArray.Length / array.ArraySize;
            int indice        = request.Indice * subImageCount;

            // Allocating memory
            int elementSize = 0;

            for (int i = 0; i < subImageCount; ++i)
            {
                elementSize += array.SubImageArray[i].DataSize;
            }
            int    newSize = array.DataSize - elementSize;
            IntPtr buffer  = Marshal.AllocHGlobal(newSize);

            long bufferData = buffer.ToInt64();

            TexImage.SubImage[] subImages = new TexImage.SubImage[array.SubImageArray.Length - subImageCount];
            int offset = 0;

            for (int i = 0; i < indice; ++i)
            {
                subImages[i]      = array.SubImageArray[i];
                subImages[i].Data = new IntPtr(bufferData + offset);
                Utilities.CopyMemory(subImages[i].Data, array.SubImageArray[i].Data, array.SubImageArray[i].DataSize);
                offset += array.SubImageArray[i].DataSize;
            }

            int ct = indice;

            for (int i = indice + subImageCount; i < array.SubImageArray.Length; ++i)
            {
                subImages[indice]      = array.SubImageArray[i];
                subImages[indice].Data = new IntPtr(bufferData + offset);
                Utilities.CopyMemory(subImages[indice].Data, array.SubImageArray[i].Data, array.SubImageArray[i].DataSize);
                offset += array.SubImageArray[i].DataSize;
                ++indice;
            }

            // Freeing memory
            if (array.DisposingLibrary != null)
            {
                array.DisposingLibrary.Dispose(array);
            }

            // Updating the array
            array.Data     = buffer;
            array.DataSize = newSize;
            --array.ArraySize;
            array.SubImageArray    = subImages;
            array.DisposingLibrary = this;
        }
        /// <summary>
        /// Removes the specified texture from the array.
        /// </summary>
        /// <param name="array">The array.</param>
        /// <param name="request">The request.</param>
        /// <exception cref="TexLibraryException">You can't remove a texture from a texture cube.</exception>
        private void Remove(TexImage array, ArrayElementRemovalRequest request)
        {
            Log.Info("Removing texture at rank " + request.Indice + " from the texture array ...");

            if (array.Dimension == TexImage.TextureDimension.TextureCube)
            {
                Log.Error("You can't remove a texture from a texture cube.");
                throw new TextureToolsException("You can't remove a texture from a texture cube.");
            }

            int subImageCount = array.SubImageArray.Length / array.ArraySize;
            int indice = request.Indice * subImageCount;

            // Allocating memory
            int elementSize = 0;
            for (int i = 0; i < subImageCount; ++i) elementSize += array.SubImageArray[i].DataSize;
            int newSize = array.DataSize - elementSize;
            IntPtr buffer = Marshal.AllocHGlobal(newSize);

            long bufferData = buffer.ToInt64();
            TexImage.SubImage[] subImages = new TexImage.SubImage[array.SubImageArray.Length - subImageCount];
            int offset = 0;

            for (int i = 0; i < indice; ++i)
            {
                subImages[i] = array.SubImageArray[i];
                subImages[i].Data = new IntPtr(bufferData + offset);
                Utilities.CopyMemory(subImages[i].Data, array.SubImageArray[i].Data, array.SubImageArray[i].DataSize);
                offset += array.SubImageArray[i].DataSize;
            }

            int ct = indice;
            for (int i = indice + subImageCount; i < array.SubImageArray.Length; ++i)
            {
                subImages[indice] = array.SubImageArray[i];
                subImages[indice].Data = new IntPtr(bufferData + offset);
                Utilities.CopyMemory(subImages[indice].Data, array.SubImageArray[i].Data, array.SubImageArray[i].DataSize);
                offset += array.SubImageArray[i].DataSize;
                ++indice;
            }

            // Freeing memory
            if (array.DisposingLibrary != null) array.DisposingLibrary.Dispose(array);

            // Updating the array
            array.Data = buffer;
            array.DataSize = newSize;
            --array.ArraySize;
            array.SubImageArray = subImages;
            array.DisposingLibrary = this;
        }
        /// <summary>
        /// Inserts the specified texture into the array at a given position.
        /// </summary>
        /// <param name="array">The array.</param>
        /// <param name="request">The request.</param>
        /// <exception cref="TexLibraryException">You can't add a texture to a texture cube.</exception>
        private void Insert(TexImage array, ArrayInsertionRequest request)
        {
            Log.Info("Inserting texture at rank " + request.Indice + " in the texture array ...");

            if (array.Dimension == TexImage.TextureDimension.TextureCube)
            {
                Log.Error("You can't add a texture to a texture cube.");
                throw new TextureToolsException("You can't add a texture to a texture cube.");
            }

            CheckConformity(array, request.Texture);

            int subImageCount = array.SubImageArray.Length / array.ArraySize;
            int indice = request.Indice * subImageCount;

            // Allocating memory
            int newSize = array.DataSize + request.Texture.DataSize;
            IntPtr buffer = Marshal.AllocHGlobal(newSize);

            long bufferData = buffer.ToInt64();
            TexImage.SubImage[] subImages = new TexImage.SubImage[array.SubImageArray.Length + subImageCount];
            int offset = 0;

            // Copying memory of the textures positioned before the new texture
            for (int i = 0; i < indice; ++i)
            {
                subImages[i] = array.SubImageArray[i];
                subImages[i].Data = new IntPtr(bufferData + offset);
                Utilities.CopyMemory(subImages[i].Data, array.SubImageArray[i].Data, array.SubImageArray[i].DataSize);
                offset += array.SubImageArray[i].DataSize;
            }

            // copying new texture data
            int ct = indice;
            for (int i = 0; i < subImageCount; ++i)
            {
                subImages[ct] = request.Texture.SubImageArray[i];
                Utilities.CopyMemory(subImages[ct].Data, request.Texture.SubImageArray[i].Data, request.Texture.SubImageArray[i].DataSize);
                offset += request.Texture.SubImageArray[i].DataSize;
                ++ct;
            }

            // Copying memory of the textures positioned after the new texture
            for (int i = indice; i < array.SubImageArray.Length; ++i)
            {
                subImages[ct] = array.SubImageArray[i];
                subImages[ct].Data = new IntPtr(bufferData + offset);
                Utilities.CopyMemory(subImages[ct].Data, array.SubImageArray[i].Data, array.SubImageArray[i].DataSize);
                offset += array.SubImageArray[i].DataSize;
                ++ct;
            }

            // Freeing memory
            if (array.DisposingLibrary != null) array.DisposingLibrary.Dispose(array);

            // Updating the array
            array.Data = buffer;
            array.DataSize = newSize;
            ++array.ArraySize;
            array.SubImageArray = subImages;
            array.DisposingLibrary = this;
        }
Exemple #4
0
        /// <summary>
        /// Inserts the specified texture into the array at a given position.
        /// </summary>
        /// <param name="array">The array.</param>
        /// <param name="request">The request.</param>
        /// <exception cref="TexLibraryException">You can't add a texture to a texture cube.</exception>
        private void Insert(TexImage array, ArrayInsertionRequest request)
        {
            Log.Info("Inserting texture at rank " + request.Indice + " in the texture array ...");

            if (array.Dimension == TexImage.TextureDimension.TextureCube)
            {
                Log.Error("You can't add a texture to a texture cube.");
                throw new TextureToolsException("You can't add a texture to a texture cube.");
            }

            CheckConformity(array, request.Texture);

            int subImageCount = array.SubImageArray.Length / array.ArraySize;
            int indice        = request.Indice * subImageCount;

            // Allocating memory
            int    newSize = array.DataSize + request.Texture.DataSize;
            IntPtr buffer  = Marshal.AllocHGlobal(newSize);

            long bufferData = buffer.ToInt64();

            TexImage.SubImage[] subImages = new TexImage.SubImage[array.SubImageArray.Length + subImageCount];
            int offset = 0;

            // Copying memory of the textures positioned before the new texture
            for (int i = 0; i < indice; ++i)
            {
                subImages[i]      = array.SubImageArray[i];
                subImages[i].Data = new IntPtr(bufferData + offset);
                Utilities.CopyMemory(subImages[i].Data, array.SubImageArray[i].Data, array.SubImageArray[i].DataSize);
                offset += array.SubImageArray[i].DataSize;
            }

            // copying new texture data
            int ct = indice;

            for (int i = 0; i < subImageCount; ++i)
            {
                subImages[ct] = request.Texture.SubImageArray[i];
                Utilities.CopyMemory(subImages[ct].Data, request.Texture.SubImageArray[i].Data, request.Texture.SubImageArray[i].DataSize);
                offset += request.Texture.SubImageArray[i].DataSize;
                ++ct;
            }

            // Copying memory of the textures positioned after the new texture
            for (int i = indice; i < array.SubImageArray.Length; ++i)
            {
                subImages[ct]      = array.SubImageArray[i];
                subImages[ct].Data = new IntPtr(bufferData + offset);
                Utilities.CopyMemory(subImages[ct].Data, array.SubImageArray[i].Data, array.SubImageArray[i].DataSize);
                offset += array.SubImageArray[i].DataSize;
                ++ct;
            }

            // Freeing memory
            if (array.DisposingLibrary != null)
            {
                array.DisposingLibrary.Dispose(array);
            }

            // Updating the array
            array.Data     = buffer;
            array.DataSize = newSize;
            ++array.ArraySize;
            array.SubImageArray    = subImages;
            array.DisposingLibrary = this;
        }