public static int Init() { lock (initLock) { if (inited) { return(IJL_OK); } pjcore = Marshal.AllocHGlobal(Marshal.SizeOf(jcore)); Marshal.StructureToPtr(jcore, pjcore, true); lastErrCode = ijlInit(pjcore); if (lastErrCode == IJL_OK) { jcore = (TJPEG_CORE_PROPERTIES)Marshal.PtrToStructure(pjcore, typeof(TJPEG_CORE_PROPERTIES)); jcore.DIBChannels = 3; jcore.DIBColor = TIJL_COLOR.IJL_BGR; jcore.JPGChannels = 3; jcore.JPGColor = TIJL_COLOR.IJL_YCBCR; jcore.jprops.processor_type = TPROCESSOR_TYPE.IJL_NEW_PROCESSOR; inited = true; } return(lastErrCode); } }
public static int RawToJpg(byte[] raw, int width, int height, out byte[] jpg, bool bottomUp = true, uint CompressionQuality = 75) { jpg = new byte[0]; if (lastErrCode == IJL_OK) { IntPtr buffIn = Marshal.AllocCoTaskMem(raw.Length); IntPtr buffOut = Marshal.AllocCoTaskMem(raw.Length); Marshal.Copy(raw, 0, buffIn, raw.Length); try { lock (initLock) { jcore.DIBBytes = buffIn; jcore.DIBHeight = bottomUp ? -height : height; jcore.DIBWidth = (uint)width; jcore.DIBPadBytes = (uint)IJL_DIB_PAD_BYTES(Convert.ToInt32(jcore.DIBWidth), Convert.ToInt32(jcore.DIBChannels)); jcore.JPGFile = (IntPtr)0; jcore.JPGBytes = buffOut; jcore.JPGSizeBytes = (uint)raw.Length; jcore.JPGWidth = (uint)width; jcore.JPGHeight = (uint)height; jcore.jquality = CompressionQuality; Marshal.StructureToPtr(jcore, pjcore, true); if (inited) { lastErrCode = ijlWrite(pjcore, TIJLIOType.IJL_JBUFF_WRITEWHOLEIMAGE); } else { lastErrCode = IJL_NOT_INITED; } if (lastErrCode == IJL_OK) { jcore = (TJPEG_CORE_PROPERTIES)Marshal.PtrToStructure(pjcore, typeof(TJPEG_CORE_PROPERTIES)); jpg = new byte[(int)jcore.JPGSizeBytes]; Marshal.Copy(buffOut, jpg, 0, jpg.Length); } } } finally { Marshal.FreeCoTaskMem(buffOut); Marshal.FreeCoTaskMem(buffIn); } return(lastErrCode); } else { return(lastErrCode); } }
public static int JpgToRaw(byte[] jpg, out byte[] raw, out int width, out int height, bool bottomUp = true) { raw = new byte[0]; width = 0; height = 0; if (lastErrCode == IJL_OK) { IntPtr buffIn = Marshal.AllocCoTaskMem(jpg.Length); Marshal.Copy(jpg, 0, buffIn, jpg.Length); try { lock (initLock) { jcore.JPGFile = (IntPtr)0; jcore.JPGBytes = buffIn; jcore.JPGSizeBytes = (uint)jpg.Length; Marshal.StructureToPtr(jcore, pjcore, true); lastErrCode = ijlRead(pjcore, TIJLIOType.IJL_JBUFF_READPARAMS); if (lastErrCode == IJL_OK) { jcore = (TJPEG_CORE_PROPERTIES)Marshal.PtrToStructure(pjcore, typeof(TJPEG_CORE_PROPERTIES)); width = (int)jcore.JPGWidth; height = (int)jcore.JPGHeight; jcore.DIBChannels = 3; jcore.DIBColor = TIJL_COLOR.IJL_RGB; jcore.DIBWidth = (uint)width; jcore.DIBHeight = bottomUp ? -height : height; jcore.DIBPadBytes = (uint)IJL_DIB_PAD_BYTES(width, (int)jcore.DIBChannels); int rawSize = width * height * 4 + 1; IntPtr buffOut = Marshal.AllocCoTaskMem(rawSize); jcore.DIBBytes = buffOut; try { Marshal.StructureToPtr(jcore, pjcore, true); if (inited) { lastErrCode = ijlRead(pjcore, TIJLIOType.IJL_JBUFF_READWHOLEIMAGE); } else { lastErrCode = IJL_NOT_INITED; } if (lastErrCode == IJL_OK) { raw = new byte[rawSize]; Marshal.Copy(buffOut, raw, 0, rawSize); } } finally { Marshal.FreeCoTaskMem(buffOut); } } } } finally { Marshal.FreeCoTaskMem(buffIn); } return(lastErrCode); } else { return(lastErrCode); } }