示例#1
0
        public IList <IKanvasImage> Load(Stream input)
        {
            using var br = new BinaryReaderX(input);

            // Read chunks
            var chunks = ReadSections(br);

            // Flatten chunks
            var infChunk     = chunks.FirstOrDefault(x => x.sectionMagic == "TXIF");
            var dataChunks   = chunks.Where(x => x.sectionMagic == "TXIM").ToArray();
            var tx4iChunk    = chunks.FirstOrDefault(x => x.sectionMagic == "TX4I");
            var paletteChunk = chunks.FirstOrDefault(x => x.sectionMagic == "TXPL");

            // Read information chunk
            using var infBr = new BinaryReaderX(new MemoryStream(infChunk.data));
            var texInfo = infBr.ReadType <ChnkInfo>();

            // Detect index depth by data length and palette size
            var paddedWidth = ChnkSupport.ToPowerOfTwo(texInfo.width);
            var bitDepth    = texInfo.dataSize * 8 / paddedWidth / texInfo.height;

            // Detect image format
            var imageFormat = -1;

            if (tx4iChunk == null)
            {
                switch (bitDepth)
                {
                case 2:
                    imageFormat = 2;
                    break;

                case 4:
                    imageFormat = 3;
                    break;

                case 8:
                    switch (paletteChunk.data.Length / 2)
                    {
                    case 8: imageFormat = 6; break;

                    case 32: imageFormat = 1; break;

                    case 256: imageFormat = 4; break;
                    }
                    break;

                case 16:
                    imageFormat = 7;
                    break;
                }
            }

            // Create image info
            var result = new List <IKanvasImage>();

            if (imageFormat != -1)
            {
                var definition = ChnkSupport.GetEncodingDefinition();
                foreach (var dataChunk in dataChunks)
                {
                    var imageInfo = new ImageInfo(dataChunk.data, imageFormat, new Size(texInfo.width, texInfo.height));

                    if (imageFormat < 7)
                    {
                        imageInfo.PaletteData   = paletteChunk.data;
                        imageInfo.PaletteFormat = 0;
                    }

                    imageInfo.PadSize.Width.ToPowerOfTwo();

                    result.Add(new KanvasImage(definition, imageInfo));
                }
            }
            else
            {
                // Expand TX4I data to RGBA8888
                foreach (var dataChunk in dataChunks)
                {
                    result.Add(new BitmapKanvasImage(ExpandTX4I(dataChunk.data, tx4iChunk.data, paletteChunk.data)
                                                     .ToBitmap(new Size(texInfo.width, texInfo.height), new Size(paddedWidth, texInfo.height),
                                                               new BcSwizzle(new SwizzlePreparationContext(new Rgba(8, 8, 8, 8), new Size(paddedWidth, texInfo.height))), ImageAnchor.TopLeft)));
                }
            }

            return(result);
        }
示例#2
0
        public ChnkState()
        {
            _img = new Chnk();

            EncodingDefinition = ChnkSupport.GetEncodingDefinition();
        }