/// <summary> /// This API usage is optimized for multiple runs of the same image size, by reusing the byte arrays instead of allocating new ones for every operation. /// </summary> private static void benchTJOptimized() { byte[] data = File.ReadAllBytes(inputFilePath); byte[] recompressed; int recompressedSize = 0; System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch(); sw.Start(); using (TJDecompressor decomp = new TJDecompressor(data)) { // Optimize by pre-allocating the buffers and re-using them. byte[] rawImg = new byte[decomp.getWidth() * decomp.getHeight() * TJ.getPixelSize(PixelFormat.RGB)]; recompressed = new byte[TJ.bufSize(decomp.getWidth(), decomp.getHeight(), decomp.getSubsamp())]; using (TJCompressor comp = new TJCompressor(rawImg, decomp.getWidth(), decomp.getHeight())) { comp.setJPEGQuality(jpegQuality); for (int i = 0; i < numIterations; i++) { decomp.decompress(rawImg); comp.compress(ref recompressed, Flag.NONE); } recompressedSize = comp.getCompressedSize(); } } sw.Stop(); PrintBenchmarkResult("turbojpegCLI optimized", sw.ElapsedMilliseconds); using (FileStream fs = new FileStream("out-libjpeg-turbo-optimized.jpg", FileMode.Create)) { fs.Write(recompressed, 0, recompressedSize); } }
public static void DoThing() { try { //Console.WriteLine("DoThing"); int pos = 0; bool isTop = false; bool iscomplete = false; int lastSeq = 0; int lastSub = 0; bool loop = true; while (loop) { int length = udpClient.Client.ReceiveFrom(data, ref udpEP); if (data[3] == 0x00) { lastSeq = data[0]; lastSub = -1; pos = 0; } if (data[0] == lastSeq && lastSub + 1 == data[3]) { lastSub = data[3]; if (data[1] == 0x01 || data[1] == 0x11) { isTop = true; } else if (isTop) { isTop = false; //LIES! } //Need to valid that the entire UDP stream is received before proceeding... Otherwise TDJ crashes. //Console.WriteLine("{0:x2} {1:x2} {2:x2} {3:x2}", data[0], data[1], data[2], data[3]); if (pos + length > jpegImage.Length) { loop = false; pos = 0; } else { Array.Copy(data, 4, jpegImage, pos, length - 4); pos += length - 4; if (data[1] >= 0x10) { iscomplete = true; loop = false; //Console.WriteLine("S"); } } } else { loop = false; pos = 0; //Console.WriteLine("FB"); } } if (iscomplete) { byte[] decom; tjd = new TJDecompressor(jpegImage); if (isTop) { decom = new byte[240 * 400 * TJ.getPixelSize(turbojpegCLI.PixelFormat.RGB)]; tjd.decompress(decom, turbojpegCLI.PixelFormat.RGB, Flag.NONE); if (FormMain.viewerTop != null) { FormMain.viewerTop.LoadTexture(400, 240, decom); } } else { decom = new byte[240 * 320 * TJ.getPixelSize(turbojpegCLI.PixelFormat.RGB)]; tjd.decompress(decom, turbojpegCLI.PixelFormat.RGB, Flag.NONE); if (FormMain.viewerTop != null) { FormMain.viewerBottom.LoadTexture(320, 240, decom); } } tjd.Dispose(); } } catch (Exception ex) { Console.WriteLine(ex.ToString()); } }