Beispiel #1
0
        public virtual void ConvertToImage(out Image destImage, bool includeMipMaps)
#endif
        {
            var numMips  = includeMipMaps ? MipmapCount + 1 : 1;
            var dataSize = Image.CalculateSize(numMips, FaceCount, Width, Height, Depth, Format);

            var pixData = new byte[dataSize];
            // if there are multiple faces and mipmaps we must pack them into the data
            // faces, then mips
            var currentPixData = BufferBase.Wrap(pixData);

            for (var face = 0; face < FaceCount; ++face)
            {
                for (var mip = 0; mip < numMips; ++mip)
                {
                    var mipDataSize = PixelUtil.GetMemorySize(Width, Height, Depth, Format);

                    var pixBox = new PixelBox(Width, Height, Depth, Format, currentPixData);
                    GetBuffer(face, mip).BlitToMemory(pixBox);

                    currentPixData += mipDataSize;
                }
            }

            currentPixData.Dispose();

            // load, and tell Image to delete the memory when it's done.
            destImage = (new Image()).FromDynamicImage(pixData, Width, Height, Depth, Format, true, FaceCount, numMips - 1);
        }
Beispiel #2
0
        public static byte[] GetBytes <T>(T value)
        {
            int size;

            byte[]     buffer;
            BufferBase dst;

            if (!typeof(T).IsArray)
            {
                size   = Memory.SizeOf(typeof(T));
                buffer = new byte[size];
                using (dst = BufferBase.Wrap(buffer)) {
                    Marshal.StructureToPtr(value, dst.Pin(), true);
                    dst.UnPin();
                }
            }
            else
            {
                size = Memory.SizeOf(typeof(T).GetElementType()) *
                       (int)typeof(T).GetProperty("Length").GetValue(value, null);
                buffer = new byte[size];
                using (dst = BufferBase.Wrap(buffer)) {
                    using (var src = BufferBase.Wrap(value as Array, size))
                        Memory.Copy(src, dst, size);
                }
            }

            return(buffer);
        }
Beispiel #3
0
        public static void SetBytes <T>(byte[] buffer, out T[] dest)
        {
            var size = buffer.Length / Memory.SizeOf(typeof(T));

            dest = new T[size];
            using (var src = BufferBase.Wrap(buffer)) {
                using (var dst = BufferBase.Wrap(dest, buffer.Length))
                    Memory.Copy(src, dst, buffer.Length);
            }
        }
Beispiel #4
0
        public static T SetBytes <T>(byte[] buffer)
        {
            var size = Memory.SizeOf(typeof(T));
            T   retStruct;

            using (var src = BufferBase.Wrap(buffer)) {
                retStruct = src.Pin().PtrToStructure <T>();
                src.UnPin();
            }
            return(retStruct);
        }
