Exemplo n.º 1
0
        public override void ReadData(BinaryReader stream, IDictionary <object, object> userdata, int totalSize)
        {
            int array = 1, mipmap = 1;

            if (userdata.TryGetValue(KEY_ARRAY, out var arrayObj))
            {
                array = (int)arrayObj;
            }
            if (userdata.TryGetValue(KEY_MIPMAP, out var mipmapObj))
            {
                mipmap = (int)mipmapObj;
            }
            if (!userdata.TryGetValue(KEY_WIDTH, out var widthObj) ||
                !userdata.TryGetValue(KEY_HEIGHT, out var heightObj) ||
                !userdata.TryGetValue(KEY_FORMAT, out var formatObj))
            {
                throw new ArgumentException("No enough user data for width, height and format");
            }

            int           width     = (int)widthObj;
            int           height    = (int)heightObj;
            TextureFormat format    = (TextureFormat)formatObj;
            int           aligh     = format.GetAlignSize();
            int           pixelSize = format.GetBitsPerPixel();

            //var raw = stream.ReadBytes(totalSize);

            var raw = new byte[array][][];

            for (int a = 0; a < array; a++)
            {
                raw[a] = new byte[mipmap][];
                int imageWidth  = width;
                int imageHeight = height;
                for (int m = 0; m < mipmap; m++)
                {
                    int alignedWidth  = imageWidth;
                    int alignedHeight = imageHeight;
                    if (alignedWidth % aligh != 0)
                    {
                        alignedWidth += aligh - (alignedWidth % aligh);
                    }
                    if (alignedHeight % aligh != 0)
                    {
                        alignedHeight += aligh - (alignedHeight % aligh);
                    }
                    raw[a][m]   = stream.ReadBytes((alignedWidth * alignedHeight * pixelSize) / 8);
                    imageWidth  = Math.Max(imageWidth >> 1, 1);
                    imageHeight = Math.Max(imageHeight >> 1, 1);
                }
            }

            RawImage        = raw;
            PrimaryRawImage = raw[0][0];
        }
Exemplo n.º 2
0
        public override void ReadData(BinaryReader stream, IDictionary <object, object> userdata, int totalSize)
        {
            int array = 1, mipmap = 1;

            if (userdata.TryGetValue(KEY_ARRAY, out var arrayObj))
            {
                array = (int)arrayObj;
            }
            if (userdata.TryGetValue(KEY_MIPMAP, out var mipmapObj))
            {
                mipmap = (int)mipmapObj;
            }
            if (!userdata.TryGetValue(KEY_WIDTH, out var widthObj) ||
                !userdata.TryGetValue(KEY_HEIGHT, out var heightObj) ||
                !userdata.TryGetValue(KEY_FORMAT, out var formatObj))
            {
                throw new ArgumentException("No enough user data for width, height and format");
            }

            int           width     = (int)widthObj;
            int           height    = (int)heightObj;
            TextureFormat format    = (TextureFormat)formatObj;
            int           aligh     = format.GetAlignSize();
            int           pixelSize = format.GetBitsPerPixel();

            //var raw = stream.ReadBytes(totalSize);

            var raw = new byte[array][][];

            for (int a = 0; a < array; a++)
            {
                raw[a] = new byte[mipmap][];
                int imageWidth  = width;
                int imageHeight = height;
                for (int m = 0; m < mipmap; m++)
                {
                    int alignedWidth  = imageWidth;
                    int alignedHeight = imageHeight;
                    if (alignedWidth % aligh != 0)
                    {
                        alignedWidth += aligh - (alignedWidth % aligh);
                    }
                    if (alignedHeight % aligh != 0)
                    {
                        alignedHeight += aligh - (alignedHeight % aligh);
                    }
                    // prevent from overflow for mega textures or underflow for very small mipmap
                    // should be alignedWidth * alignedHeight * pixelSize / 8
                    int readSize = alignedWidth * alignedHeight;
                    // if readSize & 7 != 0, then it cannot be divided exactly
                    if (readSize >= 8 && (readSize & 7) == 0)
                    {
                        readSize = readSize / 8 * pixelSize;
                    }
                    else
                    {
                        readSize = readSize * pixelSize / 8;
                    }
                    raw[a][m]   = stream.ReadBytes(readSize);
                    imageWidth  = Math.Max(imageWidth >> 1, 1);
                    imageHeight = Math.Max(imageHeight >> 1, 1);
                }
            }

            RawImage        = raw;
            PrimaryRawImage = raw[0][0];
        }