예제 #1
0
파일: Program.cs 프로젝트: dested/Screening
        private static byte[] writeImagePayload(Bitmap bitmap)
        {
            List<byte> bytes = new List<byte>(bitmap.Width * bitmap.Height);

            UnsafeBitmap bmp = new UnsafeBitmap(bitmap);
            bmp.LockBitmap();

            List<PixelData> consecutive = new List<PixelData>();
            var width = bitmap.Width;
            var height = bitmap.Height;
            writeLength(bytes, width);
            writeLength(bytes, height);

            for (int y = 0; y < height; y++)
            {
                Point start = new Point(-1, -1);
                Point startColor = new Point(-1, -1);
                PixelData pixLast = PixelData.Empty;
                int consecutiveColor = 0;
                for (int x = 0; x < width; x++)
                {
                    var pix = bmp.GetPixel(x, y);
                    if (!(pix.red == 255 && pix.green == 255 && pix.blue == 255))
                    {

                        if (pixLast.red == pix.red && pixLast.green == pix.green && pixLast.blue == pix.blue)
                        {
                            if (consecutive.Count > 0)
                            {
                                //                                consecutive.RemoveAt(consecutive.Count - 1);
                                writeOutConsecutive(consecutive, bytes, ref start);
                            }
                            if (startColor.X == -1)
                            {
                                startColor = new Point(x, y);
                            }
                            consecutiveColor++;
                        }
                        else
                        {

                            if (consecutiveColor > 0)
                            {
                                writeOutConsecutiveColor(ref consecutiveColor, pixLast, bytes, ref startColor);
                            }

                            consecutiveColor++;
                            if (startColor.X == -1)
                            {
                                startColor = new Point(x, y);
                            }
                            if (start.X == -1)
                            {
                                start = new Point(x, y);
                            }
                            consecutive.Add(pix);
                            pixLast = pix;
                        }
                    }
                    else
                    {
                        if (consecutiveColor > 1)
                        {
                            writeOutConsecutiveColor(ref consecutiveColor, pixLast, bytes, ref startColor);
                        }
                        else
                        {
                            writeOutConsecutive(consecutive, bytes, ref start);
                        }

                    }
                }
                if (consecutiveColor > 1)
                {
                    writeOutConsecutiveColor(ref consecutiveColor, pixLast, bytes, ref startColor);
                }
                else
                {
                    writeOutConsecutive(consecutive, bytes, ref start);
                }
            }
            writeStatus(bytes, 255);
            /*
                        writeLength(bytes, bitmap.Width);
                        writeLength(bytes, bitmap.Height);
                        writeXY(bytes, 484, 997);
                        writeColor(bytes, new PixelData(155, 255, 24));
                        writeColor(bytes, new PixelData(15, 55, 24));
                        writeColor(bytes, new PixelData(100, 100, 100));
                        writeLength(bytes, bitmap.Height);

                        var diffPayload = bytes.ToArray();

                        fixed (byte* ps = &diffPayload[0])
                        {
                            byte* bb = ps;

                            var m1 = readLength( ref bb);
                            var m2 = readLength(ref bb);
                            var m3x = readLength(ref bb);
                            var m3y = readLength(ref bb);
                            var col1 = readColor(ref bb);
                            var col2 = readColor(ref bb);
                            var col3 = readColor(ref bb);
                            var m3 = readLength(ref bb);
                        }

            */

            bmp.UnlockBitmap();
            return bytes.ToArray();
        }
예제 #2
0
파일: Program.cs 프로젝트: dested/Screening
        private static RectCollection buildBoxes(Bitmap bitmap)
        {
            List<CRectangle> rt = new List<CRectangle>();

            UnsafeBitmap bmp = new UnsafeBitmap(bitmap);
            bmp.LockBitmap();
            var gapHeight = 40;

            for (int y = 0; y < bitmap.Height; y++)
            {
                List<CRectangle> rtc = new List<CRectangle>();

                foreach (var r in rt)
                {
                    if (r.Y + r.Height + gapHeight + 1 >= y)
                    {
                        rtc.Add(r);
                    }
                }

                for (int x = 0; x < bitmap.Width; x++)
                {
                    var pix = bmp.GetPixel(x, y);
                    if (!(pix.red == 255 && pix.green == 255 && pix.blue == 255))
                    {

                        List<CRectangle> ins = new List<CRectangle>();
                        if (rtc.Count > 1)
                        {

                        }
                        foreach (var rectangle in rtc)
                        {
                            if (rectangle.ShouldContain(x, y))
                            {
                                if (rectangle.Contains(x, y))
                                {
                                    ins.Add(rectangle);
                                    //                                    Console.WriteLine($"rt: {rt.Count} rtc:{rtc.Count} contians");

                                }
                                else if (rectangle.Contains(x, y - 1))
                                {
                                    ins.Add(rectangle);
                                    rectangle.Height = rectangle.Height + 1;
                                    //                                    Console.WriteLine($"rt: {rt.Count} rtc:{rtc.Count} height");
                                }
                                else if (rectangle.Contains(x - 1, y))
                                {
                                    ins.Add(rectangle);
                                    rectangle.Width = rectangle.Width + 1;
                                    //                                    Console.WriteLine($"rt: {rt.Count} rtc:{rtc.Count} width");
                                }
                            }
                        }
                        if (ins.Count == 0)
                        {
                            var cRectangle = new CRectangle(x, y, gapHeight * 5, gapHeight);
                            rt.Add(cRectangle);
                            rtc.Add(cRectangle);
                            //                            Console.WriteLine($"rt: {rt.Count} rtc:{rtc.Count} new");
                        }
                        if (ins.Count > 1)
                        {
                            var ba = ins[0];

                            for (int index = ins.Count - 1; index >= 0; index--)
                            {
                                ba = CRectangle.Union(ba, ins[index]);
                                rt.Remove(ins[index]);
                                rtc.Remove(ins[index]);
                            }
                            rt.Add(ba);
                            rtc.Add(ba);
                            //                            Console.WriteLine($"rt: {rt.Count} rtc:{rtc.Count} union");

                        }

                    }
                }
            }
            Console.WriteLine($"{rt.Count} boxes");

            bmp.Dispose();

            byte[] bytes = new byte[10000000];
            bytes[0] = 0xff;
            RectCollection col = new RectCollection();

            for (int index = 0; index < rt.Count; index++)
            {
                var cRectangle = rt[index];
                var dc = CopyImage(bitmap, cRectangle);

                col.Rectangles.Add(new RectObject()
                {
                    X = cRectangle.X,
                    Y = cRectangle.Y,
                    Width = cRectangle.Width,
                    Height = cRectangle.Height,
                    Index = index,
                    Bitmap = dc
                });
            }

            return col;

            /*
                        Bitmap done = new Bitmap(bitmap.Width, bitmap.Height);
                        var random = new Random();
                        using (Graphics g = Graphics.FromImage(done))
                        {
                            foreach (var rectangle in rt)
                            {
                                g.FillRectangle(new SolidBrush(Color.FromArgb(random.Next(0, 255), random.Next(0, 255), random.Next(0, 255))),

                                    rectangle.X, rectangle.Y, rectangle.Width, rectangle.Height);
                            }

                        }
                        return done;
            */
        }