Пример #1
0
        private ushort[,] GetPixelsFrom12BitByteArray(byte[] bytes, ushort[,] prevFramePixels, GetByteMode byteMode, ref int idx, out bool crcOkay)
        {
            var rv = new ushort[Width, Height];

            bool isLittleEndian = m_ImageSection.ByteOrder == AdvImageSection.ImageByteOrder.LittleEndian;
            bool convertTo12Bit = this.m_ImageSection.BitsPerPixel == 12;
            bool convertTo16Bit = this.m_ImageSection.BitsPerPixel == 16;

            bool isKeyFrame      = byteMode == GetByteMode.KeyFrameBytes;
            bool keyFrameNotUsed = byteMode == GetByteMode.Normal;
            bool isDiffCorrFrame = byteMode == GetByteMode.DiffCorrBytes;

            int counter = 0;

            for (int y = 0; y < Height; ++y)
            {
                for (int x = 0; x < Width; ++x)
                {
                    counter++;
                    // Every 2 12-bit values can be encoded in 3 bytes
                    // xxxxxxxx|xxxxyyyy|yyyyyyy

                    switch (counter % 2)
                    {
                    case 1:
                        byte bt1 = bytes[idx];
                        idx++;
                        byte bt2 = bytes[idx];

                        ushort val = (ushort)(((ushort)bt1 << 4) + ((bt2 >> 4) & 0x0F));
                        if (!isLittleEndian)
                        {
                            val = (ushort)(val << 4);
                            val = (ushort)((ushort)((val & 0xFF) << 8) + (ushort)(val >> 8));

                            if (convertTo12Bit)
                            {
                                throw new NotSupportedException();
                            }
                        }
                        else
                        if (convertTo16Bit)
                        {
                            val = (ushort)(val << 4);
                        }

                        if (isDiffCorrFrame)
                        {
                            val = (ushort)((ushort)prevFramePixels[x, y] + (ushort)val);
                            if (convertTo12Bit && val > 4095)
                            {
                                val -= 4095;
                            }
                        }

                        rv[x, y] = val;
                        if (counter < 10 || counter > Height * Width - 10)
                        {
                            Trace.WriteLine(string.Format("{0}: {1}", counter, val));
                        }
                        break;

                    case 0:
                        bt1 = bytes[idx];
                        idx++;
                        bt2 = bytes[idx];
                        idx++;

                        val = (ushort)((((ushort)bt1 & 0x0F) << 8) + bt2);
                        if (!isLittleEndian)
                        {
                            val = (ushort)(val << 4);
                            val = (ushort)((ushort)((val & 0xFF) << 8) + (ushort)(val >> 8));

                            if (convertTo12Bit)
                            {
                                throw new NotSupportedException();
                            }
                        }
                        else
                        if (convertTo16Bit)
                        {
                            val = (ushort)(val << 4);
                        }

                        if (isDiffCorrFrame)
                        {
                            val = (ushort)((ushort)prevFramePixels[x, y] + (ushort)val);
                            if (convertTo12Bit && val > 4095)
                            {
                                val -= 4095;
                            }
                        }

                        rv[x, y] = val;
                        if (counter < 10 || counter > Height * Width - 10)
                        {
                            Trace.WriteLine(string.Format("{0}: {1}", counter, val));
                        }
                        break;
                    }
                }
            }

            if (m_UsesCRC)
            {
                uint savedFrameCrc = (uint)(bytes[idx] + (bytes[idx + 1] << 8) + (bytes[idx + 2] << 16) + (bytes[idx + 3] << 24));
                idx += 4;

                uint crc3 = ComputePixelsCRC(rv);

                crcOkay = crc3 == savedFrameCrc;
            }
            else
            {
                crcOkay = true;
            }

            return(rv);
        }
