/// <summary>
        /// Convert encoded band layer structure into byte stream.
        /// </summary>
        /// <param name = "bandData">The structure data to be converted into byte stream.</param>
        public byte[] ToBytes(CLEARCODEC_BAND bandData)
        {
            List <byte> bufList = new List <byte>();

            bufList.AddRange(TypeMarshal.ToBytes <ushort>(bandData.xStart));
            bufList.AddRange(TypeMarshal.ToBytes <ushort>(bandData.xEnd));
            bufList.AddRange(TypeMarshal.ToBytes <ushort>(bandData.yStart));
            bufList.AddRange(TypeMarshal.ToBytes <ushort>(bandData.yEnd));

            bufList.AddRange(TypeMarshal.ToBytes <byte>(bandData.blueBkg));
            bufList.AddRange(TypeMarshal.ToBytes <byte>(bandData.greenBkg));
            bufList.AddRange(TypeMarshal.ToBytes <byte>(bandData.redBkg));

            for (int i = 0; i < bandData.vBars.Count(); i++)
            {
                if (bandData.vBars[i].type == VBAR_TYPE.VBAR_CACHE_HIT)  // 1 bit check.
                {
                    VBAR_CACHE_HIT vbarCacheHit = bandData.vBars[i].vbarCacheHit;
                    bufList.AddRange(ToBytes(vbarCacheHit));

                    if (ccTestType == RdpegfxNegativeTypes.ClearCodec_Band_VBarCacheHit_ShortVBarPixelsExist)
                    {
                        // Add an additional pixel into encoded data.
                        Color_RGB pixel = new Color_RGB();
                        bufList.AddRange(TypeMarshal.ToBytes <byte>(pixel.B));
                        bufList.AddRange(TypeMarshal.ToBytes <byte>(pixel.G));
                        bufList.AddRange(TypeMarshal.ToBytes <byte>(pixel.R));
                    }
                }
                else if (bandData.vBars[i].type == VBAR_TYPE.SHORT_VBAR_CACHE_HIT)  // 2 bits check.
                {
                    SHORT_VBAR_CACHE_HIT svbarCacheHit = bandData.vBars[i].shortVbarCacheHit;
                    bufList.AddRange(ToBytes(svbarCacheHit));

                    if (ccTestType == RdpegfxNegativeTypes.ClearCodec_Band_ShortVBarCacheHit_ShortVBarPixelsExist)
                    {
                        // add an additional pixel into encoded data
                        Color_RGB pixel = new Color_RGB();
                        bufList.AddRange(TypeMarshal.ToBytes <byte>(pixel.B));
                        bufList.AddRange(TypeMarshal.ToBytes <byte>(pixel.G));
                        bufList.AddRange(TypeMarshal.ToBytes <byte>(pixel.R));
                    }
                }
                else if (bandData.vBars[i].type == VBAR_TYPE.SHORT_VBAR_CACHE_MISS)  // 2 bits check.
                {
                    SHORT_VBAR_CACHE_MISS svbarCacheMiss = bandData.vBars[i].shortVbarCacheMiss;
                    bufList.AddRange(ToBytes(svbarCacheMiss));
                }
            }
            return(bufList.ToArray());
        }
        /// <summary>
        /// It decode band layer byte stream into CLEARCODEC_BAND_DATA structure
        /// </summary>
        /// <param name = "bands"> The structure that decode result save to. </param>
        /// <return> true if decode success, otherwise return false </return>
        public bool Decode(ref CLEARCODEC_BAND_DATA bands)
        {
            if (decodeData == null) return false;

            List<CLEARCODEC_BAND> bandList = new List<CLEARCODEC_BAND>();
            while (offset < decodeData.Count())
            {
                CLEARCODEC_BAND band = new CLEARCODEC_BAND();
                if (!DecodeBand(ref band)) return false;
                bandList.Add(band);
            }
            bands.bandArr = bandList.ToArray();
            return true;
        }
Ejemplo n.º 3
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>
        /// Encode multiple band bitmap into byte stream
        /// </summary>
        /// <param name="bandDict">The structure saves multiple band bitmap and position.</param>
        /// <param name="vbarCacheEnabled">if vbar or short vbar cache is used when encoding band</param>
        public CLEARCODEC_BAND_DATA Encode(Dictionary <ClearCodec_RECT16, Bitmap> bandDict)
        {
            List <CLEARCODEC_BAND> bandList = new List <CLEARCODEC_BAND>();

            foreach (KeyValuePair <ClearCodec_RECT16, Bitmap> band in bandDict)
            {
                if (band.Value == null)
                {
                    continue;
                }
                CLEARCODEC_BAND bandData = EncodeBand(band.Value, band.Key);

                bandList.Add(bandData);
            }

            CLEARCODEC_BAND_DATA bands = new CLEARCODEC_BAND_DATA();

            bands.bandArr = bandList.ToArray();
            return(bands);
        }
