Пример #1
0
        public unsafe static void OnFillBuffer(byte * pData, long lDataLen)
        {
            Trace.WriteLine(++i + ": _EncodeInternal: " + lDataLen);

            using (var f = File.Open("dump.bin", FileMode.OpenOrCreate, FileAccess.ReadWrite))
            {
                f.Seek(0, SeekOrigin.End);

                using (var b = new BinaryWriter(f))
                {
                    b.Write((int)lDataLen);

                    using (UnmanagedMemoryStream ms = new UnmanagedMemoryStream(pData, lDataLen))
                    {
                        ms.CopyTo(f);
                    }
                }
            }

            //if (m_Bitmap == null)
            //{
            //    m_Bitmap = new Bitmap(@"D:\Dev\comDemo\Filters\logo.bmp");

            //    Rectangle bounds = new Rectangle(0, 0, m_Bitmap.Width, m_Bitmap.Height);
            //    var m_BitmapData = m_Bitmap.LockBits(bounds, ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
            //    var RowSizeBytes = m_BitmapData.Stride;

            //    int total_size = m_BitmapData.Stride * m_BitmapData.Height;
            //    if (ImageBytes == null || ImageBytes.Length != total_size)
            //    {
            //        ImageBytes = new byte[total_size];
            //    }
            //    // Copy the data into the ImageBytes array.
            //    Marshal.Copy(m_BitmapData.Scan0, ImageBytes, 0, total_size);

            //    encoder.EncodeRGBtoI420(ImageBytes, m_Bitmap.Width, m_Bitmap.Height, ref yuv, true);
            //}
            //else
            //if(f != null)
            {
                //f.OnFillBuffer(pData, lDataLen);
            }
        }
Пример #2
0
 public void Update()
 {
     //bool mutexCreated;
     //Mutex mutex = new Mutex(true, "InteractiveSpaceRectifiedTabletopMutex2", out mutexCreated);
     
     using (MemoryMappedViewStream mmfStream = rectifiedTabletopMmf.CreateViewStream())
     {
         unsafe
         {
             ReadLockedWrapperPtr ptr = ResultsDllWrapper.lockFactoryImage(ImageProductType.RectifiedTabletopProduct);
             using (UnmanagedMemoryStream srcStream = new UnmanagedMemoryStream(ptr.BytePtr, imgSize, imgSize, FileAccess.Read))
             {
                 srcStream.CopyTo(mmfStream);
             }
             ResultsDllWrapper.releaseReadLockedWrapperPtr(ptr);
         }
     }
     //mutex.ReleaseMutex();
 }
Пример #3
0
        /// <summary>
        /// Sends the specified UIImage to the AirPlay device.
        /// </summary>
        /// <param name='service'>
        /// NSNetService (extension method target) representing the AirPlay device.
        /// </param>
        /// <param name='image'>
        /// The UIImage to be send to the device.
        /// </param>
        /// <param name='complete'>
        /// Optional method to be called when the operation is complete. True will be supplied if the action was
        /// successful, false if a problem occured.
        /// </param>
        public static unsafe void SendTo(this NSNetService service, UIImage image, Action<bool> complete)
        {
            if (service == null) {
                if (complete != null)
                    complete (false);
                return;
            }

            // In general I prefer WebClient *Async methods but it does not provide methods to
            // upload Stream and allocating a (not really required) byte[] is a huge waste
            ThreadPool.QueueUserWorkItem (delegate {
                bool ok = true;
                try {
                    string url = String.Format ("http://{0}:{1}/photo", service.HostName, service.Port);
                    var req = (HttpWebRequest) WebRequest.Create (url);
                    using (var data = image.AsJPEG ()) {
                        req.Method = "PUT";
                        req.ContentLength = (int) data.Length;
                        req.UserAgent = "AirPlay/160.4 (Photos)";
                        req.Headers.Add ("X-Apple-AssetKey", Guid.NewGuid ().ToString ());
                        req.Headers.Add ("X-Apple-Session-ID", session.ToString ());
                        var s = req.GetRequestStream ();
                        using (Stream ums = new UnmanagedMemoryStream ((byte *) data.Bytes, (int) data.Length))
                            ums.CopyTo (s);
                    }
                    req.GetResponse ().Dispose ();
                }
                catch {
                    ok = false;
                }
                finally {
                    if (complete != null) {
                        NSRunLoop.Main.InvokeOnMainThread (delegate {
                            complete (ok);
                        });
                    }
                }
            });
        }
Пример #4
0
 public Sound CreateSound(UnmanagedMemoryStream data, SoundMode mode = SoundMode.Default)
 {
     var stream = new MemoryStream();
     data.CopyTo(stream);
     return CreateSound(stream.ToArray(), mode);
 }
Пример #5
0
        public unsafe void Compile(ScriptSource source, Stream compiledCodeDestination)
        {
            ClaimContext();

            uint bufferSize = 0;
            Errors.ThrowIfIs(api_.JsSerializeScript(source.SourceText, IntPtr.Zero, ref bufferSize));
            if (bufferSize > int.MaxValue)
                throw new OutOfMemoryException();

            IntPtr mem = Marshal.AllocCoTaskMem(unchecked((int)bufferSize));
            var error = api_.JsSerializeScript(source.SourceText, mem, ref bufferSize);
            if (error != JsErrorCode.JsNoError)
            {
                Marshal.FreeCoTaskMem(mem);
                Errors.ThrowFor(error);
            }

            using (UnmanagedMemoryStream ums = new UnmanagedMemoryStream((byte*)mem.ToPointer(), bufferSize))
            {
                ums.CopyTo(compiledCodeDestination);
            }
            Marshal.FreeCoTaskMem(mem);
        }
