예제 #1
0
        protected override void OnSaveT(Document input, Stream output, PropertyBasedSaveConfigToken token, Surface scratchSurface, ProgressEventHandler progressCallback)
        {
            int        quality = token.GetProperty <Int32Property>(PropertyNames.Quality).Value;
            WebPPreset preset  = (WebPPreset)token.GetProperty(PropertyNames.Preset).Value;

            WebPFile.Save(input, output, quality, preset, scratchSurface, progressCallback);
        }
예제 #2
0
        public static unsafe void Save(Document input, Stream output, PropertyBasedSaveConfigToken token, ProgressEventHandler progressCallback)
        {
            bool           rle         = token.GetProperty <PaintDotNet.PropertySystem.BooleanProperty>(PropertyNames.RLE).Value;
            AbrFileVersion fileVersion = (AbrFileVersion)token.GetProperty(PropertyNames.FileVersion).Value;

            double progressPercentage = 0.0;
            double progressDelta      = (1.0 / input.Layers.Count) * 100.0;

            using (BinaryReverseWriter writer = new BinaryReverseWriter(output, true))
            {
                writer.Write((short)fileVersion);
                writer.Write((short)input.Layers.Count);

                foreach (Layer item in input.Layers)
                {
                    BitmapLayer layer = (BitmapLayer)item;

                    SaveLayer(writer, layer, fileVersion, rle);

                    progressPercentage += progressDelta;

                    progressCallback(null, new ProgressEventArgs(progressPercentage));
                }
            }
        }
예제 #3
0
        protected override void OnSaveT(Document input, Stream output, PropertyBasedSaveConfigToken token, Surface scratchSurface, ProgressEventHandler progressCallback)
        {
            PSPFile file = new PSPFile();

            CompressionFormats format  = (CompressionFormats)token.GetProperty(PropertyNames.CompressionType).Value;
            FileVersion        version = (FileVersion)token.GetProperty(PropertyNames.FileVersion).Value;

            file.Save(input, output, format, scratchSurface, (ushort)version, progressCallback);
        }
예제 #4
0
        protected override void OnSaveT(Document input, Stream output, PropertyBasedSaveConfigToken token, Surface scratchSurface, ProgressEventHandler progressCallback)
        {
            DdsFileFormat      fileFormat      = (DdsFileFormat)token.GetProperty(PropertyNames.FileFormat).Value;
            BC7CompressionMode compressionMode = (BC7CompressionMode)token.GetProperty(PropertyNames.BC7CompressionMode).Value;
            DdsErrorMetric     errorMetric     = (DdsErrorMetric)token.GetProperty(PropertyNames.ErrorMetric).Value;
            bool           generateMipmaps     = token.GetProperty <BooleanProperty>(PropertyNames.GenerateMipMaps).Value;
            MipMapSampling mipSampling         = (MipMapSampling)token.GetProperty(PropertyNames.MipMapResamplingAlgorithm).Value;

            DdsFile.Save(input, output, fileFormat, errorMetric, compressionMode, generateMipmaps, mipSampling, scratchSurface, progressCallback);
        }
        protected override bool IsReflexive(PropertyBasedSaveConfigToken token)
        {
            if (token.GetProperty <BooleanProperty>(PropertyNames.CubeMap).Value)
            {
                return(false);
            }

            DdsFileFormat format = (DdsFileFormat)token.GetProperty(PropertyNames.FileFormat).Value;

            return(format == DdsFileFormat.B8G8R8A8 || format == DdsFileFormat.R8G8B8A8);
        }
예제 #6
0
        protected override bool IsReflexive(PropertyBasedSaveConfigToken token)
        {
            PngBitDepthUIChoices bitDepth = (PngBitDepthUIChoices)token.GetProperty<StaticListChoiceProperty>(PropertyNames.BitDepth).Value;

            // Only 32-bit is reflexive
            return (bitDepth == PngBitDepthUIChoices.Bpp32);
        }
