private Helix.TextureModel ReadTexture(IGeometryMaterial mat)
        {
            var sub = mat?.Submaterials.FirstOrDefault(s => s.Usage == MaterialUsage.Diffuse);

            if (string.IsNullOrEmpty(sub?.Bitmap?.Name))
            {
                return(null);
            }

            var key = mat.Flags.HasFlag(MaterialFlags.Transparent) ? -sub.Bitmap.Id : sub.Bitmap.Id;

            Helix.TextureModel tex;
            textureCache.TryGetValue(key, out tex);
            if (tex == null)
            {
                try
                {
                    var args   = new DdsOutputArgs(mat.Flags.HasFlag(MaterialFlags.Transparent) ? DecompressOptions.Default : DecompressOptions.Bgr24);
                    var stream = new System.IO.MemoryStream();
                    sub.Bitmap.ToDds(0).WriteToStream(stream, System.Drawing.Imaging.ImageFormat.Png, args);
                    tex = new Helix.TextureModel(stream);
                    if (!textureCache.TryAdd(key, tex))
                    {
                        stream.Dispose(); //another thread beat us to it
                    }
                }
                catch
                {
                    return(null);
                }
            }

            return(tex);
        }
示例#2
0
        public static void WriteToTarga(this DdsImage dds, string fileName, DdsOutputArgs args)
        {
            var source = dds.ToBitmapSource(args);
            var format = args.Options.HasFlag(DecompressOptions.Bgr24) ? Imaging.PixelFormat.Format24bppRgb : Imaging.PixelFormat.Format32bppArgb;

            WriteToTarga(source, fileName, format);
        }
        private bool SaveImage(IBitmap bitmap, string baseDir)
        {
            var extracted = 0;

            for (int i = 0; i < bitmap.SubmapCount; i++)
            {
                var fileName = MakePath(bitmap.Class, bitmap.Name, baseDir);
                var ext      = "." + Settings.BitmapFormat.ToString().ToLower();

                if (bitmap.SubmapCount > 1)
                {
                    fileName += $"[{i}]";
                }

                if (Settings.BitmapFormat == BitmapFormat.DDS)
                {
                    if (Settings.OverwriteExisting || !File.Exists(fileName + ext))
                    {
                        var rawDds = bitmap.ToDds(i);
                        rawDds.WriteToDxgi(fileName + ext);
                        extracted++;
                    }
                    continue;
                }

                var outputs = new List <Tuple <string, DecompressOptions> >();

                ImageFormat format;
                if (Settings.BitmapFormat == BitmapFormat.PNG)
                {
                    format = ImageFormat.Png;
                }
                else if (Settings.BitmapFormat == BitmapFormat.TIF)
                {
                    format = ImageFormat.Tiff;
                }
                else if (Settings.BitmapFormat == BitmapFormat.JPEG)
                {
                    format = ImageFormat.Jpeg;
                }
                else //if (Settings.BitmapFormat == BitmapFormat.TGA)
                {
                    format = null;
                }

                if (Settings.BitmapMode == BitmapMode.Default)
                {
                    outputs.Add(Tuple.Create(fileName + ext, DecompressOptions.Default));
                }
                else if (Settings.BitmapMode == BitmapMode.Bgr24)
                {
                    outputs.Add(Tuple.Create(fileName + ext, DecompressOptions.Bgr24));
                }
                else if (Settings.BitmapMode == BitmapMode.IsolateAlpha)
                {
                    outputs.AddRange(GetParamsIsolateAlpha(fileName, ext));
                }
                else if (Settings.BitmapMode == BitmapMode.IsolateAll)
                {
                    outputs.AddRange(GetParamsIsolateAll(fileName, ext));
                }
                else if (Settings.BitmapMode == BitmapMode.MixedIsolate)
                {
                    outputs.AddRange(GetParamsMixedIsolate(fileName, ext));
                }

                DdsImage dds = null;
                foreach (var param in outputs)
                {
                    if (!Settings.OverwriteExisting && File.Exists(param.Item1))
                    {
                        continue;
                    }

                    if (dds == null)
                    {
                        dds = bitmap.ToDds(i);
                    }

                    var args = new DdsOutputArgs(param.Item2, bitmap.CubeLayout);
                    if (format != null)
                    {
                        dds.WriteToDisk(param.Item1, format, args);
                    }
                    else //if (Settings.BitmapFormat == BitmapFormat.TGA)
                    {
                        dds.WriteToTarga(param.Item1, args);
                    }

                    extracted++;
                }
            }

            return(extracted > 0);
        }