AddrOfPinnedObject() private method

private AddrOfPinnedObject ( ) : IntPtr
return System.IntPtr
コード例 #1
0
        private static void EnsureFontLoaded()
        {
            if (_FontAwesome == null)
            {
                _PrivateFontCollection = new PrivateFontCollection();

                byte[] fontAwesomeBuffer = BarFunctions.LoadFont("SystemImages.FontAwesome.ttf");
                _FontAwesomeHandle = GCHandle.Alloc(fontAwesomeBuffer, GCHandleType.Pinned);
                _PrivateFontCollection.AddMemoryFont(_FontAwesomeHandle.AddrOfPinnedObject(), fontAwesomeBuffer.Length);
                uint rsxCnt = 1;
                _FontAwesomePointer = AddFontMemResourceEx(_FontAwesomeHandle.AddrOfPinnedObject(),
                                                     (uint)fontAwesomeBuffer.Length, IntPtr.Zero, ref rsxCnt);
                using (FontFamily ff = _PrivateFontCollection.Families[0])
                {
                    if (ff.IsStyleAvailable(FontStyle.Regular))
                    {
                        _FontAwesome = new Font(ff, _FontAwesomeDefaultSize, FontStyle.Regular, GraphicsUnit.Point);
                        _FontAwesomeCache.Add(_FontAwesomeDefaultSize, _FontAwesome);
                    }
                    else
                    {
                        // Error use default font...
                        _FontAwesome = SystemInformation.MenuFont;
                    }
                }
            }
        }
コード例 #2
0
ファイル: FFTWtest.cs プロジェクト: pacificIT/FFTWSharp
        // Initializes FFTW and all arrays
        // n: Logical size of the transform
        public FFTWtest(int n)
        {
            System.Console.WriteLine("Starting test with n = " + n + " complex numbers");
            fftLength = n;

            // create two unmanaged arrays, properly aligned
            pin = fftwf.malloc(n * 8);
            pout = fftwf.malloc(n * 8);

            // create two managed arrays, possibly misalinged
            // n*2 because we are dealing with complex numbers
            fin = new float[n * 2];
            fout = new float[n * 2];
            // and two more for double FFTW
            din = new double[n * 2];
            dout = new double[n * 2];

            // get handles and pin arrays so the GC doesn't move them
            hin = GCHandle.Alloc(fin, GCHandleType.Pinned);
            hout = GCHandle.Alloc(fout, GCHandleType.Pinned);
            hdin = GCHandle.Alloc(din, GCHandleType.Pinned);
            hdout = GCHandle.Alloc(dout, GCHandleType.Pinned);

            // create a few test transforms
            fplan1 = fftwf.dft_1d(n, pin, pout, fftw_direction.Forward, fftw_flags.Estimate);
            fplan2 = fftwf.dft_1d(n, hin.AddrOfPinnedObject(), hout.AddrOfPinnedObject(),
                fftw_direction.Forward, fftw_flags.Estimate);
            fplan3 = fftwf.dft_1d(n, hout.AddrOfPinnedObject(), pin,
                fftw_direction.Backward, fftw_flags.Measure);
            // end with transforming back to original array
            fplan4 = fftwf.dft_1d(n, hout.AddrOfPinnedObject(), hin.AddrOfPinnedObject(),
                fftw_direction.Backward, fftw_flags.Estimate);
            // and check a quick one with doubles, just to be sure
            fplan5 = fftw.dft_1d(n, hdin.AddrOfPinnedObject(), hdout.AddrOfPinnedObject(),
                fftw_direction.Backward, fftw_flags.Measure);

            // create a managed plan as well
            min = new fftw_complexarray(din);
            mout = new fftw_complexarray(dout);
            mplan = fftw_plan.dft_1d(n, min, mout, fftw_direction.Forward, fftw_flags.Estimate);

            // fill our arrays with an arbitrary complex sawtooth-like signal
            for (int i = 0; i < n * 2; i++)
                fin[i] = i % 50;
            for (int i = 0; i < n * 2; i++)
                fout[i] = i % 50;
            for (int i = 0; i < n * 2; i++)
                din[i] = i % 50;

            // copy managed arrays to unmanaged arrays
            Marshal.Copy(fin, 0, pin, n * 2);
            Marshal.Copy(fout, 0, pout, n * 2);
        }
コード例 #3
0
ファイル: WSARecvCall.cs プロジェクト: shhsu/corefxlab
        private int Invoke(SOCKET socket)
        {
            _recvOverlapped.hEvent = SocketImports.WSACreateEvent();
            if (_recvOverlapped.hEvent == IntPtr.Zero)
            {
                // dead, close socket?
                return -1;
            }

            _gcWSABuffer = GCHandle.Alloc(_WSABuffer, GCHandleType.Pinned);
            var lpWSABuffer = (WSABuffer*)_gcWSABuffer.AddrOfPinnedObject();

            _gcRecvOverlapped = GCHandle.Alloc(_recvOverlapped, GCHandleType.Pinned);
            var plRecvOverlapped = (WSAOverlapped*)_gcRecvOverlapped.AddrOfPinnedObject();

            int rc = SocketImports.WSARecv(socket, lpWSABuffer, 1, out _bytesTransferred, ref _socketFlags, plRecvOverlapped, IntPtr.Zero);

            _gcWSABuffer.Free();
            _gcRecvOverlapped.Free();

            if (rc == SocketImports.SOCKET_ERROR)
            {
                int err = SocketImports.WSAGetLastError();
                if (SocketImports.WSA_IO_PENDING != err)
                {
                    Console.WriteLine("WSARecv failed with error: {0}/{1}", rc, err);
                }
            }

            return rc;
        }
コード例 #4
0
 public ColorCodec(RGBAPixel[] pixels)
 {
     _srcCount = pixels.Length;
     _handle = GCHandle.Alloc(pixels, GCHandleType.Pinned);
     _pData = (RGBAPixel*)_handle.AddrOfPinnedObject();
     Evaluate();
 }
コード例 #5
0
        public AVPacket()
        {
            AutoGen.AVPacket packet = new AutoGen.AVPacket();
            _handle = GCHandle.Alloc(packet, GCHandleType.Pinned);

            _packet = (AutoGen.AVPacket*)_handle.AddrOfPinnedObject().ToPointer();
        }
コード例 #6
0
 public FpuRegisters()
 {
     m_Regs = new ulong[32];
     m_RegHandle = GCHandle.Alloc(m_Regs, GCHandleType.Pinned);
     m_RegsPtr = m_RegHandle.AddrOfPinnedObject();
     /* TODO: Free handle on dispose */
 }
コード例 #7
0
ファイル: SoundEffect.Android.cs プロジェクト: cg123/xenko
        private unsafe void AdaptAudioDataImpl()
        {
            var isStereo = WaveFormat.Channels == 2;
            
            // allocate the new audio buffer 
            var sampleRateRatio = Math.Min(1, WaveFormat.SampleRate / (float)SoundEffectInstance.SoundEffectInstanceFrameRate); // we don't down-sample in current version
            var newWaveDataSize = (int)Math.Floor(WaveDataSize / sampleRateRatio) * (isStereo ? 1 : 2);
            WaveDataArray = new byte[newWaveDataSize];

            fixed (byte* pNewWaveData = WaveDataArray)
            {
                // re-sample the audio data
                if (Math.Abs(sampleRateRatio - 1f) < MathUtil.ZeroTolerance && !isStereo)
                    DuplicateTracks(WaveDataPtr, (IntPtr)pNewWaveData, newWaveDataSize);
                else if (Math.Abs(sampleRateRatio - 0.5f) < MathUtil.ZeroTolerance)
                    UpSampleByTwo(WaveDataPtr, (IntPtr)pNewWaveData, newWaveDataSize, WaveFormat.Channels, !isStereo);
                else
                    UpSample(WaveDataPtr, (IntPtr)pNewWaveData, newWaveDataSize, sampleRateRatio, WaveFormat.Channels, !isStereo);
            }

            // update the wave data buffer
            pinnedWaveData = GCHandle.Alloc(WaveDataArray, GCHandleType.Pinned);
            WaveDataPtr = pinnedWaveData.AddrOfPinnedObject();
            WaveDataSize = newWaveDataSize;
            
            // Free unused anymore C# data buffer
            Utilities.FreeMemory(nativeDataBuffer);
            nativeDataBuffer = IntPtr.Zero;
        }
コード例 #8
0
 public MemoryPoolSlab(byte[] data)
 {
     _data = data;
     _gcHandle = GCHandle.Alloc(data, GCHandleType.Pinned);
     _nativePointer = _gcHandle.AddrOfPinnedObject();
     _isActive = true;
 }
コード例 #9
0
ファイル: PinnedMetadata.cs プロジェクト: CAPCHIK/roslyn
 public unsafe PinnedMetadata(ImmutableArray<byte> metadata)
 {
     _bytes = GCHandle.Alloc(metadata.DangerousGetUnderlyingArray(), GCHandleType.Pinned);
     this.Pointer = _bytes.AddrOfPinnedObject();
     this.Size = metadata.Length;
     this.Reader = new MetadataReader((byte*)this.Pointer, this.Size, MetadataReaderOptions.None, null);
 }
コード例 #10
0
        /// <summary>
        ///   Constructs a new Integral image of the given size.
        /// </summary>
        /// 
        protected IntegralImage2(int width, int height, bool computeTilted)
        {
            this.width = width;
            this.height = height;

            this.nWidth = width + 1;
            this.nHeight = height + 1;

            this.tWidth = width + 2;
            this.tHeight = height + 2;

            this.nSumImage = new int[nHeight, nWidth];
            this.nSumHandle = GCHandle.Alloc(nSumImage, GCHandleType.Pinned);
            this.nSum = (int*)nSumHandle.AddrOfPinnedObject().ToPointer();

            this.sSumImage = new int[nHeight, nWidth];
            this.sSumHandle = GCHandle.Alloc(sSumImage, GCHandleType.Pinned);
            this.sSum = (int*)sSumHandle.AddrOfPinnedObject().ToPointer();

            if (computeTilted)
            {
                this.tSumImage = new int[tHeight, tWidth];
                this.tSumHandle = GCHandle.Alloc(tSumImage, GCHandleType.Pinned);
                this.tSum = (int*)tSumHandle.AddrOfPinnedObject().ToPointer();
            }
        }
コード例 #11
0
 public unsafe DecomposedResult(int maxInstructions)
 {
     MaxInstructions = maxInstructions;
       _instMem = new byte[maxInstructions * sizeof(DecomposedInstructionStruct)];
       _gch = GCHandle.Alloc(_instMem, GCHandleType.Pinned);
       _instructionsPointer = (DecomposedInstructionStruct *)_gch.AddrOfPinnedObject();
 }
コード例 #12
0
        public override void CommitToSolver()
        {
            if (skinIndices != null && skinPoints != null && skinNormals != null && skinRadiiBackstops != null && skinStiffnesses != null){

                Oni.UnpinMemory(skinIndicesHandle);
                Oni.UnpinMemory(skinPointsHandle);
                Oni.UnpinMemory(skinNormalsHandle);
                Oni.UnpinMemory(skinRadiiBackstopsHandle);
                Oni.UnpinMemory(skinStiffnessesHandle);

                skinIndicesHandle = Oni.PinMemory(skinIndices);
                skinPointsHandle = Oni.PinMemory(skinPoints);
                skinNormalsHandle = Oni.PinMemory(skinNormals);
                skinRadiiBackstopsHandle = Oni.PinMemory(skinRadiiBackstops);
                skinStiffnessesHandle = Oni.PinMemory(skinStiffnesses);

                Oni.SetSkinConstraints(solver.Solver,
                                       skinIndicesHandle.AddrOfPinnedObject(),
                                       skinPointsHandle.AddrOfPinnedObject(),
                                       skinNormalsHandle.AddrOfPinnedObject(),
                                       skinRadiiBackstopsHandle.AddrOfPinnedObject(),
                                       skinStiffnessesHandle.AddrOfPinnedObject());

                CommitActive();
            }
        }
