예제 #1
0
        public override void Export(Stream writeStream)
        {
            CheckStreamAndTexture(writeStream);

            int width   = (int)Texture.Width;
            int height  = (int)Texture.Height;
            var builder = PngBuilder.Create(width, height, Texture.Format.HasAlpha());
            var writer  = new PngWriter(builder, width);

            byte[] data = Texture.TexturePixels.Data.PrimaryRawImage;
            if (SpecifyArray >= 0 && SpecifyArray < Texture.ArrayCount)
            {
                data = Texture.TexturePixels.Data.RawImage[SpecifyArray][0];
            }
            TextureUtil.DecodeTextureDataToWriter(data, width, height, Texture.Format, writer);
            builder.Save(writeStream);
        }
예제 #2
0
        public static BitmapSource GetImage(Texture texture, int clampSize, int mode)
        {
            BitmapSource target       = null;
            int          width        = (int)texture.Width;
            int          height       = (int)texture.Height;
            int          targetMipmap = 0;
            int          maxMipmap    = texture.MipmapCount - 1;

            while (clampSize != 0 && width > clampSize && height > clampSize && targetMipmap < maxMipmap)
            {
                width  >>= 1;
                height >>= 1;
                targetMipmap++;
            }
            var key = Tuple.Create(texture, targetMipmap, mode);

            lock (imageCache)
            {
                if (!imageCache.TryGetValue(key, out var weakRef) || !weakRef.TryGetTarget(out target))
                {
                    if (++imageCacheCleanCounter > 100)
                    {
                        imageCacheCleanCounter = 0;
                        var removed = imageCache.Where(pair => !pair.Value.TryGetTarget(out var unused)).ToArray();
                        foreach (var item in removed)
                        {
                            imageCache.Remove(item.Key);
                        }
                    }

                    PixelFormat pf      = PixelFormats.Default;
                    byte[]      rawData = texture.TexturePixels.Data.RawImage[0][targetMipmap];
                    int         stride  = 0;
                    switch (mode)
                    {
                    case CHANNEL_MODE_RGBA:
                        pf     = PixelFormats.Bgra32;
                        stride = width * 4;
                        break;

                    case CHANNEL_MODE_RGB:
                        pf     = PixelFormats.Bgr24;
                        stride = width * 3;
                        break;

                    case CHANNEL_MODE_RG:
                        pf     = PixelFormats.Bgr24;
                        stride = width * 3;
                        break;

                    case CHANNEL_MODE_R:
                        pf     = PixelFormats.Gray8;
                        stride = width * 1;
                        break;

                    case CHANNEL_MODE_ALPHA:
                        pf     = PixelFormats.Gray8;
                        stride = width * 1;
                        break;

                    default:
                        throw new ArgumentOutOfRangeException("mode");
                    }
                    byte[] rawImage = new byte[stride * height];
                    var    writer   = new ImageWriter(rawImage, width, mode);
                    TextureUtil.DecodeTextureDataToWriter(rawData, width, height, texture.Format, writer);

                    /*int length = width * height;
                     * for (int i = 0; i < length; i++)
                     * {
                     *      rawImage[i * 4] = 255; // b
                     *      rawImage[i * 4 + 1] = 127; // g
                     *      rawImage[i * 4 + 2] = 63; // r
                     *      rawImage[i * 4 + 3] = 255; // a
                     * }*/
                    target = BitmapSource.Create(width, height,
                                                 96, 96, pf, null,
                                                 rawImage, stride);
                    imageCache[key] = new WeakReference <BitmapSource>(target);
                }
            }
            return(target);
        }