Beispiel #5
0
        public virtual void LoadImages(Image[] images)
        {
            if (images.Length < 1)
            {
                throw new AxiomException("Cannot load empty vector of images");
            }

            // Set desired texture size and properties from images[0]
            this.srcWidth  = this.width = images[0].Width;
            this.srcHeight = this.height = images[0].Height;
            this.srcDepth  = this.depth = images[0].Depth;

            // Get source image format and adjust if required
            this.srcFormat = images[0].Format;
            if (this.treatLuminanceAsAlpha && this.srcFormat == PixelFormat.L8)
            {
                this.srcFormat = PixelFormat.A8;
            }

            if (this.desiredFormat != PixelFormat.Unknown)
            {
                // If have desired format, use it
                this.format = this.desiredFormat;
            }
            else
            {
                // Get the format according with desired bit depth
                this.format = PixelUtil.GetFormatForBitDepths(this.srcFormat, this.desiredIntegerBitDepth,
                                                              this.desiredFloatBitDepth);
            }

            // The custom mipmaps in the image have priority over everything
            var imageMips = images[0].NumMipMaps;

            if (imageMips > 0)
            {
                this.mipmapCount = this.requestedMipmapCount = imageMips;
                // Disable flag for auto mip generation
                this.usage &= ~TextureUsage.AutoMipMap;
            }

            // Create the texture
            CreateInternalResources();

            // Check if we're loading one image with multiple faces
            // or a vector of images representing the faces
            int  faces;
            bool multiImage;             // Load from multiple images?

            if (images.Length > 1)
            {
                faces      = images.Length;
                multiImage = true;
            }
            else
            {
                faces      = images[0].NumFaces;
                multiImage = false;
            }

            // Check wether number of faces in images exceeds number of faces
            // in this texture. If so, clamp it.
            if (faces > FaceCount)
            {
                faces = FaceCount;
            }

            // Say what we're doing
            if (TextureManager.Instance.Verbose)
            {
                var msg = new StringBuilder();
                msg.AppendFormat("Texture: {0}: Loading {1} faces( {2}, {3}x{4}x{5} ) with", _name, faces,
                                 PixelUtil.GetFormatName(images[0].Format), images[0].Width, images[0].Height,
                                 images[0].Depth);
                if (!(this.mipmapsHardwareGenerated && this.mipmapCount == 0))
                {
                    msg.AppendFormat(" {0}", this.mipmapCount);
                }

                if ((this.usage & TextureUsage.AutoMipMap) == TextureUsage.AutoMipMap)
                {
                    msg.AppendFormat("{0} generated mipmaps", this.mipmapsHardwareGenerated ? " hardware" : string.Empty);
                }
                else
                {
                    msg.Append(" custom mipmaps");
                }

                msg.AppendFormat(" from {0}.\n\t", multiImage ? "multiple Images" : "an Image");

                // Print data about first destination surface
                var buf = GetBuffer(0, 0);
                msg.AppendFormat(" Internal format is {0} , {1}x{2}x{3}.", PixelUtil.GetFormatName(buf.Format), buf.Width,
                                 buf.Height, buf.Depth);

                LogManager.Instance.Write(msg.ToString());
            }

            // Main loading loop
            // imageMips == 0 if the image has no custom mipmaps, otherwise contains the number of custom mips
            for (var mip = 0; mip <= imageMips; ++mip)
            {
                for (var i = 0; i < faces; ++i)
                {
                    PixelBox src;
                    if (multiImage)
                    {
                        // Load from multiple images
                        src = images[i].GetPixelBox(0, mip);
                    }
                    else
                    {
                        // Load from faces of images[0]
                        src = images[0].GetPixelBox(i, mip);
                    }

                    // Sets to treated format in case is difference
                    src.Format = this.srcFormat;

                    if (this.gamma != 1.0f)
                    {
                        // Apply gamma correction
                        // Do not overwrite original image but do gamma correction in temporary buffer
                        var bufSize = PixelUtil.GetMemorySize(src.Width, src.Height, src.Depth, src.Format);
                        var buff    = new byte[bufSize];
                        var buffer  = BufferBase.Wrap(buff);

                        var corrected = new PixelBox(src.Width, src.Height, src.Depth, src.Format, buffer);
                        PixelConverter.BulkPixelConversion(src, corrected);

                        Image.ApplyGamma(corrected.Data, this.gamma, corrected.ConsecutiveSize, PixelUtil.GetNumElemBits(src.Format));

                        // Destination: entire texture. BlitFromMemory does
                        // the scaling to a power of two for us when needed
                        GetBuffer(i, mip).BlitFromMemory(corrected);
                    }
                    else
                    {
                        // Destination: entire texture. BlitFromMemory does
                        // the scaling to a power of two for us when needed
                        GetBuffer(i, mip).BlitFromMemory(src);
                    }
                }
            }

            // Update size (the final size, not including temp space)
            Size = FaceCount * PixelUtil.GetMemorySize(this.width, this.height, this.depth, this.format);
        }