コード例 #13
0
 protected override void Initialize()
 {
     optionsgchandle = GCHandle.Alloc(options, GCHandleType.Pinned);
     opaque = optionsgchandle.AddrOfPinnedObject();
     hoedown_html_toc_renderer_new(ref callbacks, opaque, 0);
     base.Initialize();
 }
コード例 #14
0
ファイル: Common.cs プロジェクト: giawa/ee2015presentation
        public static VBO<Vector3> BufferData(VBO<Vector3> vbo, Vector3[] data, GCHandle handle)
        {
            if (vbo == null) return new VBO<Vector3>(data, BufferTarget.ArrayBuffer, BufferUsageHint.StaticDraw);

            vbo.BufferSubDataPinned(BufferTarget.ArrayBuffer, 12 * data.Length, handle.AddrOfPinnedObject());
            return vbo;
        }
コード例 #15
0
        /// <summary>
        /// imageHelper Constructor passing in width and height and whether we want to tilt the feature
        /// </summary>
        /// <param name="pWidth"></param>
        /// <param name="pHeight"></param>
        /// <param name="pComputeTilted"></param>
        protected ImageHelper(int pWidth, int pHeight, bool pComputeTilted)
        {
            // figure out the width and height for each
            this.iWidth = pWidth;
            this.iHeight = pHeight;

            this.nWidth = pWidth + 1;
            this.nHeight = pHeight + 1;

            this.tWidth = pWidth + 2;
            this.tHeight = pHeight + 2;

            // grab the image and create a GCHandle pointer to the memory for that image
            this.integralImage = new int[nHeight, nWidth];
            this.intPointer = GCHandle.Alloc(integralImage, GCHandleType.Pinned);
            this.intImage = (int*)intPointer.AddrOfPinnedObject().ToPointer();

            this.squaredIntImage = new int[nHeight, nWidth];
            this.squaredPointer = GCHandle.Alloc(squaredIntImage, GCHandleType.Pinned);
            this.squaredImage = (int*)squaredPointer.AddrOfPinnedObject().ToPointer();

            if (pComputeTilted)
            {
                this.tiltedIntImage = new int[tHeight, tWidth];
                this.tiltedPointer = GCHandle.Alloc(tiltedIntImage, GCHandleType.Pinned);
                this.tiltedImage = (int*)tiltedPointer.AddrOfPinnedObject().ToPointer();
            }
        }
コード例 #16
0
ファイル: PSP.cs プロジェクト: henke37/BizHawk
		public PSP(CoreComm comm, string isopath)
		{
			ServiceProvider = new BasicServiceProvider(this);
			if (attachedcore != null)
			{
				attachedcore.Dispose();
				attachedcore = null;
			}
			CoreComm = comm;

			logcallback = new PPSSPPDll.LogCB(LogCallbackFunc);

			bool good = PPSSPPDll.init(isopath, logcallback);
			LogFlush();
			if (!good)
				throw new Exception("PPSSPP Init failed!");
			vidhandle = GCHandle.Alloc(screenbuffer, GCHandleType.Pinned);
			PPSSPPDll.setvidbuff(vidhandle.AddrOfPinnedObject());

			CoreComm.VsyncDen = 1;
			CoreComm.VsyncNum = 60;
			CoreComm.RomStatusDetails = "It puts the scythe in the chicken or it gets the abyss again!";

			attachedcore = this;
		}
コード例 #17
0
ファイル: Program.cs プロジェクト: blombas/StructuredSight
        static void Main(string[] args)
        {
            Console.Write("Enter Passphrase: ");
            using (var passphrase = GetPassphraseFromConsole())
            {
                Console.WriteLine("Your password:"******"Exception thrown.");
                            }

                            Marshal.ZeroFreeBSTR(ptr);

                            for (var ii = 0; ii < arPass.Length; ii++)
                            {
                                arPass[ii] = '\0';
                            }

                            handle.Free();

                        },
                            null
                        );
                }
            }
        }
コード例 #18
0
ファイル: WGScript.cs プロジェクト: Bzdun/NewProject
        public void Execute()
        {
            // Create GS Instance (GS-API)
            gsapi_new_instance(out _gsInstancePtr, IntPtr.Zero);
            // Build Argument Arrays
            _gsArgStrHandles = new GCHandle[_gsParams.Count];
            _gsArgPtrs = new IntPtr[_gsParams.Count];

            // Populate Argument Arrays
            for (int i = 0; i < _gsParams.Count; i++)
            {
                _gsArgStrHandles[i] = GCHandle.Alloc(System.Text.ASCIIEncoding.ASCII.GetBytes(_gsParams[i].ToString()), GCHandleType.Pinned);
                _gsArgPtrs[i] = _gsArgStrHandles[i].AddrOfPinnedObject();
            }

            // Allocate memory that is protected from Garbage Collection
            _gsArgPtrsHandle = GCHandle.Alloc(_gsArgPtrs, GCHandleType.Pinned);
            // Init args with GS instance (GS-API)
            gsapi_init_with_args(_gsInstancePtr, _gsArgStrHandles.Length, _gsArgPtrsHandle.AddrOfPinnedObject());
            // Free unmanaged memory
            for (int i = 0; i < _gsArgStrHandles.Length; i++)
                _gsArgStrHandles[i].Free();
            _gsArgPtrsHandle.Free();

            // Exit the api (GS-API)
            gsapi_exit(_gsInstancePtr);
            // Delete GS Instance (GS-API)
            gsapi_delete_instance(_gsInstancePtr);
        }
コード例 #19
0
ファイル: MatDataAllocator.cs プロジェクト: Delaley/emgucv
      private IntPtr AllocateData(CvEnum.DepthType type, int channels, int totalInBytes)
      {
         FreeData();

         switch (type)
         {
            //case CvEnum.DepthType.Cv8U:
            //   _data = new byte[totalInBytes];
            //   break;
            case CvEnum.DepthType.Cv8S:
               _data = new SByte[totalInBytes];
               break;
            case CvEnum.DepthType.Cv16U:
               _data = new UInt16[totalInBytes >> 1];
               break;
            case CvEnum.DepthType.Cv16S:
               _data = new Int16[totalInBytes >> 1];
               break;
            case CvEnum.DepthType.Cv32S:
               _data = new Int32[totalInBytes >> 2];
               break;
            case CvEnum.DepthType.Cv32F:
               _data = new float[totalInBytes >> 2];
               break;
            case CvEnum.DepthType.Cv64F:
               _data = new double[totalInBytes >> 3];
               break;
            default:
               _data = new byte[totalInBytes];
               break;
         }

         _dataHandle = GCHandle.Alloc(_data, GCHandleType.Pinned);
         return _dataHandle.AddrOfPinnedObject();
      }
コード例 #20
0
        public MarshalMultipleValueStructure(byte[] key, byte[][] values)
        {
            if (values == null)
                throw new ArgumentNullException(nameof(values));

            _size = GetSize(values);
            _count = GetCount(values);
            _flattened = values.SelectMany(x => x).ToArray();
            _valuesHandle = GCHandle.Alloc(_flattened, GCHandleType.Pinned);

            _key = key;
            _keyHandle = GCHandle.Alloc(_key, GCHandleType.Pinned);

            Values = new[]
            {
                new ValueStructure
                {
                    size = new IntPtr(_size),
                    data = _valuesHandle.AddrOfPinnedObject()
                },
                new ValueStructure
                {
                    size = new IntPtr(_count)
                }
            };

            Key = new ValueStructure
            {
                size = new IntPtr(_key.Length),
                data = _keyHandle.AddrOfPinnedObject()
            };
        }
コード例 #21
0
ファイル: CFDataBuffer.cs プロジェクト: baulig/maccore
        public CFDataBuffer(byte[] buffer)
        {
            this.buffer = buffer;
            this.handle = GCHandle.Alloc (this.buffer, GCHandleType.Pinned);

            data = CFData.FromDataNoCopy (handle.AddrOfPinnedObject (), buffer.Length);
            owns = true;
        }
コード例 #22
0
ファイル: PortablePdbReader.cs プロジェクト: CAPCHIK/roslyn
        internal unsafe static MetadataReader CreateMetadataReader(byte[] buffer, int size, out GCHandle pinnedImage)
        {
            Debug.Assert(buffer != null);
            Debug.Assert(size >= 0 && size <= buffer.Length);

            pinnedImage = GCHandle.Alloc(buffer, GCHandleType.Pinned);
            return new MetadataReader((byte*)pinnedImage.AddrOfPinnedObject(), size);
        }
コード例 #23
0
ファイル: OneSegmentOnlyPool.cs プロジェクト: Scooletz/RampUp
 public OneSegmentOnlyPool()
 {
     _handle = GCHandle.Alloc(_managedBytes, GCHandleType.Pinned);
     var bytes = (byte*) _handle.AddrOfPinnedObject();
     _segment = (Segment*) (bytes + SingleThreadSegmentPool.SegmentSize);
     *_segment = new Segment(bytes, SingleThreadSegmentPool.SegmentSize);
     SegmentLength = SingleThreadSegmentPool.SegmentSize;
 }
コード例 #24
0
      private Int32 m_size = 0;     // The size (in bytes) unless overridden in ctor

      private SafePinnedObject(Object obj, Int32 offsetIntoObject, Int32 size) : base(true) {
         if (obj == null) return;

         // Pin the buffer, set the native address, and save the object's size
         m_gcHandle = GCHandle.Alloc(obj, GCHandleType.Pinned);
         unsafe { SetHandle((IntPtr) ((byte*) m_gcHandle.AddrOfPinnedObject() + offsetIntoObject)); }
         m_size = size;
      }
コード例 #25
0
ファイル: HtmlRenderer.cs プロジェクト: uonun/sundown.net
		protected override void Initialize()
		{
			optionsgchandle = GCHandle.Alloc(options, GCHandleType.Pinned);
			opaque = optionsgchandle.AddrOfPinnedObject();
			sdhtml_renderer(ref callbacks, opaque, flags);

			base.Initialize();
		}
コード例 #26
0
ファイル: BufferPin.cs プロジェクト: txdv/Mono.Interop
		public BufferPin(byte[] buffer, IntPtr offset, IntPtr count)
		{
			Buffer = buffer;
			Offset = (IntPtr)offset;
			Count = (IntPtr)count;
			GCHandle = GCHandle.Alloc(Buffer, GCHandleType.Pinned);
			Pointer = GCHandle.AddrOfPinnedObject();
		}
コード例 #27
0
		protected InternalReaderBase(Encoding encoding, int bufferSize)
		{
			encoding_ = encoding;
			isSingleByte_ = encoding_.GetMaxByteCount(1) == 1;
			buffer_ = new byte[bufferSize];
			handle_ = GCHandle.Alloc(buffer_, GCHandleType.Pinned);
			basePointer_ = handle_.AddrOfPinnedObject();
		}
コード例 #28
0
ファイル: DesFile.cs プロジェクト: Fun33/code
 /// <summary>
 /// 清除記憶體
 /// </summary>
 private void Clear(string sSecretKey)
 {
     // For additional security Pin the key.
     System.Runtime.InteropServices.GCHandle Gch = System.Runtime.InteropServices.GCHandle.Alloc(sSecretKey, System.Runtime.InteropServices.GCHandleType.Pinned);
     // Remove the Key from memory.
     ZeroMemory(Gch.AddrOfPinnedObject(), sSecretKey.Length * 2);
     Gch.Free();
 }
