Example #1
0
        public BufferedMatrix(Stream source, QuantizationType quantization, int cacheSize) : base()
        {
            Source      = source;
            Rows        = MessagePackBinary.ReadInt32(source);
            Columns     = MessagePackBinary.ReadInt32(source);
            BeginOfData = source.Position;

            var tempArray = MessagePackBinary.ReadBytes(source);

            RowSize = source.Position - BeginOfData;

            Quantization   = quantization;
            DisposingToken = new CancellationTokenSource();

            CacheThread = new Thread(() =>
            {
                while (!DisposingToken.Token.IsCancellationRequested)
                {
                    if (Interlocked.Read(ref CacheCount) > cacheSize)
                    {
                        CacheLock.EnterWriteLock();
                        Cache.Clear();
                        CacheCount = 0;
                        CacheLock.ExitWriteLock();
                    }
                    Thread.Sleep(1000);
                }
            });
            CacheThread.IsBackground = true; //Won't block when terminating the process
            CacheThread.Start();
        }
Example #2
0
        public void ToStream(Stream stream, QuantizationType quantization)
        {
            MessagePackBinary.WriteInt32(stream, Rows);
            MessagePackBinary.WriteInt32(stream, Columns);

            var data = Data.AsSpan();

            switch (quantization)
            {
            case QuantizationType.None:
            {
                var byteArray = new byte[Columns * sizeof(float)];
                for (int i = 0; i < Rows; i++)
                {
                    var row = data.Slice(i * Columns, Columns);
                    MemoryMarshal.Cast <float, byte>(row).CopyTo(byteArray);
                    MessagePackBinary.WriteBytes(stream, byteArray);
                }
                break;
            }

            case QuantizationType.OneBit:
            {
                var bits = new BitArray(Columns);

                byte[] byteArray = new byte[(bits.Length - 1) / 8 + 1];
                for (int i = 0; i < Rows; i++)
                {
                    var row = data.Slice(i * Columns, Columns);
                    for (int j = 0; j < Columns; j++)
                    {
                        bits.Set(j, row[j] > 0);
                    }
                    bits.CopyTo(byteArray, 0);
                    MessagePackBinary.WriteBytes(stream, byteArray);
                }
                break;
            }

            default: throw new NotImplementedException();
            }

            stream.Flush();
        }
Example #3
0
        public void ToStream(Stream stream, QuantizationType quantization)
        {
            MessagePackBinary.WriteInt32(stream, Rows);
            MessagePackBinary.WriteInt32(stream, Columns);

            switch (quantization)
            {
            case QuantizationType.None:
            {
                var byteArray = new byte[Columns * sizeof(float)];
                for (int i = 0; i < Rows; i++)
                {
                    System.Buffer.BlockCopy(Data[i], 0, byteArray, 0, byteArray.Length);
                    MessagePackBinary.WriteBytes(stream, byteArray);
                }
                break;
            }

            case QuantizationType.OneBit:
            {
                var bits = new BitArray(Columns);

                byte[] byteArray = new byte[(bits.Length - 1) / 8 + 1];
                for (int i = 0; i < Rows; i++)
                {
                    for (int j = 0; j < Columns; j++)
                    {
                        bits.Set(j, Data[i][j] > 0);
                    }
                    bits.CopyTo(byteArray, 0);
                    System.Buffer.BlockCopy(Data[i], 0, byteArray, 0, byteArray.Length);
                    MessagePackBinary.WriteBytes(stream, byteArray);
                }
                break;
            }

            default: throw new NotImplementedException();
            }

            stream.Flush();
        }
Example #4
0
        public static Matrix FromStream(Stream stream, QuantizationType quantization)
        {
            var Rows    = MessagePackBinary.ReadInt32(stream);
            var Columns = MessagePackBinary.ReadInt32(stream);
            var m       = new Matrix(Rows, Columns);

            var data = m.Data.AsSpan();

            switch (quantization)
            {
            case QuantizationType.None:
            {
                for (int i = 0; i < Rows; i++)
                {
                    var row       = data.Slice(i * Columns, Columns);
                    var byteArray = MessagePackBinary.ReadBytes(stream);
                    MemoryMarshal.Cast <byte, float>(byteArray.AsSpan()).CopyTo(row);
                }
                break;
            }

            case QuantizationType.OneBit:
            {
                for (int i = 0; i < Rows; i++)
                {
                    var byteArray = MessagePackBinary.ReadBytes(stream);
                    var bits      = new BitArray(byteArray);
                    var row       = data.Slice(i * Columns, Columns);
                    for (int j = 0; j < Columns; j++)
                    {
                        row[j] = bits[j] ? 0.33f : -0.33f;
                    }
                }
                break;
            }

            default: throw new NotImplementedException();
            }

            return(m);
        }