Пример #2
0
        private ushort[,] GetPixelsFrom12BitByteArray(byte[] bytes, ushort[,] prevFramePixels, GetByteMode byteMode, ref int idx, out bool crcOkay)
        {
            var rv = new ushort[Width, Height];

            bool isLittleEndian = m_ImageSection.ByteOrder == AdvImageSection.ImageByteOrder.LittleEndian;
            bool convertTo12Bit = this.m_ImageSection.BitsPerPixel == 12;
            bool convertTo16Bit = this.m_ImageSection.BitsPerPixel == 16;

            bool isKeyFrame = byteMode == GetByteMode.KeyFrameBytes;
            bool keyFrameNotUsed = byteMode == GetByteMode.Normal;
            bool isDiffCorrFrame = byteMode == GetByteMode.DiffCorrBytes;

            int counter = 0;
            for (int y = 0; y < Height; ++y)
            {
                for (int x = 0; x < Width; ++x)
                {
                    counter++;
                    // Every 2 12-bit values can be encoded in 3 bytes
                    // xxxxxxxx|xxxxyyyy|yyyyyyy

                    switch (counter % 2)
                    {
                        case 1:
                            byte bt1 = bytes[idx];
                            idx++;
                            byte bt2 = bytes[idx];

                            ushort val = (ushort)(((ushort)bt1 << 4) + ((bt2 >> 4) & 0x0F));
                            if (!isLittleEndian)
                            {
                                val = (ushort)(val << 4);
                                val = (ushort)((ushort)((val & 0xFF) << 8) + (ushort)(val >> 8));

                                if (convertTo12Bit)
                                    throw new NotSupportedException();
                            }
                            else
                                if (convertTo16Bit) val = (ushort)(val << 4);

                            if (isDiffCorrFrame)
                            {
                                val = (ushort)((ushort)prevFramePixels[x, y] + (ushort)val);
                                if (convertTo12Bit && val > 4095) val -= 4095;
                            }

                            rv[x, y] = val;
                            if (counter < 10 || counter > Height * Width - 10) Trace.WriteLine(string.Format("{0}: {1}", counter, val));
                            break;

                        case 0:
                            bt1 = bytes[idx];
                            idx++;
                            bt2 = bytes[idx];
                            idx++;

                            val = (ushort)((((ushort)bt1 & 0x0F) << 8) + bt2);
                            if (!isLittleEndian)
                            {
                                val = (ushort)(val << 4);
                                val = (ushort)((ushort)((val & 0xFF) << 8) + (ushort)(val >> 8));

                                if (convertTo12Bit)
                                    throw new NotSupportedException();
                            }
                            else
                                if (convertTo16Bit) val = (ushort)(val << 4);

                            if (isDiffCorrFrame)
                            {
                                val = (ushort)((ushort)prevFramePixels[x, y] + (ushort)val);
                                if (convertTo12Bit && val > 4095) val -= 4095;
                            }

                            rv[x, y] = val;
                            if (counter < 10 || counter > Height * Width - 10) Trace.WriteLine(string.Format("{0}: {1}", counter, val));
                            break;
                    }
                }
            }

            if (m_UsesCRC)
            {
                uint savedFrameCrc = (uint)(bytes[idx] + (bytes[idx + 1] << 8) + (bytes[idx + 2] << 16) + (bytes[idx + 3] << 24));
                idx += 4;

                uint crc3 = ComputePixelsCRC(rv);

                crcOkay = crc3 == savedFrameCrc;
            }
            else
                crcOkay = true;

            return rv;
        }