コード例 #29
0
        public void Open(byte[] res_array)
        {
            Debug.Assert(_har == IntPtr.Zero);

            _pinnedArray = GCHandle.Alloc(res_array, GCHandleType.Pinned);
            _har = _api.SciterOpenArchive(_pinnedArray.AddrOfPinnedObject(), (uint) res_array.Length);
            Debug.Assert(_har != IntPtr.Zero);
        }
コード例 #30
0
 public VertexCodec(Vector2[] vertices)
 {
     _srcCount = vertices.Length;
     _srcElements = 2;
     _handle = GCHandle.Alloc(vertices, GCHandleType.Pinned);
     _pData = (float*)_handle.AddrOfPinnedObject();
     Evaluate(false);
 }
コード例 #31
0
 public VertexCodec(Vector3[] vertices, bool removeZ)
 {
     _srcCount = vertices.Length;
     _srcElements = 3;
     _handle = GCHandle.Alloc(vertices, GCHandleType.Pinned);
     _pData = (float*)_handle.AddrOfPinnedObject();
     Evaluate(removeZ);
 }
コード例 #32
0
ファイル: Glu.cs プロジェクト: zero10/scallion
 unsafe void TessVertex(IntPtr tess, double *location, [In, Out] object data)
 {
     System.Runtime.InteropServices.GCHandle data_ptr = System.Runtime.InteropServices.GCHandle.Alloc(data, System.Runtime.InteropServices.GCHandleType.Pinned);
     try
     {
         Delegates.gluTessVertex((IntPtr)tess, (double *)location, (IntPtr)data_ptr.AddrOfPinnedObject());
     }
     finally
     {
         data_ptr.Free();
     }
 }
コード例 #33
0
ファイル: Glu.cs プロジェクト: zero10/scallion
 void NurbsCallbackData(IntPtr nurb, [In, Out] object userData)
 {
     unsafe
     {
         System.Runtime.InteropServices.GCHandle userData_ptr = System.Runtime.InteropServices.GCHandle.Alloc(userData, System.Runtime.InteropServices.GCHandleType.Pinned);
         try
         {
             Delegates.gluNurbsCallbackDataEXT((IntPtr)nurb, (IntPtr)userData_ptr.AddrOfPinnedObject());
         }
         finally
         {
             userData_ptr.Free();
         }
     }
 }
コード例 #34
0
ファイル: Glu.cs プロジェクト: zero10/scallion
 void TessBeginPolygon(IntPtr tess, [In, Out] object data)
 {
     unsafe
     {
         System.Runtime.InteropServices.GCHandle data_ptr = System.Runtime.InteropServices.GCHandle.Alloc(data, System.Runtime.InteropServices.GCHandleType.Pinned);
         try
         {
             Delegates.gluTessBeginPolygon((IntPtr)tess, (IntPtr)data_ptr.AddrOfPinnedObject());
         }
         finally
         {
             data_ptr.Free();
         }
     }
 }
コード例 #35
0
        public static bool GetProcessModules(Process process, out string[] modules)
        {
            System.Collections.Generic.List <string> list = new System.Collections.Generic.List <string>();
            System.IntPtr   hProcess = Process32.OpenProcess(1040u, 0, (uint)process.Id);
            System.IntPtr[] array    = new System.IntPtr[1024];
            uint            num      = 0u;
            uint            cb       = (uint)(System.Runtime.InteropServices.Marshal.SizeOf(typeof(System.IntPtr)) * array.Length);

            System.Runtime.InteropServices.GCHandle gCHandle = System.Runtime.InteropServices.GCHandle.Alloc(array, System.Runtime.InteropServices.GCHandleType.Pinned);
            System.IntPtr lphModule = gCHandle.AddrOfPinnedObject();
            if (Process32.EnumProcessModules(hProcess, lphModule, cb, out num) == 1)
            {
                int num2 = (int)((ulong)num / (ulong)((long)System.Runtime.InteropServices.Marshal.SizeOf(typeof(System.IntPtr))));
                for (int i = 0; i < num2; i++)
                {
                    System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder(1024);
                    Process32.GetModuleFileNameEx(hProcess, array[i], stringBuilder, stringBuilder.Capacity);
                    list.Add(stringBuilder.ToString());
                }
            }
            gCHandle.Free();
            modules = list.ToArray();
            return(true);
        }
コード例 #36
0
        public static Tensor ReadTensorFromImageFile(String fileName, int inputHeight = -1, int inputWidth = -1, float inputMean = 0.0f, float scale = 1.0f, Status status = null)
        {
#if __ANDROID__
            Android.Graphics.Bitmap bmp = BitmapFactory.DecodeFile(fileName);

            if (inputHeight > 0 || inputWidth > 0)
            {
                Bitmap resized = Bitmap.CreateScaledBitmap(bmp, inputWidth, inputHeight, false);
                bmp.Dispose();
                bmp = resized;
            }
            int[]   intValues   = new int[bmp.Width * bmp.Height];
            float[] floatValues = new float[bmp.Width * bmp.Height * 3];
            bmp.GetPixels(intValues, 0, bmp.Width, 0, 0, bmp.Width, bmp.Height);
            for (int i = 0; i < intValues.Length; ++i)
            {
                int val = intValues[i];
                floatValues[i * 3 + 0] = (((val >> 16) & 0xFF) - inputMean) * scale;
                floatValues[i * 3 + 1] = (((val >> 8) & 0xFF) - inputMean) * scale;
                floatValues[i * 3 + 2] = ((val & 0xFF) - inputMean) * scale;
            }

            Tensor t = new Tensor(DataType.Float, new int[] { 1, bmp.Height, bmp.Width, 3 });
            System.Runtime.InteropServices.Marshal.Copy(floatValues, 0, t.DataPointer, floatValues.Length);
            return(t);
#elif __IOS__
            UIImage image = new UIImage(fileName);
            if (inputHeight > 0 || inputWidth > 0)
            {
                UIImage resized = image.Scale(new CGSize(inputWidth, inputHeight));
                image.Dispose();
                image = resized;
            }
            int[]   intValues   = new int[(int)(image.Size.Width * image.Size.Height)];
            float[] floatValues = new float[(int)(image.Size.Width * image.Size.Height * 3)];
            System.Runtime.InteropServices.GCHandle handle = System.Runtime.InteropServices.GCHandle.Alloc(intValues, System.Runtime.InteropServices.GCHandleType.Pinned);
            using (CGImage cgimage = image.CGImage)
                using (CGColorSpace cspace = CGColorSpace.CreateDeviceRGB())
                    using (CGBitmapContext context = new CGBitmapContext(
                               handle.AddrOfPinnedObject(),
                               (nint)image.Size.Width,
                               (nint)image.Size.Height,
                               8,
                               (nint)image.Size.Width * 4,
                               cspace,
                               CGImageAlphaInfo.PremultipliedLast
                               ))
                    {
                        context.DrawImage(new CGRect(new CGPoint(), image.Size), cgimage);
                    }
            handle.Free();

            for (int i = 0; i < intValues.Length; ++i)
            {
                int val = intValues[i];
                floatValues[i * 3 + 0] = (((val >> 16) & 0xFF) - inputMean) * scale;
                floatValues[i * 3 + 1] = (((val >> 8) & 0xFF) - inputMean) * scale;
                floatValues[i * 3 + 2] = ((val & 0xFF) - inputMean) * scale;
            }

            Tensor t = new Tensor(DataType.Float, new int[] { 1, (int)image.Size.Height, (int)image.Size.Width, 3 });
            System.Runtime.InteropServices.Marshal.Copy(floatValues, 0, t.DataPointer, floatValues.Length);
            return(t);
#else
            if (Emgu.TF.Util.Platform.OperationSystem == OS.Windows)
            {
                Tensor t = new Tensor(DataType.Float, new int[] { 1, (int)inputHeight, (int)inputWidth, 3 });
                NativeImageIO.ReadImageFileToTensor(fileName, t.DataPointer, inputHeight, inputWidth, inputMean, scale);
                return(t);
            }
            else
            {
                using (StatusChecker checker = new StatusChecker(status))
                {
                    var       graph = new Graph();
                    Operation input = graph.Placeholder(DataType.String);

                    Operation jpegDecoder = graph.DecodeJpeg(input, 3);                    //dimension 3

                    Operation floatCaster = graph.Cast(jpegDecoder, DstT: DataType.Float); //cast to float

                    Tensor    axis         = new Tensor(0);
                    Operation axisOp       = graph.Const(axis, axis.Type, opName: "axis");
                    Operation dimsExpander = graph.ExpandDims(floatCaster, axisOp); //turn it to dimension [1,3]

                    Operation resized;
                    bool      resizeRequired = (inputHeight > 0) && (inputWidth > 0);
                    if (resizeRequired)
                    {
                        Tensor    size   = new Tensor(new int[] { inputHeight, inputWidth }); // new size;
                        Operation sizeOp = graph.Const(size, size.Type, opName: "size");
                        resized = graph.ResizeBilinear(dimsExpander, sizeOp);                 //resize image
                    }
                    else
                    {
                        resized = dimsExpander;
                    }

                    Tensor    mean        = new Tensor(inputMean);
                    Operation meanOp      = graph.Const(mean, mean.Type, opName: "mean");
                    Operation substracted = graph.Sub(resized, meanOp);

                    Tensor    scaleTensor = new Tensor(scale);
                    Operation scaleOp     = graph.Const(scaleTensor, scaleTensor.Type, opName: "scale");
                    Operation scaled      = graph.Mul(substracted, scaleOp);

                    Session  session      = new Session(graph);
                    Tensor   imageTensor  = Tensor.FromString(File.ReadAllBytes(fileName), status);
                    Tensor[] imageResults = session.Run(new Output[] { input }, new Tensor[] { imageTensor },
                                                        new Output[] { scaled });
                    return(imageResults[0]);
                }
            }
#endif
        }
