private (byte[] indexData, byte[] paletteData) EncodeIndexInternal(Bitmap image, IProgressContext progress = null) { // Prepare information and instances var paddedSize = GetPaddedSize(image.Size); var swizzle = GetPixelRemapper(_indexEncoding, paddedSize); var finalSize = GetFinalSize(paddedSize, swizzle); var(indices, palette) = QuantizeImage(image, paddedSize, swizzle, progress); // Save palette colors // This step can be skipped if no palette encoding is given. // That saves time in the scenario when the palette is not needed or already exists as encoded data from somewhere else. var paletteData = _paletteEncoding?.Save(palette, new EncodingSaveContext(new Size(1, palette.Count), _taskCount)); // Save image indexColors var indexData = _indexEncoding.Save(indices, palette, new EncodingSaveContext(finalSize, _taskCount)); return(indexData, paletteData); }
private byte[] EncodeColorInternal(Bitmap image, IProgressContext progress = null) { // Prepare information and instances var paddedSize = GetPaddedSize(image.Size); var swizzle = GetPixelRemapper(_colorEncoding, paddedSize); var finalSize = GetFinalSize(paddedSize, swizzle); var colorShader = _shadeColorsFunc?.Invoke(); // If we have quantization enabled IEnumerable <Color> colors; if (_quantizer != null) { var scopedProgresses = progress?.SplitIntoEvenScopes(2); // HINT: Color shader is applied by QuantizeImage var(indices, palette) = QuantizeImage(image, paddedSize, swizzle, scopedProgresses?[0]); // Recompose indices to colors var setMaxProgress = scopedProgresses?[1]?.SetMaxValue(finalSize.Width * finalSize.Height); colors = indices.ToColors(palette).AttachProgress(setMaxProgress, "Encode indices"); } else { // Decompose image to colors var setMaxProgress = progress?.SetMaxValue(finalSize.Width * finalSize.Height); colors = image.ToColors(paddedSize, swizzle, _anchor).AttachProgress(setMaxProgress, "Encode colors"); // Apply color shader if (colorShader != null) { colors = colors.Select(colorShader.Write); } } // Save color data return(_colorEncoding.Save(colors, new EncodingSaveContext(finalSize, _taskCount))); }