public PaintShopSource(PaintShopSource red, PaintShopSource green, PaintShopSource blue, PaintShopSource alpha)
 {
     RedChannelSource   = red;
     GreenChannelSource = green;
     BlueChannelSource  = blue;
     AlphaChannelSource = alpha;
 }
        public Task SaveTexturePatternAsync(string filename, PaintShopSource ao, PaintShopSource pattern, PaintShopSource overlay,
                                            Color[] colors)
        {
            if (ao.UseInput)
            {
                ao = new PaintShopSource(Path.GetFileName(filename) ?? "").SetFrom(ao);
            }

            var aoView = GetOriginal(ref _aoBase, ao, int.MaxValue);

            if (aoView == null)
            {
                return(Task.Delay(0));
            }

            var patternView = GetOriginal(ref _patternBase, pattern, int.MaxValue);

            if (patternView == null)
            {
                return(Task.Delay(0));
            }

            var overlayView = overlay == null ? null : GetOriginal(ref _overlayBase, overlay, OptionMaxPatternSize);

            return(SaveTextureAsync(filename,
                                    PatternAction(patternView, aoView, overlayView, colors),
                                    GetSize(pattern) ?? (overlay == null ? null : GetSize(overlay)) ??
                                    GetSize(ao) ?? new Size(OptionMaxPatternSize, OptionMaxPatternSize)));
        }
        private byte[] GetBytes([CanBeNull] PaintShopSource source)
        {
            if (source == null)
            {
                return(null);
            }

            if (!source.UseInput)
            {
                if (source.Data != null)
                {
                    return(source.Data);
                }

                if (source.Name != null && MainSlot.Kn5?.TexturesData.ContainsKey(source.Name) == true)
                {
                    return(MainSlot.Kn5.TexturesData[source.Name]);
                }

                if (source.Color != null)
                {
                    using (var texture = DeviceContextHolder.CreateTexture(1, 1, (x, y) => source.Color.Value))
                        using (var stream = new MemoryStream()) {
                            Texture2D.ToStream(DeviceContext, texture, ImageFileFormat.Dds, stream);
                            return(stream.ToArray());
                        }
                }
            }

            AcToolsLogging.Write("Can’t get bytes: " + source);
            return(null);
        }
        public bool OverrideTexturePattern(string textureName, PaintShopSource ao, PaintShopSource pattern, PaintShopSource overlay,
                                           Color[] colors)
        {
            if (ao.UseInput)
            {
                ao = new PaintShopSource(textureName).SetFrom(ao);
            }

            var aoView = GetOriginal(ref _aoBase, ao, OptionMaxPatternSize);

            if (aoView == null)
            {
                return(false);
            }

            var patternView = GetOriginal(ref _patternBase, pattern, OptionMaxPatternSize);

            if (patternView == null)
            {
                return(false);
            }

            var overlayView = overlay == null ? null : GetOriginal(ref _overlayBase, overlay, OptionMaxPatternSize);

            return(OverrideTexture(textureName,
                                   PatternAction(patternView, aoView, overlayView, colors),
                                   OptionMaxPatternSize));
        }