コード例 #37
0
        public static void ReadImageFileToTensor(String fileName, IntPtr dest, int inputHeight = -1, int inputWidth = -1, float inputMean = 0.0f, float scale = 1.0f)
        {
#if __ANDROID__
            Android.Graphics.Bitmap bmp = BitmapFactory.DecodeFile(fileName);

            if (inputHeight > 0 || inputWidth > 0)
            {
                Bitmap resized = Bitmap.CreateScaledBitmap(bmp, inputWidth, inputHeight, false);
                bmp.Dispose();
                bmp = resized;
            }
            int[]   intValues   = new int[bmp.Width * bmp.Height];
            float[] floatValues = new float[bmp.Width * bmp.Height * 3];
            bmp.GetPixels(intValues, 0, bmp.Width, 0, 0, bmp.Width, bmp.Height);
            for (int i = 0; i < intValues.Length; ++i)
            {
                int val = intValues[i];
                floatValues[i * 3 + 0] = (((val >> 16) & 0xFF) - inputMean) * scale;
                floatValues[i * 3 + 1] = (((val >> 8) & 0xFF) - inputMean) * scale;
                floatValues[i * 3 + 2] = ((val & 0xFF) - inputMean) * scale;
            }

            System.Runtime.InteropServices.Marshal.Copy(floatValues, 0, dest, floatValues.Length);
#elif __IOS__
            UIImage image = new UIImage(fileName);
            if (inputHeight > 0 || inputWidth > 0)
            {
                UIImage resized = image.Scale(new CGSize(inputWidth, inputHeight));
                image.Dispose();
                image = resized;
            }
            int[]   intValues   = new int[(int)(image.Size.Width * image.Size.Height)];
            float[] floatValues = new float[(int)(image.Size.Width * image.Size.Height * 3)];
            System.Runtime.InteropServices.GCHandle handle = System.Runtime.InteropServices.GCHandle.Alloc(intValues, System.Runtime.InteropServices.GCHandleType.Pinned);
            using (CGImage cgimage = image.CGImage)
                using (CGColorSpace cspace = CGColorSpace.CreateDeviceRGB())
                    using (CGBitmapContext context = new CGBitmapContext(
                               handle.AddrOfPinnedObject(),
                               (nint)image.Size.Width,
                               (nint)image.Size.Height,
                               8,
                               (nint)image.Size.Width * 4,
                               cspace,
                               CGImageAlphaInfo.PremultipliedLast
                               ))
                    {
                        context.DrawImage(new CGRect(new CGPoint(), image.Size), cgimage);
                    }
            handle.Free();

            for (int i = 0; i < intValues.Length; ++i)
            {
                int val = intValues[i];
                floatValues[i * 3 + 0] = (((val >> 16) & 0xFF) - inputMean) * scale;
                floatValues[i * 3 + 1] = (((val >> 8) & 0xFF) - inputMean) * scale;
                floatValues[i * 3 + 2] = ((val & 0xFF) - inputMean) * scale;
            }
            System.Runtime.InteropServices.Marshal.Copy(floatValues, 0, dest, floatValues.Length);
#elif __UNIFIED__
            NSImage image = new NSImage(fileName);
            if (inputHeight > 0 || inputWidth > 0)
            {
                NSImage resized = new NSImage(new CGSize(inputWidth, inputHeight));
                resized.LockFocus();
                image.DrawInRect(new CGRect(0, 0, inputWidth, inputHeight), CGRect.Empty, NSCompositingOperation.SourceOver, 1.0f);
                resized.UnlockFocus();
                image.Dispose();
                image = resized;
            }
            int[]   intValues   = new int[(int)(image.Size.Width * image.Size.Height)];
            float[] floatValues = new float[(int)(image.Size.Width * image.Size.Height * 3)];
            System.Runtime.InteropServices.GCHandle handle = System.Runtime.InteropServices.GCHandle.Alloc(intValues, System.Runtime.InteropServices.GCHandleType.Pinned);
            using (CGImage cgimage = image.CGImage)
                using (CGColorSpace cspace = CGColorSpace.CreateDeviceRGB())
                    using (CGBitmapContext context = new CGBitmapContext(
                               handle.AddrOfPinnedObject(),
                               (nint)image.Size.Width,
                               (nint)image.Size.Height,
                               8,
                               (nint)image.Size.Width * 4,
                               cspace,
                               CGImageAlphaInfo.PremultipliedLast
                               ))
                    {
                        context.DrawImage(new CGRect(new CGPoint(), image.Size), cgimage);
                    }
            handle.Free();

            for (int i = 0; i < intValues.Length; ++i)
            {
                int val = intValues[i];
                floatValues[i * 3 + 0] = (((val >> 16) & 0xFF) - inputMean) * scale;
                floatValues[i * 3 + 1] = (((val >> 8) & 0xFF) - inputMean) * scale;
                floatValues[i * 3 + 2] = ((val & 0xFF) - inputMean) * scale;
            }
            System.Runtime.InteropServices.Marshal.Copy(floatValues, 0, dest, floatValues.Length);
#else
            if (Emgu.TF.Util.Platform.OperationSystem == OS.Windows)
            {
                //Do something for Windows
                System.Drawing.Bitmap bmp = new Bitmap(fileName);

                if (inputHeight > 0 || inputWidth > 0)
                {
                    //resize bmp
                    System.Drawing.Bitmap newBmp = new Bitmap(bmp, inputWidth, inputHeight);
                    bmp.Dispose();
                    bmp = newBmp;
                    //bmp.Save("tmp.png");
                }

                byte[] byteValues = new byte[bmp.Width * bmp.Height * 3];
                System.Drawing.Imaging.BitmapData bd = new System.Drawing.Imaging.BitmapData();

                bmp.LockBits(
                    new Rectangle(0, 0, bmp.Width, bmp.Height),
                    System.Drawing.Imaging.ImageLockMode.ReadOnly,
                    System.Drawing.Imaging.PixelFormat.Format24bppRgb, bd);
                System.Runtime.InteropServices.Marshal.Copy(bd.Scan0, byteValues, 0, byteValues.Length);
                bmp.UnlockBits(bd);

                float[] floatValues = new float[bmp.Width * bmp.Height * 3];
                for (int i = 0; i < byteValues.Length; ++i)
                {
                    floatValues[i] = ((float)byteValues[i] - inputMean) * scale;
                }

                System.Runtime.InteropServices.Marshal.Copy(floatValues, 0, dest, floatValues.Length);
            }
            else
            {
                throw new Exception("Not implemented");
            }
#endif
        }
コード例 #38
0
 public static Packet.PacketHeader ByteArrayToNewStuff(byte[] bytes)
 {
     System.Runtime.InteropServices.GCHandle handle = System.Runtime.InteropServices.GCHandle.Alloc(bytes, System.Runtime.InteropServices.GCHandleType.Pinned);
     Packet.PacketHeader stuff = (Packet.PacketHeader)System.Runtime.InteropServices.Marshal.PtrToStructure(handle.AddrOfPinnedObject(), typeof(Packet.PacketHeader));
     handle.Free();
     return(stuff);
 }
コード例 #39
0
        private static tCLIFile *LoadPEFile(byte[] image)
        {
            tCLIFile *pRet = ((tCLIFile *)Mem.malloc((SIZE_T)sizeof(tCLIFile)));

            System.Runtime.InteropServices.GCHandle handle = System.Runtime.InteropServices.GCHandle.Alloc(image, GCHandleType.Pinned);
            byte *pData = (byte *)handle.AddrOfPinnedObject();

            byte *pMSDOSHeader = (byte *)&(((byte *)pData)[0]);
            byte *pPEHeader;
            byte *pPEOptionalHeader;
            byte *pPESectionHeaders;
            byte *pCLIHeader;
            byte *pRawMetaData;

            int    i;
            uint   lfanew;
            ushort machine;
            int    numSections;
            //uint imageBase;
            //int fileAlignment;
            uint cliHeaderRVA;
            //uint cliHeaderSize;
            uint metaDataRVA;
            //uint metaDataSize;
            tMetaData *pMetaData;

            pRet->pRVA      = RVA.New();
            pRet->gcHandle  = (PTR)(System.IntPtr)handle;
            pRet->pMetaData = pMetaData = MetaData.New();

            lfanew            = *(uint *)&(pMSDOSHeader[0x3c]);
            pPEHeader         = pMSDOSHeader + lfanew + 4;
            pPEOptionalHeader = pPEHeader + 20;
            pPESectionHeaders = pPEOptionalHeader + 224;

            machine = *(ushort *)&(pPEHeader[0]);
            if (machine != DOT_NET_MACHINE)
            {
                return(null);
            }
            numSections = *(ushort *)&(pPEHeader[2]);

            //imageBase = *(uint*)&(pPEOptionalHeader[28]);
            //fileAlignment = *(int*)&(pPEOptionalHeader[36]);

            for (i = 0; i < numSections; i++)
            {
                byte *pSection = pPESectionHeaders + i * 40;
                RVA.Create(pRet->pRVA, pData, pSection);
            }

            cliHeaderRVA = *(uint *)&(pPEOptionalHeader[208]);
            //cliHeaderSize = *(uint*)&(pPEOptionalHeader[212]);

            pCLIHeader = (byte *)RVA.FindData(pRet->pRVA, cliHeaderRVA);

            metaDataRVA = *(uint *)&(pCLIHeader[8]);
            //metaDataSize = *(uint*)&(pCLIHeader[12]);
            pRet->entryPoint = *(uint *)&(pCLIHeader[20]);
            pRawMetaData     = (byte *)RVA.FindData(pRet->pRVA, metaDataRVA);

            // Load all metadata
            {
                uint  versionLen = *(uint *)&(pRawMetaData[12]);
                uint  ofs, numberOfStreams;
                void *pTableStream    = null;
                uint  tableStreamSize = 0;
                pRet->pVersion = &(pRawMetaData[16]);
                Sys.log_f(1, "CLI version: %s\n", (PTR)pRet->pVersion);
                ofs             = 16 + versionLen;
                numberOfStreams = *(ushort *)&(pRawMetaData[ofs + 2]);
                ofs            += 4;

                for (i = 0; i < (int)numberOfStreams; i++)
                {
                    uint  streamOffset = *(uint *)&pRawMetaData[ofs];
                    uint  streamSize   = *(uint *)&pRawMetaData[ofs + 4];
                    byte *pStreamName  = &pRawMetaData[ofs + 8];
                    void *pStream      = pRawMetaData + streamOffset;
                    ofs += (uint)((S.strlen(pStreamName) + 4) & (~0x3)) + 8;
                    if (S.strcasecmp(pStreamName, "#Strings") == 0)
                    {
                        MetaData.LoadStrings(pMetaData, pStream, streamSize);
                    }
                    else if (S.strcasecmp(pStreamName, "#US") == 0)
                    {
                        MetaData.LoadUserStrings(pMetaData, pStream, streamSize);
                    }
                    else if (S.strcasecmp(pStreamName, "#Blob") == 0)
                    {
                        MetaData.LoadBlobs(pMetaData, pStream, streamSize);
                    }
                    else if (S.strcasecmp(pStreamName, "#GUID") == 0)
                    {
                        MetaData.LoadGUIDs(pMetaData, pStream, streamSize);
                    }
                    else if (S.strcasecmp(pStreamName, "#~") == 0)
                    {
                        pTableStream    = pStream;
                        tableStreamSize = streamSize;
                    }
                }
                // Must load tables last
                if (pTableStream != null)
                {
                    MetaData.LoadTables(pMetaData, pRet->pRVA, pTableStream, (uint)tableStreamSize);
                }
            }

            // Mark all generic definition type and methods as such
            for (i = (int)pMetaData->tables.numRows[MetaDataTable.MD_TABLE_GENERICPARAM]; i > 0; i--)
            {
                tMD_GenericParam * pGenericParam;
                /*IDX_TABLE*/ uint ownerIdx;

                pGenericParam = (tMD_GenericParam *)MetaData.GetTableRow
                                    (pMetaData, MetaData.MAKE_TABLE_INDEX(MetaDataTable.MD_TABLE_GENERICPARAM, (uint)i));
                ownerIdx = pGenericParam->owner;
                switch (MetaData.TABLE_ID(ownerIdx))
                {
                case MetaDataTable.MD_TABLE_TYPEDEF:
                {
                    tMD_TypeDef *pTypeDef = (tMD_TypeDef *)MetaData.GetTableRow(pMetaData, ownerIdx);
                    pTypeDef->isGenericDefinition = 1;
                }
                break;

                case MetaDataTable.MD_TABLE_METHODDEF:
                {
                    tMD_MethodDef *pMethodDef = (tMD_MethodDef *)MetaData.GetTableRow(pMetaData, ownerIdx);
                    pMethodDef->isGenericDefinition = 1;
                }
                break;

                default:
                    Sys.Crash("Wrong generic parameter owner: 0x%08x", ownerIdx);
                    break;
                }
            }

            // Mark all nested classes as such
            for (i = (int)pMetaData->tables.numRows[MetaDataTable.MD_TABLE_NESTEDCLASS]; i > 0; i--)
            {
                tMD_NestedClass *pNested;
                tMD_TypeDef *    pParent;
                tMD_TypeDef *    pChild;

                pNested           = (tMD_NestedClass *)MetaData.GetTableRow(pMetaData, MetaData.MAKE_TABLE_INDEX(MetaDataTable.MD_TABLE_NESTEDCLASS, (uint)i));
                pParent           = (tMD_TypeDef *)MetaData.GetTableRow(pMetaData, pNested->enclosingClass);
                pChild            = (tMD_TypeDef *)MetaData.GetTableRow(pMetaData, pNested->nestedClass);
                pChild->pNestedIn = pParent;
            }

            return(pRet);
        }