예제 #7
0
        internal override HashSet <InternalFileType.SavableBitDepths> CreateAllowedBitDepthListFromToken(PropertyBasedSaveConfigToken token)
        {
            PngBitDepthUIChoices choices = (PngBitDepthUIChoices)token.GetProperty <StaticListChoiceProperty>(PropertyNames.BitDepth).Value;
            HashSet <InternalFileType.SavableBitDepths> collection = new HashSet <InternalFileType.SavableBitDepths>();

            switch (choices)
            {
            case PngBitDepthUIChoices.AutoDetect:
                collection.AddRange <InternalFileType.SavableBitDepths>(new InternalFileType.SavableBitDepths[] { InternalFileType.SavableBitDepths.Rgb24 });
                return(collection);

            case PngBitDepthUIChoices.Bpp32:
                collection.AddRange <InternalFileType.SavableBitDepths>(new InternalFileType.SavableBitDepths[1]);
                return(collection);

            case PngBitDepthUIChoices.Bpp24:
            {
                InternalFileType.SavableBitDepths[] items = new InternalFileType.SavableBitDepths[] { InternalFileType.SavableBitDepths.Rgb24 };
                collection.AddRange <InternalFileType.SavableBitDepths>(items);
                return(collection);
            }

            case PngBitDepthUIChoices.Bpp8:
            {
                InternalFileType.SavableBitDepths[] depthsArray2 = new InternalFileType.SavableBitDepths[] { InternalFileType.SavableBitDepths.Rgb8, InternalFileType.SavableBitDepths.Rgba8 };
                collection.AddRange <InternalFileType.SavableBitDepths>(depthsArray2);
                return(collection);
            }
            }
            throw ExceptionUtil.InvalidEnumArgumentException <PngBitDepthUIChoices>(choices, "bitDepthFromToken");
        }
예제 #8
0
        internal override Set <SavableBitDepths> CreateAllowedBitDepthListFromToken(PropertyBasedSaveConfigToken token)
        {
            TgaBitDepthUIChoices bitDepth = (TgaBitDepthUIChoices)token.GetProperty <StaticListChoiceProperty>(PropertyNames.BitDepth).Value;

            Set <SavableBitDepths> bitDepths = new Set <SavableBitDepths>();

            switch (bitDepth)
            {
            case TgaBitDepthUIChoices.AutoDetect:
                bitDepths.Add(SavableBitDepths.Rgb24);
                bitDepths.Add(SavableBitDepths.Rgba32);
                break;

            case TgaBitDepthUIChoices.Bpp24:
                bitDepths.Add(SavableBitDepths.Rgb24);
                break;

            case TgaBitDepthUIChoices.Bpp32:
                bitDepths.Add(SavableBitDepths.Rgba32);
                break;

            default:
                throw new InvalidEnumArgumentException("bitDepth", (int)bitDepth, typeof(TgaBitDepthUIChoices));
            }

            return(bitDepths);
        }
예제 #9
0
        /// <summary>
        /// Saves a document to a stream respecting the properties
        /// </summary>
        protected override void OnSaveT(Document input, Stream output, PropertyBasedSaveConfigToken token, Surface scratchSurface, ProgressEventHandler progressCallback)
        {
            int quality = token.GetProperty <Int32Property>(PropertyNames.Quality).Value;
            CompressionSpeed     compressionSpeed  = (CompressionSpeed)token.GetProperty(PropertyNames.CompressionSpeed).Value;
            YUVChromaSubsampling chromaSubsampling = (YUVChromaSubsampling)token.GetProperty(PropertyNames.YUVChromaSubsampling).Value;
            bool preserveExistingTileSize          = token.GetProperty <BooleanProperty>(PropertyNames.PreserveExistingTileSize).Value;

            AvifFile.Save(input,
                          output,
                          quality,
                          compressionSpeed,
                          chromaSubsampling,
                          preserveExistingTileSize,
                          this.maxEncoderThreadsOverride,
                          scratchSurface,
                          progressCallback,
                          this.byteArrayPool.Value);
        }
예제 #10
0
 protected override bool IsReflexive(PropertyBasedSaveConfigToken token)
 {
     if ((TgaBitDepthUIChoices)token.GetProperty<StaticListChoiceProperty>(PropertyNames.BitDepth).Value == TgaBitDepthUIChoices.Bpp32)
     {
         return true;
     }
     else
     {
         return base.IsReflexive(token);
     }
 }
