Exemplo n.º 1
0
        public override void PixBltPixelBuffer24(GDIDIBSection fPixMap, int x, int y)
        {
            MemoryStream ms = new MemoryStream();
            PixelAccessorBGRb accessor = new PixelAccessorBGRb(fPixMap.Width, fPixMap.Height, fPixMap.Orientation, fPixMap.Pixels);

            // 2. Run length encode the image to a memory stream
            NewTOAPIA.Imaging.TargaRunLengthCodec rlc = new NewTOAPIA.Imaging.TargaRunLengthCodec();
            rlc.Encode(accessor, ms);

            // 3. Get the bytes from the stream
            byte[] imageBytes = ms.GetBuffer();
            int dataLength = (int)imageBytes.Length;

            // 4. Allocate a buffer chunk to accomodate the bytes, plus some more
            BufferChunk chunk = new BufferChunk(dataLength + 128);

            // 5. Put the command, destination, and data size into the buffer first
            chunk += (int)UICommands.PixBltRLE;
            ChunkUtils.Pack(chunk, x, y);
            ChunkUtils.Pack(chunk, accessor.Width, accessor.Height);
            chunk += dataLength;

            // 6. Put the image bytes into the chunk
            chunk += imageBytes;


            // 6. Finally, send the packet
            SendCommand(chunk);
        }
Exemplo n.º 2
0
        void fCamera_NewFrame(object sender, CameraEventArgs e)
        {
            // Wrap the frame up in a PixelAccessor
            PixelAccessorBGRb accessor = new PixelAccessorBGRb(e.Width, e.Height, PixmapOrientation.BottomToTop, e.fData, e.Width*3);

            fAttendee.SendVideoFrame(accessor);
        }
Exemplo n.º 3
0
        void ReceiveFrameFromCamera(object sender, CameraEventArgs camEvent)
        {
            // Wrap the frame up in a PixelAccessor
            PixelAccessorBGRb accessor = new PixelAccessorBGRb(camEvent.Width, camEvent.Height, PixmapOrientation.BottomToTop, camEvent.fData, camEvent.Width * 3);

            // Tell whomever is listening that we've received a video event
            if (null != ReceivedVideoFrameEvent)
                ReceivedVideoFrameEvent(accessor);

            SendVideoFrame(accessor);
        }
Exemplo n.º 4
0
        public PictureWindow(int locx, int locy)
            : base("Run Length Encoding Viewer", locx, locy, 640, 480)
        {
            //picture = NewTOAPIA.Drawing.TargaLoader.CreatePixelDataFromFile("marbles.tga");
            //picture = NewTOAPIA.Drawing.TargaLoader.CreatePixelDataFromFile("xing_b24.tga");
            //picture = NewTOAPIA.Drawing.TargaLoader.CreatePixelDataFromFile("xing_b32.tga");
            //picture = NewTOAPIA.Drawing.TargaLoader.CreatePixelDataFromFile("xing_t24.tga");
            //picture = NewTOAPIA.Drawing.TargaLoader.CreatePixelDataFromFile("xing_t32.tga");
            //picture = NewTOAPIA.Drawing.TargaLoader.CreatePixelDataFromFile("ctc24.tga");
            //picture = NewTOAPIA.Drawing.TargaLoader.CreatePixelDataFromFile("flag_t32.tga");
            //picture = NewTOAPIA.Drawing.TargaLoader.CreatePixelDataFromFile("flag_b32.tga");
            //picture = NewTOAPIA.Drawing.TargaLoader.CreatePixelDataFromFile("utc32.tga");
            //picture = NewTOAPIA.Drawing.TargaLoader.CreatePixelDataFromFile("utc24.tga");
            //picture = CreateColorTestImage(40,40);
            //picture = CreateSteppedTestImage(128, 32);
            picture = CreateBarsTestImage(10);

            // Perform the runlength encoding on the image
            pixMap = new PixelArray<BGRb>(picture);

            RLC rlc = new RLC();
            //NewTOAPIA.Imaging.RunLengthCodec rlc = new RunLengthCodec();
            MemoryStream ms = new MemoryStream();

            rlc.Encode(new PixelAccessorBGRb(pixMap), ms);
            byte[] imageBytes = ms.GetBuffer();


            //Console.WriteLine("Image Bytes: {0}", imageBytes.Length);

            // Now decode the bytes back into a new image
            ms.Seek(0, SeekOrigin.Begin);

            tPic = new PixelArray<BGRb>(pixMap.Width, pixMap.Height, pixMap.Orientation, new PixelInformation(PixelLayout.Bgr, PixelComponentType.Byte));
            PixelAccessorBGRb accessor = new PixelAccessorBGRb(tPic);
            rlc.Decode(ms, accessor);
        }