Example #5
0
        public static Matrix FromStream(Stream stream, QuantizationType quantization)
        {
            var Rows    = MessagePackBinary.ReadInt32(stream);
            var Columns = MessagePackBinary.ReadInt32(stream);
            var m       = new Matrix(Rows, Columns);

            switch (quantization)
            {
            case QuantizationType.None:
            {
                for (int i = 0; i < Rows; i++)
                {
                    var byteArray = MessagePackBinary.ReadBytes(stream);
                    System.Buffer.BlockCopy(byteArray, 0, m.Data[i], 0, byteArray.Length);
                }
                break;
            }

            case QuantizationType.OneBit:
            {
                for (int i = 0; i < Rows; i++)
                {
                    var byteArray = MessagePackBinary.ReadBytes(stream);
                    var bits      = new BitArray(byteArray);
                    for (int j = 0; j < Columns; j++)
                    {
                        m.Data[i][j] = bits[j] ? 0.33f : -0.33f;
                    }
                }
                break;
            }

            default: throw new NotImplementedException();
            }

            return(m);
        }
Example #6
0
 public void ToStream(Stream stream, QuantizationType quantization)
 {
     throw new NotImplementedException();
 }
Example #7
0
            //Tables as given in JPEG standard / LibJPEG
            public QuantizationTable(QuantizationType type, int quality)
            {
                switch (type)
                {
                    case QuantizationType.Zero:
                        aTable = new byte[64];
                        nPrecisionAndIdentifier = 0;
                        break;
                    case QuantizationType.Luminance:
                        aTable = new byte[] {   16,  11,  10,  16,  24,  40,  51,  61,
                                                12,  12,  14,  19,  26,  58,  60,  55,
                                                14,  13,  16,  24,  40,  57,  69,  56,
                                                14,  17,  22,  29,  51,  87,  80,  62,
                                                18,  22,  37,  56,  68, 109, 103,  77,
                                                24,  35,  55,  64,  81, 104, 113,  92,
                                                49,  64,  78,  87, 103, 121, 120, 101,
                                                72,  92,  95,  98, 112, 100, 103,  99};
                        nPrecisionAndIdentifier = 0;
                        break;
                    case QuantizationType.Chroma:
                        aTable = new byte[] {   17,  18,  24,  47,  99,  99,  99,  99,
                                                18,  21,  26,  66,  99,  99,  99,  99,
                                                24,  26,  56,  99,  99,  99,  99,  99,
                                                47,  66,  99,  99,  99,  99,  99,  99,
                                                99,  99,  99,  99,  99,  99,  99,  99,
                                                99,  99,  99,  99,  99,  99,  99,  99,
                                                99,  99,  99,  99,  99,  99,  99,  99,
                                                99,  99,  99,  99,  99,  99,  99,  99 };
                        nPrecisionAndIdentifier = 1;
                        break;
                    default:
                        aTable = new byte[64];
                        break;
                }

                if (type != QuantizationType.Zero)
                {
                    if (quality <= 0) quality = 1;
                    if (quality > 100) quality = 100;

                    if (quality < 50)
                        quality = 5000 / quality;
                    else
                        quality = 200 - quality * 2;

                    for (int i = 0; i < aTable.Length; i++)
                    {
                        int temp = ((int)aTable[i] * quality + 50) / 100;
                        /* limit the values to the valid range */
                        if (temp <= 0L) temp = 1;
                        if (temp > 32767L) temp = 32767; /* max quantizer needed for 12 bits */
                        bool force_baseline = true;
                        if (force_baseline && temp > 255L)
                            temp = 255;		/* limit to baseline range if requested */
                        aTable[i] = (byte)temp;
                    }
                }
            }