예제 #11
0
 protected override bool IsReflexive(PropertyBasedSaveConfigToken token)
 {
     if ((TgaBitDepthUIChoices)token.GetProperty <StaticListChoiceProperty>(PropertyNames.BitDepth).Value == TgaBitDepthUIChoices.Bpp32)
     {
         return(true);
     }
     else
     {
         return(base.IsReflexive(token));
     }
 }
예제 #12
0
        internal override void FinalSave(
            Document input,
            Stream output,
            Surface scratchSurface,
            int ditherLevel,
            SavableBitDepths bitDepth,
            PropertyBasedSaveConfigToken token,
            ProgressEventHandler progressCallback)
        {
            bool rleCompress = token.GetProperty <BooleanProperty>(PropertyNames.RleCompress).Value;

            SaveTga(scratchSurface, output, bitDepth, rleCompress, progressCallback);
        }
예제 #13
0
        protected override void OnSaveT(Document input, Stream output, PropertyBasedSaveConfigToken token, Surface scratchSurface, ProgressEventHandler progressCallback)
        {
            var quality = (int)token.GetProperty(PropertyNames.Quality).Value;
            var comment = (string)token.GetProperty(PropertyNames.Comment).Value;

            using (var args = new RenderArgs(scratchSurface))
                input.Render(args, false);

            var enc = new WsqEncoder();

            byte[] bytes = null;
            using (var bitmap = scratchSurface.CreateAliasedBitmap())
            {
                SetResolution(bitmap, input);
                enc.Comment = comment;
                bytes       = enc.EncodeQualityGdi(bitmap, quality);
            }

            if (bytes != null)
            {
                output.Write(bytes, 0, (int)bytes.Length);
            }
        }
예제 #14
0
        protected override void OnSaveT(Document input, Stream output, PropertyBasedSaveConfigToken token, Surface scratchSurface, ProgressEventHandler progressCallback)
        {
            int               num            = token.GetProperty <Int32Property>(PropertyNames.Quality).Value;
            ImageCodecInfo    imageCodecInfo = GdiPlusFileType.GetImageCodecInfo(ImageFormat.Jpeg);
            EncoderParameters encoderParams  = new EncoderParameters(1);
            EncoderParameter  parameter      = new EncoderParameter(Encoder.Quality, (long)num);

            encoderParams.Param[0] = parameter;
            scratchSurface.Clear(ColorBgra.White);
            using (RenderArgs args = new RenderArgs(scratchSurface))
            {
                input.Render(args, false);
            }
            using (Bitmap bitmap = scratchSurface.CreateAliasedBitmap())
            {
                GdiPlusFileType.LoadProperties(bitmap, input);
                bitmap.Save(output, imageCodecInfo, encoderParams);
            }
        }
예제 #15
0
        protected override void OnSaveT(Document input, Stream output, PropertyBasedSaveConfigToken token, Surface scratchSurface, ProgressEventHandler callback)
        {
            scratchSurface.Clear(ColorBgra.Zero);
            using (RenderArgs args = new RenderArgs(scratchSurface))
            {
                input.Render(args, true);
            }
            DdsFileFormat     fileFormat     = (DdsFileFormat)token.GetProperty <StaticListChoiceProperty>(PropertyNames.FileFormat).Value;
            DdsCompressorType compressorType = (DdsCompressorType)token.GetProperty <StaticListChoiceProperty>(PropertyNames.CompressorType).Value;
            DdsErrorMetric    errorMetric    = (DdsErrorMetric)token.GetProperty <StaticListChoiceProperty>(PropertyNames.ErrorMetric).Value;
            bool weightColorByAlpha          = token.GetProperty <BooleanProperty>(PropertyNames.WeightColorByAlpha).Value;
            bool generateMipMaps             = token.GetProperty <BooleanProperty>(PropertyNames.GenerateMipMaps).Value;
            ResamplingAlgorithm mipMapResamplingAlgorithm = (ResamplingAlgorithm)token.GetProperty <StaticListChoiceProperty>(PropertyNames.MipMapResamplingAlgorithm).Value;

            new DdsFile().Save(output, scratchSurface, fileFormat, compressorType, errorMetric, generateMipMaps, mipMapResamplingAlgorithm, weightColorByAlpha, callback);
        }