コード例 #40
0
ファイル: Glu.cs プロジェクト: zero10/scallion
 Int32 ScaleImage(PixelFormat format, Int32 wIn, Int32 hIn, PixelType typeIn, [In, Out] object dataIn, Int32 wOut, Int32 hOut, PixelType typeOut, [In, Out] object dataOut)
 {
     unsafe
     {
         System.Runtime.InteropServices.GCHandle dataIn_ptr  = System.Runtime.InteropServices.GCHandle.Alloc(dataIn, System.Runtime.InteropServices.GCHandleType.Pinned);
         System.Runtime.InteropServices.GCHandle dataOut_ptr = System.Runtime.InteropServices.GCHandle.Alloc(dataOut, System.Runtime.InteropServices.GCHandleType.Pinned);
         try
         {
             return(Delegates.gluScaleImage((PixelFormat)format, (Int32)wIn, (Int32)hIn, (PixelType)typeIn, (IntPtr)dataIn_ptr.AddrOfPinnedObject(), (Int32)wOut, (Int32)hOut, (PixelType)typeOut, (IntPtr)dataOut_ptr.AddrOfPinnedObject()));
         }
         finally
         {
             dataIn_ptr.Free();
             dataOut_ptr.Free();
         }
     }
 }
コード例 #41
0
        /// <summary>
        /// Read the image file into a Tensorflow tensor
        /// </summary>
        /// <typeparam name="T">The tensor data type, e.g. float</typeparam>
        /// <param name="fileName">The name of the image file</param>
        /// <param name="inputHeight">The height of the input tensor. If zero or negative, will use the image height from the file</param>
        /// <param name="inputWidth">The width of the input tensor. If zero or negative, will use the image width from the file</param>
        /// <param name="inputMean">The input mean, will be subtracted from the image pixel value</param>
        /// <param name="scale">The optional scale, after input means is substracted, the pixel value will multiply with the scale to produce the tensor value</param>
        /// <param name="flipUpSideDown">If true, the image will be flipped upside down</param>
        /// <param name="swapBR">If true, the blue and red channels will be swapped</param>
        /// <returns>The tensorflow tensor.</returns>
        public static Tensor ReadTensorFromImageFile <T>(
            String fileName,
            int inputHeight     = -1,
            int inputWidth      = -1,
            float inputMean     = 0.0f,
            float scale         = 1.0f,
            bool flipUpSideDown = false,
            bool swapBR         = false) where T : struct
        {
#if __ANDROID__
            return(NativeReadTensorFromImageFile <T>(fileName, inputHeight, inputWidth, inputMean, scale,
                                                     flipUpSideDown, swapBR));
#elif __IOS__
            UIImage image = new UIImage(fileName);
            if (inputHeight > 0 || inputWidth > 0)
            {
                UIImage resized = image.Scale(new CGSize(inputWidth, inputHeight));
                image.Dispose();
                image = resized;
            }
            int[]   intValues   = new int[(int)(image.Size.Width * image.Size.Height)];
            float[] floatValues = new float[(int)(image.Size.Width * image.Size.Height * 3)];
            System.Runtime.InteropServices.GCHandle handle = System.Runtime.InteropServices.GCHandle.Alloc(intValues, System.Runtime.InteropServices.GCHandleType.Pinned);
            using (CGImage cgimage = image.CGImage)
                using (CGColorSpace cspace = CGColorSpace.CreateDeviceRGB())
                    using (CGBitmapContext context = new CGBitmapContext(
                               handle.AddrOfPinnedObject(),
                               (nint)image.Size.Width,
                               (nint)image.Size.Height,
                               8,
                               (nint)image.Size.Width * 4,
                               cspace,
                               CGImageAlphaInfo.PremultipliedLast
                               ))
                    {
                        context.DrawImage(new CGRect(new CGPoint(), image.Size), cgimage);
                    }
            handle.Free();

            for (int i = 0; i < intValues.Length; ++i)
            {
                int val = intValues[i];
                floatValues[i * 3 + 0] = (((val >> 16) & 0xFF) - inputMean) * scale;
                floatValues[i * 3 + 1] = (((val >> 8) & 0xFF) - inputMean) * scale;
                floatValues[i * 3 + 2] = ((val & 0xFF) - inputMean) * scale;
            }

            Tensor t = new Tensor(DataType.Float, new int[] { 1, (int)image.Size.Height, (int)image.Size.Width, 3 });
            System.Runtime.InteropServices.Marshal.Copy(floatValues, 0, t.DataPointer, floatValues.Length);
            return(t);
#else
            FileInfo fi        = new FileInfo(fileName);
            String   extension = fi.Extension.ToLower();

            //Use tensorflow to decode the following image formats
            if ((typeof(T) == typeof(float))
                &&
                (extension.Equals(".jpeg") ||
                 extension.Equals(".jpg") ||
                 extension.Equals(".png") ||
                 extension.Equals(".gif")))
            {
                using (Graph graph = new Graph())
                {
                    Operation input = graph.Placeholder(DataType.String);

                    //output dimension [height, width, 3] where 3 is the number of channels
                    //DecodeJpeg can decode JPEG, PNG and GIF
                    Operation jpegDecoder = graph.DecodeJpeg(input, 3);

                    Operation floatCaster = graph.Cast(jpegDecoder, DstT: DataType.Float); //cast to float

                    Tensor    zeroConst    = new Tensor(0);
                    Operation zeroConstOp  = graph.Const(zeroConst, zeroConst.Type, opName: "zeroConstOp");
                    Operation dimsExpander = graph.ExpandDims(floatCaster, zeroConstOp); //turn it to dimension [1, height, width, 3]

                    Operation resized;
                    bool      resizeRequired = (inputHeight > 0) && (inputWidth > 0);
                    if (resizeRequired)
                    {
                        Tensor    size   = new Tensor(new int[] { inputHeight, inputWidth }); // new size;
                        Operation sizeOp = graph.Const(size, size.Type, opName: "size");
                        resized = graph.ResizeBilinear(dimsExpander, sizeOp);                 //resize image
                    }
                    else
                    {
                        resized = dimsExpander;
                    }

                    Tensor    mean       = new Tensor(inputMean);
                    Operation meanOp     = graph.Const(mean, mean.Type, opName: "mean");
                    Operation subtracted = graph.Sub(resized, meanOp);

                    Tensor    scaleTensor = new Tensor(scale);
                    Operation scaleOp     = graph.Const(scaleTensor, scaleTensor.Type, opName: "scale");
                    Operation scaled      = graph.Mul(subtracted, scaleOp);

                    Operation swapedBR;
                    if (swapBR)
                    {
                        Tensor    threeConst   = new Tensor(new int[] { 3 });
                        Operation threeConstOp = graph.Const(threeConst, threeConst.Type, "threeConstOp");
                        swapedBR = graph.ReverseV2(scaled, threeConstOp, "swapBR");
                    }
                    else
                    {
                        swapedBR = scaled;
                    }

                    Operation flipped;
                    if (flipUpSideDown)
                    {
                        Tensor    oneConst   = new Tensor(new int[] { 1 });
                        Operation oneConstOp = graph.Const(oneConst, oneConst.Type, "oneConstOp");
                        flipped = graph.ReverseV2(swapedBR, oneConstOp, "flipUpSideDownOp");
                    }
                    else
                    {
                        flipped = swapedBR;
                    }

                    using (Session session = new Session(graph))
                    {
                        Tensor imageTensor = Tensor.FromString(File.ReadAllBytes(fileName));

                        Tensor[] imageResults = session.Run(new Output[] { input }, new Tensor[] { imageTensor },
                                                            new Output[] { flipped });
                        return(imageResults[0]);
                    }
                }
            }
            else
            {
                return(NativeReadTensorFromImageFile <T>(fileName, inputHeight, inputWidth, inputMean, scale,
                                                         flipUpSideDown, swapBR));
            }
#endif
        }
コード例 #42
0
        /// <summary>
        /// Read the image file into a Tensorflow tensor
        /// </summary>
        /// <typeparam name="T">The tensor data type, e.g. float</typeparam>
        /// <param name="fileName">The name of the image file</param>
        /// <param name="inputHeight">The height of the input tensor. If zero or negative, will use the image height from the file</param>
        /// <param name="inputWidth">The width of the input tensor. If zero or negative, will use the image width from the file</param>
        /// <param name="inputMean">The input mean, will be subtracted from the image pixel value</param>
        /// <param name="scale">The optional scale, after input means is substracted, the pixel value will multiply with the scale to produce the tensor value</param>
        /// <param name="flipUpSideDown">If true, the image will be flipped upside down</param>
        /// <param name="swapBR">If true, the blue and red channels will be swapped</param>
        /// <returns>The tensorflow tensor.</returns>
        public static Tensor ReadTensorFromImageFile <T>(
            String fileName,
            int inputHeight     = -1,
            int inputWidth      = -1,
            float inputMean     = 0.0f,
            float scale         = 1.0f,
            bool flipUpSideDown = false,
            bool swapBR         = false) where T : struct
        {
#if __ANDROID__
            return(NativeReadTensorFromImageFile <T>(fileName, inputHeight, inputWidth, inputMean, scale,
                                                     flipUpSideDown, swapBR));
#elif __IOS__
            UIImage image = new UIImage(fileName);
            if (inputHeight > 0 || inputWidth > 0)
            {
                UIImage resized = image.Scale(new CGSize(inputWidth, inputHeight));
                image.Dispose();
                image = resized;
            }
            int[]   intValues   = new int[(int)(image.Size.Width * image.Size.Height)];
            float[] floatValues = new float[(int)(image.Size.Width * image.Size.Height * 3)];
            System.Runtime.InteropServices.GCHandle handle = System.Runtime.InteropServices.GCHandle.Alloc(intValues, System.Runtime.InteropServices.GCHandleType.Pinned);
            using (CGImage cgimage = image.CGImage)
                using (CGColorSpace cspace = CGColorSpace.CreateDeviceRGB())
                    using (CGBitmapContext context = new CGBitmapContext(
                               handle.AddrOfPinnedObject(),
                               (nint)image.Size.Width,
                               (nint)image.Size.Height,
                               8,
                               (nint)image.Size.Width * 4,
                               cspace,
                               CGImageAlphaInfo.PremultipliedLast
                               ))
                    {
                        context.DrawImage(new CGRect(new CGPoint(), image.Size), cgimage);
                    }
            handle.Free();

            for (int i = 0; i < intValues.Length; ++i)
            {
                int val = intValues[i];
                floatValues[i * 3 + 0] = (((val >> 16) & 0xFF) - inputMean) * scale;
                floatValues[i * 3 + 1] = (((val >> 8) & 0xFF) - inputMean) * scale;
                floatValues[i * 3 + 2] = ((val & 0xFF) - inputMean) * scale;
            }

            Tensor t = new Tensor(DataType.Float, new int[] { 1, (int)image.Size.Height, (int)image.Size.Width, 3 });
            System.Runtime.InteropServices.Marshal.Copy(floatValues, 0, t.DataPointer, floatValues.Length);
            return(t);
#else
            FileInfo fi        = new FileInfo(fileName);
            String   extension = fi.Extension.ToLower();

            //Use tensorflow to decode the following image formats
            if ((typeof(T) == typeof(float))
                &&
                (extension.Equals(".jpeg") ||
                 extension.Equals(".jpg") ||
                 extension.Equals(".png") ||
                 extension.Equals(".gif")))
            {
                return(DecodeImageToTensor32F3C(fileName, inputHeight, inputWidth, inputMean, scale, flipUpSideDown, swapBR));
            }
            else
            {
                return(NativeReadTensorFromImageFile <T>(fileName, inputHeight, inputWidth, inputMean, scale,
                                                         flipUpSideDown, swapBR));
            }
#endif
        }
