Esempio n. 1
0
        public override void WriteData()
        {
            base.WriteData();
            rfb.WriteUint32(Convert.ToUInt32(VncHost.Encoding.RreEncoding));
            rfb.WriteUInt32(Convert.ToUInt32(subrects.Length));
            WritePixel32(bgpixel);

            using (var ms = new MemoryStream())
            {
                for (var i = 0; i < subrects.Length; i++)
                {
                    var data = PixelGrabber.GrabBytes(subrects[i].pixel, framebuffer);

                    //This is how BigEndianBinaryWriter writes short values :)
                    var x = Flip(BitConverter.GetBytes(subrects[i].x));
                    var y = Flip(BitConverter.GetBytes(subrects[i].y));
                    var w = Flip(BitConverter.GetBytes(subrects[i].w));
                    var h = Flip(BitConverter.GetBytes(subrects[i].h));

                    ms.Write(data, 0, data.Length);
                    ms.Write(x, 0, x.Length);
                    ms.Write(y, 0, y.Length);
                    ms.Write(w, 0, w.Length);
                    ms.Write(h, 0, h.Length);
                }
                rfb.Write(ms.ToArray());
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Creates an object type derived from EncodedRectangle, based on the value of encoding.
        /// </summary>
        /// <param name="rectangle">A node object from the Screen Handler defining the bounds of the rectangle and the pixel data. IT SHOULD BE CONSIDERED LOCALLY AGAINST pixels param, not globally against the screen size</param>
        /// <param name="encoding">An Integer indicating the encoding type to be used for this rectangle.  Used to determine the type of EncodedRectangle to create.</param>
        /// <returns></returns>
        public EncodedRectangle Build(Rectangle rectangle, VncHost.Encoding encoding)
        {
            Bitmap bmp = PixelGrabber.CreateScreenCapture(rectangle);

            int[] pixels = PixelGrabber.GrabPixels(bmp, rectangle.X, rectangle.Y, rectangle.Width, rectangle.Height, bmp.PixelFormat);
            return(Build(rectangle, pixels, encoding));
        }
Esempio n. 3
0
 public override void Encode()
 {
     if (bytes == null)
     {
         bytes = PixelGrabber.GrabPixels(pixels, new Rectangle(0, 0, rectangle.Width, rectangle.Height), framebuffer);
     }
 }
Esempio n. 4
0
        /// <summary>
        ///     Creates an object type derived from EncodedRectangle, based on the value of encoding.
        /// </summary>
        /// <param name="rectangle">
        ///     A node object from the Screen Handler defining the bounds of the rectangle and the pixel data.
        ///     IT SHOULD BE CONSIDERED LOCALLY AGAINST pixels param, not globally against the screen size
        /// </param>
        /// <param name="encoding">
        ///     An Integer indicating the encoding type to be used for this rectangle.  Used to determine the
        ///     type of EncodedRectangle to create.
        /// </param>
        /// <returns></returns>
        public EncodedRectangle Build(Rectangle2 rectangle, VncHost.Encoding encoding)
        {
            var bmp    = PixelGrabber.CreateScreenCapture(rectangle.ToRectangle());
            var pixels = PixelGrabber.GrabPixels(bmp, rectangle.X, rectangle.Y, rectangle.Width, rectangle.Height,
                                                 bmp.PixelFormat);

            return(Build(rectangle, pixels, encoding));
        }
Esempio n. 5
0
 public override void Encode()
 {
     /*
      * bytes = PixelGrabber.GrabPixels(bmp, PixelFormat.Format32bppArgb);
      * for (int i = 0; i < pixels.Length; i++)
      *  framebuffer[i] = pixels[i];
      */
     if (bytes == null)
     {
         bytes = PixelGrabber.GrabPixels(pixels, new Rectangle(0, 0, rectangle.Width, rectangle.Height), framebuffer);
     }
 }
Esempio n. 6
0
 public static bool hasAlpha(Image paramImage)
 {
     try
     {
         PixelGrabber pixelGrabber = new PixelGrabber(paramImage, 0, 0, 1, 1, false);
         pixelGrabber.grabPixels();
         return(pixelGrabber.ColorModel.hasAlpha());
     }
     catch (InterruptedException)
     {
         return(false);
     }
 }
Esempio n. 7
0
        /// <summary>
        /// Called to downsample the image and store it in the down sample component.
        /// </summary>
        /// <param name="height">The height of the image.</param>
        /// <param name="width">The width of the image</param>
        /// <returns></returns>
        public double[] DownSample(int height, int width)
        {
            double[] result = new double[height * width];

            PixelGrabber grabber = new PixelGrabber(this.image, 0, 0,
                                                    this.imageWidth, this.imageWidth, true);

            try
            {
                grabber.grabPixels();
            }
            catch (InterruptedException e)
            {
                throw new EncogError(e);
            }

            this.pixelMap = (int[])grabber.getPixels();

            // now downsample

            this.ratioX = (double)(this.downSampleRight - this.downSampleLeft)
                          / (double)width;
            this.ratioY = (double)(this.downSampleBottom - this.downSampleTop)
                          / (double)height;

            int index = 0;

            for (int y = 0; y < height; y++)
            {
                for (int x = 0; x < width; x++)
                {
                    result[index++] = downSampleRegion(x, y);
                }
            }

            return(result);
        }
Esempio n. 8
0
        public override void WriteData()
        {
            base.WriteData();
            rfb.WriteUInt32(Convert.ToUInt32(VncHost.Encoding.HextileEncoding));

            Tile tile;
            int  mask;
            var  oldBgpixel = 0x10000000;
            var  fgpixel    = 0x10000000;
            int  j;

            //Console.WriteLine("Tiles: " + tiles.Length);

            //Writing to a MemoryStream is faster, than writing to a NetworkStream, while being read chunk by chunk
            //Data is sent fast, when it is sent as one ordered byte array
            using (var ms = new MemoryStream())
            {
                for (var i = 0; i < tiles.Length; i++)
                {
                    if (tiles[i] is Tile)
                    {
                        tile = (Tile)tiles[i];
                        mask = 0;

                        // Do we have subrects?
                        if (tile.subrects.Length > 0)
                        {
                            // We have subrects
                            mask |= ANY_SUBRECTS;

                            // Do all subrects have the same pixel?
                            fgpixel = tile.subrects[0].pixel;
                            for (j = 1; j < tile.subrects.Length; j++)
                            {
                                if (tile.subrects[j].pixel != fgpixel)
                                {
                                    // Subrects are of varying colors
                                    mask |= SUBRECTS_COLORED;
                                    break;
                                }
                            }

                            if ((mask & SUBRECTS_COLORED) == 0)
                            {
                                // All subrects have the same pixel
                                mask |= FOREGROUND_SPECIFIED;
                            }
                        }

                        // Has the background changed?
                        if (tile.bgpixel != oldBgpixel)
                        {
                            oldBgpixel = tile.bgpixel;
                            mask      |= BACKGROUND_SPECIFIED;
                        }

                        //pwriter.Write((byte)mask);
                        ms.WriteByte((byte)mask);

                        // Background pixel
                        if ((mask & BACKGROUND_SPECIFIED) != 0)
                        {
                            var pd = PixelGrabber.GrabBytes(tile.bgpixel, framebuffer);
                            ms.Write(pd, 0, pd.Length);

                            //pwriter.WritePixel(tile.bgpixel);
                        }

                        // Foreground pixel
                        if ((mask & FOREGROUND_SPECIFIED) != 0)
                        {
                            var pd = PixelGrabber.GrabBytes(fgpixel, framebuffer);
                            ms.Write(pd, 0, pd.Length);
                            //pwriter.WritePixel(fgpixel);
                        }

                        // Subrects
                        if ((mask & ANY_SUBRECTS) != 0)
                        {
                            ms.WriteByte((byte)tile.subrects.Length);
                            //pwriter.Write((byte)tile.subrects.Length);

                            //using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
                            //{
                            for (j = 0; j < tile.subrects.Length; j++)
                            {
                                // Subrects colored
                                if ((mask & SUBRECTS_COLORED) != 0)
                                {
                                    var x = PixelGrabber.GrabBytes(tile.subrects[j].pixel, framebuffer);
                                    ms.Write(x, 0, x.Length);
                                }
                                ms.WriteByte((byte)((tile.subrects[j].x << 4) | tile.subrects[j].y));
                                ms.WriteByte((byte)(((tile.subrects[j].w - 1) << 4) | (tile.subrects[j].h - 1)));
                            }
                            //pwriter.Write(ms.ToArray());
                            //}
                        }
                    }
                    else
                    {
                        ms.WriteByte(RAW);
                        //pwriter.Write((byte)RAW);

                        ms.Write((byte[])tiles[i], 0, ((byte[])tiles[i]).Length);
                        //pwriter.Write((byte[])tiles[i]);
                    }
                }
                rfb.Write(ms.ToArray());
            }
        }
Esempio n. 9
0
        /// <summary>
        /// Called to downsample the image and store it in the down sample component.
        /// </summary>
        /// <param name="height">The height of the image.</param>
        /// <param name="width">The width of the image</param>
        /// <returns></returns>
        public double[] DownSample(int height, int width)
        {

            double[] result = new double[height * width];

            PixelGrabber grabber = new PixelGrabber(this.image, 0, 0,
                   this.imageWidth, this.imageWidth, true);

            try
            {
                grabber.grabPixels();
            }
            catch (InterruptedException e)
            {
                throw new EncogError(e);
            }

            this.pixelMap = (int[])grabber.getPixels();

            // now downsample

            this.ratioX = (double)(this.downSampleRight - this.downSampleLeft)
                    / (double)width;
            this.ratioY = (double)(this.downSampleBottom - this.downSampleTop)
                    / (double)height;

            int index = 0;
            for (int y = 0; y < height; y++)
            {
                for (int x = 0; x < width; x++)
                {
                    result[index++] = downSampleRegion(x, y);
                }
            }

            return result;
        }
Esempio n. 10
0
        public override void Encode()
        {
            var x = 0; //rectangle.X;
            var y = 0; //rectangle.Y;
            var w = rectangle.Width;
            var h = rectangle.Height;

            Trace.WriteLine("Landed at ZRLE start!");

            //int rawDataSize = w * h * (framebuffer.BitsPerPixel / 8);
            //byte[] data = new byte[rawDataSize];

            //Bitmap bmp = PixelGrabber.GrabImage(rectangle.Width, rectangle.Height, pixels);
            using (var ms = new MemoryStream())
            {
                for (var currentY = y; currentY < y + h; currentY += TILE_HEIGHT)
                {
                    var tileH = TILE_HEIGHT;
                    tileH = Math.Min(tileH, y + h - currentY);
                    for (var currentX = x; currentX < x + w; currentX += TILE_WIDTH)
                    {
                        var tileW = TILE_WIDTH;
                        tileW = Math.Min(tileW, x + w - currentX);

                        var subencoding = rectangle.IsSolidColor ? (byte)1 : (byte)0;
                        ms.WriteByte(subencoding);

                        if (subencoding == 0)
                        {
                            var pixelz = PixelGrabber.CopyPixels(pixels, w, currentX, currentY, tileW, tileH);
                            for (var i = 0; i < pixelz.Length; ++i)
                            {
                                var b = 0;

                                //The CPixel structure (Compressed Pixel) has 3 bytes, opposed to the normal pixel which has 4.
                                var pixel  = pixelz[i];
                                var pbytes = new byte[3];

                                pbytes[b++] = (byte)(pixel & 0xFF);
                                pbytes[b++] = (byte)((pixel >> 8) & 0xFF);
                                pbytes[b++] = (byte)((pixel >> 16) & 0xFF);
                                //bytes[b++] = (byte)((pixel >> 24) & 0xFF);

                                ms.Write(pbytes, 0, pbytes.Length);
                            }
                        }
                        else
                        {
                            var b      = 0;
                            var pixel  = rectangle.SolidColor;
                            var pbytes = new byte[3];

                            pbytes[b++] = (byte)(pixel & 0xFF);
                            pbytes[b++] = (byte)((pixel >> 8) & 0xFF);
                            pbytes[b++] = (byte)((pixel >> 16) & 0xFF);
                            //bytes[b++] = (byte)((pixel >> 24) & 0xFF);

                            ms.Write(pbytes, 0, pbytes.Length);
                        }
                    }
                }
                var uncompressed = ms.ToArray();
                bytes = uncompressed;
            }
        }
Esempio n. 11
0
        public void GenerateChildren()
        {
            int westX      = Bounds.X; //Keep X and Y just in case, needed on client side
            int westY      = Bounds.Y;
            int westWidth  = Bounds.Width / 2;
            int westHeight = Bounds.Height / 2;

            int eastX      = westX + westWidth;
            int eastY      = westY;
            int eastWidth  = Bounds.Width - westWidth;
            int eastHeight = Bounds.Height - westHeight;

            Rectangle2 nw = new Rectangle2(westX, westY, westWidth, westHeight);

            int[] nwd = PixelGrabber.CopyPixels(NodeData, Bounds.Width, 0, 0, westWidth, westHeight);
            childrenData[(int)Direction.NW] = nwd;//NodeData;
            childrenRect[(int)Direction.NW] = nw;

            int parentWidth = Bounds.Width;
            int scanline    = parentWidth;
            int size        = westWidth * westHeight;
            int jump        = scanline - westWidth;
            int s           = 0;
            int p           = 0 * scanline + 0;

            Trace.WriteLine("My offset: " + p);
            for (int i = 0; i < size; i++, s++, p++)
            {
                if (s == westWidth)
                {
                    s  = 0;
                    p += jump;
                }
            }
            Trace.WriteLine("My end: " + --p);

            Rectangle2 ne = new Rectangle2(eastX, eastY, eastWidth, eastHeight);

            // TODO: Keep relative pixel start and end instead of copying the part of the array
            // The current implementation is a naive one and very slow, but it works as intended
            childrenData[(int)Direction.NE] = PixelGrabber.CopyPixels(NodeData, Bounds.Width, westWidth, 0, eastWidth, eastHeight);
            childrenRect[(int)Direction.NE] = ne;

            s = 0;
            p = 0 * scanline + westWidth;
            Trace.WriteLine("My offset: " + p);
            for (int i = 0; i < size; i++, s++, p++)
            {
                if (s == westWidth)
                {
                    s  = 0;
                    p += jump;
                }
            }
            Trace.WriteLine("My end: " + --p);


            Rectangle2 sw = new Rectangle2(westX, westY + westHeight, westWidth, westHeight);

            childrenData[(int)Direction.SW] = PixelGrabber.CopyPixels(NodeData, Bounds.Width, 0, 0 + westHeight, westWidth, westHeight);
            childrenRect[(int)Direction.SW] = sw;

            Rectangle2 se = new Rectangle2(eastX, eastY + eastHeight, eastWidth, eastHeight);

            childrenData[(int)Direction.SE] = PixelGrabber.CopyPixels(NodeData, Bounds.Width, westWidth, 0 + eastHeight, eastWidth, eastHeight);
            childrenRect[(int)Direction.SE] = se;
        }
Esempio n. 12
0
    protected internal virtual bool writeImageData()
    {
      int num1 = this.height;
      int num2 = 0;
      this.bytesPerPixel = !this.encodeAlpha ? 3 : 4;
      Deflater deflater = new Deflater(this.compressionLevel);
      ByteArrayOutputStream arrayOutputStream = new ByteArrayOutputStream(1024);
      DeflaterOutputStream deflaterOutputStream = new DeflaterOutputStream((OutputStream) arrayOutputStream, deflater);
      IOException ioException1;
      IOException ioException2;
      while (true)
      {
        int num3;
        int[] numArray1;
        PixelGrabber pixelGrabber;
        try
        {
          if (num1 > 0)
          {
            int num4 = (int) short.MaxValue;
            int num5 = this.width * (this.bytesPerPixel + 1);
            int num6 = -1;
            num3 = Math.max(Math.min(num5 != num6 ? num4 / num5 : -num4, num1), 1);
            numArray1 = new int[this.width * num3];
            pixelGrabber = new PixelGrabber(this.image, 0, num2, this.width, num3, numArray1, 0, this.width);
            try
            {
              pixelGrabber.grabPixels();
            }
            catch (Exception ex)
            {
              int num7 = 2;
              if (ByteCodeHelper.MapException<Exception>(ex, (ByteCodeHelper.MapFlags) num7) == null)
                throw;
              else
                break;
            }
          }
          else
            goto label_33;
        }
        catch (IOException ex)
        {
          int num4 = 1;
          ioException1 = (IOException) ByteCodeHelper.MapException<IOException>((Exception) ex, (ByteCodeHelper.MapFlags) num4);
          goto label_8;
        }
        try
        {
          if ((pixelGrabber.getStatus() & 128) != 0)
          {
            System.get_err().println("image fetch aborted or errored");
            return false;
          }
          else
          {
            byte[] pixels = new byte[this.width * num3 * this.bytesPerPixel + num3];
            if (this.filter == 1)
              this.leftBytes = new byte[16];
            if (this.filter == 2)
              this.priorRow = new byte[this.width * this.bytesPerPixel];
            int num4 = 0;
            int startPos = 1;
            for (int index1 = 0; index1 < this.width * num3; ++index1)
            {
              int num5 = index1;
              int num6 = this.width;
              int num7 = -1;
              if ((num6 != num7 ? num5 % num6 : 0) == 0)
              {
                byte[] numArray2 = pixels;
                int index2 = num4;
                ++num4;
                int num8 = (int) (sbyte) this.filter;
                numArray2[index2] = (byte) num8;
                startPos = num4;
              }
              byte[] numArray3 = pixels;
              int index3 = num4;
              int num9 = num4 + 1;
              int num10 = (int) (sbyte) (numArray1[index1] >> 16 & (int) byte.MaxValue);
              numArray3[index3] = (byte) num10;
              byte[] numArray4 = pixels;
              int index4 = num9;
              int num11 = num9 + 1;
              int num12 = (int) (sbyte) (numArray1[index1] >> 8 & (int) byte.MaxValue);
              numArray4[index4] = (byte) num12;
              byte[] numArray5 = pixels;
              int index5 = num11;
              num4 = num11 + 1;
              int num13 = (int) (sbyte) (numArray1[index1] & (int) byte.MaxValue);
              numArray5[index5] = (byte) num13;
              if (this.encodeAlpha)
              {
                byte[] numArray2 = pixels;
                int index2 = num4;
                ++num4;
                int num8 = (int) (sbyte) (numArray1[index1] >> 24 & (int) byte.MaxValue);
                numArray2[index2] = (byte) num8;
              }
              int num14 = index1;
              int num15 = this.width;
              int num16 = -1;
              if ((num15 != num16 ? num14 % num15 : 0) == this.width - 1 && this.filter != 0)
              {
                if (this.filter == 1)
                  this.filterSub(pixels, startPos, this.width);
                if (this.filter == 2)
                  this.filterUp(pixels, startPos, this.width);
              }
            }
            deflaterOutputStream.write(pixels, 0, num4);
            num2 += num3;
            num1 -= num3;
          }
        }
        catch (IOException ex)
        {
          int num4 = 1;
          ioException2 = (IOException) ByteCodeHelper.MapException<IOException>((Exception) ex, (ByteCodeHelper.MapFlags) num4);
          goto label_32;
        }
      }
      IOException ioException3;
      try
      {
        System.get_err().println("interrupted waiting for pixels!");
        return false;
      }
      catch (IOException ex)
      {
        int num3 = 1;
        ioException3 = (IOException) ByteCodeHelper.MapException<IOException>((Exception) ex, (ByteCodeHelper.MapFlags) num3);
      }
      IOException ioException4 = ioException3;
      goto label_37;
label_8:
      ioException4 = ioException1;
      goto label_37;
label_32:
      ioException4 = ioException2;
      goto label_37;
label_33:
      int num17;
      IOException ioException5;
      try
      {
        deflaterOutputStream.close();
        byte[] data = arrayOutputStream.toByteArray();
        int length = data.Length;
        this.crc.reset();
        this.bytePos = this.writeInt4(length, this.bytePos);
        this.bytePos = this.writeBytes(PngEncoder.__\u003C\u003EIDAT, this.bytePos);
        this.crc.update(PngEncoder.__\u003C\u003EIDAT);
        this.bytePos = this.writeBytes(data, length, this.bytePos);
        this.crc.update(data, 0, length);
        this.crcValue = this.crc.getValue();
        this.bytePos = this.writeInt4((int) this.crcValue, this.bytePos);
        deflater.finish();
        deflater.end();
        num17 = 1;
      }
      catch (IOException ex)
      {
        int num3 = 1;
        ioException5 = (IOException) ByteCodeHelper.MapException<IOException>((Exception) ex, (ByteCodeHelper.MapFlags) num3);
        goto label_36;
      }
      return num17 != 0;
label_36:
      ioException4 = ioException5;
label_37:
      System.get_err().println(Throwable.instancehelper_toString((Exception) ioException4));
      return false;
    }