/// <summary> /// Associate the JPEG image of length <code>imageSize</code> bytes stored in <code>jpegImage</code> with this /// decompressor instance. This image will be used as the source image for subsequent decompress operations. /// </summary> /// <param name="jpegImage"> JPEG image buffer</param> public void SetJpegImage(byte[] jpegImage) { Contract.Requires(jpegImage != null); this.jpegBuffer = jpegImage; int width; int height; Subsampling chroma; Colourspace colourspace; if (NativeMethods.decompressHeader(this.Handle, this.jpegBuffer, this.jpegBuffer.Length, out width, out height, out chroma, out colourspace) != 0) { throw new Exception(Marshal.PtrToStringAnsi(NativeMethods.getErrorMessage())); } this.jpegWidth = width; this.jpegHeight = height; this.jpegSubsampling = chroma; this.JpegColourspace = colourspace; }
public static void Encode(string inPath, string outPath, int q, Subsampling subsampling, bool printSubsampling = true) { try { Bitmap bmp = (Bitmap)IOUtils.GetImage(inPath); var compressor = new TJCompressor(); byte[] compressed; TJSubsamplingOption subSample = TJSubsamplingOption.Chrominance420; if (subsampling == Subsampling.Chroma422) { subSample = TJSubsamplingOption.Chrominance422; } if (subsampling == Subsampling.Chroma444) { subSample = TJSubsamplingOption.Chrominance444; } if (printSubsampling) { Program.Print("-> Chroma Subsampling: " + subSample.ToString().Replace("Chrominance", "")); } compressed = compressor.Compress(bmp, subSample, q, TJFlags.None); File.WriteAllBytes(outPath, compressed); //Program.Print("[MozJpeg] Written image to " + outPath); } catch (Exception e) { Program.Print("MozJpeg Error: " + e.Message); } }
/// <summary>Returns the MCU block height for the specified chroma subsampling.</summary> /// <param name="subsampling">The chroma subsampling.</param> /// <returns>The MCU block height.</returns> public static int GetMcuHeight(this Subsampling subsampling) { Contract.Requires(Enum.IsDefined(typeof(Subsampling), subsampling)); Contract.Assume((int)subsampling < LibJpegTurbo.Net.TurboJpegUtilities.mcuHeight.Length); return(TurboJpegUtilities.mcuHeight[(int)subsampling]); }
internal static extern int decompressHeader(IntPtr handle, [In][MarshalAs(UnmanagedType.LPArray, ArraySubType = UnmanagedType.U1)] byte[] sourceBuffer, int size, out int width, out int height, [MarshalAs(UnmanagedType.I4)] out Subsampling chroma, [MarshalAs(UnmanagedType.I4)] out Colourspace colourspace);
internal static extern int encodeYUV(IntPtr handle, byte[] sourceBuffer, int width, int pitch, int height, [MarshalAs(UnmanagedType.I4)] PixelFormat pixelFormat, byte[] destinationBuffer, [MarshalAs(UnmanagedType.I4)] Subsampling subsamp, [MarshalAs(UnmanagedType.I4)] TurboJpegFlags flags);
internal static extern int compress(IntPtr handle, byte[] sourceBuffer, int width, int pitch, int height, [MarshalAs(UnmanagedType.I4)] PixelFormat pixelFormat, ref IntPtr destinationBuffer, ref ulong bufferSize, [MarshalAs(UnmanagedType.I4)] Subsampling jpegSubsamp, int jpegQual, [MarshalAs(UnmanagedType.I4)] TurboJpegFlags flags);
internal static extern int bufSizeYUV(int width, int pad, int height, [MarshalAs(UnmanagedType.I4)] Subsampling subsamp);
internal static extern int bufSize(int width, int height, [MarshalAs(UnmanagedType.I4)] Subsampling jpegSubsamp);