コード例 #43
0
ファイル: Glu.cs プロジェクト: zero10/scallion
 Int32 Build2DMipmapLevel(TextureTarget target, Int32 internalFormat, Int32 width, Int32 height, PixelFormat format, PixelType type, Int32 level, Int32 @base, Int32 max, [In, Out] object data)
 {
     unsafe
     {
         System.Runtime.InteropServices.GCHandle data_ptr = System.Runtime.InteropServices.GCHandle.Alloc(data, System.Runtime.InteropServices.GCHandleType.Pinned);
         try
         {
             return(Delegates.gluBuild2DMipmapLevels((TextureTarget)target, (Int32)internalFormat, (Int32)width, (Int32)height, (PixelFormat)format, (PixelType)type, (Int32)level, (Int32)@base, (Int32)max, (IntPtr)data_ptr.AddrOfPinnedObject()));
         }
         finally
         {
             data_ptr.Free();
         }
     }
 }
コード例 #44
0
        /// <summary>
        /// Read an image file, covert the data and save it to the native pointer
        /// </summary>
        /// <typeparam name="T">The type of the data to covert the image pixel values to. e.g. "float" or "byte"</typeparam>
        /// <param name="fileName">The name of the image file</param>
        /// <param name="dest">The native pointer where the image pixels values will be saved to.</param>
        /// <param name="inputHeight">The height of the image, must match the height requirement for the tensor</param>
        /// <param name="inputWidth">The width of the image, must match the width requirement for the tensor</param>
        /// <param name="inputMean">The mean value, it will be substracted from the input image pixel values</param>
        /// <param name="scale">The scale, after mean is substracted, the scale will be used to multiply the pixel values</param>
        /// <param name="flipUpSideDown">If true, the image needs to be flipped up side down</param>
        /// <param name="swapBR">If true, will flip the Blue channel with the Red. e.g. If false, the tensor's color channel order will be RGB. If true, the tensor's color channle order will be BGR </param>
        public static void ReadImageFileToTensor <T>(
            String fileName,
            IntPtr dest,
            int inputHeight     = -1,
            int inputWidth      = -1,
            float inputMean     = 0.0f,
            float scale         = 1.0f,
            bool flipUpSideDown = false,
            bool swapBR         = false)
            where T : struct
        {
            if (!File.Exists(fileName))
            {
                throw new FileNotFoundException(String.Format("File {0} do not exist.", fileName));
            }

#if __ANDROID__
            Android.Graphics.Bitmap bmp = BitmapFactory.DecodeFile(fileName);
            if (inputHeight > 0 || inputWidth > 0)
            {
                Bitmap resized = Bitmap.CreateScaledBitmap(bmp, inputWidth, inputHeight, false);
                bmp.Dispose();
                bmp = resized;
            }

            if (flipUpSideDown)
            {
                Matrix matrix = new Matrix();
                matrix.PostScale(1, -1, bmp.Width / 2, bmp.Height / 2);
                Bitmap flipped = Bitmap.CreateBitmap(bmp, 0, 0, bmp.Width, bmp.Height, matrix, true);
                bmp.Dispose();
                bmp = flipped;
            }

            int[]   intValues   = new int[bmp.Width * bmp.Height];
            float[] floatValues = new float[bmp.Width * bmp.Height * 3];
            bmp.GetPixels(intValues, 0, bmp.Width, 0, 0, bmp.Width, bmp.Height);

            if (swapBR)
            {
                for (int i = 0; i < intValues.Length; ++i)
                {
                    int val = intValues[i];
                    floatValues[i * 3 + 0] = ((val & 0xFF) - inputMean) * scale;
                    floatValues[i * 3 + 1] = (((val >> 8) & 0xFF) - inputMean) * scale;
                    floatValues[i * 3 + 2] = (((val >> 16) & 0xFF) - inputMean) * scale;
                }
            }
            else
            {
                for (int i = 0; i < intValues.Length; ++i)
                {
                    int val = intValues[i];
                    floatValues[i * 3 + 0] = (((val >> 16) & 0xFF) - inputMean) * scale;
                    floatValues[i * 3 + 1] = (((val >> 8) & 0xFF) - inputMean) * scale;
                    floatValues[i * 3 + 2] = ((val & 0xFF) - inputMean) * scale;
                }
            }

            if (typeof(T) == typeof(float))
            {
                Marshal.Copy(floatValues, 0, dest, floatValues.Length);
            }
            else if (typeof(T) == typeof(byte))
            {
                //copy float to bytes
                byte[] byteValues = new byte[floatValues.Length];
                for (int i = 0; i < floatValues.Length; i++)
                {
                    byteValues[i] = (byte)floatValues[i];
                }
                Marshal.Copy(byteValues, 0, dest, byteValues.Length);
            }
            else
            {
                throw new NotImplementedException(String.Format("Destination data type {0} is not supported.", typeof(T).ToString()));
            }
#elif __IOS__
            if (flipUpSideDown)
            {
                throw new NotImplementedException("Flip Up Side Down is Not implemented");
            }

            UIImage image = new UIImage(fileName);
            if (inputHeight > 0 || inputWidth > 0)
            {
                UIImage resized = image.Scale(new CGSize(inputWidth, inputHeight));
                image.Dispose();
                image = resized;
            }
            int[]   intValues   = new int[(int)(image.Size.Width * image.Size.Height)];
            float[] floatValues = new float[(int)(image.Size.Width * image.Size.Height * 3)];
            System.Runtime.InteropServices.GCHandle handle = System.Runtime.InteropServices.GCHandle.Alloc(intValues, System.Runtime.InteropServices.GCHandleType.Pinned);
            using (CGImage cgimage = image.CGImage)
                using (CGColorSpace cspace = CGColorSpace.CreateDeviceRGB())
                    using (CGBitmapContext context = new CGBitmapContext(
                               handle.AddrOfPinnedObject(),
                               (nint)image.Size.Width,
                               (nint)image.Size.Height,
                               8,
                               (nint)image.Size.Width * 4,
                               cspace,
                               CGImageAlphaInfo.PremultipliedLast
                               ))
                    {
                        context.DrawImage(new CGRect(new CGPoint(), image.Size), cgimage);
                    }
            handle.Free();

            if (swapBR)
            {
                for (int i = 0; i < intValues.Length; ++i)
                {
                    int val = intValues[i];
                    floatValues[i * 3 + 0] = ((val & 0xFF) - inputMean) * scale;
                    floatValues[i * 3 + 1] = (((val >> 8) & 0xFF) - inputMean) * scale;
                    floatValues[i * 3 + 2] = (((val >> 16) & 0xFF) - inputMean) * scale;
                }
            }
            else
            {
                for (int i = 0; i < intValues.Length; ++i)
                {
                    int val = intValues[i];
                    floatValues[i * 3 + 0] = (((val >> 16) & 0xFF) - inputMean) * scale;
                    floatValues[i * 3 + 1] = (((val >> 8) & 0xFF) - inputMean) * scale;
                    floatValues[i * 3 + 2] = ((val & 0xFF) - inputMean) * scale;
                }
            }

            if (typeof(T) == typeof(float))
            {
                Marshal.Copy(floatValues, 0, dest, floatValues.Length);
            }
            else if (typeof(T) == typeof(byte))
            {
                //copy float to bytes
                byte[] byteValues = new byte[floatValues.Length];
                for (int i = 0; i < floatValues.Length; i++)
                {
                    byteValues[i] = (byte)floatValues[i];
                }
                Marshal.Copy(byteValues, 0, dest, byteValues.Length);
            }
            else
            {
                throw new NotImplementedException(String.Format("Destination data type {0} is not supported.", typeof(T).ToString()));
            }

            //System.Runtime.InteropServices.Marshal.Copy(floatValues, 0, dest, floatValues.Length);
#elif __UNIFIED__
            if (flipUpSideDown)
            {
                throw new NotImplementedException("Flip Up Side Down is Not implemented");
            }
            //if (swapBR)
            //    throw new NotImplementedException("swapBR is Not implemented");
            NSImage image = new NSImage(fileName);
            if (inputHeight > 0 || inputWidth > 0)
            {
                NSImage resized = new NSImage(new CGSize(inputWidth, inputHeight));
                resized.LockFocus();
                image.DrawInRect(new CGRect(0, 0, inputWidth, inputHeight), CGRect.Empty, NSCompositingOperation.SourceOver, 1.0f);
                resized.UnlockFocus();
                image.Dispose();
                image = resized;
            }
            int[]   intValues   = new int[(int)(image.Size.Width * image.Size.Height)];
            float[] floatValues = new float[(int)(image.Size.Width * image.Size.Height * 3)];
            System.Runtime.InteropServices.GCHandle handle = System.Runtime.InteropServices.GCHandle.Alloc(intValues, System.Runtime.InteropServices.GCHandleType.Pinned);
            using (CGImage cgimage = image.CGImage)
                using (CGColorSpace cspace = CGColorSpace.CreateDeviceRGB())
                    using (CGBitmapContext context = new CGBitmapContext(
                               handle.AddrOfPinnedObject(),
                               (nint)image.Size.Width,
                               (nint)image.Size.Height,
                               8,
                               (nint)image.Size.Width * 4,
                               cspace,
                               CGImageAlphaInfo.PremultipliedLast
                               ))
                    {
                        context.DrawImage(new CGRect(new CGPoint(), image.Size), cgimage);
                    }
            handle.Free();
            if (swapBR)
            {
                for (int i = 0; i < intValues.Length; ++i)
                {
                    int val = intValues[i];
                    floatValues[i * 3 + 0] = ((val & 0xFF) - inputMean) * scale;
                    floatValues[i * 3 + 1] = (((val >> 8) & 0xFF) - inputMean) * scale;
                    floatValues[i * 3 + 2] = (((val >> 16) & 0xFF) - inputMean) * scale;
                }
            }
            else
            {
                for (int i = 0; i < intValues.Length; ++i)
                {
                    int val = intValues[i];
                    floatValues[i * 3 + 0] = (((val >> 16) & 0xFF) - inputMean) * scale;
                    floatValues[i * 3 + 1] = (((val >> 8) & 0xFF) - inputMean) * scale;
                    floatValues[i * 3 + 2] = ((val & 0xFF) - inputMean) * scale;
                }
            }

            if (typeof(T) == typeof(float))
            {
                Marshal.Copy(floatValues, 0, dest, floatValues.Length);
            }
            else if (typeof(T) == typeof(byte))
            {
                //copy float to bytes
                byte[] byteValues = new byte[floatValues.Length];
                for (int i = 0; i < floatValues.Length; i++)
                {
                    byteValues[i] = (byte)floatValues[i];
                }
                Marshal.Copy(byteValues, 0, dest, byteValues.Length);
            }
            else
            {
                throw new NotImplementedException(String.Format("Destination data type {0} is not supported.", typeof(T).ToString()));
            }

            //System.Runtime.InteropServices.Marshal.Copy(floatValues, 0, dest, floatValues.Length);
#elif UNITY_EDITOR || UNITY_IOS || UNITY_ANDROID || UNITY_STANDALONE
            Texture2D texture = ReadTexture2DFromFile(fileName);
            ReadTensorFromTexture2D <T>(texture, dest, inputHeight, inputWidth, inputMean, scale, flipUpSideDown, false);
#else
            //if (System.Runtime.InteropServices.RuntimeInformation.IsOSPlatform(System.Runtime.InteropServices.OSPlatform.Windows))
            {
                //Read the file using Bitmap class
                System.Drawing.Bitmap bmp = new Bitmap(fileName);

                if (inputHeight > 0 || inputWidth > 0)
                {
                    //resize bmp
                    System.Drawing.Bitmap newBmp = new Bitmap(bmp, inputWidth, inputHeight);
                    bmp.Dispose();
                    bmp = newBmp;
                    //bmp.Save("tmp.png");
                }

                if (flipUpSideDown)
                {
                    bmp.RotateFlip(RotateFlipType.RotateNoneFlipY);
                }

                int bmpWidth  = bmp.Width;
                int bmpHeight = bmp.Height;
                System.Drawing.Imaging.BitmapData bd = new System.Drawing.Imaging.BitmapData();
                bmp.LockBits(
                    new Rectangle(0, 0, bmpWidth, bmpHeight),
                    System.Drawing.Imaging.ImageLockMode.ReadOnly,
                    System.Drawing.Imaging.PixelFormat.Format24bppRgb, bd);
                int stride = bd.Stride;

                byte[] byteValues = new byte[bmpHeight * stride];
                Marshal.Copy(bd.Scan0, byteValues, 0, byteValues.Length);
                bmp.UnlockBits(bd);

                if (typeof(T) == typeof(float))
                {
                    int     imageSize   = bmpWidth * bmpHeight;
                    float[] floatValues = new float[imageSize * 3];
                    if (swapBR)
                    {
                        int idx       = 0;
                        int rowOffset = 0;
                        for (int i = 0; i < bmpHeight; ++i)
                        {
                            int rowPtr = rowOffset;
                            for (int j = 0; j < bmpWidth; ++j)
                            {
                                float b = ((float)byteValues[rowPtr++] - inputMean) * scale;
                                float g = ((float)byteValues[rowPtr++] - inputMean) * scale;
                                float r = ((float)byteValues[rowPtr++] - inputMean) * scale;
                                floatValues[idx++] = r;
                                floatValues[idx++] = g;
                                floatValues[idx++] = b;
                            }
                            rowOffset += stride;
                        }
                    }
                    else
                    {
                        int idx       = 0;
                        int rowOffset = 0;
                        for (int i = 0; i < bmpHeight; ++i)
                        {
                            int rowPtr = rowOffset;
                            for (int j = 0; j < bmpWidth; ++j)
                            {
                                floatValues[idx++] = ((float)byteValues[rowPtr++] - inputMean) * scale;
                                floatValues[idx++] = ((float)byteValues[rowPtr++] - inputMean) * scale;
                                floatValues[idx++] = ((float)byteValues[rowPtr++] - inputMean) * scale;
                            }
                            rowOffset += stride;
                        }
                    }
                    Marshal.Copy(floatValues, 0, dest, floatValues.Length);
                }
                else if (typeof(T) == typeof(byte))
                {
                    int imageSize = bmp.Width * bmp.Height;
                    if (swapBR)
                    {
                        int idx = 0;
                        for (int i = 0; i < bmpHeight; ++i)
                        {
                            int offset = i * stride;
                            for (int j = 0; j < bmpWidth; ++j)
                            {
                                byte b = (byte)(((float)byteValues[offset++] - inputMean) * scale);
                                byte g = (byte)(((float)byteValues[offset++] - inputMean) * scale);
                                byte r = (byte)(((float)byteValues[offset++] - inputMean) * scale);
                                byteValues[idx++] = r;
                                byteValues[idx++] = g;
                                byteValues[idx++] = b;
                            }
                        }
                    }
                    else
                    {
                        int idx = 0;
                        for (int i = 0; i < bmpHeight; ++i)
                        {
                            int offset = i * stride;
                            for (int j = 0; j < bmpWidth * 3; ++j)
                            {
                                byteValues[idx++] = (byte)(((float)byteValues[offset++] - inputMean) * scale);
                            }
                        }
                    }
                    Marshal.Copy(byteValues, 0, dest, imageSize * 3);
                }
                else
                {
                    throw new NotImplementedException(String.Format("Destination data type {0} is not supported.", typeof(T).ToString()));
                }
            }

            /*
             * else //Unix
             * {
             *  //if (flipUpSideDown)
             *  //    throw new NotImplementedException("Flip Up Side Down is Not implemented");
             *
             *  throw new NotImplementedException("Not implemented");
             * }*/
#endif
        }