Exemplo n.º 5
0
        public static IPixelArray CreatePixelDataFromFile(string filename)
        {
            int tgaSize;
            bool isExtendedFile;

            // Open the file.
            if ((null == filename) || (string.Empty == filename))
                return null;

            FileStream filestream = new FileStream(filename, FileMode.Open, FileAccess.Read);
            BinaryReader reader = new BinaryReader(filestream);

            // Targa images come in many different formats, and there are a couple of different versions
            // of the specification.
            // First thing to do is determine if the file is adhereing to version 2.0 of the spcification.
            // We do that by reading a 'footer', which is the last 26 bytes of the file.
            long fileLength = filestream.Length;
            long seekPosition = filestream.Seek(fileLength - 26, SeekOrigin.Begin);
            byte[] targaFooterBytes = reader.ReadBytes(26);
            string targaXFileID = "TRUEVISION-XFILE";
            string targaFooterSignature = System.Text.ASCIIEncoding.ASCII.GetString(targaFooterBytes, 8, 16);
            TargaFooter footer = null;
            isExtendedFile = (0 == string.Compare(targaFooterSignature, targaXFileID));
            if (isExtendedFile)
            {
                // Since we now know it's an extended file, 
                // we'll create the footer object and fill
                // in the details.
                footer = new TargaFooter();

                // Of the 26 bytes we read from the end of the file
                // the bytes are layed out as follows.
                //Bytes 0-3: The Extension Area Offset
                //Bytes 4-7: The Developer Directory Offset
                //Bytes 8-23: The Signature
                //Byte 24: ASCII Character “.”
                //Byte 25: Binary zero string terminator (0x00)

                // We take those raw bytes, and turn them into meaningful fields
                // in the footer object.
                footer.ExtensionAreaOffset = BitConverter.ToInt32(targaFooterBytes, 0);
                footer.DeveloperDirectoryOffset = BitConverter.ToInt32(targaFooterBytes, 4);
                footer.Signature = targaFooterSignature;
                footer.Period = (byte)'.';
                footer.BinaryZero = 0;

            }

            TargaHeader fHeader = new TargaHeader();

            // If you want to use unsafe code, you could do the following
            // there are two primary drawbacks.
            // 1. The targa image data is in Little-Endian format.  On platforms
            // that are big-endian, the data will not necessarily show up correctly in the header.
            // 2. You must compile with unsafe code.  That may or may not be a problem depending
            // on the application, but it really isn't a necessity.
            // 
            // The speed gain may not be realized, so there's really no reason for the added complexity.
            // First, just reader enough of bytes from the file to capture the 
            // header into a chunk of memory.
            //byte[] headerBytes = reader.ReadBytes(Marshal.SizeOf(fHeader));
            //IntPtr headerPtr;

            //unsafe
            //{
            //    GCHandle dataHandle = GCHandle.Alloc(headerBytes, GCHandleType.Pinned);
            //    try
            //    {
            //        headerPtr = (IntPtr)dataHandle.AddrOfPinnedObject();

            //        // Now use the marshaller to copy the header bytes into a structure
            //        fHeader = (TargaHeader)Marshal.PtrToStructure(headerPtr, typeof(TargaHeader));

            //    }
            //    finally
            //    {
            //        dataHandle.Free();
            //    }
            //}

            filestream.Seek(0, SeekOrigin.Begin);
            fHeader.IDLength = reader.ReadByte();
            fHeader.ColorMapType = (TargaColorMapType)reader.ReadByte();
            fHeader.ImageType = (TargaImageType)reader.ReadByte();
            fHeader.CMapStart = reader.ReadInt16();
            fHeader.CMapLength = reader.ReadInt16();
            fHeader.CMapDepth = reader.ReadByte();


            // Image description
            fHeader.XOffset = reader.ReadInt16();
            fHeader.YOffset = reader.ReadInt16();
            fHeader.Width = reader.ReadInt16();         // Width of image in pixels
            fHeader.Height = reader.ReadInt16();        // Height of image in pixels
            fHeader.PixelDepth = reader.ReadByte();     // How many bits per pixel
            fHeader.ImageDescriptor = reader.ReadByte();


            //              Image Descriptor Byte.                        |
            //  Bits 3-0 - number of attribute bits associated with each  |
            //               pixel.  For the Targa 16, this would be 0 or |
            //               1.  For the Targa 24, it should be 0.  For   |
            //               Targa 32, it should be 8.                    |
            //  Bit 4    - controls left/right transfer of pixels to 
            ///             the screen.
            ///             0 = left to right
            ///             1 = right to left
            //  Bit 5    - controls top/bottom transfer of pixels to 
            ///             the screen.
            ///             0 = bottom to top
            ///             1 = top to bottom
            ///             
            ///             In Combination bits 5/4, they would have these values
            ///             00 = bottom left
            ///             01 = bottom right
            ///             10 = top left
            ///             11 = top right
            ///             
            //  Bits 7-6 - Data storage interleaving flag.                |
            //             00 = non-interleaved.                          |
            //             01 = two-way (even/odd) interleaving.          |
            //             10 = four way interleaving.                    |
            //             11 = reserved.         

            byte desc = fHeader.ImageDescriptor;
            byte attrBits = (byte)(desc & 0x0F);
            ImageOrigin origin = (ImageOrigin)(desc & (byte)ImageOrigin.OriginMask);
            byte interleave = (byte)((desc & 0xC0) >> 6);


            // This routine can only deal with the uncompressed image types.
            // So, fail if this is not the case.
            if ((TargaImageType.TrueColor != fHeader.ImageType) && (TargaImageType.Monochrome != fHeader.ImageType))
            {
                filestream.Close();
                return null;
            }


            PixmapOrientation pixmapOrientation = PixmapOrientation.BottomToTop;
            if ((ImageOrigin.BottomLeft == origin) || (ImageOrigin.BottomRight == origin))
                pixmapOrientation = PixmapOrientation.BottomToTop;
            else
                pixmapOrientation = PixmapOrientation.TopToBottom;

            int bytesPerPixel = fHeader.PixelDepth / 8;


            // Skip past the Image Identification field
            byte[] ImageIdentification;
            if (fHeader.IDLength > 0)
                ImageIdentification = reader.ReadBytes(fHeader.IDLength);

            // calculate image size based on bytes per pixel, width and height.
            tgaSize = fHeader.Width * fHeader.Height * bytesPerPixel;
            byte[] imageData = reader.ReadBytes((int)tgaSize);
            filestream.Close();

            // Pin the array in mememory, and get a data pointer to it
            IntPtr dataPtr;

            unsafe
            {
                GCHandle dataHandle = GCHandle.Alloc(imageData, GCHandleType.Pinned);
                dataPtr = (IntPtr)dataHandle.AddrOfPinnedObject();

                // Create the appropriate PixelArray
                // then create an accessor to match
                // Copy the data from the buffer pointer to the new array
                switch (bytesPerPixel)
                {
                    case 3:
                        {
                            PixelAccessorBGRb srcAccess = new PixelAccessorBGRb(fHeader.Width, fHeader.Height, pixmapOrientation, dataPtr);
                            PixelArray<BGRb> pixmap = new PixelArray<BGRb>(srcAccess);

                            return pixmap;
                        }
                        break;

                    case 4:
                        {
                            PixelAccessorBGRAb srcAccess = new PixelAccessorBGRAb(fHeader.Width, fHeader.Height, pixmapOrientation, dataPtr);
                            PixelArray<BGRAb> pixmap = new PixelArray<BGRAb>(srcAccess);

                            return pixmap;
                        }
                        break;

                    case 1:
                        {
                            PixelAccessorLumb srcAccess = new PixelAccessorLumb(fHeader.Width, fHeader.Height, pixmapOrientation, dataPtr);
                            PixelArray<Lumb> pixmap = new PixelArray<Lumb>(srcAccess);

                            return pixmap;
                        }
                        break;
                }
            }

            return null;
        }
