private Palette?InitializePalette(IReadableBitmapData source, IAsyncContext context) { using var alg = new TAlg(); alg.Initialize(quantizer.maxColors, source); int width = source.Width; IReadableBitmapDataRow row = source.FirstRow; context.Progress?.New(DrawingOperation.InitializingQuantizer, source.Height); do { if (context.IsCancellationRequested) { return(null); } // TODO: parallel if possible for (int x = 0; x < width; x++) { Color32 c = row[x]; // handling alpha including full transparency if (c.A != Byte.MaxValue) { c = c.A < quantizer.alphaThreshold ? default : c.BlendWithBackground(quantizer.backColor); } alg.AddColor(c); } context.Progress?.Increment(); } while (row.MoveNextRow()); Color32[]? palette = alg.GeneratePalette(context); return(context.IsCancellationRequested ? null : new Palette(palette !, quantizer.backColor, quantizer.alphaThreshold)); }
static void ClearIndexed(IAsyncContext context, IBitmapDataInternal bitmapData, int bpp, Color32 color, int width) { int index = bitmapData.Palette.GetNearestColorIndex(color); byte byteValue = bpp == 8 ? (byte)index : bpp == 4 ? (byte)((index << 4) | index) : index == 1 ? Byte.MaxValue : Byte.MinValue; int left = 0; if (bitmapData.RowSize > 0) { int factor = bpp == 8 ? 0 : bpp == 4 ? 1 : 3; // writing as longs if ((bitmapData.RowSize & 0b111) == 0) { int longWidth = bitmapData.RowSize >> 3; uint intValue = (uint)((byteValue << 24) | (byteValue << 16) | (byteValue << 8) | byteValue); ClearRaw(context, bitmapData, longWidth, ((ulong)intValue << 32) | intValue); left = (longWidth << 3) << factor; } // writing as integers else if ((bitmapData.RowSize & 0b11) == 0) { int intWidth = bitmapData.RowSize >> 2; ClearRaw(context, bitmapData, intWidth, (byteValue << 24) | (byteValue << 16) | (byteValue << 8) | byteValue); left = (intWidth << 2) << factor; }
protected override async Task ConsumeMessageReaderAsync(IAsyncContext asyncContext, RecordConfiguration configuration, DbDataReader sourceDataReader, CancellationToken cancellationToken) { IAsyncEnumerable <IAdoNetStreamingResult> results; IEnumerable <DbParameter> dbParameters; long recordCount = 0; if ((object)asyncContext == null) { throw new ArgumentNullException(nameof(asyncContext)); } if ((object)configuration == null) { throw new ArgumentNullException(nameof(configuration)); } if ((object)sourceDataReader == null) { throw new ArgumentNullException(nameof(sourceDataReader)); } this.AssertValidConfiguration(); AdoNetConnectorSpecificConfiguration fsConfig = this.Configuration.StageSpecificConfiguration; if ((object)fsConfig.ExecuteCommand == null) { throw new InvalidOperationException(string.Format("Configuration missing: '{0}'.", nameof(fsConfig.ExecuteCommand))); } if (SolderFascadeAccessor.DataTypeFascade.IsNullOrWhiteSpace(fsConfig.ExecuteCommand.CommandText)) { throw new InvalidOperationException(string.Format("Configuration missing: '{0}.{1}'.", nameof(fsConfig.ExecuteCommand), nameof(fsConfig.ExecuteCommand.CommandText))); } do { while (await sourceDataReader.ReadAsync(cancellationToken)) { dbParameters = fsConfig.ExecuteCommand.GetDbDataParameters(this.DestinationUnitOfWork); dbParameters = dbParameters.Select(p => { // prevent modified closure bug DbDataReader _sourceDataReader = sourceDataReader; // lazy load p.Value = _sourceDataReader[p.SourceColumn]; return(p); }); results = this.DestinationUnitOfWork.ExecuteResultsAsync(fsConfig.ExecuteCommand.CommandType ?? CommandType.Text, fsConfig.ExecuteCommand.CommandText, dbParameters, cancellationToken); await results.ForceAsyncEnumeration(cancellationToken); // force execution recordCount++; } }while (await sourceDataReader.NextResultAsync(cancellationToken)); //System.Console.WriteLine("DESTINATION (update): recordCount={0}", recordCount); }
public Color32[] GeneratePalette(IAsyncContext context) { // Original comment from Xiaolin Wu: // We now convert histogram into moments so that we can rapidly calculate // the sums of the above quantities over any desired box. HistogramToMoments(); List <Box> cubes = CreatePartitions(context); if (context.IsCancellationRequested) { return(null); } var result = new Color32[cubes.Count + (hasTransparency ? 1 : 0)]; for (int k = 0; k < cubes.Count; k++) { // The original algorithm here marks an array of tags but we don't need it because // we don't want to produce an array of quantized pixels but just the palette. long weight = cubes[k].Volume(ref wt); if (weight <= 0) { Debug.Assert(cubes.Count == 1 && hasTransparency, $"bogus box {k}"); continue; } result[k] = new Color32( (byte)(cubes[k].Volume(ref mr) / weight), (byte)(cubes[k].Volume(ref mg) / weight), (byte)(cubes[k].Volume(ref mb) / weight)); } return(result); }
protected override async Task ConsumeAsyncInternal(IAsyncContext asyncContext, RecordConfiguration configuration, IAsyncChannel asyncChannel, CancellationToken cancellationToken) { IAsyncEnumerable <IRecord> records; if ((object)asyncContext == null) { throw new ArgumentNullException(nameof(asyncContext)); } if ((object)configuration == null) { throw new ArgumentNullException(nameof(configuration)); } if ((object)asyncChannel == null) { throw new ArgumentNullException(nameof(asyncChannel)); } this.AssertValidConfiguration(); records = asyncChannel.Records; if ((object)records == null) { throw new SyncPremException(nameof(records)); } var payloads = asyncChannel.Records.Select(p => p.Payload); await this.TextualWriter.WriteRecordsAsync(payloads, cancellationToken); }
protected override Task <IAsyncChannel> ProduceAsyncInternal(IAsyncContext asyncContext, RecordConfiguration configuration, CancellationToken cancellationToken) { IAsyncChannel asyncChannel; ISchema schema; IAsyncEnumerable <IPayload> payloads; IAsyncEnumerable <IAsyncRecord> records; if ((object)asyncContext == null) { throw new ArgumentNullException(nameof(asyncContext)); } if ((object)configuration == null) { throw new ArgumentNullException(nameof(configuration)); } this.AssertValidConfiguration(); TTextualConnectorSpecificConfiguration fsConfig = this.Configuration.StageSpecificConfiguration; if (!asyncContext.LocalState.TryGetValue(this, out IDictionary <string, object> localState)) { localState = new Dictionary <string, object>(StringComparer.OrdinalIgnoreCase); asyncContext.LocalState.Add(this, localState); } schema = localState[Constants.ContextComponentScopedSchema] as ISchema; if ((object)schema == null) { throw new SyncPremException(nameof(schema)); } schema = localState[Constants.ContextComponentScopedSchema] as ISchema; if ((object)schema == null) { throw new SyncPremException(nameof(schema)); } payloads = this.TextualReader.ReadRecordsAsync(cancellationToken); if ((object)payloads == null) { throw new SyncPremException(nameof(payloads)); } records = payloads.Select(rec => new DefaultAsyncRecord(schema, rec, string.Empty, Partition.None, Offset.None)); if ((object)records == null) { throw new SyncPremException(nameof(records)); } asyncChannel = asyncContext.CreateChannelAsync(records); return(Task.FromResult(asyncChannel)); }
protected override async Task PreExecuteAsyncInternal(IAsyncContext asyncContext, RecordConfiguration configuration, CancellationToken cancellationToken) { ISchema schema; if ((object)asyncContext == null) { throw new ArgumentNullException(nameof(asyncContext)); } if ((object)configuration == null) { throw new ArgumentNullException(nameof(configuration)); } this.AssertValidConfiguration(); if (!asyncContext.LocalState.TryGetValue(this, out IDictionary <string, object> localState)) { localState = new Dictionary <string, object>(StringComparer.OrdinalIgnoreCase); asyncContext.LocalState.Add(this, localState); } schema = await GetRandomSchemaAsync(cancellationToken); if ((object)schema == null) { throw new SyncPremException(nameof(schema)); } localState.Add(Constants.ContextComponentScopedSchema, schema); }
protected override async Task ConsumeAsyncInternal(IAsyncContext asyncContext, RecordConfiguration configuration, IAsyncChannel asyncChannel, CancellationToken cancellationToken) { IAsyncEnumerable <IAsyncRecord> asyncRecords; if ((object)asyncContext == null) { throw new ArgumentNullException(nameof(asyncContext)); } if ((object)configuration == null) { throw new ArgumentNullException(nameof(configuration)); } if ((object)asyncChannel == null) { throw new ArgumentNullException(nameof(asyncChannel)); } this.AssertValidConfiguration(); asyncRecords = asyncChannel.Records; if ((object)asyncRecords == null) { throw new SyncPremException(nameof(asyncRecords)); } await asyncRecords.ForceAsyncEnumeration(cancellationToken); // force execution }
static void Clear16Bpp(IAsyncContext context, IBitmapDataInternal bitmapData, Color32 color, int width) { int longWidth = bitmapData.RowSize >> 3; // writing as longs if (longWidth > 0) { ushort shortValue = bitmapData.PixelFormat switch { PixelFormat.Format16bppArgb1555 => new Color16Argb1555(color).Value, PixelFormat.Format16bppRgb565 => new Color16Rgb565(color).Value, PixelFormat.Format16bppRgb555 => new Color16Rgb555(color).Value, _ => new Color16Gray(color).Value }; uint uintValue = (uint)((shortValue << 16) | shortValue); ClearRaw(context, bitmapData, longWidth, ((ulong)uintValue << 32) | uintValue); } // handling the rest (or even the whole content if RowSize is 0) int left = longWidth << 2; if (left < width && !context.IsCancellationRequested) { ClearDirectFallback(context, bitmapData, color, left); } }
static void Clear32Bpp(IAsyncContext context, IBitmapDataInternal bitmapData, Color32 color, int width) { int longWidth = bitmapData.RowSize >> 3; // writing as longs if (longWidth > 0) { Color32 rawColor = bitmapData.PixelFormat switch { PixelFormat.Format32bppPArgb => color.ToPremultiplied(), PixelFormat.Format32bppRgb => color.BlendWithBackground(bitmapData.BackColor), _ => color, }; uint argb = (uint)rawColor.ToArgb(); ClearRaw(context, bitmapData, longWidth, ((ulong)argb << 32) | argb); } // handling the rest (can be either the last column if width is odd, or even the whole content if RowSize is 0) int left = longWidth << 1; if (left < width && !context.IsCancellationRequested) { ClearDirectFallback(context, bitmapData, color, left); } }
private static void DoSaveWidePlatformDependent(IAsyncContext context, IBitmapDataInternal bitmapData, Rectangle rect, BinaryWriter writer) { PixelFormat pixelFormat = bitmapData.PixelFormat; int byteLength = pixelFormat.ToBitsPerPixel() >> 3; // using a temp 1x1 managed bitmap data for the conversion using IBitmapDataInternal tempData = CreateManagedBitmapData(new Size(1, 1), pixelFormat, bitmapData.BackColor, bitmapData.AlphaThreshold); IBitmapDataRowInternal tempRow = tempData.DoGetRow(0); IBitmapDataRowInternal row = bitmapData.DoGetRow(rect.Top); for (int y = 0; y < rect.Height; y++) { if (context.IsCancellationRequested) { return; } for (int x = rect.Left; x < rect.Right; x++) { tempRow.DoSetColor32(0, row.DoGetColor32(x)); for (int i = 0; i < byteLength; i++) { writer.Write(tempRow.DoReadRaw <byte>(i)); } } row.MoveNextRow(); context.Progress?.Increment(); } }
private static async Task <IAsyncChannel> NullProcessorAsyncMethod(IAsyncContext asyncContext, RecordConfiguration configuration, IAsyncChannel asyncChannel, AsyncProcessDelegate asyncNext, CancellationToken cancellationToken) { IAsyncChannel newAsyncChannel; if ((object)asyncContext == null) { throw new ArgumentNullException(nameof(asyncContext)); } if ((object)configuration == null) { throw new ArgumentNullException(nameof(configuration)); } if ((object)asyncChannel == null) { throw new ArgumentNullException(nameof(asyncChannel)); } await StdOut.WriteLineAsync(string.Format("{1} (before next) processor: '{0}'", nameof(NullProcessor), nameof(NullProcessorAsyncMethod))); if ((object)asyncNext != null) { newAsyncChannel = await asyncNext(asyncContext, configuration, asyncChannel, cancellationToken); } else { newAsyncChannel = asyncChannel; } await StdOut.WriteLineAsync(string.Format("{1} (after next) processor: '{0}'", nameof(NullProcessor), nameof(NullProcessorAsyncMethod))); return(newAsyncChannel); }
public static bool CheckIfExistsNullable(this IAsyncContext context, IEntity entity) { if (context == null) { return(entity.Exists); } return(context.CheckIfExists(entity)); }
public static bool CheckIfExistsNullable(this IAsyncContext context, IWorldObject worldObject) { if (context == null) { return(worldObject.Exists); } return(context.CheckIfExists(worldObject)); }
public static bool CheckIfExistsNullable(this IAsyncContext context, IBaseObject baseObject) { if (context == null) { return(baseObject.Exists); } return(context.CheckIfExists(baseObject)); }
internal CopySession(IAsyncContext context, IBitmapDataInternal sessionSource, IBitmapDataInternal sessionTarget, Rectangle actualSourceRectangle, Rectangle actualTargetRectangle) { this.context = context; Source = sessionSource; Target = sessionTarget; SourceRectangle = actualSourceRectangle; TargetRectangle = actualTargetRectangle; }
public void SetUp() { extender = new FetchCommitsExtender(); extender[RepositoryType.Git] = new Mock <IRepositoryService>() { DefaultValue = DefaultValue.Mock }.Object; context = new Mock <IAsyncContext>().Object; }
protected override async Task ConsumeMessageReaderAsync(IAsyncContext asyncContext, RecordConfiguration configuration, DbDataReader sourceDataReader, CancellationToken cancellationToken) { long recordCount = 0; //SqlRowsCopiedEventHandler callback; if ((object)asyncContext == null) { throw new ArgumentNullException(nameof(asyncContext)); } if ((object)configuration == null) { throw new ArgumentNullException(nameof(configuration)); } if ((object)sourceDataReader == null) { throw new ArgumentNullException(nameof(sourceDataReader)); } this.AssertValidConfiguration(); AdoNetConnectorSpecificConfiguration fsConfig = this.Configuration.StageSpecificConfiguration; if ((object)fsConfig.ExecuteCommand == null) { throw new InvalidOperationException(string.Format("Configuration missing: '{0}'.", nameof(fsConfig.ExecuteCommand))); } if (SolderFascadeAccessor.DataTypeFascade.IsNullOrWhiteSpace(fsConfig.ExecuteCommand.CommandText)) { throw new InvalidOperationException(string.Format("Configuration missing: '{0}.{1}'.", nameof(fsConfig.ExecuteCommand), nameof(fsConfig.ExecuteCommand.CommandText))); } using (SqlBulkCopy sqlBulkCopy = new SqlBulkCopy((SqlConnection)this.DestinationUnitOfWork.Connection, SqlBulkCopyOptions.Default, (SqlTransaction)this.DestinationUnitOfWork.Transaction)) { //callback = (sender, e) => Console.WriteLine(_rowsCopied = e.RowsCopied); foreach (FieldConfiguration columnConfiguration in configuration.ColumnConfigurations) { sqlBulkCopy.ColumnMappings.Add(columnConfiguration.FieldName, columnConfiguration.FieldName); } sqlBulkCopy.EnableStreaming = true; sqlBulkCopy.BatchSize = 2500; //sqlBulkCopy.NotifyAfter = 2500; //sqlBulkCopy.SqlRowsCopied += callback; sqlBulkCopy.DestinationTableName = fsConfig.ExecuteCommand.CommandText; await sqlBulkCopy.WriteToServerAsync(sourceDataReader, cancellationToken); //sqlBulkCopy.SqlRowsCopied -= callback; } }
public static bool TryToAsync(this IBlip thisValue, IAsyncContext asyncContext, out IBlip blip) { if (!asyncContext.CreateRef(thisValue, true)) { blip = default; return(false); } blip = new AsyncBlip(thisValue, asyncContext); return(true); }
public static bool TryToAsync(this IColShape thisValue, IAsyncContext asyncContext, out IColShape colShape) { if (!asyncContext.CreateRef(thisValue, true)) { colShape = default; return(false); } colShape = new AsyncColShape(thisValue, asyncContext); return(true); }
public static bool TryToAsync(this IVehicle thisValue, IAsyncContext asyncContext, out IVehicle vehicle) { if (!asyncContext.CreateRef(thisValue, true)) { vehicle = default; return(false); } vehicle = new AsyncVehicle(thisValue, asyncContext); return(true); }
public static bool TryToAsync(this IPlayer thisValue, IAsyncContext asyncContext, out IPlayer player) { if (!asyncContext.CreateRef(thisValue, true)) { player = default; return(false); } player = new AsyncPlayer(thisValue, asyncContext); return(true); }
public static void RunOnMainThreadBlockingNullable(this IAsyncContext context, Action action) { if (context == null) { using var semaphore = new SemaphoreSlim(0, 1); AltAsync.RunOnMainThreadBlocking(action, semaphore); return; } context.RunOnMainThreadBlocking(action); }
public static bool TryToAsync(this ICheckpoint thisValue, IAsyncContext asyncContext, out ICheckpoint checkpoint) { if (!asyncContext.CreateRef(thisValue, true)) { checkpoint = default; return(false); } checkpoint = new AsyncCheckpoint(thisValue, asyncContext); return(true); }
public static bool TryToAsync(this IVoiceChannel thisValue, IAsyncContext asyncContext, out IVoiceChannel voiceChannel) { if (!asyncContext.CreateRef(thisValue, true)) { voiceChannel = default; return(false); } voiceChannel = new AsyncVoiceChannel(thisValue, asyncContext); return(true); }
private void SetContext(IAsyncContext context = null) { if (context == null) { System.Windows.Application.Current.Dispatcher?.Invoke(() => this.Context = new AsyncContext()); // Context = new AsyncContext(); //on non dispatcher SynchronizationContext.Current is null } else { this.Context = context; } }
public ResultCode EnsureIdTokenCacheAsync(ServiceCtx context, out IAsyncContext asyncContext) { KEvent asyncEvent = new KEvent(context.Device.System.KernelContext); AsyncExecution asyncExecution = new AsyncExecution(asyncEvent); asyncExecution.Initialize(1000, EnsureIdTokenCacheAsyncImpl); asyncContext = new IAsyncContext(asyncExecution); // return ResultCode.NullObject if the IAsyncContext pointer is null. Doesn't occur in our case. return(ResultCode.Success); }
protected override async Task PreExecuteAsyncInternal(IAsyncContext asyncContext, RecordConfiguration configuration, CancellationToken cancellationToken) { if ((object)asyncContext == null) { throw new ArgumentNullException(nameof(asyncContext)); } if ((object)configuration == null) { throw new ArgumentNullException(nameof(configuration)); } await StdOut.WriteLineAsync(string.Format("{1} processor: '{0}'", nameof(NullProcessor), nameof(this.PreExecuteAsyncInternal))); }
internal bool SplitBuckets(IAsyncContext context, ref int index) { bool splitOccurred = false; // saving length because we add new buckets during the iteration int endIndex = buckets.Count; if (index >= endIndex) { index = 0; } while (index < endIndex) { ColorBucket currentBucket = buckets[index]; Debug.Assert(currentBucket.Count > 0, "Empty bucket"); Debug.Assert(!currentBucket.IsSingleColor); if (context.IsCancellationRequested) { return(false); } splitOccurred = true; // on equal distance splitting on the green range in the first place because of human perception if (currentBucket.RangeG >= currentBucket.RangeR && currentBucket.RangeG >= currentBucket.RangeB) { currentBucket.Split(ColorComponent.G, this, ref index, ref endIndex); } else if (currentBucket.RangeR >= currentBucket.RangeB) { currentBucket.Split(ColorComponent.R, this, ref index, ref endIndex); } else { currentBucket.Split(ColorComponent.B, this, ref index, ref endIndex); } context.Progress?.SetProgressValue(ColorsCount); // Stopping if we reached maxColors. Note that Split may increase ColorsCount. if (ColorsCount == maxColors) { return(false); } } return(splitOccurred); }
internal static void DoSaveBitmapData(IAsyncContext context, IBitmapDataInternal bitmapData, Rectangle rect, Stream stream) { PixelFormat pixelFormat = bitmapData.PixelFormat; Debug.Assert(pixelFormat == PixelFormat.Format32bppArgb || bitmapData.RowSize >= pixelFormat.GetByteWidth(rect.Right)); Debug.Assert(pixelFormat.IsAtByteBoundary(rect.Left)); context.Progress?.New(DrawingOperation.Saving, rect.Height + 1); var writer = new BinaryWriter(stream); writer.Write(magicNumber); writer.Write(rect.Width); writer.Write(rect.Height); writer.Write((int)pixelFormat); writer.Write(bitmapData.BackColor.ToArgb()); writer.Write(bitmapData.AlphaThreshold); Palette?palette = bitmapData.Palette; writer.Write(palette?.Count ?? 0); if (palette != null) { foreach (Color32 entry in palette.Entries) { writer.Write(entry.ToArgb()); } } context.Progress?.Increment(); if (context.IsCancellationRequested) { return; } try { if (pixelFormat.ToBitsPerPixel() > 32 && bitmapData is NativeBitmapDataBase && ColorExtensions.Max16BppValue != UInt16.MaxValue) { DoSaveWidePlatformDependent(context, bitmapData, rect, writer); return; } DoSaveRaw(context, bitmapData, rect, writer); } finally { stream.Flush(); } }