Example #8
0
                    public FloatSegment(EndianBinaryReader er)
                    {
                        StartFrame = er.ReadSingle();
                        EndFrame   = er.ReadSingle();
                        Flags      = er.ReadUInt32();
                        if ((Flags & 1) == 1)
                        {
                            SingleKeyValue = er.ReadSingle();
                        }
                        else
                        {
                            NrKeys = er.ReadUInt32();
                            Speed  = er.ReadSingle();

                            uint             interp = (Flags >> 2) & 7;
                            QuantizationType quanti = (QuantizationType)((Flags >> 5) & 7);
                            if (quanti != QuantizationType.Hermite128 && quanti != QuantizationType.StepLinear64)
                            {
                                if (quanti != QuantizationType.UnifiedHermite96)
                                {
                                    Scale      = er.ReadSingle();
                                    Offset     = er.ReadSingle();
                                    FrameScale = er.ReadSingle();
                                }
                            }
                            Keys = new Key[NrKeys];
                            for (int i = 0; i < NrKeys; i++)
                            {
                                Key k = new Key();
                                switch (quanti)
                                {
                                case QuantizationType.Hermite128:
                                {
                                    k.Frame    = er.ReadSingle();
                                    k.Value    = er.ReadSingle();
                                    k.InSlope  = er.ReadSingle();
                                    k.OutSlope = er.ReadSingle();
                                    break;
                                }

                                case QuantizationType.Hermite64:
                                {
                                    UInt32 FrameValue = er.ReadUInt32();
                                    k.InSlope  = (float)er.ReadInt16() * 0.00390625f;
                                    k.OutSlope = (float)er.ReadInt16() * 0.00390625f;
                                    break;
                                }

                                case QuantizationType.Hermite48:
                                {
                                    byte[] FrameValue = er.ReadBytes(3);
                                    byte[] InOutSlope = er.ReadBytes(3);
                                    break;
                                }

                                case QuantizationType.UnifiedHermite96:
                                {
                                    k.Frame   = er.ReadSingle();
                                    k.Value   = er.ReadSingle();
                                    k.InSlope = k.OutSlope = er.ReadSingle();
                                    break;
                                }

                                case QuantizationType.UnifiedHermite48:
                                {
                                    k.Frame   = (float)er.ReadUInt16() * 0.03125f;
                                    k.Value   = er.ReadInt16();
                                    k.InSlope = k.OutSlope = (float)er.ReadInt16() * 0.00390625f;
                                    break;
                                }

                                case QuantizationType.UnifiedHermite32:
                                {
                                    k.Frame = er.ReadByte();
                                    byte[] ValueSlope = er.ReadBytes(3);
                                    break;
                                }

                                case QuantizationType.StepLinear64:
                                {
                                    k.Frame = er.ReadSingle();
                                    k.Value = er.ReadSingle();
                                    break;
                                }

                                case QuantizationType.StepLinear32:
                                {
                                    UInt32 FrameValue = er.ReadUInt32();
                                    k.Frame = FrameValue & 0xFFF;
                                    k.Value = ((int)FrameValue) >> 12;
                                    break;
                                }
                                }
                                Keys[i] = k;
                            }
                        }
                    }
Example #9
0
            //Tables as given in JPEG standard / LibJPEG
            public QuantizationTable(QuantizationType type, int quality)
            {
                switch (type)
                {
                case QuantizationType.Zero:
                    aTable = new byte[64];
                    nPrecisionAndIdentifier = 0;
                    break;

                case QuantizationType.Luminance:
                    aTable = new byte[] { 16, 11, 10, 16, 24, 40, 51, 61,
                                          12, 12, 14, 19, 26, 58, 60, 55,
                                          14, 13, 16, 24, 40, 57, 69, 56,
                                          14, 17, 22, 29, 51, 87, 80, 62,
                                          18, 22, 37, 56, 68, 109, 103, 77,
                                          24, 35, 55, 64, 81, 104, 113, 92,
                                          49, 64, 78, 87, 103, 121, 120, 101,
                                          72, 92, 95, 98, 112, 100, 103, 99 };
                    nPrecisionAndIdentifier = 0;
                    break;

                case QuantizationType.Chroma:
                    aTable = new byte[] { 17, 18, 24, 47, 99, 99, 99, 99,
                                          18, 21, 26, 66, 99, 99, 99, 99,
                                          24, 26, 56, 99, 99, 99, 99, 99,
                                          47, 66, 99, 99, 99, 99, 99, 99,
                                          99, 99, 99, 99, 99, 99, 99, 99,
                                          99, 99, 99, 99, 99, 99, 99, 99,
                                          99, 99, 99, 99, 99, 99, 99, 99,
                                          99, 99, 99, 99, 99, 99, 99, 99 };
                    nPrecisionAndIdentifier = 1;
                    break;

                default:
                    aTable = new byte[64];
                    break;
                }

                if (type != QuantizationType.Zero)
                {
                    if (quality <= 0)
                    {
                        quality = 1;
                    }
                    if (quality > 100)
                    {
                        quality = 100;
                    }

                    if (quality < 50)
                    {
                        quality = 5000 / quality;
                    }
                    else
                    {
                        quality = 200 - quality * 2;
                    }

                    for (int i = 0; i < aTable.Length; i++)
                    {
                        int temp = ((int)aTable[i] * quality + 50) / 100;
                        /* limit the values to the valid range */
                        if (temp <= 0L)
                        {
                            temp = 1;
                        }
                        if (temp > 32767L)
                        {
                            temp = 32767;                                        /* max quantizer needed for 12 bits */
                        }
                        bool force_baseline = true;
                        if (force_baseline && temp > 255L)
                        {
                            temp = 255;                                         /* limit to baseline range if requested */
                        }
                        aTable[i] = (byte)temp;
                    }
                }
            }