예제 #16
0
        internal override HashSet <InternalFileType.SavableBitDepths> CreateAllowedBitDepthListFromToken(PropertyBasedSaveConfigToken token)
        {
            TgaBitDepthUIChoices choices = (TgaBitDepthUIChoices)token.GetProperty <StaticListChoiceProperty>(PropertyNames.BitDepth).Value;
            HashSet <InternalFileType.SavableBitDepths> set = new HashSet <InternalFileType.SavableBitDepths>();

            switch (choices)
            {
            case TgaBitDepthUIChoices.AutoDetect:
                set.Add(InternalFileType.SavableBitDepths.Rgb24);
                set.Add(InternalFileType.SavableBitDepths.Rgba32);
                return(set);

            case TgaBitDepthUIChoices.Bpp32:
                set.Add(InternalFileType.SavableBitDepths.Rgba32);
                return(set);

            case TgaBitDepthUIChoices.Bpp24:
                set.Add(InternalFileType.SavableBitDepths.Rgb24);
                return(set);
            }
            throw ExceptionUtil.InvalidEnumArgumentException <TgaBitDepthUIChoices>(choices, "bitDepth");
        }
예제 #17
0
        protected override void OnSaveT(Document input, Stream output, PropertyBasedSaveConfigToken token, Surface scratchSurface, ProgressEventHandler progressCallback)
        {
            int quality = (int)token.GetProperty(PropertyNames.Quality).Value;

            FileIO.EncodeParams parameters = new FileIO.EncodeParams();
            parameters.quality = quality;

            switch (input.DpuUnit)
            {
            case MeasurementUnit.Centimeter:
                parameters.dpcmX = input.DpuX;
                parameters.dpcmY = input.DpuY;
                break;

            case MeasurementUnit.Inch:
                parameters.dpcmX = Document.DotsPerInchToDotsPerCm(input.DpuX);
                parameters.dpcmY = Document.DotsPerInchToDotsPerCm(input.DpuY);
                break;

            case MeasurementUnit.Pixel:
                parameters.dpcmX = Document.DefaultDpcm;
                parameters.dpcmY = Document.DefaultDpcm;
                break;
            }

            using (RenderArgs ra = new RenderArgs(scratchSurface))
            {
                input.Render(ra, true);
            }

            FileIO.EncodeFile(
                scratchSurface.Scan0.Pointer,
                scratchSurface.Width,
                scratchSurface.Height,
                scratchSurface.Stride,
                CountChannels(scratchSurface),
                parameters,
                output);
        }
예제 #18
0
        protected override void OnSaveT(Document input, Stream output, PropertyBasedSaveConfigToken token, Surface scratchSurface, ProgressEventHandler progressCallback)
        {
            int quality = token.GetProperty<Int32Property>(PropertyNames.Quality).Value;

            ImageCodecInfo icf = GdiPlusFileType.GetImageCodecInfo(ImageFormat.Jpeg);
            EncoderParameters parms = new EncoderParameters(1);
            EncoderParameter parm = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, quality);
            parms.Param[0] = parm;

            scratchSurface.Clear(ColorBgra.White);

            using (RenderArgs ra = new RenderArgs(scratchSurface))
            {
                input.Render(ra, false);
            }

            using (Bitmap bitmap = scratchSurface.CreateAliasedBitmap())
            {
                GdiPlusFileType.LoadProperties(bitmap, input);
                bitmap.Save(output, icf, parms);
            }
        }
예제 #19
0
        protected override bool IsReflexive(PropertyBasedSaveConfigToken token)
        {
            PngBitDepthUIChoices choices = (PngBitDepthUIChoices)token.GetProperty <StaticListChoiceProperty>(PropertyNames.BitDepth).Value;

            return(choices == PngBitDepthUIChoices.Bpp32);
        }
