Exemplo n.º 1
0
        public byte[] Compress(byte[] image)
        {
            uint position = 0;
            // check format
            List <byte> result = new List <byte>();


            // cut headers
            var header = new BmpHeader(image);

            foreach (byte b in header.ToArray())
            {
                result.Add(b);
            }
            position = header.OffsetBits;

            Format fmt = Format.BGR;

            #region PRE actions

            List <Color> difRow = new List <Color>();
            Color        prev   = ReadColor(image, position, fmt);
            position += 3;
            Color current = ReadColor(image, position, fmt);
            position += 3;
            Color next = Color.FromArgb(2, 45, 78);

            sbyte row = 0;
            if (prev == current)
            {
                row = 0;
            }
            else
            {
                row = -1;
            }

            #endregion PRE actions

            #region MAIN loop

            for (; position < image.Length; position += 3)
            {
                next = ReadColor(image, position, fmt);

                if (current == prev) // AAx
                {
                    if (row >= 0)
                    {
                        row++;
                        if (row >= 127)// TODO check overflow
                        {
                            result.Add((byte)row);
                            result.Add(prev.R);
                            result.Add(prev.G);
                            result.Add(prev.B);
                            row = 0;
                        }
                    }
                    else
                    {
                        throw new Exception("Something went wrong");
                    }
                }
                else // ABx
                {
                    if (row >= 0)
                    {
                        row++;
                        result.Add((byte)row);
                        result.Add(prev.R);
                        result.Add(prev.G);
                        result.Add(prev.B);

                        if (current == next)
                        {
                            row = 0;
                        }
                        else
                        {
                            row = -1;
                        }
                    }
                    else
                    {
                        difRow.Add(prev);
                        if (current == next || difRow.Count >= 128) // Check overflow
                        {
                            SaveDif(result, difRow);
                            row = 0;
                        }
                    }
                }

                prev    = current;
                current = next;
            }

            #endregion MAIN loop

            #region POST actions

            if (current == prev) // AAx
            {
                if (row >= 0)
                {
                    row++;
                    if (row >= 127)
                    {
                        result.Add((byte)row);
                        result.Add(prev.R);
                        result.Add(prev.G);
                        result.Add(prev.B);
                        row = 0;
                    }
                    row++;
                    result.Add((byte)row);
                    result.Add(prev.R);
                    result.Add(prev.G);
                    result.Add(prev.B);
                }
                else
                {
                    throw new Exception("Something went wrong");
                }
            }
            else // ABx
            {
                if (row >= 0)
                {
                    row++;
                    result.Add((byte)row);
                    result.Add(prev.R);
                    result.Add(prev.G);
                    result.Add(prev.B);
                }
                else
                {
                    difRow.Add(prev);
                    difRow.Add(current);
                    SaveDif(result, difRow);
                }
            }

            #endregion POST actions

            return(result.ToArray());
        }