//----------------------------------// /// <summary> /// Get a byte collection of the specified string representation of a /// hexidecimal number. /// </summary> public static byte[] ToBytes(this string str) { // is the string the correct length? if (str.Length == 0 || str.Length % 2 != 0) { return(new byte[0]); } byte[] buffer = BufferCache.Get(); int length = str.Length / 2; char c; for (int bx = 0, sx = 0; bx < length; ++bx, ++sx) { // convert first half of byte c = str[sx]; buffer[bx] = (byte)((c > Chars.n9 ? (c > Chars.Z ? (c - Chars.a + 10) : (c - Chars.A + 10)) : (c - Chars.n0)) << 4); // convert second half of byte c = str[++sx]; buffer[bx] |= (byte)(c > Chars.n9 ? (c > Chars.Z ? (c - Chars.a + 10) : (c - Chars.A + 10)) : (c - Chars.n0)); } return(buffer); }
// Start is called before the first frame update void Start() { //Vector2 a = new Vector2(0f, 0f); //Vector2 b = new Vector2(1.0f, 1.0f); //var v = Vector2.Lerp(a, b,0.3f); //FSFloat t = 1; //Debug.Log(v.ToString()); //FSQuaternion r = new FSVector4(1,1,1,1); //Quaternion var r = new Transporter(null, null); var obj = new CustomProtocol.GameStart(); obj.playId = 1001; obj.udpPort = 3001; ushort t = 0; var seg = r.Encode(obj, out t); var c = new BufferCache(); c.Write(seg.Array, seg.Array.Length); var y = r.Decode(c); Debug.Log("id:" + y.MsgId); var result = y.MsgObject as GameStart; Debug.Log("playId:" + result.playId + " " + result.udpPort); }
/// <summary> /// Creates a new AsyncSocket. You must call Start() after creating the AsyncSocket /// in order to begin receive data. /// </summary> internal AsyncUdpSocket(Socket socket, IPEndPoint localEndpoint, Action <IPEndPoint, byte[], int> onReceive, Action <Exception> onError) { Socket = socket; OnReceive = onReceive; OnError = onError; LocalEndPoint = localEndpoint; RemoteEndPoint = new IPEndPoint(IPAddress.Any, 0); TargetEndPoint = RemoteEndPoint; _sendLock = new Lock(); _readLock = new Lock(); _enqueueBuffer = new BufferQueue(); _sendQueue = new Shared <ArrayRig <QueuedBuffer> >(new ArrayRig <QueuedBuffer>()); _syncLock = new LockReadWrite(); _onReceive = new ActionSequence(); _receiveBuffer = BufferCache.Get(); _receivedChunks = new Dictionary <int, ChunkedGram>(); _disposing = false; }
/// <summary> /// Pass the 'inStream' through exiftool and write the output to the 'outStream'. /// </summary> private void WriteTemporaryFile(Stream file, Stream input) { // get a buffer var buffer = BufferCache.Get(); // read from the input stream int count = input.Read(buffer, 0, Global.BufferSizeLocal); // write the buffer to the file file.Write(buffer, 0, count); // reset the buffer BufferCache.Set(buffer); // is this the final buffer? if (count < Global.BufferSizeLocal) { // yes, dispose of the file stream file.Dispose(); var process = _coordinator.Process.TakeItem(); process.StandardInput.BaseStream.Write(_bytesRemoveMetadata, 0, _bytesRemoveMetadata.Length); byte[] pathBytes = System.Text.Encoding.ASCII.GetBytes(Path); process.StandardInput.BaseStream.Write(pathBytes, 0, pathBytes.Length); process.StandardInput.BaseStream.Write(_coordinator.BytesExecute, 0, _coordinator.BytesExecute.Length); process.StandardInput.BaseStream.Flush(); _coordinator.Process.Release(); // add this task to run with the coordinators process _coordinator.Add(this); } else { // continue writing the temporary file ManagerUpdate.Control.AddSingle(WriteTemporaryFile, file, input); } }
/// <summary> /// Send the specified string to the client. /// </summary> public unsafe void Send(string str, HttpSendOptions options = null, IAction onSent = null) { Log.Info("Sending string '" + str + "' to client " + this + "."); // is the string empty? skip if (string.IsNullOrEmpty(str)) { return; } _lock.Take(); // process send options ProcessOptions(options); _onSent = onSent; var buffer = BufferCache.Get(str.Length + str.Length); int count; // get pointers to the string and the send byte buffer fixed(char *src = str) fixed(byte *dst = &buffer[0]) { // get the bytes that the string represents count = _encoder.GetBytes(src, str.Length, dst, buffer.Length, true); } SendBytes(buffer, 0, count); BufferCache.Set(buffer); }
public static RecogReply PredictionFunc(Guid id, int timeBudgetInMS, RecogRequest req) { byte[] imgBuf = req.Data; byte[] imgType = System.Text.Encoding.UTF8.GetBytes("jpg"); Guid imgID = BufferCache.HashBufferAndType(imgBuf, imgType); string imgFileName = imgID.ToString() + ".jpg"; string filename = Path.Combine(saveImageDir, imgFileName); if (!File.Exists(filename)) { FileTools.WriteBytesToFileConcurrent(filename, imgBuf); } Stopwatch timer = Stopwatch.StartNew(); CaffeModel.SetDevice(gpu); string resultString = predictor.Predict(filename); timer.Stop(); File.Delete(filename); numImageRecognized++; Console.WriteLine("Image {0}:{1}:{2}: {3}", numImageRecognized, imgFileName, timer.Elapsed, resultString); return(VHubRecogResultHelper.FixedClassificationResult(resultString, resultString)); }
public void Dispose() { ImageCache = null; TextureCache = null; MaterialCache = null; BufferCache.Clear(); MeshCache = null; }
/// <summary> /// Allocate an event arg for a receive operation /// </summary> public static SocketAsyncEventArgs AllocateForReceive(EventHandler <SocketAsyncEventArgs> ioCompletedHandler) { SocketAsyncEventArgs result; if (!_eventArgsReceive.Dequeue(out result)) { result = new SocketAsyncEventArgs(); result.SetBuffer(BufferCache.Get(), 0, Global.BufferSizeLocal); } result.Completed += ioCompletedHandler; return(result); }
public unsafe ComposedLut(IComposableLut[] luts, BufferCache<int> cache, BufferCache<double> doubleCache) { //luts.Validate(); int lutCount; IComposableLut firstLut, lastLut; GetFirstAndLastLut(luts, out firstLut, out lastLut, out lutCount); _minInputValue = (int) Math.Round(firstLut.MinInputValue); _maxInputValue = (int) Math.Round(firstLut.MaxInputValue); _minOutputValue = (int) Math.Round(lastLut.MinOutputValue); _maxOutputValue = (int) Math.Round(lastLut.MaxOutputValue); _length = _maxInputValue - _minInputValue + 1; _data = cache != null ? cache.Allocate(_length) : MemoryManager.Allocate<int>(_length); const int intermediateDataSize = 8192; // each double entry is 8 bytes so the entire array is 64kB - just small enough to stay off the large object heap var intermediateData = doubleCache != null ? doubleCache.Allocate(intermediateDataSize) : MemoryManager.Allocate<double>(intermediateDataSize); try { fixed (double* intermediateLutData = intermediateData) fixed (int* composedLutData = _data) { var min = _minInputValue; var max = _maxInputValue + 1; var pComposed = composedLutData; // performs the bulk lookups in 64kB chunks (8k entries @ 8 bytes per) in order to keep the intermediate buffer off the large object heap for (var start = min; start < max; start += intermediateDataSize) { var stop = Math.Min(max, start + intermediateDataSize); var count = stop - start; var pIntermediate = intermediateLutData; for (var i = start; i < stop; ++i) *pIntermediate++ = i; for (var j = 0; j < lutCount; ++j) luts[j].LookupValues(intermediateData, intermediateData, count); pIntermediate = intermediateLutData; for (var i = 0; i < count; ++i) *pComposed++ = (int) Math.Round(*pIntermediate++); } } } finally { if (doubleCache != null) doubleCache.Return(intermediateData); } }
public Transporter(NetClient nc, Socket netSocket) { buffer = new byte[MAX_PACKAGE_LENGTH]; memoryStream = new MemoryStream(buffer); binaryWriter = new BinaryWriter(memoryStream); revCache = new BufferCache(MAX_PACKAGE_LENGTH); if (nc == null || netSocket == null) { Debug.Log("netclient or socket is null"); return; } this.nClient = nc; this.socket = netSocket; }
/// <summary> /// Lookup characters nominal glyphs and width /// </summary> /// <param name="charBufferRange">character buffer range</param> /// <param name="emSize">height of Em</param> /// <param name="toIdeal"> scaling factor from real to ideal unit </param> /// <param name="nominalWidths">glyph nominal advances in ideal units</param> /// <param name="idealWidth">total width in ideal units</param> /// <returns>true for success</returns> /// <remarks>This function is only used in fast path, and can only be called /// if CheckFastPathNominalGlyphs has previously returned true.</remarks> internal void GetCharacterNominalWidthsAndIdealWidth( CharacterBufferRange charBufferRange, double emSize, float pixelsPerDip, double toIdeal, TextFormattingMode textFormattingMode, bool isSideways, out int[] nominalWidths, out int idealWidth ) { // This function should only be called if CheckFastPathNominalGlyphs has // returned true so we can assume the ITypefaceMetrics is a GlyphTypeface. GlyphTypeface glyphTypeface = TryGetGlyphTypeface(); Invariant.Assert(glyphTypeface != null); MS.Internal.Text.TextInterface.GlyphMetrics[] glyphMetrics = BufferCache.GetGlyphMetrics(charBufferRange.Length); glyphTypeface.GetGlyphMetricsOptimized(charBufferRange, emSize, pixelsPerDip, textFormattingMode, isSideways, glyphMetrics); nominalWidths = new int[charBufferRange.Length]; idealWidth = 0; if (TextFormattingMode.Display == textFormattingMode) { double designToEm = emSize / glyphTypeface.DesignEmHeight; for (int i = 0; i < charBufferRange.Length; i++) { nominalWidths[i] = (int)Math.Round(TextFormatterImp.RoundDipForDisplayMode(glyphMetrics[i].AdvanceWidth * designToEm, pixelsPerDip) * toIdeal); idealWidth += nominalWidths[i]; } } else { double designToEm = emSize * toIdeal / glyphTypeface.DesignEmHeight; for (int i = 0; i < charBufferRange.Length; i++) { nominalWidths[i] = (int)Math.Round(glyphMetrics[i].AdvanceWidth * designToEm); idealWidth += nominalWidths[i]; } } BufferCache.ReleaseGlyphMetrics(glyphMetrics); }
/// <summary> /// 释放数据缓存 /// </summary> public void FreeDataCache() { if (BufferCache != null) { if (IsFixCache) { BufferCache.FreeBuffer(Offset); } else { BufferCache.FreePoolBuffer(Buffer); } } }
public static RecogReply PredictionFunc(Guid id, int timeBudgetInMS, RecogRequest req) { byte[] imgBuf = req.Data; byte[] imgType = System.Text.Encoding.UTF8.GetBytes("jpg"); Guid imgID = BufferCache.HashBufferAndType(imgBuf, imgType); string imgFileName = imgID.ToString() + ".jpg"; string resultString = ""; try { string filename = Path.Combine(saveImageDir, imgFileName); if (!File.Exists(filename)) { FileTools.WriteBytesToFileConcurrent(filename, imgBuf); } Directory.SetCurrentDirectory(rootDir); var processStartInfo = new ProcessStartInfo { FileName = @"main.bat", Arguments = filename, //RedirectStandardOutput = true, UseShellExecute = false }; var process = Process.Start(processStartInfo); //var resultString = process.StandardOutput.ReadToEnd(); process.WaitForExit(); if (File.Exists(filename + ".caption")) { resultString = File.ReadAllLines(filename + ".caption")[0]; } File.Delete(filename); File.Delete(filename + "_1.txt"); File.Delete(filename + "_1.txt.detections.prec.txt"); File.Delete(filename + "_1.txt.detections.sc.txt"); File.Delete(filename + "_1.txt.img.dmsm.fea"); File.Delete(filename + ".caption"); } catch (Exception e) { Logger.Log(LogLevel.Error, e.Message); resultString = "Service exception. Please try again."; } numImageRecognized++; Console.WriteLine("Image {0}: {1}", numImageRecognized, resultString); return(VHubRecogResultHelper.FixedClassificationResult(resultString, resultString)); }
/// <summary> /// Dispose this file reader. /// </summary> public void Dispose() { Running = false; _timeout.Run = false; Extractor.Dispose(); if (_connection != null) { _connection.Dispose(); } BufferCache.Set(_buffer); Decoder = null; _parser = null; _files = null; _lock = null; }
/// <summary> /// Prepare the specified number of bytes. This ensures that at least the specified /// number of bytes are sent to the 'Next' method each call. /// </summary> protected void BufferCount(int length) { if (length == 0) { _buffer = false; } else { _buffer = true; _bufferTarget = length; if (_bufferBytes == null || _bufferBytes.Length < _bufferTarget) { _bufferBytes = BufferCache.Get(length); } } }
/// <summary> /// Append the specified bytes to the next read. /// </summary> protected void Buffer(byte[] bytes, int offset, int length) { _buffer = true; // does the buffer have room for the specified bytes? if (length > _bufferTarget - _bufferIndex) { // no, resize the prepared byte array _bufferBytes = BufferCache.Get(_bufferTarget + length); } // copy the byte buffer into the prepared collection Micron.CopyMemory(bytes, offset, _bufferBytes, _bufferIndex, length); // increment the index _bufferIndex += length; }
/// <summary> /// Send the specified element to the client. /// </summary> public unsafe void Send(Element element, bool buildStyle = true, HttpSendOptions options = null, IAction onSent = null) { Log.Info("Sending element '" + element + "' to client " + this + "."); if (buildStyle) { // build the style var style = element.FindChild("style"); style.ContentString = element.BuildCss(); element.EncodeContent = false; } // build the element into a string string str = "<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01//EN\" \"http://www.w3.org/TR/html4/strict.dtd\">" + element.Build(); _lock.Take(); // have the options been specified? yes, process them if (options == null || options.ContentType == null) { Headers[HttpResponseHeader.ContentType] = "text/html; charset=UTF-8"; } ProcessOptions(options); _onSent = onSent; // resize buffer as required var buffer = BufferCache.Get(str.Length + str.Length); // get pointers to the string and the send byte buffer int count; fixed(char *src = str) fixed(byte *dst = &buffer[0]) { // get the bytes that the string represents count = _encoder.GetBytes(src, str.Length, dst, buffer.Length, true); } // send the bytes SendBytes(buffer, 0, count); BufferCache.Set(buffer); }
/// <summary> /// Send the specified string to the client. /// </summary> public unsafe void Send(string str, TransportOptions options = null, IAction onSent = null) { // is disposed? if (_disposed) { Log.Warning("Cannot send from disposed connection."); return; } // reset the timeout if (_timeoutTimer != null) { _timeoutTimer.Reset(_timeoutMilliseconds); } // is the string empty? skip if (string.IsNullOrEmpty(str)) { return; } _lock.Take(); // process send options ProcessOptions(options); _onSent = onSent; var buffer = BufferCache.Get(str.Length + str.Length); int count; // get pointers to the string and the send byte buffer fixed(char *src = str) fixed(byte *dst = &buffer[0]) { // get the bytes that the string represents count = _encoder.GetBytes(src, str.Length, dst, buffer.Length, true); } // send the byte buffer SendBytes(buffer, 0, count); // cache the buffer BufferCache.Set(buffer); }
/// <summary> /// Buffer processed data and send to the receive callback. /// </summary> private void ProcessReceivedData() { // get a buffer to dequeue into var buffer = BufferCache.Get(); try { // dequeue a buffer from the pending received data int count = _receivedBuffer.TakeItem().Dequeue(buffer, 0, Global.BufferSizeLocal); _receivedBuffer.Release(); if (count == 0) { // allocate the buffer cache once more BufferCache.Set(buffer); } else { // remove //Log.Debug(new string(System.Text.Encoding.UTF8.GetChars(buffer, 0, count))); // run the callback action _onReceive.AddRun(ActionSet.New(OnReceive, buffer, count)); if (count == Global.BufferSizeLocal) { ManagerUpdate.Control.AddSingle(ProcessReceivedData); return; } } // no bytes to receive CompleteReceive(); } catch (SocketException ex) { _readLock.Release(); BufferCache.Set(buffer); if (ex.SocketErrorCode == SocketError.ConnectionReset) { return; } ProcessError(ex); } catch (Exception ex) { _readLock.Release(); BufferCache.Set(buffer); ProcessError(ex); } }
public ReceiveData Decode(BufferCache cache) { if (cache.Length < sizeof(Int16) * 2) { return(null); } int startPtr = cache.ReadPtr; int packageLength = cache.ReadUInt16(); #if net_log Debug.Log("decode read first 2 byte packageLength:" + packageLength); #endif if (packageLength > Transporter.MAX_PACKAGE_LENGTH) { Debug.Log("Transporter receive package length over max length :" + packageLength); cache.ReadPtr = startPtr; return(null); } if (cache.Length < packageLength) { Debug.Log("decode packageLenght over cache length packageLength:" + packageLength + "cache.Length:" + cache.Length); cache.ReadPtr = startPtr; return(null); } UInt16 typeId = cache.ReadUInt16(); #if net_log Debug.Log("decode typeId:" + typeId); #endif Type packageType = ClientProtocol.GetTypeById(typeId); if (packageType == null) { Debug.Log("Transporter receive package can not find id" + typeId); return(null); } int packageBodyLength = packageLength - 2 - 4; cache.ReadUInt16(); cache.ReadUInt16(); ReceiveData data = new ReceiveData(); data.MsgId = typeId; data.MsgObject = Activator.CreateInstance(packageType); MemoryStream mStream = cache.GetMemoryStream(packageBodyLength); RuntimeTypeModel.Default.Deserialize(mStream, data.MsgObject, packageType); return(data); }
public static DynamicOutputBuffer Get(int code) { lock (BufferCache) { foreach (var db in BufferCache) { if (!db.Used()) { db.Reset(code); return(db); } } DynamicOutputBuffer b = new DynamicOutputBuffer(code); b.Reset(code); BufferCache.Add(b); return(b); } }
public ComposedLut(LutCollection luts, BufferCache <int> cache) { //luts.Validate(); int lutCount; IComposableLut firstLut, lastLut; GetFirstAndLastLut(luts, out firstLut, out lastLut, out lutCount); _minInputValue = (int)Math.Round(firstLut.MinInputValue); _maxInputValue = (int)Math.Round(firstLut.MaxInputValue); _length = _maxInputValue - _minInputValue + 1; _data = cache != null?cache.Allocate(_length) : MemoryManager.Allocate <int>(_length); //copy to array because accessing ObservableList's indexer in a tight loop is very expensive IComposableLut[] lutArray = new IComposableLut[lutCount]; luts.CopyTo(lutArray, 0); unsafe { fixed(int *composedLutData = _data) { int *pLutData = composedLutData; int min = _minInputValue; int max = _maxInputValue + 1; for (int i = min; i < max; ++i) { double val = i; for (int j = 0; j < lutCount; ++j) { val = lutArray[j][val]; } *pLutData = (int)Math.Round(val); ++pLutData; } } } }
/// <summary> /// Send a buffer of bytes to the client. Releases the lock. /// </summary> protected void SendBytes(byte[] bytes, int offset, int count) { try { // yes, does the buffer need to be compressed? if (_header.Compression != DecompressionMethods.None) { // yes, compress the bytes bytes = StreamHelper.Compress(_header.Compression, System.IO.Compression.CompressionLevel.Optimal, bytes, offset, ref count); offset = 0; } // are the bytes to be encrypted? if (_header.EncryptionPassword != null) { // yes, encrypt them byte[] buffer = BufferCache.Get(count); Buffer.BlockCopy(bytes, offset, buffer, 0, count); bytes = Crypto.EncryptWithPassword(buffer, _header.EncryptionPassword); // send headers with the content length EnqueueHeader(bytes.Length); // send the bytes AsyncSocket.Enqueue(bytes, 0, bytes.Length); AsyncSocket.Send(_onSent); } else { // send headers with the content length EnqueueHeader(count); // send the bytes AsyncSocket.Enqueue(bytes, offset, count); AsyncSocket.Send(_onSent); } } finally { _onSent = null; _lock.Release(); } }
/// <summary> /// Initialize a file extractor with a collection of file paths, the extraction instance and the /// encoding of the file. /// </summary> public FileExtractor(ArrayRig <string> files, Extract extractor = null, Encoding encoding = null) { _files = new Queue <string>(); // add the files to the queue foreach (string file in files) { _files.Enqueue(file); } // persist the extractor Extractor = extractor; Decoder = (encoding ?? Encoding.UTF8).GetDecoder(); // initialize the buffers _buffer = BufferCache.Get(); _chars = new char[(encoding ?? Encoding.UTF8).GetMaxCharCount(Global.BufferSizeLocal)]; _timeout = new Timer(Timeout, Run, false); _lock = new Lock(); }
public static RecogReply PredictionFunc(Guid id, int timeBudgetInMS, RecogRequest req) { byte[] data = req.Data; byte[] dataType = System.Text.Encoding.UTF8.GetBytes("mllr"); Guid dataID = BufferCache.HashBufferAndType(data, dataType); string dataFileName = dataID.ToString() + ".mllr"; string filename = Path.Combine(saveDataDir, dataFileName); if (!File.Exists(filename)) { FileTools.WriteBytesToFileConcurrent(filename, data); } Directory.SetCurrentDirectory(rootDir); var processStartInfo = new ProcessStartInfo { FileName = @"main.bat", Arguments = filename, //RedirectStandardOutput = true, UseShellExecute = false }; var process = Process.Start(processStartInfo); //var resultString = process.StandardOutput.ReadToEnd(); process.WaitForExit(); string resultString = ""; if (File.Exists(filename + ".mllr")) { resultString = File.ReadAllText(filename + ".mllr"); } File.Delete(filename); File.Delete(filename + ".mllr"); numDataProcessed++; Console.WriteLine("Data {0}: {1}", numDataProcessed, resultString); return(VHubRecogResultHelper.FixedClassificationResult(resultString, resultString)); }
/// <summary> /// Pass the 'inStream' through exiftool and write the output to the 'outStream'. /// </summary> private void ReadTemporaryFile(Stream file) { // get a buffer byte[] buffer = BufferCache.Get(); // read from the stream int count = file.Read(buffer, 0, Global.BufferSizeLocal); // write the buffer to the output stream Output.Write(buffer, 0, count); // is this the final buffer? if (count < Global.BufferSizeLocal) { // yes, dispose of the file stream file.Dispose(); // reset the buffer BufferCache.Set(buffer); Success = true; // run callback OnComplete.Run(); // should the file be removed? if (_removeTempFile) { ManagerResources.RemoveFile(Path); } } else { // no, reset the buffer BufferCache.Set(buffer); // add a task to clear the metadata ManagerUpdate.Control.AddSingle(ReadTemporaryFile, file); } }
public ComposedLut(IComposableLut[] luts, BufferCache<int> cache) { //luts.Validate(); int lutCount; IComposableLut firstLut, lastLut; GetFirstAndLastLut(luts, out firstLut, out lastLut, out lutCount); _minInputValue = (int)Math.Round(firstLut.MinInputValue); _maxInputValue = (int)Math.Round(firstLut.MaxInputValue); _minOutputValue = (int)Math.Round(lastLut.MinOutputValue); _maxOutputValue = (int)Math.Round(lastLut.MaxOutputValue); _length = _maxInputValue - _minInputValue + 1; _data = cache != null ? cache.Allocate(_length) : MemoryManager.Allocate<int>(_length); //copy to array because accessing ObservableList's indexer in a tight loop is very expensive unsafe { fixed (int* composedLutData = _data) { int* pLutData = composedLutData; int min = _minInputValue; int max = _maxInputValue + 1; for (int i = min; i < max; ++i) { double val = i; for (int j = 0; j < lutCount; ++j) val = luts[j][val]; *pLutData = (int) Math.Round(val); ++pLutData; } } } }
/// <summary>Clean up any resources being used.</summary> /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param> protected override void Dispose(bool disposing) { if (!_isDisposed) { if (disposing) { if (components != null) { components.Dispose(); } if (BufferCache != null) { BufferCache.Dispose(); BufferCache = null; } BufferBack.Dispose(); BufferBack = null; BufferMap.Dispose(); BufferMap = null; BufferShading.Dispose(); BufferShading = null; BufferUnits.Dispose(); BufferUnits = null; } _isDisposed = true; base.Dispose(disposing); } }
public static RecogReply PredictionFunc(Guid id, int timeBudgetInMS, RecogRequest req) { byte[] imgBuf = req.Data; byte[] imgType = System.Text.Encoding.UTF8.GetBytes("jpg"); Guid imgID = BufferCache.HashBufferAndType(imgBuf, imgType); string imgFileName = imgID.ToString() + ".jpg"; string filename = Path.Combine(saveImageDir, imgFileName); if (!File.Exists(filename)) { FileTools.WriteBytesToFileConcurrent(filename, imgBuf); } string result_words = wordDetector.DetectWords(filename); string result_feature = wordDetector.ExtractFeature(filename); string resultString = result_words + "\n" + result_feature; File.Delete(filename); numImageRecognized++; Console.WriteLine("Image {0}: {1}", numImageRecognized, resultString); return(VHubRecogResultHelper.FixedClassificationResult(resultString, resultString)); }
/// <summary> /// Scan through specified character string checking for valid character /// nominal glyph. /// </summary> /// <param name="charBufferRange">character buffer range</param> /// <param name="emSize">height of Em</param> /// <param name="scalingFactor">This is the factor by which we will scale up /// the metrics. Typically this value to used to convert metrics from the real /// space to the ideal space</param> /// <param name="widthMax">maximum width allowed</param> /// <param name="keepAWord">do not stop arbitrarily in the middle of a word</param> /// <param name="numberSubstitution">digits require complex shaping</param> /// <param name="cultureInfo">CultureInfo of the text</param> /// <param name="textFormattingMode">The TextFormattingMode used (Ideal vs. Display)</param> /// <param name="isSideways">Indicates whether to rotate glyphs.</param> /// <param name="breakOnTabs">Determines whether to stop checking at a tab and /// break the run there</param> /// <param name="stringLengthFit">number of character fit in given width</param> /// <returns>whether the specified string can be optimized by nominal glyph lookup</returns> internal bool CheckFastPathNominalGlyphs( CharacterBufferRange charBufferRange, double emSize, double scalingFactor, double widthMax, bool keepAWord, bool numberSubstitution, CultureInfo cultureInfo, TextFormattingMode textFormattingMode, bool isSideways, bool breakOnTabs, out int stringLengthFit ) { stringLengthFit = 0; if (CachedTypeface.NullFont) { return(false); } GlyphTypeface glyphTypeface = TryGetGlyphTypeface(); if (glyphTypeface == null) { return(false); } double totalWidth = 0; int i = 0; ushort blankGlyph = glyphTypeface.BlankGlyphIndex; ushort glyph = blankGlyph; ushort charFlagsMask = numberSubstitution ? (ushort)(CharacterAttributeFlags.CharacterComplex | CharacterAttributeFlags.CharacterDigit) : (ushort)CharacterAttributeFlags.CharacterComplex; ushort charFlags = 0; ushort charFastTextCheck = (ushort)(CharacterAttributeFlags.CharacterFastText | CharacterAttributeFlags.CharacterIdeo); bool symbolTypeface = glyphTypeface.Symbol; if (symbolTypeface) { // we don't care what code points are present if it's a non-Unicode font such as Symbol or Wingdings; // the code points don't have any standardized meanings, and we always want to bypass shaping charFlagsMask = 0; } bool ignoreWidths = widthMax == double.MaxValue; ushort[] glyphIndices = BufferCache.GetUShorts(charBufferRange.Length); MS.Internal.Text.TextInterface.GlyphMetrics[] glyphMetrics = ignoreWidths ? null : BufferCache.GetGlyphMetrics(charBufferRange.Length); glyphTypeface.GetGlyphMetricsOptimized(charBufferRange, emSize, glyphIndices, glyphMetrics, textFormattingMode, isSideways ); double designToEm = emSize / glyphTypeface.DesignEmHeight; // // This block will advance until one of: // 1. The end of the charBufferRange is reached // 2. The charFlags have some of the charFlagsMask values // 3. The glyph is BlankGlyph (space) // 4. Glyph index is 0 (unless symbol font) // // At this point totalWidth includes all of the widths including the stop character (which fits above) // i indexes the next character (not included in the width) // if (keepAWord) { do { char ch = charBufferRange[i++]; if (ch == TextStore.CharLineFeed || ch == TextStore.CharCarriageReturn || (breakOnTabs && ch == TextStore.CharTab)) { --i; break; } else { int charClass = (int)Classification.GetUnicodeClassUTF16(ch); charFlags = Classification.CharAttributeOf(charClass).Flags; charFastTextCheck &= charFlags; glyph = glyphIndices[i - 1]; if (!ignoreWidths) { totalWidth += TextFormatterImp.RoundDip(glyphMetrics[i - 1].AdvanceWidth * designToEm, textFormattingMode) * scalingFactor; } } } while( i < charBufferRange.Length && ((charFlags & charFlagsMask) == 0) && (glyph != 0 || symbolTypeface) && glyph != blankGlyph ); // i is now at a character immediately following a leading blank } // // This block will advance until one of: // 1. The end of the charBufferRange is reached // 2. The charFlags have some of the charFlagsMask values // 3. Glyph index is 0 (unless symbol font) // 4. totalWidth > widthMax // while ( i < charBufferRange.Length && (ignoreWidths || totalWidth <= widthMax) && ((charFlags & charFlagsMask) == 0) && (glyph != 0 || symbolTypeface) ) { char ch = charBufferRange[i++]; if (ch == TextStore.CharLineFeed || ch == TextStore.CharCarriageReturn || (breakOnTabs && ch == TextStore.CharTab)) { --i; break; } else { int charClass = (int)Classification.GetUnicodeClassUTF16(ch); charFlags = Classification.CharAttributeOf(charClass).Flags; charFastTextCheck &= charFlags; glyph = glyphIndices[i - 1]; if (!ignoreWidths) { totalWidth += TextFormatterImp.RoundDip(glyphMetrics[i - 1].AdvanceWidth * designToEm, textFormattingMode) * scalingFactor; } } } BufferCache.ReleaseUShorts(glyphIndices); glyphIndices = null; BufferCache.ReleaseGlyphMetrics(glyphMetrics); glyphMetrics = null; if (symbolTypeface) { // always optimize for non-Unicode font as we don't support shaping or typographic features; // we also don't fall back from non-Unicode fonts so we don't care if there are missing glyphs stringLengthFit = i; return(true); } if (glyph == 0) { // character is not supported by the font return(false); } if ((charFlags & charFlagsMask) != 0) { // complex character encountered, exclude it Debug.Assert(i > 0); if (--i <= 0) { // first char is complex, fail the call return(false); } } stringLengthFit = i; TypographyAvailabilities typography = glyphTypeface.FontFaceLayoutInfo.TypographyAvailabilities; if ((charFastTextCheck & (byte)CharacterAttributeFlags.CharacterFastText) != 0) { // all input code points are Fast Text if ((typography & (TypographyAvailabilities.FastTextTypographyAvailable | TypographyAvailabilities.FastTextMajorLanguageLocalizedFormAvailable ) ) != 0 ) { // Considered too risky to optimize. It is either because the font // has required features or the font has 'locl' lookup for major languages. return(false); } else if ((typography & TypographyAvailabilities.FastTextExtraLanguageLocalizedFormAvailable) != 0) { // The font has 'locl' lookup for FastText code points for non major languages. // Check whether the input is major langauge. If it is, we are still good to optimize. return(MajorLanguages.Contains(cultureInfo)); } else { // No FastText flags are present, safe to optimize return(true); } } else if ((charFastTextCheck & (byte)CharacterAttributeFlags.CharacterIdeo) != 0) { // The input are all ideographs, check the IdeoTypographyAvailable bit. It is safe if // the bit is not set. return((typography & TypographyAvailabilities.IdeoTypographyAvailable) == 0); } else { // for all the rest of the cases, just check whether there is any required typography // present at all. If none exists, it is optimizable. We might under-optimize here but // it will be non-major languages. return((typography & TypographyAvailabilities.Available) == 0); } }
internal sealed override unsafe void GetAdvanceWidthsUnshaped( char *characterString, int characterLength, double scalingFactor, int *advanceWidthsUnshaped ) { if (!IsShapingRequired) { if ((_shapeTypeface != null) && (_shapeTypeface.DeviceFont != null)) { // Use device font to compute advance widths _shapeTypeface.DeviceFont.GetAdvanceWidths( characterString, characterLength, _emSize * scalingFactor, advanceWidthsUnshaped ); } else { bool nullFont; GlyphTypeface glyphTypeface = GetGlyphTypeface(out nullFont); Invariant.Assert(glyphTypeface != null); glyphTypeface.GetAdvanceWidthsUnshaped( characterString, characterLength, _emSize, (float)_properties.PixelsPerDip, scalingFactor, advanceWidthsUnshaped, nullFont, _textFormattingMode, _isSideways ); } } else { GlyphTypeface glyphTypeface = _shapeTypeface.GlyphTypeface; Invariant.Assert(glyphTypeface != null); Invariant.Assert(characterLength > 0); CharacterBufferRange newBuffer = new CharacterBufferRange(characterString, characterLength); MS.Internal.Text.TextInterface.GlyphMetrics[] glyphMetrics = BufferCache.GetGlyphMetrics(characterLength); glyphTypeface.GetGlyphMetricsOptimized(newBuffer, _emSize, (float)_properties.PixelsPerDip, _textFormattingMode, _isSideways, glyphMetrics ); if (_textFormattingMode == TextFormattingMode.Display && TextFormatterContext.IsSpecialCharacter(*characterString)) { // (DDVSO 92892) If the run starts with a special character (in // the LineServices sense), we apply display-mode rounding now, // as we won't get another chance. This assumes that the characters // in a run are either all special or all non-special; that assumption // is valid in the current LS implementation. double designToEm = _emSize / glyphTypeface.DesignEmHeight; double pixelsPerDip = _properties.PixelsPerDip; for (int i = 0; i < characterLength; i++) { advanceWidthsUnshaped[i] = (int)Math.Round(TextFormatterImp.RoundDipForDisplayMode(glyphMetrics[i].AdvanceWidth * designToEm, pixelsPerDip) * scalingFactor); } } else { // For the normal case, rounding is applied later on when LS // invokes the callback GetGlyphPositions, so that adjustments // due to justification and shaping are taken into account. double designToEm = _emSize * scalingFactor / glyphTypeface.DesignEmHeight; for (int i = 0; i < characterLength; i++) { advanceWidthsUnshaped[i] = (int)Math.Round(glyphMetrics[i].AdvanceWidth * designToEm); } } BufferCache.ReleaseGlyphMetrics(glyphMetrics); } }
/// <exception cref="WinrtCifs.Dcerpc.DcerpcException"></exception> /// <exception cref="System.IO.IOException"></exception> public virtual void Sendrecv(DcerpcMessage msg) { byte[] stub; byte[] frag; NdrBuffer buf; NdrBuffer fbuf; bool isDirect; DcerpcException de; if (State == 0) { Bind(); } isDirect = true; stub = BufferCache.GetBuffer(); try { int off; int tot; int n; buf = new NdrBuffer(stub, 0); msg.Flags = DcerpcConstants.DcerpcFirstFrag | DcerpcConstants.DcerpcLastFrag; msg.CallId = _callId++; msg.Encode(buf); if (SecurityProvider != null) { buf.SetIndex(0); SecurityProvider.Wrap(buf); } tot = buf.GetLength() - 24; off = 0; while (off < tot) { n = tot - off; if ((24 + n) > MaxXmit) { msg.Flags &= ~DcerpcConstants.DcerpcLastFrag; n = MaxXmit - 24; } else { msg.Flags |= DcerpcConstants.DcerpcLastFrag; isDirect = false; msg.AllocHint = n; } msg.Length = 24 + n; if (off > 0) { msg.Flags &= ~DcerpcConstants.DcerpcFirstFrag; } if ((msg.Flags & (DcerpcConstants.DcerpcFirstFrag | DcerpcConstants.DcerpcLastFrag)) != (DcerpcConstants.DcerpcFirstFrag | DcerpcConstants.DcerpcLastFrag)) { buf.Start = off; buf.Reset(); msg.Encode_header(buf); buf.Enc_ndr_long(msg.AllocHint); buf.Enc_ndr_short(0); buf.Enc_ndr_short(msg.GetOpnum()); } DoSendFragment(stub, off, msg.Length, isDirect); off += n; } DoReceiveFragment(stub, isDirect); buf.Reset(); buf.SetIndex(8); buf.SetLength(buf.Dec_ndr_short()); if (SecurityProvider != null) { SecurityProvider.Unwrap(buf); } buf.SetIndex(0); msg.Decode_header(buf); off = 24; if (msg.Ptype == 2 && msg.IsFlagSet(DcerpcConstants.DcerpcLastFrag) == false) { off = msg.Length; } frag = null; fbuf = null; while (msg.IsFlagSet(DcerpcConstants.DcerpcLastFrag) == false) { int stubFragLen; if (frag == null) { frag = new byte[MaxRecv]; fbuf = new NdrBuffer(frag, 0); } DoReceiveFragment(frag, isDirect); fbuf.Reset(); fbuf.SetIndex(8); fbuf.SetLength(fbuf.Dec_ndr_short()); if (SecurityProvider != null) { SecurityProvider.Unwrap(fbuf); } fbuf.Reset(); msg.Decode_header(fbuf); stubFragLen = msg.Length - 24; if ((off + stubFragLen) > stub.Length) { // shouldn't happen if alloc_hint is correct or greater byte[] tmp = new byte[off + stubFragLen]; Array.Copy(stub, 0, tmp, 0, off); stub = tmp; } Array.Copy(frag, 24, stub, off, stubFragLen); off += stubFragLen; } buf = new NdrBuffer(stub, 0); msg.Decode(buf); } finally { BufferCache.ReleaseBuffer(stub); } if ((de = msg.GetResult()) != null) { throw de; } }