Пример #3
0
        public object GetDataFromDataBytes(byte[] dataBytes, ushort[,] prevImageData, GetByteMode byteMode, int size, int startIndex)
        {
            byte[] bytes;
            int    readIndex = 0;

            if (!m_UsesCompression)
            {
                bytes     = dataBytes;
                readIndex = startIndex;
            }
            else if (m_Compression == AdvCompressionMethods.COMPR_QUICKLZ)
            {
                byte[] compressedBytes = new byte[size];
                Array.Copy(dataBytes, startIndex, compressedBytes, 0, size);

                readIndex = 0;
                bytes     = QuickLZ.decompress(compressedBytes);
            }
            else if (m_Compression == AdvCompressionMethods.COMPR_LAGARITH16)
            {
                byte[] compressedBytes = new byte[size];
                Array.Copy(dataBytes, startIndex, compressedBytes, 0, size);

                readIndex = 0;
                bytes     = TangraCore.Lagarith16Decompress(Width, Height, compressedBytes);
            }
            else
            {
                throw new NotSupportedException(string.Format("Don't know how to apply compression '{0}'", m_Compression));
            }


            ushort[,] imageData;
            bool crcOkay;

            if (BitsPerPixel == 12)
            {
                imageData = GetPixelsFrom12BitByteArray(bytes, prevImageData, byteMode, ref readIndex, out crcOkay);
            }
            else if (BitsPerPixel == 16)
            {
                if (m_IsRawDataLayout)
                {
                    imageData = GetPixelsFrom16BitByteArrayRawLayout(bytes, prevImageData, ref readIndex, out crcOkay);
                }
                else
                {
                    imageData = GetPixelsFrom16BitByteArrayDiffCorrLayout(bytes, prevImageData, ref readIndex, out crcOkay);
                }
            }
            else if (BitsPerPixel == 8)
            {
                if (m_IsRawDataLayout)
                {
                    imageData = GetPixelsFrom8BitByteArrayRawLayout(bytes, prevImageData, ref readIndex, out crcOkay);
                }
                else
                {
                    imageData = GetPixelsFrom8BitByteArrayDiffCorrLayout(bytes, prevImageData, ref readIndex, out crcOkay);
                }
            }
            else
            {
                throw new NotSupportedException();
            }

            return(new AdvImageData()
            {
                ImageData = imageData,
                CRCOkay = m_UsesCRC ? crcOkay : true,
                Bpp = m_ImageSection.BitsPerPixel
            });
        }
Пример #4
0
        public object GetDataFromDataBytes(byte[] dataBytes, ushort[,] prevImageData, GetByteMode byteMode, int size, int startIndex)
        {
            byte[] bytes;
            int readIndex = 0;
            if (!m_UsesCompression)
            {
                bytes = dataBytes;
                readIndex = startIndex;
            }
            else if (m_Compression == AdvCompressionMethods.COMPR_DIFF_CORR_QUICKLZ)
            {
                byte[] compressedBytes = new byte[size];
                Array.Copy(dataBytes, startIndex, compressedBytes, 0, size);

                readIndex = 0;
                bytes = QuickLZ.decompress(compressedBytes);
            }
            else
                throw new NotSupportedException(string.Format("Don't know how to apply compression '{0}'", m_Compression));

            ushort[,] imageData;
            bool crcOkay;
            if (BitsPerPixel == 12)
                imageData = GetPixelsFrom12BitByteArray(bytes, prevImageData, byteMode, ref readIndex, out crcOkay);
            else if (BitsPerPixel == 16)
            {
                if (m_IsRawDataLayout)
                    imageData = GetPixelsFrom16BitByteArrayRawLayout(bytes, prevImageData, ref readIndex, out crcOkay);
                else
                    imageData = GetPixelsFrom16BitByteArrayDiffCorrLayout(bytes, prevImageData, ref readIndex, out crcOkay);
            }
            else if (BitsPerPixel == 8)
            {
                if (m_IsRawDataLayout)
                    imageData = GetPixelsFrom8BitByteArrayRawLayout(bytes, prevImageData, ref readIndex, out crcOkay);
                else
                    imageData = GetPixelsFrom8BitByteArrayDiffCorrLayout(bytes, prevImageData, ref readIndex, out crcOkay);
            }
            else
                throw new NotSupportedException();

            return new AdvImageData()
            {
                ImageData = imageData,
                CRCOkay = m_UsesCRC ? crcOkay : true,
                Bpp = m_ImageSection.BitsPerPixel
            };
        }