Ejemplo n.º 5
0
        /// <summary>
        /// It decode band layer byte stream into CLEARCODEC_BAND_DATA structure
        /// </summary>
        /// <param name = "bands"> The structure that decode result save to. </param>
        /// <return> true if decode success, otherwise return false </return>
        public bool Decode(ref CLEARCODEC_BAND_DATA bands)
        {
            if (decodeData == null)
            {
                return(false);
            }

            List <CLEARCODEC_BAND> bandList = new List <CLEARCODEC_BAND>();

            while (offset < decodeData.Count())
            {
                CLEARCODEC_BAND band = new CLEARCODEC_BAND();
                if (!DecodeBand(ref band))
                {
                    return(false);
                }
                bandList.Add(band);
            }
            bands.bandArr = bandList.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>
        /// 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>
        /// Convert encoded band layer structure into byte stream.
        /// </summary>
        /// <param name = "bandData">The structure data to be converted into byte stream.</param>
        public byte[] ToBytes(CLEARCODEC_BAND bandData)
        {
            List<byte> bufList = new List<byte>();

            bufList.AddRange(TypeMarshal.ToBytes<ushort>(bandData.xStart));
            bufList.AddRange(TypeMarshal.ToBytes<ushort>(bandData.xEnd));
            bufList.AddRange(TypeMarshal.ToBytes<ushort>(bandData.yStart));
            bufList.AddRange(TypeMarshal.ToBytes<ushort>(bandData.yEnd));

            bufList.AddRange(TypeMarshal.ToBytes<byte>(bandData.blueBkg));
            bufList.AddRange(TypeMarshal.ToBytes<byte>(bandData.greenBkg));
            bufList.AddRange(TypeMarshal.ToBytes<byte>(bandData.redBkg));

            for (int i = 0; i < bandData.vBars.Count(); i++)
            {
                if (bandData.vBars[i].type == VBAR_TYPE.VBAR_CACHE_HIT)  // 1 bit check.
                {
                    VBAR_CACHE_HIT vbarCacheHit = bandData.vBars[i].vbarCacheHit;
                    bufList.AddRange(ToBytes(vbarCacheHit));

                    if (ccTestType == RdpegfxNegativeTypes.ClearCodec_Band_VBarCacheHit_ShortVBarPixelsExist)
                    {
                        // Add an additional pixel into encoded data.
                        Color_RGB pixel = new Color_RGB();
                        bufList.AddRange(TypeMarshal.ToBytes<byte>(pixel.B));
                        bufList.AddRange(TypeMarshal.ToBytes<byte>(pixel.G));
                        bufList.AddRange(TypeMarshal.ToBytes<byte>(pixel.R));
                    }
                }
                else if (bandData.vBars[i].type == VBAR_TYPE.SHORT_VBAR_CACHE_HIT)  // 2 bits check.
                {
                    SHORT_VBAR_CACHE_HIT svbarCacheHit = bandData.vBars[i].shortVbarCacheHit;
                    bufList.AddRange(ToBytes(svbarCacheHit));

                    if (ccTestType == RdpegfxNegativeTypes.ClearCodec_Band_ShortVBarCacheHit_ShortVBarPixelsExist)
                    {
                        // add an additional pixel into encoded data
                        Color_RGB pixel = new Color_RGB();
                        bufList.AddRange(TypeMarshal.ToBytes<byte>(pixel.B));
                        bufList.AddRange(TypeMarshal.ToBytes<byte>(pixel.G));
                        bufList.AddRange(TypeMarshal.ToBytes<byte>(pixel.R));
                    }
                }
                else if (bandData.vBars[i].type == VBAR_TYPE.SHORT_VBAR_CACHE_MISS)  // 2 bits check.
                {
                    SHORT_VBAR_CACHE_MISS svbarCacheMiss = bandData.vBars[i].shortVbarCacheMiss;
                    bufList.AddRange(ToBytes(svbarCacheMiss));
                }
            }
            return bufList.ToArray();
        }
        /// <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;
        }