예제 #1
0
 private Color CreateColor(long value, int redBitMask, int greenBitMask, int redShift)
 {
     return(Color.FromArgb(
                255,
                (RedDepth == 0) ? 255 : Convert.ChangeBitDepth((int)(value >> redShift & redBitMask), RedDepth, 8),
                (GreenDepth == 0) ? 255 : Convert.ChangeBitDepth((int)(value & greenBitMask), GreenDepth, 8),
                255));
 }
예제 #2
0
 private Color CreateColor(long value, int alphaBitMask, int lumBitMask, int lumShift)
 {
     return(Color.FromArgb(
                (AlphaDepth == 0) ? 255 : Convert.ChangeBitDepth((int)(value & alphaBitMask), AlphaDepth, 8),
                (LuminenceDepth == 0) ? 255 : Convert.ChangeBitDepth((int)(value >> lumShift & lumBitMask), LuminenceDepth, 8),
                (LuminenceDepth == 0) ? 255 : Convert.ChangeBitDepth((int)(value >> lumShift & lumBitMask), LuminenceDepth, 8),
                (LuminenceDepth == 0) ? 255 : Convert.ChangeBitDepth((int)(value >> lumShift & lumBitMask), LuminenceDepth, 8)));
 }
예제 #3
0
        public byte[] Save(IEnumerable <Color> colors)
        {
            byte nibbleBuffer = 0;
            bool writeNibble  = false;

            var ms = new MemoryStream();

            using (var bw = new BinaryWriter(ms, System.Text.Encoding.ASCII, true))
            {
                foreach (var color in colors)
                {
                    var r = (RedDepth == 0) ? 0 : Convert.ChangeBitDepth(color.R, 8, RedDepth);
                    var g = (GreenDepth == 0) ? 0 : Convert.ChangeBitDepth(color.G, 8, GreenDepth);

                    var rShift = GreenDepth;

                    long value = g;
                    value |= (uint)(r << rShift);

                    switch (BitDepth)
                    {
                    case 4:
                        if (writeNibble)
                        {
                            nibbleBuffer |= (byte)(value & 0xF);
                            bw.Write(nibbleBuffer);
                        }
                        else
                        {
                            nibbleBuffer = (byte)((value & 0xF) << 4);
                        }

                        writeNibble = !writeNibble;
                        break;

                    case 8:
                        bw.Write((byte)value);
                        break;

                    case 16:
                        bw.Write(Convert.ToByteArray((ushort)value, 2, ByteOrder));
                        break;

                    default:
                        throw new InvalidOperationException($"BitDepth {BitDepth} not supported!");
                    }
                }

                if (writeNibble)
                {
                    bw.Write(nibbleBuffer);
                }
            }

            return(ms.ToArray());
        }
예제 #4
0
        /// <inheritdoc cref="IIndexEncoding.Quantize(IEnumerable{Color},QuantizationSettings)"/>
        public (IEnumerable <IndexData> indices, IList <Color> palette) Quantize(IEnumerable <Color> colors, QuantizationSettings settings)
        {
            if (settings.ColorModel == ColorModel.RGBA)
            {
                settings.ColorModel = ColorModel.RGB;
            }

            var colorList = colors.ToList();

            var(indices, palette) = Kolors.Quantize(colorList, settings);

            var alphaIndexData = indices.
                                 Zip(colorList, (x, y) => new { index = x, alpha = y.A }).
                                 Select(x => new AlphaIndexData(Convert.ChangeBitDepth(x.alpha, 8, _alphaDepth), x.index));

            return(alphaIndexData, palette);
        }
예제 #5
0
        /// <inheritdoc cref="IIndexEncoding.Load(byte[])"/>
        public IEnumerable <IndexData> Load(byte[] input)
        {
            var alphaShift   = _indexDepth;
            var indexBitMask = (1 << _indexDepth) - 1;

            using (var br = new BinaryReader(new MemoryStream(input)))
                while (br.BaseStream.Position < br.BaseStream.Length)
                {
                    switch (_bitDepth)
                    {
                    case 8:
                        var value = br.ReadByte();
                        yield return(new AlphaIndexData(
                                         Convert.ChangeBitDepth(value >> alphaShift, _alphaDepth, 8),
                                         value & indexBitMask));

                        break;

                    default:
                        throw new InvalidOperationException($"IndexDepth {_indexDepth} not supported.");
                    }
                }
        }
예제 #6
0
 private static ushort From565To555(ushort value) => (ushort)(value & 0x1F | (Convert.ChangeBitDepth((value >> 5) & 0x3F, 6, 5) << 5) | (((value >> 11) & 0x1F) << 10));