Beispiel #5
0
 public PaintShopDestination([NotNull] string textureName, PreferredDdsFormat preferredFormat,
                             [CanBeNull] PaintShopSource outputMask = null, Size?forceSize = null)
 {
     TextureName     = textureName;
     PreferredFormat = preferredFormat;
     OutputMask      = outputMask;
     ForceSize       = forceSize;
 }
        public Task SaveTextureMapsAsync(string filename, double reflection, double gloss, double specular, bool fixGloss,
                                         PaintShopSource source, PaintShopSource maskSource)
        {
            if (source.UseInput)
            {
                source = new PaintShopSource(Path.GetFileName(filename) ?? "").SetFrom(source);
            }
            var original = GetOriginal(ref _mapsBase, source, int.MaxValue);
            var mask     = maskSource == null ? null : GetOriginal(ref _mapsMasks, maskSource, int.MaxValue);

            return(original == null?Task.Delay(0) : SaveTextureAsync(filename,
                                                                     MapsAction(original, mask, reflection, gloss, specular, fixGloss),
                                                                     GetSize(source) ?? new Size(OptionMaxMapSize, OptionMaxMapSize)));
        }
        public bool OverrideTextureMaps(string textureName, double reflection, double gloss, double specular, bool fixGloss,
                                        PaintShopSource source, PaintShopSource maskSource)
        {
            if (source.UseInput)
            {
                source = new PaintShopSource(textureName).SetFrom(source);
            }
            var original = GetOriginal(ref _mapsBase, source, OptionMaxMapSize);
            var mask     = maskSource == null ? null : GetOriginal(ref _mapsMasks, maskSource, int.MaxValue);

            return(original != null && OverrideTexture(textureName,
                                                       MapsAction(original, mask, reflection, gloss, specular, fixGloss),
                                                       OptionMaxMapSize));
        }
        public Task SaveTextureTintAsync(string filename, Color[] colors, double alphaAdd, PaintShopSource source, PaintShopSource maskSource,
                                         PaintShopSource overlaySource)
        {
            if (source.UseInput)
            {
                source = new PaintShopSource(Path.GetFileName(filename) ?? "").SetFrom(source);
            }
            var original = GetOriginal(ref _tintBase, source, int.MaxValue);
            var mask     = maskSource == null ? null : GetOriginal(ref _tintMask, maskSource, int.MaxValue);
            var overlay  = overlaySource == null ? null : GetOriginal(ref _tintOverlay, overlaySource, int.MaxValue);

            return(original == null?Task.Delay(0) : SaveTextureAsync(filename,
                                                                     TintAction(original, mask, overlay, colors, alphaAdd),
                                                                     GetSize(source) ?? new Size(OptionMaxTintSize, OptionMaxTintSize)));
        }
        public bool OverrideTextureTint(string textureName, Color[] colors, double alphaAdd, PaintShopSource source, PaintShopSource maskSource,
                                        PaintShopSource overlaySource)
        {
            if (source.UseInput)
            {
                source = new PaintShopSource(textureName).SetFrom(source);
            }
            var original = GetOriginal(ref _tintBase, source, OptionMaxTintSize);
            var mask     = maskSource == null ? null : GetOriginal(ref _tintMask, maskSource, OptionMaxTintSize);
            var overlay  = overlaySource == null ? null : GetOriginal(ref _tintOverlay, overlaySource, OptionMaxTintSize);

            return(original != null && OverrideTexture(textureName,
                                                       TintAction(original, mask, overlay, colors, alphaAdd),
                                                       OptionMaxTintSize));
        }
        public bool OverrideTexture(string textureName, PaintShopSource source)
        {
            if (source?.Custom != true)
            {
                return(CarNode == null || CarNode.OverrideTexture(DeviceContextHolder, textureName, GetBytes(source)));
            }

            if (source.UseInput)
            {
                source = new PaintShopSource(textureName).SetFrom(source);
            }
            var original = GetOriginal(ref _override, source, OptionMaxMapSize);

            return(original != null && OverrideTexture(textureName, OverrideAction(original), OptionMaxMapSize));
        }
        public Task SaveTextureAsync(string filename, PaintShopSource source)
        {
            if (source.Custom != true)
            {
                if (source.UseInput)
                {
                    // we don’t have to save anything here — why waste space and override texture by itself?
                    // source = new PaintShopSource(Path.GetFileName(filename) ?? "").SetFrom(source);
                    return(Task.Delay(0));
                }

                var bytes = GetBytes(source);
                return(bytes != null?FileUtils.WriteAllBytesAsync(filename, bytes) : Task.Delay(0));
            }

            if (source.UseInput)
            {
                source = new PaintShopSource(Path.GetFileName(filename) ?? "").SetFrom(source);
            }
            var original = GetOriginal(ref _override, source, int.MaxValue);

            return(original == null?Task.Delay(0) : SaveTextureAsync(filename, OverrideAction(original),
                                                                     GetSize(source) ?? new Size(OptionMaxMapSize, OptionMaxMapSize)));
        }
        private SourceReady GetOriginal(ref Dictionary <int, ShaderResourceView> storage, [NotNull] PaintShopSource source, int maxSize,
                                        Func <ShaderResourceView, ShaderResourceView> preparation = null)
        {
            if (MainSlot.Kn5 == null)
            {
                return(null);
            }

            try {
                if (storage == null)
                {
                    storage = new Dictionary <int, ShaderResourceView>(2);
                    if (_sizes == null)
                    {
                        _sizes = new Dictionary <int, Size>();
                    }
                }

                ShaderResourceView original;
                var sourceHashCode = source.GetHashCode();
                var hashCode       = (sourceHashCode * 397) ^ maxSize.GetHashCode();
                if (!storage.TryGetValue(hashCode, out original))
                {
                    Size size;

                    if (source.ByChannels)
                    {
                        var red   = source.RedChannelSource == null ? null : GetOriginal(ref storage, source.RedChannelSource, maxSize);
                        var green = source.GreenChannelSource == null ? null : GetOriginal(ref storage, source.GreenChannelSource, maxSize);
                        var blue  = source.BlueChannelSource == null ? null : GetOriginal(ref storage, source.BlueChannelSource, maxSize);
                        var alpha = source.AlphaChannelSource == null ? null : GetOriginal(ref storage, source.AlphaChannelSource, maxSize);

                        var redSize   = source.RedChannelSource == null ? null : GetSize(source.RedChannelSource);
                        var greenSize = source.GreenChannelSource == null ? null : GetSize(source.GreenChannelSource);
                        var blueSize  = source.BlueChannelSource == null ? null : GetSize(source.BlueChannelSource);
                        var alphaSize = source.AlphaChannelSource == null ? null : GetSize(source.AlphaChannelSource);

                        size = Max(redSize, Max(greenSize, Max(blueSize, alphaSize))) ?? new Size(16, 16);
                        _sizes[sourceHashCode] = size;

                        if (size.Width > maxSize || size.Height > maxSize)
                        {
                            size = new Size(maxSize, maxSize);
                        }

                        using (var combined = TargetResourceTexture.Create(Format.R8G8B8A8_UNorm)) {
                            combined.Resize(DeviceContextHolder, size.Width, size.Height, null);
                            UseEffect(e => {
                                red.Set(e.FxAoMap, e.FxAoMapChannels);
                                green.Set(e.FxInputMap, e.FxInputMapChannels);
                                blue.Set(e.FxMaskMap, e.FxMaskMapChannels);
                                alpha.Set(e.FxOverlayMap, e.FxOverlayMapChannels);
                                e.TechCombineChannels.DrawAllPasses(DeviceContext, 6);
                            }, combined);

                            combined.KeepView = true;
                            original          = combined.View;
                        }
                    }
                    else
                    {
                        var decoded = GetBytes(source);
                        if (decoded == null)
                        {
                            return(null);
                        }

                        using (var texture = Texture2D.FromMemory(Device, decoded)) {
                            original = new ShaderResourceView(Device, texture);

                            size = new Size(texture.Description.Width, texture.Description.Height);
                            _sizes[sourceHashCode] = size;

                            if (size.Width > maxSize || size.Height > maxSize)
                            {
                                size = new Size(maxSize, maxSize);

                                using (var resized = TargetResourceTexture.Create(Format.R8G8B8A8_UNorm)) {
                                    resized.Resize(DeviceContextHolder, maxSize, maxSize, null);
                                    DeviceContextHolder.GetHelper <DownsampleHelper>().Draw(DeviceContextHolder,
                                                                                            original, new Vector2(texture.Description.Width, texture.Description.Height),
                                                                                            resized.TargetView, new Vector2(maxSize, maxSize));
                                    original.Dispose();

                                    resized.KeepView = true;
                                    original         = resized.View;
                                }
                            }
                        }
                    }

                    if (source.Desaturate)
                    {
                        original = Prepare(original, view => Desaturate(view, size));
                    }

                    if (source.NormalizeMax)
                    {
                        original = Prepare(original, view => NormalizeMax(view, size));
                    }

                    storage[hashCode] = Prepare(original, preparation);
                }

                return(new SourceReady {
                    View = original,
                    ChannelsAssignments = GetChannelAssignments(source)
                });
            } catch (Exception e) {
                AcToolsLogging.NonFatalErrorNotify("Can’t load texture", null, e);
                return(null);
            }
        }
 private static Vector4 GetChannelAssignments(PaintShopSource source)
 {
     return(new Vector4(GetIndex(source.RedFrom), GetIndex(source.GreenFrom), GetIndex(source.BlueFrom), GetIndex(source.AlphaFrom)));
 }
        private Size?GetSize(PaintShopSource source)
        {
            Size result;

            return(_sizes.TryGetValue(source.GetHashCode(), out result) ? (Size?)result : null);
        }