コード例 #45
0
ファイル: NativeImageIO.cs プロジェクト: KwangsikJeon/emgutf
        public static void ReadImageFileToTensor <T>(
            String fileName,
            IntPtr dest,
            int inputHeight     = -1,
            int inputWidth      = -1,
            float inputMean     = 0.0f,
            float scale         = 1.0f,
            bool flipUpSideDown = false,
            bool swapBR         = false)
            where T : struct
        {
#if __ANDROID__
            if (flipUpSideDown)
            {
                throw new NotImplementedException("Flip Up Side Down is Not implemented");
            }
            if (swapBR)
            {
                throw new NotImplementedException("swapBR is Not implemented");
            }
            Android.Graphics.Bitmap bmp = BitmapFactory.DecodeFile(fileName);

            if (inputHeight > 0 || inputWidth > 0)
            {
                Bitmap resized = Bitmap.CreateScaledBitmap(bmp, inputWidth, inputHeight, false);
                bmp.Dispose();
                bmp = resized;
            }
            int[]   intValues   = new int[bmp.Width * bmp.Height];
            float[] floatValues = new float[bmp.Width * bmp.Height * 3];
            bmp.GetPixels(intValues, 0, bmp.Width, 0, 0, bmp.Width, bmp.Height);
            for (int i = 0; i < intValues.Length; ++i)
            {
                int val = intValues[i];
                floatValues[i * 3 + 0] = (((val >> 16) & 0xFF) - inputMean) * scale;
                floatValues[i * 3 + 1] = (((val >> 8) & 0xFF) - inputMean) * scale;
                floatValues[i * 3 + 2] = ((val & 0xFF) - inputMean) * scale;
            }

            Marshal.Copy(floatValues, 0, dest, floatValues.Length);
#elif __IOS__
            if (flipUpSideDown)
            {
                throw new NotImplementedException("Flip Up Side Down is Not implemented");
            }
            if (swapBR)
            {
                throw new NotImplementedException("swapBR is Not implemented");
            }
            UIImage image = new UIImage(fileName);
            if (inputHeight > 0 || inputWidth > 0)
            {
                UIImage resized = image.Scale(new CGSize(inputWidth, inputHeight));
                image.Dispose();
                image = resized;
            }
            int[]   intValues   = new int[(int)(image.Size.Width * image.Size.Height)];
            float[] floatValues = new float[(int)(image.Size.Width * image.Size.Height * 3)];
            System.Runtime.InteropServices.GCHandle handle = System.Runtime.InteropServices.GCHandle.Alloc(intValues, System.Runtime.InteropServices.GCHandleType.Pinned);
            using (CGImage cgimage = image.CGImage)
                using (CGColorSpace cspace = CGColorSpace.CreateDeviceRGB())
                    using (CGBitmapContext context = new CGBitmapContext(
                               handle.AddrOfPinnedObject(),
                               (nint)image.Size.Width,
                               (nint)image.Size.Height,
                               8,
                               (nint)image.Size.Width * 4,
                               cspace,
                               CGImageAlphaInfo.PremultipliedLast
                               ))
                    {
                        context.DrawImage(new CGRect(new CGPoint(), image.Size), cgimage);
                    }
            handle.Free();

            for (int i = 0; i < intValues.Length; ++i)
            {
                int val = intValues[i];
                floatValues[i * 3 + 0] = (((val >> 16) & 0xFF) - inputMean) * scale;
                floatValues[i * 3 + 1] = (((val >> 8) & 0xFF) - inputMean) * scale;
                floatValues[i * 3 + 2] = ((val & 0xFF) - inputMean) * scale;
            }
            System.Runtime.InteropServices.Marshal.Copy(floatValues, 0, dest, floatValues.Length);
#elif __UNIFIED__
            if (flipUpSideDown)
            {
                throw new NotImplementedException("Flip Up Side Down is Not implemented");
            }
            if (swapBR)
            {
                throw new NotImplementedException("swapBR is Not implemented");
            }
            NSImage image = new NSImage(fileName);
            if (inputHeight > 0 || inputWidth > 0)
            {
                NSImage resized = new NSImage(new CGSize(inputWidth, inputHeight));
                resized.LockFocus();
                image.DrawInRect(new CGRect(0, 0, inputWidth, inputHeight), CGRect.Empty, NSCompositingOperation.SourceOver, 1.0f);
                resized.UnlockFocus();
                image.Dispose();
                image = resized;
            }
            int[]   intValues   = new int[(int)(image.Size.Width * image.Size.Height)];
            float[] floatValues = new float[(int)(image.Size.Width * image.Size.Height * 3)];
            System.Runtime.InteropServices.GCHandle handle = System.Runtime.InteropServices.GCHandle.Alloc(intValues, System.Runtime.InteropServices.GCHandleType.Pinned);
            using (CGImage cgimage = image.CGImage)
                using (CGColorSpace cspace = CGColorSpace.CreateDeviceRGB())
                    using (CGBitmapContext context = new CGBitmapContext(
                               handle.AddrOfPinnedObject(),
                               (nint)image.Size.Width,
                               (nint)image.Size.Height,
                               8,
                               (nint)image.Size.Width * 4,
                               cspace,
                               CGImageAlphaInfo.PremultipliedLast
                               ))
                    {
                        context.DrawImage(new CGRect(new CGPoint(), image.Size), cgimage);
                    }
            handle.Free();

            for (int i = 0; i < intValues.Length; ++i)
            {
                int val = intValues[i];
                floatValues[i * 3 + 0] = (((val >> 16) & 0xFF) - inputMean) * scale;
                floatValues[i * 3 + 1] = (((val >> 8) & 0xFF) - inputMean) * scale;
                floatValues[i * 3 + 2] = ((val & 0xFF) - inputMean) * scale;
            }
            System.Runtime.InteropServices.Marshal.Copy(floatValues, 0, dest, floatValues.Length);
#elif UNITY_EDITOR || UNITY_IOS || UNITY_ANDROID || UNITY_STANDALONE
            Texture2D texture = ReadTexture2DFromFile(fileName);
            ReadTensorFromTexture2D <T>(texture, dest, inputHeight, inputWidth, inputMean, scale, flipUpSideDown, false);
#else
            if (Emgu.TF.Util.Platform.OperationSystem == OS.Windows)
            {
                //Do something for Windows
                System.Drawing.Bitmap bmp = new Bitmap(fileName);

                if (inputHeight > 0 || inputWidth > 0)
                {
                    //resize bmp
                    System.Drawing.Bitmap newBmp = new Bitmap(bmp, inputWidth, inputHeight);
                    bmp.Dispose();
                    bmp = newBmp;
                    //bmp.Save("tmp.png");
                }

                if (flipUpSideDown)
                {
                    bmp.RotateFlip(RotateFlipType.RotateNoneFlipY);
                }

                byte[] byteValues = new byte[bmp.Width * bmp.Height * 3];
                System.Drawing.Imaging.BitmapData bd = new System.Drawing.Imaging.BitmapData();

                bmp.LockBits(
                    new Rectangle(0, 0, bmp.Width, bmp.Height),
                    System.Drawing.Imaging.ImageLockMode.ReadOnly,
                    System.Drawing.Imaging.PixelFormat.Format24bppRgb, bd);
                Marshal.Copy(bd.Scan0, byteValues, 0, byteValues.Length);
                bmp.UnlockBits(bd);

                if (typeof(T) == typeof(float))
                {
                    int     imageSize   = bmp.Width * bmp.Height;
                    float[] floatValues = new float[imageSize * 3];
                    if (swapBR)
                    {
                        for (int i = 0; i < imageSize; ++i)
                        {
                            floatValues[i * 3]     = (byte)(((float)byteValues[i * 3 + 2] - inputMean) * scale);
                            floatValues[i * 3 + 1] = (byte)(((float)byteValues[i * 3 + 1] - inputMean) * scale);
                            floatValues[i * 3 + 2] = (byte)(((float)byteValues[i * 3 + 0] - inputMean) * scale);
                        }
                    }
                    else
                    {
                        for (int i = 0; i < byteValues.Length; ++i)
                        {
                            floatValues[i] = ((float)byteValues[i] - inputMean) * scale;
                        }
                    }
                    Marshal.Copy(floatValues, 0, dest, floatValues.Length);
                }
                else if (typeof(T) == typeof(byte))
                {
                    if (swapBR)
                    {
                        int imageSize = bmp.Width * bmp.Height;
                        //byte[] bValues = new byte[imageSize * 3];
                        for (int i = 0; i < imageSize; ++i)
                        {
                            byte c0 = (byte)(((float)byteValues[i * 3 + 2] - inputMean) * scale);
                            byte c1 = (byte)(((float)byteValues[i * 3 + 1] - inputMean) * scale);
                            byte c2 = (byte)(((float)byteValues[i * 3 + 0] - inputMean) * scale);
                            byteValues[i * 3]     = c0;
                            byteValues[i * 3 + 1] = c1;
                            byteValues[i * 3 + 2] = c2;
                        }
                    }
                    else
                    {
                        if (!(inputMean == 0.0f && scale == 1.0f))
                        {
                            for (int i = 0; i < byteValues.Length; ++i)
                            {
                                byteValues[i] = (byte)(((float)byteValues[i] - inputMean) * scale);
                            }
                        }
                    }
                    Marshal.Copy(byteValues, 0, dest, byteValues.Length);
                }
                else
                {
                    throw new Exception(String.Format("Destination data type {0} is not supported.", typeof(T).ToString()));
                }
            }
            else //Unix
            {
                if (flipUpSideDown)
                {
                    throw new NotImplementedException("Flip Up Side Down is Not implemented");
                }

                throw new Exception("Not implemented");
            }
#endif
        }