예제 #20
0
 internal override void FinalSave(
     Document input, 
     Stream output, 
     Surface scratchSurface, 
     int ditherLevel, 
     SavableBitDepths bitDepth, 
     PropertyBasedSaveConfigToken token, 
     ProgressEventHandler progressCallback)
 {
     bool rleCompress = token.GetProperty<BooleanProperty>(PropertyNames.RleCompress).Value;
     SaveTga(scratchSurface, output, bitDepth, rleCompress, progressCallback);
 }
예제 #21
0
 internal override int GetDitherLevelFromToken(PropertyBasedSaveConfigToken token) =>
 token.GetProperty <Int32Property>(PropertyNames.DitherLevel).Value;
예제 #22
0
 internal override int GetThresholdFromToken(PropertyBasedSaveConfigToken token) =>
 token.GetProperty <Int32Property>(PropertyNames.Threshold).Value;
예제 #23
0
        protected override void OnSaveT(Document input, Stream output, PropertyBasedSaveConfigToken token, Surface scratchSurface, ProgressEventHandler callback)
        {
            int width  = input.Width;
            int height = input.Height;

            using (RenderArgs args = new RenderArgs(scratchSurface))
            {
                input.Render(args, true);
            }

            int tileHeight = token.GetProperty <Int32Property>(PropertyNames.TileHeight).Value;
            int tileWidth  = token.GetProperty <Int32Property>(PropertyNames.TileWidth).Value;

            List <Rectangle> rects = new List <Rectangle>();

            for (int y = 0; y < height; y += tileHeight)
            {
                for (int x = 0; x < width; x += tileWidth)
                {
                    Rectangle bounds = new Rectangle(x, y, Math.Min(tileWidth, width - x), Math.Min(tileHeight, height - y));
                    rects.Add(bounds);
                }
            }

            double progressPercentage = 0.0;
            double progressDelta      = (1.0 / rects.Count) * 100.0;

            using (ZipOutputStream zipStream = new ZipOutputStream(output, true))
            {
                int count = rects.Count;

                ImageCodecInfo    codec         = GetImageCodecInfo(ImageFormat.Png);
                EncoderParameters encoderParams = new EncoderParameters(1);

                for (int i = 0; i < count; i++)
                {
                    Rectangle bounds = rects[i];

                    SavableBitDepths bitDepth = GetBitDepth(scratchSurface, bounds);

                    zipStream.PutNextEntry(string.Format("Image{0}.png", (i + 1)));
                    if (bitDepth == SavableBitDepths.Rgba32)
                    {
                        encoderParams.Param[0] = new EncoderParameter(Encoder.ColorDepth, 32);

                        using (Bitmap temp = scratchSurface.CreateAliasedBitmap(bounds, true))
                        {
                            temp.Save(zipStream, codec, encoderParams);
                        }
                    }
                    else if (bitDepth == SavableBitDepths.Rgb24)
                    {
                        encoderParams.Param[0] = new EncoderParameter(Encoder.ColorDepth, 24);

                        using (Surface surface = new Surface(bounds.Width, bounds.Height))
                        {
                            surface.CopySurface(scratchSurface, bounds); // Make a new surface so we do not clobber the stride of the scratchSurface.
                            SquishSurfaceTo24Bpp(surface);

                            using (Bitmap temp = CreateAliased24BppBitmap(surface))
                            {
                                temp.Save(zipStream, codec, encoderParams);
                            }
                        }
                    }
                    else if (bitDepth == SavableBitDepths.Rgb8)
                    {
                        encoderParams.Param[0] = new EncoderParameter(Encoder.ColorDepth, 8);

                        using (Surface surface = scratchSurface.CreateWindow(bounds))
                        {
                            using (Bitmap temp = Quantize(surface, 7, 256, false, null))
                            {
                                temp.Save(zipStream, codec, encoderParams);
                            }
                        }
                    }
                    else if (bitDepth == SavableBitDepths.Rgba8)
                    {
                        encoderParams.Param[0] = new EncoderParameter(Encoder.ColorDepth, 8);

                        using (Surface surface = scratchSurface.CreateWindow(bounds))
                        {
                            using (Bitmap temp = Quantize(surface, 7, 256, true, null))
                            {
                                temp.Save(zipStream, codec, encoderParams);
                            }
                        }
                    }

                    progressPercentage += progressDelta;

                    callback(this, new ProgressEventArgs(progressPercentage));
                }
            }
        }
