示例#1
0
        /// <summary>
        /// It decode byte stream into CLEARCODEC_VBAR structure
        /// </summary>
        /// <param name = "bandVBar"> The structure that decode result save to. </param>
        public bool DecodeBandVBar(ref CLEARCODEC_VBAR bandVBar)
        {
            ushort vBarStat = 0;

            if (!DecodeUShort(ref vBarStat))
            {
                return(false);
            }

            if ((vBarStat & 0x8000) != 0)  // VBAR_CACHE_HIT structure
            {
                bandVBar.type = VBAR_TYPE.VBAR_CACHE_HIT;
                bandVBar.vbarCacheHit.vBarIndex_x = vBarStat;
            }
            else if ((vBarStat & 0xc000) != 0)  // SHORT_VBAR_CACHE_HIT structure
            {
                bandVBar.type = VBAR_TYPE.SHORT_VBAR_CACHE_HIT;
                bandVBar.shortVbarCacheHit.shortVBarIndex_x = vBarStat;
                if (!DecodeByte(ref bandVBar.shortVbarCacheHit.shortVBarYOn))
                {
                    return(false);
                }
            }
            else  // Must be SHORT_VBAR_CACHE_MISS structure.
            {
                bandVBar.type = VBAR_TYPE.SHORT_VBAR_CACHE_MISS;
                bandVBar.shortVbarCacheMiss.shortVBarYOnOff_x = vBarStat;
                byte pixelCount = (byte)(bandVBar.shortVbarCacheMiss.shortVBarYOff - bandVBar.shortVbarCacheMiss.shortVBarYOn);

                // Decode shortVBarPixels.
                List <Color_RGB> shortVBarPixelList = new List <Color_RGB>();
                for (int i = 0; i < pixelCount; i++)
                {
                    Color_RGB rgb = new Color_RGB();
                    if (!DecodeByte(ref rgb.B))
                    {
                        return(false);
                    }
                    if (!DecodeByte(ref rgb.G))
                    {
                        return(false);
                    }
                    if (!DecodeByte(ref rgb.R))
                    {
                        return(false);
                    }
                    shortVBarPixelList.Add(rgb);
                }

                bandVBar.shortVbarCacheMiss.shortVBarPixels = shortVBarPixelList.ToArray();
            }
            return(true);
        }