コード例 #46
0
        /// <summary>
        /// Read an UIImage, covert the data and save it to the native pointer
        /// </summary>
        /// <typeparam name="T">The type of the data to covert the image pixel values to. e.g. "float" or "byte"</typeparam>
        /// <param name="image">The input image</param>
        /// <param name="dest">The native pointer where the image pixels values will be saved to.</param>
        /// <param name="inputHeight">The height of the image, must match the height requirement for the tensor</param>
        /// <param name="inputWidth">The width of the image, must match the width requirement for the tensor</param>
        /// <param name="inputMean">The mean value, it will be subtracted from the input image pixel values</param>
        /// <param name="scale">The scale, after mean is subtracted, the scale will be used to multiply the pixel values</param>
        /// <param name="flipUpSideDown">If true, the image needs to be flipped up side down</param>
        /// <param name="swapBR">If true, will flip the Blue channel with the Red. e.g. If false, the tensor's color channel order will be RGB. If true, the tensor's color channle order will be BGR </param>
        public static void ReadImageToTensor <T>(
            UIImage image,
            IntPtr dest,
            int inputHeight     = -1,
            int inputWidth      = -1,
            float inputMean     = 0.0f,
            float scale         = 1.0f,
            bool flipUpSideDown = false,
            bool swapBR         = false)
            where T : struct
        {
            if (inputHeight <= 0)
            {
                inputHeight = (int)image.Size.Height;
            }

            if (inputWidth <= 0)
            {
                inputWidth = (int)image.Size.Width;
            }


            int[] intValues = new int[inputWidth * inputHeight];
            System.Runtime.InteropServices.GCHandle handle = System.Runtime.InteropServices.GCHandle.Alloc(intValues, System.Runtime.InteropServices.GCHandleType.Pinned);
            using (CGImage cgimage = image.CGImage)
                using (CGColorSpace cspace = CGColorSpace.CreateDeviceRGB())
                    using (CGBitmapContext context = new CGBitmapContext(
                               handle.AddrOfPinnedObject(),
                               inputWidth,
                               inputHeight,
                               8,
                               inputWidth * 4,
                               cspace,
                               CGImageAlphaInfo.PremultipliedLast
                               ))
                    {
                        context.DrawImage(new CGRect(new CGPoint(), new CGSize(inputWidth, inputHeight)), cgimage);
                    }

            if (typeof(T) == typeof(float))
            {
                Emgu.TF.Util.Toolbox.Pixel32ToPixelFloat(
                    handle.AddrOfPinnedObject(),
                    inputWidth,
                    inputHeight,
                    inputMean,
                    scale,
                    flipUpSideDown,
                    swapBR,
                    dest
                    );
            }
            else if (typeof(T) == typeof(byte))
            {
                Emgu.TF.Util.Toolbox.Pixel32ToPixelByte(
                    handle.AddrOfPinnedObject(),
                    inputWidth,
                    inputHeight,
                    inputMean,
                    scale,
                    flipUpSideDown,
                    swapBR,
                    dest
                    );
            }
            else
            {
                throw new NotImplementedException(String.Format("Destination data type {0} is not supported.", typeof(T).ToString()));
            }
            handle.Free();
        }
コード例 #47
0
        /// <summary>
        /// Read an UIImage, covert the data and save it to the native pointer
        /// </summary>
        /// <typeparam name="T">The type of the data to covert the image pixel values to. e.g. "float" or "byte"</typeparam>
        /// <param name="image">The input image</param>
        /// <param name="dest">The native pointer where the image pixels values will be saved to.</param>
        /// <param name="inputHeight">The height of the image, must match the height requirement for the tensor</param>
        /// <param name="inputWidth">The width of the image, must match the width requirement for the tensor</param>
        /// <param name="inputMean">The mean value, it will be subtracted from the input image pixel values</param>
        /// <param name="scale">The scale, after mean is subtracted, the scale will be used to multiply the pixel values</param>
        /// <param name="flipUpSideDown">If true, the image needs to be flipped up side down</param>
        /// <param name="swapBR">If true, will flip the Blue channel with the Red. e.g. If false, the tensor's color channel order will be RGB. If true, the tensor's color channle order will be BGR </param>
        public static void ReadImageToTensor <T>(
            UIImage imageOriginal,
            IntPtr dest,
            int inputHeight     = -1,
            int inputWidth      = -1,
            float inputMean     = 0.0f,
            float scale         = 1.0f,
            bool flipUpSideDown = false,
            bool swapBR         = false)
            where T : struct
        {
            if (flipUpSideDown)
            {
                throw new NotImplementedException("Flip Up Side Down is Not implemented");
            }

            UIImage image;

            if (inputHeight > 0 || inputWidth > 0)
            {
                image = imageOriginal.Scale(new CGSize(inputWidth, inputHeight));
                //image.Dispose();
                //image = resized;
            }
            else
            {
                image = imageOriginal;
            }

            try
            {
                int[]   intValues   = new int[(int)(image.Size.Width * image.Size.Height)];
                float[] floatValues = new float[(int)(image.Size.Width * image.Size.Height * 3)];
                System.Runtime.InteropServices.GCHandle handle = System.Runtime.InteropServices.GCHandle.Alloc(intValues, System.Runtime.InteropServices.GCHandleType.Pinned);
                using (CGImage cgimage = image.CGImage)
                    using (CGColorSpace cspace = CGColorSpace.CreateDeviceRGB())
                        using (CGBitmapContext context = new CGBitmapContext(
                                   handle.AddrOfPinnedObject(),
                                   (nint)image.Size.Width,
                                   (nint)image.Size.Height,
                                   8,
                                   (nint)image.Size.Width * 4,
                                   cspace,
                                   CGImageAlphaInfo.PremultipliedLast
                                   ))
                        {
                            context.DrawImage(new CGRect(new CGPoint(), image.Size), cgimage);
                        }
                handle.Free();

                if (swapBR)
                {
                    for (int i = 0; i < intValues.Length; ++i)
                    {
                        int val = intValues[i];
                        floatValues[i * 3 + 0] = ((val & 0xFF) - inputMean) * scale;
                        floatValues[i * 3 + 1] = (((val >> 8) & 0xFF) - inputMean) * scale;
                        floatValues[i * 3 + 2] = (((val >> 16) & 0xFF) - inputMean) * scale;
                    }
                }
                else
                {
                    for (int i = 0; i < intValues.Length; ++i)
                    {
                        int val = intValues[i];
                        floatValues[i * 3 + 0] = (((val >> 16) & 0xFF) - inputMean) * scale;
                        floatValues[i * 3 + 1] = (((val >> 8) & 0xFF) - inputMean) * scale;
                        floatValues[i * 3 + 2] = ((val & 0xFF) - inputMean) * scale;
                    }
                }

                if (typeof(T) == typeof(float))
                {
                    Marshal.Copy(floatValues, 0, dest, floatValues.Length);
                }
                else if (typeof(T) == typeof(byte))
                {
                    //copy float to bytes
                    byte[] byteValues = new byte[floatValues.Length];
                    for (int i = 0; i < floatValues.Length; i++)
                    {
                        byteValues[i] = (byte)floatValues[i];
                    }
                    Marshal.Copy(byteValues, 0, dest, byteValues.Length);
                }
                else
                {
                    throw new NotImplementedException(String.Format("Destination data type {0} is not supported.", typeof(T).ToString()));
                }
            }
            finally
            {
                if (image != imageOriginal)
                {
                    image.Dispose();
                }
            }
        }
コード例 #48
0
ファイル: ConsoleWindow.cs プロジェクト: kampee/CLW-System
        /// <summary>
        /// Command handler for the history commands.
        /// The standard implementation of a console has a history function implemented when
        /// the user presses the UP or DOWN key.
        /// </summary>
        private void OnHistory(object sender, EventArgs e)
        {
            // Get the command to figure out from the ID if we have to get the previous or the
            // next element in the history.
            OleMenuCommand command = sender as OleMenuCommand;

            if (null == command ||
                command.CommandID.Guid != typeof(Microsoft.VisualStudio.VSConstants.VSStd2KCmdID).GUID)
            {
                return;
            }
            string historyEntry = null;

            if (command.CommandID.ID == (int)Microsoft.VisualStudio.VSConstants.VSStd2KCmdID.UP)
            {
                historyEntry = history.PreviousEntry();
            }
            else if (command.CommandID.ID == (int)Microsoft.VisualStudio.VSConstants.VSStd2KCmdID.DOWN)
            {
                historyEntry = history.NextEntry();
            }
            if (string.IsNullOrEmpty(historyEntry))
            {
                return;
            }

            // There is something to write on the console, so replace the current text in the
            // input line with the content of the history.

            lock (textLines)
            {
                // The input line starts by definition at the end of the text marker
                // used to mark the read only region.
                TextSpan[] span = new TextSpan[1];
                Microsoft.VisualStudio.ErrorHandler.ThrowOnFailure(
                    textStream.ReadOnlyMarker.GetCurrentSpan(span));

                // Get the last position in the buffer.
                int lastLine;
                int lastIndex;
                Microsoft.VisualStudio.ErrorHandler.ThrowOnFailure(
                    textLines.GetLastLineIndex(out lastLine, out lastIndex));

                // Now replace all this text with the text returned by the history.
                System.Runtime.InteropServices.GCHandle textHandle = GCHandle.Alloc(historyEntry, GCHandleType.Pinned);
                try
                {
                    Microsoft.VisualStudio.ErrorHandler.ThrowOnFailure(
                        textLines.ReplaceLines(span[0].iEndLine, span[0].iEndIndex, lastLine, lastIndex, textHandle.AddrOfPinnedObject(), historyEntry.Length, null));
                }
                finally
                {
                    // Free the memory inside the finally block to avoid memory leaks.
                    textHandle.Free();
                }
            }
        }