예제 #24
0
 protected override bool IsReflexive(PropertyBasedSaveConfigToken token) =>
 ((((TgaBitDepthUIChoices)token.GetProperty <StaticListChoiceProperty>(PropertyNames.BitDepth).Value) == TgaBitDepthUIChoices.Bpp32) || base.IsReflexive(token));
예제 #25
0
        internal override Set<SavableBitDepths> CreateAllowedBitDepthListFromToken(PropertyBasedSaveConfigToken token)
        {
            BmpBitDepthUIChoices bitDepth = (BmpBitDepthUIChoices)token.GetProperty<StaticListChoiceProperty>(PropertyNames.BitDepth).Value;

            Set<SavableBitDepths> bitDepths = new Set<SavableBitDepths>();

            switch (bitDepth)
            {
                case BmpBitDepthUIChoices.AutoDetect:
                    bitDepths.Add(SavableBitDepths.Rgb24);
                    bitDepths.Add(SavableBitDepths.Rgb8);
                    break;

                case BmpBitDepthUIChoices.Bpp24:
                    bitDepths.Add(SavableBitDepths.Rgb24);
                    break;

                case BmpBitDepthUIChoices.Bpp8:
                    bitDepths.Add(SavableBitDepths.Rgb8);
                    break;

                default:
                    throw new InvalidEnumArgumentException("bitDepth", (int)bitDepth, typeof(BmpBitDepthUIChoices));
            }

            return bitDepths;
        }
예제 #26
0
        protected override bool IsReflexive(PropertyBasedSaveConfigToken token)
        {
            DdsFileFormat format = (DdsFileFormat)token.GetProperty(PropertyNames.FileFormat).Value;

            return(format == DdsFileFormat.B8G8R8A8 || format == DdsFileFormat.R8G8B8A8);
        }
예제 #27
0
 internal override int GetDitherLevelFromToken(PropertyBasedSaveConfigToken token)
 {
     int ditherLevel = token.GetProperty<Int32Property>(PropertyNames.DitherLevel).Value;
     return ditherLevel;
 }
예제 #28
0
        protected override bool IsReflexive(PropertyBasedSaveConfigToken token)
        {
            int quality = token.GetProperty <Int32Property>(PropertyNames.Quality).Value;

            return(quality == 100);
        }
예제 #29
0
        protected override unsafe void OnSaveT(Document input, Stream output, PropertyBasedSaveConfigToken token, Surface scratchSurface, ProgressEventHandler progressCallback)
        {
            using (RenderArgs ra = new RenderArgs(scratchSurface)) {
                input.Render(ra, true);
            }

            int w = token.GetProperty <Int32Property>(PropertyNames.HorizCellSize).Value;
            int h = token.GetProperty <Int32Property>(PropertyNames.VerticalCellSize).Value;
            int W = scratchSurface.Width, H = scratchSurface.Height;

            string           chars;
            PresetCharacters preset = (PresetCharacters)token.GetProperty <StaticListChoiceProperty>(PropertyNames.PresetCharacters).Value;

            switch (preset)
            {
            case PresetCharacters.Custom:
                chars = token.GetProperty <StringProperty>(PropertyNames.Characters).Value;
                break;

            case PresetCharacters.AsciiChars:
                chars = asciiChars;
                break;

            case PresetCharacters.Blocks:
                chars = blocks;
                break;

            default:
                chars = "";
                break;
            }
            if (chars.Length == 0)
            {
                chars = asciiChars;
            }

            // Special case where 1 cell corresponds directly to 1 pixel
            // Written separately for performance
            if (h == 1 && w == 1)
            {
                StreamWriter writer = new StreamWriter(output);
                for (int y = 0; y < H; y++)
                {
                    ColorBgra *current = scratchSurface.GetRowAddressUnchecked(y);
                    for (int x = 0; x < W; x++)
                    {
                        int  pos = (int)((1 - current->GetIntensity()) * chars.Length);
                        char c   = chars[pos == chars.Length ? pos - 1 : pos];
                        writer.Write(c);
                        current++;
                    }
                    if (y != H - 1)
                    {
                        writer.WriteLine();
                    }
                }
                writer.Flush();
            }
            else
            {
                double[,] totals = new double[W / w, H / h];
                for (int y = 0; y < H / h * h; y++)
                {
                    ColorBgra *current = scratchSurface.GetRowAddressUnchecked(y);
                    for (int x = 0; x < W / w * w; x++)
                    {
                        totals[x / w, y / h] += 1 - current->GetIntensity();
                        current++;
                    }
                }

                int          ppc    = w * h;
                StreamWriter writer = new StreamWriter(output);
                for (int y = 0; y < H / h; y++)
                {
                    for (int x = 0; x < W / w; x++)
                    {
                        int  pos = (int)(totals[x, y] / ppc * chars.Length);
                        char c   = chars[pos == chars.Length ? pos - 1 : pos];
                        writer.Write(c);
                    }
                    if (y != H / h - 1)
                    {
                        writer.WriteLine();
                    }
                }
                writer.Flush();
            }
        }
