/// <summary> /// See <see cref="IColorQuantizer.Prepare"/> for more details. /// </summary> public void Prepare(ImageBuffer image) { OnPrepare(image); }
/// <summary> /// See <see cref="BaseColorQuantizer.OnPrepare"/> for more details. /// </summary> protected override void OnPrepare(ImageBuffer image) { // creates all the cubes cubes = new WuColorCube[MaxColor]; // initializes all the cubes for (Int32 cubeIndex = 0; cubeIndex < MaxColor; cubeIndex++) { cubes[cubeIndex] = new WuColorCube(); } // resets the reference minimums cubes[0].RedMinimum = 0; cubes[0].GreenMinimum = 0; cubes[0].BlueMinimum = 0; // resets the reference maximums cubes[0].RedMaximum = MaxSideIndex; cubes[0].GreenMaximum = MaxSideIndex; cubes[0].BlueMaximum = MaxSideIndex; weights = new Int64[SideSize, SideSize, SideSize]; momentsRed = new Int64[SideSize, SideSize, SideSize]; momentsGreen = new Int64[SideSize, SideSize, SideSize]; momentsBlue = new Int64[SideSize, SideSize, SideSize]; moments = new Single[SideSize, SideSize, SideSize]; table = new Int32[256]; for (Int32 tableIndex = 0; tableIndex < 256; ++tableIndex) { table[tableIndex] = tableIndex * tableIndex; } pixelIndex = 0; imageWidth = image.Width; imageSize = image.Width * image.Height; quantizedPixels = new Int32[imageSize]; pixels = new Int32[imageSize]; }
/// <summary> /// See <see cref="BaseColorQuantizer.OnPrepare"/> for more details. /// </summary> protected override void OnPrepare(ImageBuffer image) { base.OnPrepare(image); GetColorCache().Prepare(); }
public void Quantize(ImageBuffer target, IColorQuantizer quantizer, Int32 colorCount, Int32 parallelTaskCount = 4) { // performs the pure quantization wihout dithering Quantize(target, quantizer, null, colorCount, parallelTaskCount); }
public void TransformPerPixelAdvanced(ImageBuffer target, IList<Point> path, TransformPixelAdvancedFunction transformPixelAdvancedFunction, Int32 parallelTaskCount = 4) { TransformPerPixelBase(target, path, transformPixelAdvancedFunction, parallelTaskCount); }
public Double CalculateMeanError(ImageBuffer target, Int32 parallelTaskCount = 4) { // checks parameters Guard.CheckNull(target, "target"); // initializes the error Int64 totalError = 0; // prepares the function TransformPixelFunction calculateMeanError = (sourcePixel, targetPixel) => { Color sourceColor = GetColorFromPixel(sourcePixel); Color targetColor = GetColorFromPixel(targetPixel); totalError += ColorModelHelper.GetColorEuclideanDistance(ColorModel.RedGreenBlue, sourceColor, targetColor); return false; }; // performs the image scan, using a chosen method IList<Point> standardPath = new StandardPathProvider().GetPointPath(Width, Height); TransformPerPixel(target, standardPath, calculateMeanError, parallelTaskCount); // returns the calculates RMSD return Math.Sqrt(totalError/(3.0*Width*Height)); }
public void ChangeFormat(ImageBuffer target, IColorQuantizer quantizer, Int32 parallelTaskCount = 4) { // checks parameters Guard.CheckNull(target, "target"); Guard.CheckNull(quantizer, "quantizer"); // gathers some information about the target format Boolean hasSourceAlpha = PixelFormat.HasAlpha(); Boolean hasTargetAlpha = target.PixelFormat.HasAlpha(); Boolean isTargetIndexed = target.PixelFormat.IsIndexed(); Boolean isSourceDeepColor = PixelFormat.IsDeepColor(); Boolean isTargetDeepColor = target.PixelFormat.IsDeepColor(); // step 1 to 3 - prepares the palettes if (isTargetIndexed) SynthetizePalette(quantizer, target.PixelFormat.GetColorCount(), parallelTaskCount); // prepares the quantization function TransformPixelFunction changeFormat = (sourcePixel, targetPixel) => { // if both source and target formats are deep color formats, copies a value directly if (isSourceDeepColor && isTargetDeepColor) { //UInt64 value = sourcePixel.Value; //targetPixel.SetValue(value); } else { // retrieves a source image color Color color = GetColorFromPixel(sourcePixel); // if alpha is not present in the source image, but is present in the target, make one up if (!hasSourceAlpha && hasTargetAlpha) { Int32 argb = 255 << 24 | color.R << 16 | color.G << 8 | color.B; color = Color.FromArgb(argb); } // sets the color to a target pixel SetColorToPixel(targetPixel, color, quantizer); } // allows to write (obviously) the transformed pixel return true; }; // step 5 - generates the target image IList<Point> standardPath = new StandardPathProvider().GetPointPath(Width, Height); TransformPerPixel(target, standardPath, changeFormat, parallelTaskCount); }
public static Double CalculateImageNormalizedMeanError(Image sourceImage, Image targetImage, Int32 parallelTaskCount = 4) { // checks parameters Guard.CheckNull(sourceImage, "sourceImage"); Guard.CheckNull(targetImage, "targetImage"); // wraps source image to a buffer using (ImageBuffer source = new ImageBuffer(sourceImage, ImageLockMode.ReadOnly)) using (ImageBuffer target = new ImageBuffer(targetImage, ImageLockMode.ReadOnly)) { // use other override to calculate error return source.CalculateNormalizedMeanError(target, parallelTaskCount); } }
public static void ChangeFormat(ImageBuffer source, PixelFormat targetFormat, IColorQuantizer quantizer, out Image targetImage, Int32 parallelTaskCount = 4) { // checks parameters Guard.CheckNull(source, "source"); // creates a target bitmap in an appropriate format targetImage = new Bitmap(source.Width, source.Height, targetFormat); // wraps target image to a buffer using (ImageBuffer target = new ImageBuffer(targetImage, ImageLockMode.WriteOnly)) { source.ChangeFormat(target, quantizer, parallelTaskCount); } }
/// <summary> /// See <see cref="IColorDitherer.Prepare"/> for more details. /// </summary> public void Prepare(IColorQuantizer quantizer, Int32 colorCount, ImageBuffer sourceBuffer, ImageBuffer targetBuffer) { SourceBuffer = sourceBuffer; TargetBuffer = targetBuffer; ColorCount = colorCount; Quantizer = quantizer; OnPrepare(); }
public static Double CalculateImageNormalizedMeanError(ImageBuffer source, ImageBuffer target, Int32 parallelTaskCount = 4) { // checks parameters Guard.CheckNull(source, "source"); // use other override to calculate error return source.CalculateNormalizedMeanError(target, parallelTaskCount); }
/// <summary> /// See <see cref="BaseColorQuantizer.OnPrepare"/> for more details. /// </summary> protected override void OnPrepare(ImageBuffer image) { base.OnPrepare(image); OnFinish(); }
/// <summary> /// Initializes a new instance of the <see cref="Pixel"/> struct. /// </summary> public Pixel(ImageBuffer parent) { Parent = parent; Initialize(); }
/// <summary> /// Called when quantizer is about to be prepared for next round. /// </summary> protected virtual void OnPrepare(ImageBuffer image) { uniqueColorIndex = -1; paletteFound = false; UniqueColors.Clear(); }
public static void ScanImageColors(Image sourceImage, IColorQuantizer quantizer, Int32 parallelTaskCount = 4) { // checks parameters Guard.CheckNull(sourceImage, "sourceImage"); // wraps source image to a buffer using (ImageBuffer source = new ImageBuffer(sourceImage, ImageLockMode.ReadOnly)) { source.ScanColors(quantizer, parallelTaskCount); } }
public static void ChangeFormat(Image sourceImage, PixelFormat targetFormat, IColorQuantizer quantizer, out Image targetImage, Int32 parallelTaskCount = 4) { // checks parameters Guard.CheckNull(sourceImage, "sourceImage"); // wraps source image to a buffer using (ImageBuffer source = new ImageBuffer(sourceImage, ImageLockMode.ReadOnly)) { ChangeFormat(source, targetFormat, quantizer, out targetImage, parallelTaskCount); } }
public static List<Color> SynthetizeImagePalette(Image sourceImage, IColorQuantizer quantizer, Int32 colorCount, Int32 parallelTaskCount = 4) { // checks parameters Guard.CheckNull(sourceImage, "sourceImage"); // wraps source image to a buffer using (ImageBuffer source = new ImageBuffer(sourceImage, ImageLockMode.ReadOnly)) { return source.SynthetizePalette(quantizer, colorCount, parallelTaskCount); } }
public static void CorrectImageGamma(Image sourceImage, Single gamma, IColorQuantizer quantizer, Int32 parallelTaskCount = 4) { // checks parameters Guard.CheckNull(sourceImage, "sourceImage"); // wraps source image to a buffer using (ImageBuffer source = new ImageBuffer(sourceImage, ImageLockMode.ReadOnly)) { source.CorrectGamma(gamma, quantizer, parallelTaskCount); } }
public Double CalculateNormalizedMeanError(ImageBuffer target, Int32 parallelTaskCount = 4) { return CalculateMeanError(target, parallelTaskCount) / 255.0; }
public static void DitherImage(ImageBuffer source, ImageBuffer target, IColorDitherer ditherer, IColorQuantizer quantizer, Int32 colorCount, Int32 parallelTaskCount = 4) { // checks parameters Guard.CheckNull(source, "source"); // use other override to calculate error source.Dither(target, ditherer, quantizer, colorCount, parallelTaskCount); }
public void Dither(ImageBuffer target, IColorDitherer ditherer, IColorQuantizer quantizer, Int32 colorCount, Int32 parallelTaskCount = 4) { // checks parameters Guard.CheckNull(target, "target"); Guard.CheckNull(ditherer, "ditherer"); Guard.CheckNull(quantizer, "quantizer"); // prepares ditherer for another round ditherer.Prepare(quantizer, colorCount, this, target); // processes the image via the ditherer IList<Point> path = ditherer.GetPointPath(Width, Height); TransformPerPixel(target, path, ditherer.ProcessPixel, parallelTaskCount); }
public static void DitherImage(Image sourceImage, Image targetImage, IColorDitherer ditherer, IColorQuantizer quantizer, Int32 colorCount, Int32 parallelTaskCount = 4) { // checks parameters Guard.CheckNull(sourceImage, "sourceImage"); Guard.CheckNull(targetImage, "targetImage"); // wraps source image to a buffer using (ImageBuffer source = new ImageBuffer(sourceImage, ImageLockMode.ReadOnly)) using (ImageBuffer target = new ImageBuffer(targetImage, ImageLockMode.ReadOnly)) { // use other override to calculate error source.Dither(target, ditherer, quantizer, colorCount, parallelTaskCount); } }
public void Quantize(ImageBuffer target, IColorQuantizer quantizer, IColorDitherer ditherer, Int32 colorCount, Int32 parallelTaskCount = 4) { // checks parameters Guard.CheckNull(target, "target"); Guard.CheckNull(quantizer, "quantizer"); // initializes quantization parameters Boolean isTargetIndexed = target.PixelFormat.IsIndexed(); // step 1 - prepares the palettes List<Color> targetPalette = isTargetIndexed ? SynthetizePalette(quantizer, colorCount, parallelTaskCount) : null; // step 2 - updates the bitmap palette target.bitmap.SetPalette(targetPalette); target.UpdatePalette(true); // step 3 - prepares ditherer (optional) if (ditherer != null) ditherer.Prepare(quantizer, colorCount, this, target); // step 4 - prepares the quantization function TransformPixelFunction quantize = (sourcePixel, targetPixel) => { // reads the pixel color Color color = GetColorFromPixel(sourcePixel); // converts alpha to solid color color = QuantizationHelper.ConvertAlpha(color); // quantizes the pixel SetColorToPixel(targetPixel, color, quantizer); // marks pixel as processed by default Boolean result = true; // preforms inplace dithering (optional) if (ditherer != null && ditherer.IsInplace) { result = ditherer.ProcessPixel(sourcePixel, targetPixel); } // returns the result return result; }; // step 5 - generates the target image IList<Point> path = quantizer.GetPointPath(Width, Height); TransformPerPixel(target, path, quantize, parallelTaskCount); // step 6 - preforms non-inplace dithering (optional) if (ditherer != null && !ditherer.IsInplace) { Dither(target, ditherer, quantizer, colorCount, 1); } // step 7 - finishes the dithering (optional) if (ditherer != null) ditherer.Finish(); // step 8 - clean-up quantizer.Finish(); }
public static Image QuantizeImage(ImageBuffer source, IColorQuantizer quantizer, Int32 colorCount, Int32 parallelTaskCount = 4) { // performs the pure quantization wihout dithering return QuantizeImage(source, quantizer, null, colorCount, parallelTaskCount); }
private void TransformPerPixelBase(ImageBuffer target, IList<Point> path, Delegate transformAction, Int32 parallelTaskCount = 4) { // checks parameters Guard.CheckNull(path, "path"); Guard.CheckNull(target, "target"); Guard.CheckNull(transformAction, "transformAction"); // updates the palette UpdatePalette(); target.UpdatePalette(); // checks the dimensions if (Width != target.Width || Height != target.Height) { const String message = "Both images have to have the same dimensions."; throw new ArgumentOutOfRangeException(message); } // determines mode Boolean isAdvanced = transformAction is TransformPixelAdvancedFunction; // process the image in a parallel manner Action<LineTask> transformPerPixel = lineTask => { // creates individual pixel structures per task Pixel sourcePixel = new Pixel(this); Pixel targetPixel = new Pixel(target); // enumerates the pixels row by row for (Int32 pathOffset = lineTask.StartOffset; pathOffset < lineTask.EndOffset; pathOffset++) { Point point = path[pathOffset]; Boolean allowWrite; // enumerates the pixel, and returns the control to the outside sourcePixel.Update(point.X, point.Y); targetPixel.Update(point.X, point.Y); // when read is allowed, retrieves current value (in bytes) if (CanRead) ReadPixel(sourcePixel); if (target.CanRead) target.ReadPixel(targetPixel); // process the pixel by custom user operation if (isAdvanced) { TransformPixelAdvancedFunction transformAdvancedFunction = (TransformPixelAdvancedFunction) transformAction; allowWrite = transformAdvancedFunction(sourcePixel, targetPixel, this, target); } else // use simplified version with pixel parameters only { TransformPixelFunction transformFunction = (TransformPixelFunction) transformAction; allowWrite = transformFunction(sourcePixel, targetPixel); } // when write is allowed, copies the value back to the row buffer if (target.CanWrite && allowWrite) target.WritePixel(targetPixel); } }; // transforms image per pixel ProcessInParallel(path, transformPerPixel, parallelTaskCount); }
public static Image QuantizeImage(ImageBuffer source, IColorQuantizer quantizer, IColorDitherer ditherer, Int32 colorCount, Int32 parallelTaskCount = 4) { // checks parameters Guard.CheckNull(source, "source"); // creates a target bitmap in an appropriate format PixelFormat targetPixelFormat = Extend.GetFormatByColorCount(colorCount); Image result = new Bitmap(source.Width, source.Height, targetPixelFormat); // lock mode ImageLockMode lockMode = ditherer == null ? ImageLockMode.WriteOnly : ImageLockMode.ReadWrite; // wraps target image to a buffer using (ImageBuffer target = new ImageBuffer(result, lockMode)) { source.Quantize(target, quantizer, ditherer, colorCount, parallelTaskCount); return result; } }
/// <summary> /// See <see cref="BaseColorQuantizer.OnPrepare"/> for more details. /// </summary> protected override void OnPrepare(ImageBuffer image) { base.OnPrepare(image); OnFinish(); network = new Int32[NetworkSize][]; uniqueColors.Clear(); // initializes all the neurons in the network for (Int32 neuronIndex = 0; neuronIndex < NetworkSize; neuronIndex++) { Int32[] neuron = new Int32[4]; // calculates the base value for all the components Int32 baseValue = (neuronIndex << (NetworkBiasShift + 8)) / NetworkSize; neuron[0] = baseValue; neuron[1] = baseValue; neuron[2] = baseValue; // determines other per neuron values bias[neuronIndex] = 0; network[neuronIndex] = neuron; frequency[neuronIndex] = InitialBias / NetworkSize; } // initializes the some variables alpha = InitialAlpha; initialRadius = InitialRadius; // determines the radius Int32 potentialRadius = InitialRadius >> DefaultRadiusBiasShift; radius = potentialRadius <= 1 ? 0 : potentialRadius; Int32 radiusSquared = radius * radius; // precalculates the powers for all the radiuses for (Int32 index = 0; index < radius; index++) { Int32 indexSquared = index * index; radiusPower[index] = alpha * (((radiusSquared - indexSquared) * RadiusBias) / radiusSquared); } }
public static Image QuantizeImage(Image sourceImage, IColorQuantizer quantizer, IColorDitherer ditherer, Int32 colorCount, Int32 parallelTaskCount = 4) { // checks parameters Guard.CheckNull(sourceImage, "sourceImage"); // lock mode ImageLockMode lockMode = ditherer == null ? ImageLockMode.ReadOnly : ImageLockMode.ReadWrite; // wraps source image to a buffer using (ImageBuffer source = new ImageBuffer(sourceImage, lockMode)) { return QuantizeImage(source, quantizer, ditherer, colorCount, parallelTaskCount); } }
/// <summary> /// See <see cref="BaseColorQuantizer.OnPrepare"/> for more details. /// </summary> protected override void OnPrepare(ImageBuffer image) { base.OnPrepare(image); palette = new List<Color>(); colorMap = new ConcurrentDictionary<Int32, PopularityColorSlot>(); }
/// <summary> /// See <see cref="BaseColorQuantizer.OnPrepare"/> for more details. /// </summary> protected override void OnPrepare(ImageBuffer image) { redSlots = new UniformColorSlot[8]; greenSlots = new UniformColorSlot[8]; blueSlots = new UniformColorSlot[4]; }