示例#2
0
        /// <summary>
        /// It decode byte stream into CLEARCODEC_BAND structure
        /// </summary>
        /// <param name = "band"> The structure that decode result save to. </param>
        public bool DecodeBand(ref CLEARCODEC_BAND band)
        {
            if (!DecodeUShort(ref band.xStart))
            {
                return(false);
            }
            if (!DecodeUShort(ref band.xEnd))
            {
                return(false);
            }
            if (!DecodeUShort(ref band.yStart))
            {
                return(false);
            }
            if (!DecodeUShort(ref band.yEnd))
            {
                return(false);
            }

            if (!DecodeByte(ref band.blueBkg))
            {
                return(false);
            }
            if (!DecodeByte(ref band.greenBkg))
            {
                return(false);
            }
            if (!DecodeByte(ref band.redBkg))
            {
                return(false);
            }

            ushort vbarCount = (ushort)(band.xEnd - band.xStart + 1);
            List <CLEARCODEC_VBAR> vbarList = new List <CLEARCODEC_VBAR>();

            for (int i = 0; i < vbarCount; i++)
            {
                CLEARCODEC_VBAR vbar = new CLEARCODEC_VBAR();
                if (!DecodeBandVBar(ref vbar))
                {
                    return(false);
                }
                vbarList.Add(vbar);
            }
            band.vBars = vbarList.ToArray();

            return(true);
        }
        /// <summary>
        /// It decode byte stream into CLEARCODEC_BAND structure
        /// </summary>
        /// <param name = "band"> The structure that decode result save to. </param>
        public bool DecodeBand(ref CLEARCODEC_BAND band)
        {
            if (!DecodeUShort(ref band.xStart)) return false;
            if (!DecodeUShort(ref band.xEnd)) return false;
            if (!DecodeUShort(ref band.yStart)) return false;
            if (!DecodeUShort(ref band.yEnd)) return false;

            if (!DecodeByte(ref band.blueBkg)) return false;
            if (!DecodeByte(ref band.greenBkg)) return false;
            if (!DecodeByte(ref band.redBkg)) return false;

            ushort vbarCount = (ushort)(band.xEnd - band.xStart + 1);
            List<CLEARCODEC_VBAR> vbarList = new List<CLEARCODEC_VBAR>();
            for (int i = 0; i < vbarCount; i++)
            {
                CLEARCODEC_VBAR vbar = new CLEARCODEC_VBAR();
                if (!DecodeBandVBar(ref vbar)) return false;
                vbarList.Add(vbar);
            }
            band.vBars = vbarList.ToArray();

            return true;
        }
        /// <summary>
        /// It decode byte stream into CLEARCODEC_VBAR structure
        /// </summary>
        /// <param name = "bandVBar"> The structure that decode result save to. </param>
        public bool DecodeBandVBar(ref CLEARCODEC_VBAR bandVBar)
        {
            ushort vBarStat = 0;
            if (!DecodeUShort(ref vBarStat)) return false;

            if ((vBarStat & 0x8000) != 0)  // VBAR_CACHE_HIT structure
            {
                bandVBar.type = VBAR_TYPE.VBAR_CACHE_HIT;
                bandVBar.vbarCacheHit.vBarIndex_x = vBarStat;
            }
            else if ((vBarStat & 0xc000) != 0)  // SHORT_VBAR_CACHE_HIT structure
            {
                bandVBar.type = VBAR_TYPE.SHORT_VBAR_CACHE_HIT;
                bandVBar.shortVbarCacheHit.shortVBarIndex_x = vBarStat;
                if (!DecodeByte(ref bandVBar.shortVbarCacheHit.shortVBarYOn)) return false;
            }
            else  // Must be SHORT_VBAR_CACHE_MISS structure.
            {
                bandVBar.type = VBAR_TYPE.SHORT_VBAR_CACHE_MISS;
                bandVBar.shortVbarCacheMiss.shortVBarYOnOff_x = vBarStat;
                byte pixelCount = (byte)(bandVBar.shortVbarCacheMiss.shortVBarYOff - bandVBar.shortVbarCacheMiss.shortVBarYOn);

                // Decode shortVBarPixels.
                List<Color_RGB> shortVBarPixelList = new List<Color_RGB>();
                for (int i = 0; i < pixelCount; i++)
                {
                    Color_RGB rgb = new Color_RGB();
                    if (!DecodeByte(ref rgb.B)) return false;
                    if (!DecodeByte(ref rgb.G)) return false;
                    if (!DecodeByte(ref rgb.R)) return false;
                    shortVBarPixelList.Add(rgb);
                }

                bandVBar.shortVbarCacheMiss.shortVBarPixels = shortVBarPixelList.ToArray();
            }
            return true;
        }
        /// <summary>
        /// Encode a band bitmap into byte stream
        /// </summary>
        /// <param name="bandBmp">The bitmap to be encoded in band layer.</param>
        /// <param name="bandRect">The band position relative to bitmap left-top edge.</param>
        public CLEARCODEC_BAND EncodeBand(Bitmap bandBmp, ClearCodec_RECT16 bandRect)
        {
            CLEARCODEC_BAND bandData = new CLEARCODEC_BAND();

            Color bgColor = bandBmp.GetPixel(0, 0);

            bandData.xStart = bandRect.left;
            bandData.xEnd   = (ushort)(bandRect.right - 1);
            bandData.yStart = bandRect.top;
            bandData.yEnd   = (ushort)(bandRect.bottom - 1);

            bandData.blueBkg  = bgColor.B;
            bandData.greenBkg = bgColor.G;
            bandData.redBkg   = bgColor.R;

            List <CLEARCODEC_VBAR> vBarList = new List <CLEARCODEC_VBAR>();

            // use short vbar cache miss method
            for (ushort x = 0; x < bandBmp.Width; x++)
            {
                byte shortVBarYOn  = 0; // relative to top of V-Bar, won't excceed 52
                byte shortVBarYOff = 0; // relative to top of V-Bar, won't excceed 52

                // find shortVBarYOn from top
                byte y;  // bandBmp.Height can't exceed 52 pixels
                for (y = 0; y < bandBmp.Height; y++)
                {
                    Color pixelColor = bandBmp.GetPixel(x, y);
                    if (!bgColor.Equals(pixelColor))
                    {
                        shortVBarYOn = (byte)y;
                        break;
                    }
                }

                // the whole vbar use bgColor
                if (y == (ushort)bandBmp.Height)
                {
                    shortVBarYOn  = (byte)y;
                    shortVBarYOff = (byte)y;
                }

                // find shortVBarYOff from bottem, which is the first position for left bgcolor
                for (y = (byte)(bandBmp.Height - 1); y > shortVBarYOn; y--)
                {
                    Color pixelColor = bandBmp.GetPixel(x, y);
                    if (!bgColor.Equals(pixelColor))
                    {
                        shortVBarYOff = (byte)(y + 1);
                        break;
                    }
                }

                // only one point in shortVBar is different from bgColor
                if (y == shortVBarYOn)
                {
                    shortVBarYOff = (byte)(shortVBarYOn + 1);
                }

                // construct short Vbar Pixels

                List <Color_RGB> shortVBarPixelList = new List <Color_RGB>();
                for (y = shortVBarYOn; y < shortVBarYOff; y++)
                {
                    Color     pixelColor = bandBmp.GetPixel(x, y);
                    Color_RGB svbarColor = new Color_RGB();
                    svbarColor.B = pixelColor.B;
                    svbarColor.G = pixelColor.G;
                    svbarColor.R = pixelColor.R;

                    shortVBarPixelList.Add(svbarColor);
                }

                // construct vbar pixels
                List <Color_RGB> vBarPixelList = new List <Color_RGB>();
                for (int k = 0; k < bandBmp.Height; k++)
                {
                    Color     pixelColor = bandBmp.GetPixel(x, k);
                    Color_RGB vbarColor  = new Color_RGB();
                    vbarColor.B = pixelColor.B;
                    vbarColor.G = pixelColor.G;
                    vbarColor.R = pixelColor.R;
                    vBarPixelList.Add(vbarColor);
                }

                // check if vbar or short vbar cache matched
                ushort vbarIdx = GetVbarIndex(vBarPixelList);
                if (vbarIdx != 0xffff)
                {
                    // use cache index
                    CLEARCODEC_VBAR vBar = new CLEARCODEC_VBAR();
                    vBar.type                   = VBAR_TYPE.VBAR_CACHE_HIT;
                    vBar.vbarCacheHit.x         = 0x1;
                    vBar.vbarCacheHit.vBarIndex = vbarIdx;

                    vBarList.Add(vBar);

                    continue;
                }
                else
                {
                    vBarDict.Add(vBarPixelList, vBarCursor);
                    vBarCursor = (ushort)((vBarCursor + 1) % CLEARCODEC_CONST.CLEARCODEC_BAND_MAX_VBAR_CACHE_SLOT);
                }

                // cechk short vbar cache
                ushort shortvbarIdx = GetShortVbarIndex(shortVBarPixelList);
                if (shortvbarIdx != 0xffff)
                {
                    // use short vbar cache index
                    CLEARCODEC_VBAR svBar = new CLEARCODEC_VBAR();
                    svBar.type = VBAR_TYPE.SHORT_VBAR_CACHE_HIT;
                    svBar.shortVbarCacheHit.x = 0x1;
                    svBar.shortVbarCacheHit.shortVBarIndex = shortvbarIdx;
                    svBar.shortVbarCacheHit.shortVBarYOn   = shortVBarYOn;

                    vBarList.Add(svBar);
                    continue;
                }
                else
                {
                    // add short vbar into cache dictionary and send the short vbar vbar in SHORT_VBAR_CACHE_MISS structure later.
                    shortvBarDict.Add(shortVBarPixelList, shortvBarCursor);
                    shortvBarCursor = (ushort)((shortvBarCursor + 1) % CLEARCODEC_CONST.CLEARCODEC_BAND_MAX_SHORT_VBAR_CACHE_SLOT);
                }


                // get to here mean no vbar or short vbar cache hit
                CLEARCODEC_VBAR vBarUncache = new CLEARCODEC_VBAR();
                vBarUncache.type = VBAR_TYPE.SHORT_VBAR_CACHE_MISS;
                vBarUncache.shortVbarCacheMiss.x = 0x0;

                // pack data into vBar.shortVbarCacheMiss
                vBarUncache.shortVbarCacheMiss.shortVBarYOn  = shortVBarYOn;
                vBarUncache.shortVbarCacheMiss.shortVBarYOff = shortVBarYOff;
                if (shortVBarYOff > shortVBarYOn)
                {
                    vBarUncache.shortVbarCacheMiss.shortVBarPixels = shortVBarPixelList.ToArray();
                }

                vBarList.Add(vBarUncache);
            }

            bandData.vBars = vBarList.ToArray();

            return(bandData);
        }
        /// <summary>
        /// Encode a band bitmap into byte stream
        /// </summary>
        /// <param name="bandBmp">The bitmap to be encoded in band layer.</param>
        /// <param name="bandRect">The band position relative to bitmap left-top edge.</param>
        public CLEARCODEC_BAND EncodeBand(Bitmap bandBmp, ClearCodec_RECT16 bandRect)
        {
            CLEARCODEC_BAND bandData = new CLEARCODEC_BAND();

            Color bgColor = bandBmp.GetPixel(0, 0);

            bandData.xStart = bandRect.left;
            bandData.xEnd = (ushort)(bandRect.right-1);
            bandData.yStart = bandRect.top;
            bandData.yEnd = (ushort)(bandRect.bottom-1);

            bandData.blueBkg = bgColor.B;
            bandData.greenBkg = bgColor.G;
            bandData.redBkg = bgColor.R;

            List<CLEARCODEC_VBAR> vBarList = new List<CLEARCODEC_VBAR>();

            // use short vbar cache miss method
            for (ushort x = 0; x < bandBmp.Width; x++)
            {
                byte shortVBarYOn = 0;  // relative to top of V-Bar, won't excceed 52
                byte shortVBarYOff = 0; // relative to top of V-Bar, won't excceed 52

                // find shortVBarYOn from top
                byte y;  // bandBmp.Height can't exceed 52 pixels
                for ( y = 0; y < bandBmp.Height; y++)
                {
                    Color pixelColor = bandBmp.GetPixel(x, y);
                    if (!bgColor.Equals(pixelColor))
                    {
                        shortVBarYOn = (byte)y;
                        break;
                    }
                }

                // the whole vbar use bgColor
                if (y == (ushort)bandBmp.Height)
                {
                    shortVBarYOn = (byte)y;
                    shortVBarYOff = (byte)y;
                }

                // find shortVBarYOff from bottem, which is the first position for left bgcolor
                for (y = (byte)(bandBmp.Height - 1); y > shortVBarYOn; y--)
                {
                    Color pixelColor = bandBmp.GetPixel(x, y);
                    if (!bgColor.Equals(pixelColor))
                    {
                        shortVBarYOff = (byte)(y + 1);
                        break;
                    }
                }

                // only one point in shortVBar is different from bgColor
                if (y == shortVBarYOn)
                {
                    shortVBarYOff = (byte)(shortVBarYOn + 1);
                }

                // construct short Vbar Pixels

                List<Color_RGB> shortVBarPixelList = new List<Color_RGB>();
                for (y = shortVBarYOn; y < shortVBarYOff; y++)
                {
                    Color pixelColor = bandBmp.GetPixel(x, y);
                    Color_RGB svbarColor = new Color_RGB();
                    svbarColor.B = pixelColor.B;
                    svbarColor.G = pixelColor.G;
                    svbarColor.R = pixelColor.R;

                    shortVBarPixelList.Add(svbarColor);
                }

                // construct vbar pixels
                List<Color_RGB> vBarPixelList = new List<Color_RGB>();
                for (int k = 0; k < bandBmp.Height; k++)
                {
                    Color pixelColor = bandBmp.GetPixel(x, k);
                    Color_RGB vbarColor = new Color_RGB();
                    vbarColor.B = pixelColor.B;
                    vbarColor.G = pixelColor.G;
                    vbarColor.R = pixelColor.R;
                    vBarPixelList.Add(vbarColor);
                }

                // check if vbar or short vbar cache matched
                ushort vbarIdx = GetVbarIndex(vBarPixelList);
                if (vbarIdx != 0xffff)
                {
                    // use cache index
                    CLEARCODEC_VBAR vBar = new CLEARCODEC_VBAR();
                    vBar.type = VBAR_TYPE.VBAR_CACHE_HIT;
                    vBar.vbarCacheHit.x = 0x1;
                    vBar.vbarCacheHit.vBarIndex = vbarIdx;

                    vBarList.Add(vBar);

                    continue;
                }
                else
                {
                    vBarDict.Add(vBarPixelList, vBarCursor);
                    vBarCursor = (ushort)((vBarCursor + 1) % CLEARCODEC_CONST.CLEARCODEC_BAND_MAX_VBAR_CACHE_SLOT);
                }

                // cechk short vbar cache
                ushort shortvbarIdx = GetShortVbarIndex(shortVBarPixelList);
                if (shortvbarIdx != 0xffff)
                {
                    // use short vbar cache index
                    CLEARCODEC_VBAR svBar = new CLEARCODEC_VBAR();
                    svBar.type = VBAR_TYPE.SHORT_VBAR_CACHE_HIT;
                    svBar.shortVbarCacheHit.x = 0x1;
                    svBar.shortVbarCacheHit.shortVBarIndex = shortvbarIdx;
                    svBar.shortVbarCacheHit.shortVBarYOn = shortVBarYOn;

                    vBarList.Add(svBar);
                    continue;
                }
                else
                {
                    // add short vbar into cache dictionary and send the short vbar vbar in SHORT_VBAR_CACHE_MISS structure later.
                    shortvBarDict.Add(shortVBarPixelList, shortvBarCursor);
                    shortvBarCursor = (ushort)((shortvBarCursor + 1) % CLEARCODEC_CONST.CLEARCODEC_BAND_MAX_SHORT_VBAR_CACHE_SLOT);
                }

                // get to here mean no vbar or short vbar cache hit
                CLEARCODEC_VBAR vBarUncache = new CLEARCODEC_VBAR();
                vBarUncache.type = VBAR_TYPE.SHORT_VBAR_CACHE_MISS;
                vBarUncache.shortVbarCacheMiss.x = 0x0;

                // pack data into vBar.shortVbarCacheMiss
                vBarUncache.shortVbarCacheMiss.shortVBarYOn = shortVBarYOn;
                vBarUncache.shortVbarCacheMiss.shortVBarYOff = shortVBarYOff;
                if (shortVBarYOff > shortVBarYOn )
                {
                    vBarUncache.shortVbarCacheMiss.shortVBarPixels = shortVBarPixelList.ToArray();
                }

                vBarList.Add(vBarUncache);
            }

            bandData.vBars = vBarList.ToArray();

            return bandData;
        }