예제 #30
0
        protected unsafe override void OnSaveT(Document input, Stream output, PropertyBasedSaveConfigToken token, Surface scratchSurface, ProgressEventHandler progressCallback)
        {
            using (RenderArgs ra = new RenderArgs(scratchSurface)) {
                input.Render(ra, true);
            }

            int w = token.GetProperty<Int32Property>(PropertyNames.HorizCellSize).Value;
            int h = token.GetProperty<Int32Property>(PropertyNames.VerticalCellSize).Value;
            int W = scratchSurface.Width, H = scratchSurface.Height;

            string chars;
            PresetCharacters preset = (PresetCharacters)token.GetProperty<StaticListChoiceProperty>(PropertyNames.PresetCharacters).Value;
            switch (preset) {
                case PresetCharacters.Custom:
                    chars = token.GetProperty<StringProperty>(PropertyNames.Characters).Value;
                    break;
                case PresetCharacters.AsciiChars:
                    chars = asciiChars;
                    break;
                case PresetCharacters.Blocks:
                    chars = blocks;
                    break;
                default:
                    chars = "";
                    break;
            }
            if (chars.Length == 0) {
                chars = asciiChars;
            }

            // Special case where 1 cell corresponds directly to 1 pixel
            // Written separately for performance
            if (h == 1 && w == 1) {
                StreamWriter writer = new StreamWriter(output);
                for (int y = 0; y < H; y++) {
                    ColorBgra* current = scratchSurface.GetRowAddressUnchecked(y);
                    for (int x = 0; x < W; x++) {
                        int pos = (int)((1 - current->GetIntensity()) * chars.Length);
                        char c = chars[pos == chars.Length ? pos - 1 : pos];
                        writer.Write(c);
                        current++;
                    }
                    if (y != H - 1) {
                        writer.WriteLine();
                    }
                }
                writer.Flush();
            } else {
                double[,] totals = new double[W / w, H / h];
                for (int y = 0; y < H / h * h; y++) {
                    ColorBgra* current = scratchSurface.GetRowAddressUnchecked(y);
                    for (int x = 0; x < W / w * w; x++) {
                        totals[x / w, y / h] += 1 - current->GetIntensity();
                        current++;
                    }
                }

                int ppc = w * h;
                StreamWriter writer = new StreamWriter(output);
                for (int y = 0; y < H / h; y++) {
                    for (int x = 0; x < W / w; x++) {
                        int pos = (int)(totals[x, y] / ppc * chars.Length);
                        char c = chars[pos == chars.Length ? pos - 1 : pos];
                        writer.Write(c);
                    }
                    if (y != H / h - 1) {
                        writer.WriteLine();
                    }
                }
                writer.Flush();
            }
        }
예제 #31
0
 internal override int GetThresholdFromToken(PropertyBasedSaveConfigToken token)
 {
     int threshold = token.GetProperty<Int32Property>(PropertyNames.Threshold).Value;
     return threshold;
 }