Пример #6
0
        public unsafe void SaveTerrain(string path, string name)
        {
            using (var stream = File.Create(path))
            {
                using (var bw = new BinaryWriter(stream, Encoding.Default, true))
                {
                    bw.Write(name);

                    bw.Write(this.Size.Width);
                    bw.Write(this.Size.Height);
                    bw.Write(this.Size.Depth);
                }

                fixed (TileData* v = this.m_tileGrid)
                {
                    byte* p = (byte*)v;
                    using (var memStream = new UnmanagedMemoryStream(p, this.Size.Volume * sizeof(TileData)))
                        memStream.CopyTo(stream);
                }

                fixed (byte* v = this.m_levelMap)
                {
                    byte* p = (byte*)v;
                    using (var memStream = new UnmanagedMemoryStream(p, this.Width * this.Height * sizeof(byte)))
                        memStream.CopyTo(stream);
                }
            }
        }
Пример #7
0
 public unsafe override SslStatus Write(IntPtr data, ref nint dataLength)
 {
     using (var ms = new UnmanagedMemoryStream ((byte*) data, dataLength)) {
         try {
             ms.CopyTo (InnerStream);
         }
         catch (IOException) {
             return SslStatus.ClosedGraceful;
         }
         catch {
             return SslStatus.Internal;
         }
     }
     return SslStatus.Success;
 }
Пример #8
0
		unsafe void WriteMesh (Mesh mesh, BinaryWriter writer) {
			var boneSlotCount = this.GetBoneSlotCount(mesh);

			VertexFlags flags;
			int stride;
			this.GetVertexFormat(mesh, boneSlotCount, out flags, out stride);
			var count = mesh.VertexCount;

			writer.Write(mesh.Name);
			writer.Write((uint)flags);
			writer.Write(boneSlotCount);
			writer.Write(mesh.MaterialIndex);
			var vertices = new float[stride * count];
			int idx = 0, offset = 0;
			foreach (var pos in mesh.Vertices) {
				vertices[idx] = pos.X;
				vertices[idx + 1] = pos.Y;
				vertices[idx + 2] = pos.Z;
				idx += stride;
			}
			offset = idx = 3;
			if (mesh.HasNormals) {
				foreach (var norm in mesh.Normals) {
					vertices[idx] = norm.X;
					vertices[idx + 1] = norm.Y;
					vertices[idx + 2] = norm.Z;
					idx += stride;
				}
				offset += 3;
				idx = offset;
			}
			for (var i = 0; i < 4; i++) {
				if (mesh.HasTextureCoords(i)) {
					if (mesh.UVComponentCount[i] != 2)
						Console.WriteLine("WARNING: texture coordinates should have 2 components, but this channel has " + mesh.UVComponentCount[i]);
					foreach (var uv in mesh.TextureCoordinateChannels[i]) {
						vertices[idx] = uv.X;
						vertices[idx + 1] = uv.Y;
						idx += stride;
					}
					offset += 2;
					idx = offset;
				}
			}
			for (var i = 0; i < 4; i++) {
				if (mesh.HasVertexColors(i)) {
					foreach (var c in mesh.VertexColorChannels[i]) {
						vertices[idx] = c.R;
						vertices[idx + 1] = c.G;
						vertices[idx + 2] = c.B;
						vertices[idx + 3] = c.A;
						idx += stride;
					}
					offset += 4;
					idx = offset;
				}
			}
			// add bone information to every vertex
			for(var i = 0; i < mesh.BoneCount; i++) {
				foreach (var weight in mesh.Bones[i].VertexWeights) {
					if (weight.Weight > 0f) {
						idx = (stride * weight.VertexID) + offset;
						while (vertices[idx] != 0f)
							idx++;
						vertices[idx] = weight.Weight;
						vertices[idx + boneSlotCount] = i;
					}
				}
			}
			writer.Write(vertices.Length);
			writer.Flush();
			fixed(float *fp = &vertices[0]) {
				byte* bp = (byte*)fp;
				using (var ms = new UnmanagedMemoryStream(bp, vertices.Length * sizeof(float))) {
					ms.CopyTo(writer.BaseStream);
				}
			}

			count = mesh.FaceCount;
			var indices = new int[count * 3];
			idx = 0;
			for (var i = 0; i < count; i++) {
				var face = mesh.Faces[i];
				var faceIndices = face.Indices;
				if (face.IndexCount != 3)
					throw new InvalidOperationException("Polygonal faces are not supported, faces must be triangles.");
				indices[idx++] = faceIndices[0];
				indices[idx++] = faceIndices[1];
				indices[idx++] = faceIndices[2];
			}
			writer.Write(indices.Length);
			writer.Flush();
			fixed(int *ip = &indices[0]) {
				byte* bp = (byte*)ip;
				using (var ms = new UnmanagedMemoryStream(bp, indices.Length * sizeof(float))) {
					ms.CopyTo(writer.BaseStream);
				}
			}

			count = mesh.BoneCount;
			writer.Write(count);
			for (var i = 0; i < count; i++) {
				var bone = mesh.Bones[i];
				writer.Write(bone.Name);
				writer.Write(bone.OffsetMatrix);
				var weightCount = bone.VertexWeightCount;
				writer.Write(weightCount);
				for (var j = 0; j < weightCount; j++) {
					var weight = bone.VertexWeights[i];
					writer.Write(weight.VertexID);
					writer.Write(weight.Weight);
				}
			}
		}