Exemplo n.º 6
0
        public virtual void ReceiveChunk(BufferChunk aRecord)
        {
            // First read out the record type
            int recordType = aRecord.NextInt32();

            // Then deserialize the rest from there
            switch (recordType)
            {

                //case (int)UICommands.PixBlt:
                //    {
                //        // Get the X, Y
                //        int x = aRecord.NextInt32();
                //        int y = aRecord.NextInt32();

                //        // get the length of the image bytes buffer
                //        int dataSize = aRecord.NextInt32();

                //        byte[] imageBytes = (byte[])aRecord;

                //        // Create a memory stream from the imageBytes
                //        MemoryStream ms = new MemoryStream(imageBytes, false);

                //        // Create a pixelArray from the stream
                //        Bitmap bm = (Bitmap)Bitmap.FromStream(ms);

                //        PixelArray<BGRAb> pixMap = PixelBufferHelper.CreatePixelArrayFromBitmap(bm);
                        
                //        // And finally, call the PixBlt function
                //        PixBltBGRAb(pixMap, x, y);
                //    }
                //    break;

                case (int)UICommands.PixBltRLE:
                    {
                        // Get the X, Y
                        int x = aRecord.NextInt32();
                        int y = aRecord.NextInt32();
                        int width = aRecord.NextInt32();
                        int height = aRecord.NextInt32();

                        // get the length of the image bytes buffer
                        int dataSize = aRecord.NextInt32();

                        byte[] imageBytes = (byte[])aRecord;

                        // Create a memory stream from the imageBytes
                        MemoryStream ms = new MemoryStream(imageBytes, false);

                        // Create a pixelArray from the stream
                        if ((width != fPixMap.Width) || (height != fPixMap.Height))
                            fPixMap = new GDIDIBSection(width, height);

                        TargaRunLengthCodec rlc = new TargaRunLengthCodec();
                        PixelAccessorBGRb accessor = new PixelAccessorBGRb(fPixMap.Width, fPixMap.Height, fPixMap.Orientation, fPixMap.Pixels, fPixMap.BytesPerRow);
                        rlc.Decode(ms, accessor);

                        // And finally, call the local PixBlt function
                        PixBltPixelBuffer24(fPixMap, x, y);
                    }
                    break;

                case (int)UICommands.PixBltLuminance:
                    {
                        // Get the X, Y
                        int x = aRecord.NextInt32();
                        int y = aRecord.NextInt32();
                        int width = aRecord.NextInt32();
                        int height = aRecord.NextInt32();

                        // get the length of the image bytes buffer
                        int dataSize = aRecord.NextInt32();

                        byte[] imageBytes = (byte[])aRecord;

                        // Create a memory stream from the imageBytes
                        MemoryStream ms = new MemoryStream(imageBytes, false);

                        // Create a pixelArray from the stream
                        if ((width != fGrayImage.Width) || (height != fGrayImage.Height))
                            fGrayImage = new PixelArray<Lumb>(width, height);

                        TargaLuminanceRLE rlc = new TargaLuminanceRLE();
                        rlc.Decode(ms, fGrayImage);

                        // And finally, call the local PixBlt function
                        PixBltLumb(fGrayImage, x, y);
                    }
                    break;

                case (int)UICommands.Showcursor:
                    {
                        ShowCursor();
                    }
                    break;

                case (int)UICommands.HideCursor:
                    {
                        HideCursor();
                    }
                    break;

                case (int)UICommands.MoveCursor:
                    {
                        int x = aRecord.NextInt32();
                        int y = aRecord.NextInt32();

                        MoveCursor(x, y);
                    }
                    break;



                default:
                    //if (CommandReceived != null)
                    //    CommandReceived(aRecord);
                    break;
            }
        }
Exemplo n.º 7
0
 public virtual void PixBltPixelBuffer24(GDIDIBSection pixMap, int x, int y)
 {
     PixelAccessorBGRb accessor = new PixelAccessorBGRb(pixMap.Width, pixMap.Height, pixMap.Orientation, pixMap.Pixels, pixMap.Width * 3);
     fDynamicTexture.UpdateImage(accessor);
 }
Exemplo n.º 8
0
        public void UpdateImage(PixelAccessorBGRb accessor)
        {
            latestFrame++;

            if (accessor.Width != Width || accessor.Height != Height)
            {
                newWidth = accessor.Width;
                newHeight = accessor.Height;
                newPixels = accessor.Pixels;
                fNeedsResize = true;

                return;
            }

            fPixelPtr = accessor.Pixels;
        }
Exemplo n.º 9
0
 void fCameraSender_ReceivedVideoFrameEvent(PixelAccessorBGRb accessor)
 {
     //ClientAreaGraphPort.PixBlt(accessor, 10, 10);
 }