無損 JBIG2 編碼庫(Lossless JBIG2 Encoder)


有網友希望提供 PDFPatcher 的 JBIG2 編碼庫,因發此博文。

此處提供的編碼庫源自agl在Github的開源代碼。該代碼編譯后輸出 EXE 文件,編碼現存位圖文件或 StdIn 提供的數據,未提供被其它應用程序調用的 DLL 庫。為了在 PDF 補丁丁中增加 JBIG2 編碼功能,我修改了該代碼,去除了其有損壓縮功能及 Leptonica 圖像庫的依賴關系,減少了編碼器的文件大小。

導出函數

DLL 庫導出的函數有三個(除下列方法之外,還有原代碼提供的jbig2_encode_generic方法,該方法的調用方式請參見原代碼的說明):

uint8_t *
jbig2_encode (int width, int height, int stride, bool zeroIsWhite, uint8_t * const bw, int *const length);

void release (uint8_t * const memblock);

jbig2_encode 方法用於編碼傳入的字節數組。其參數有六個:

  1. Width:圖像寬度
  2. Height:圖像高度
  3. Stride:一行像素所占的雙字(DWORD)數
  4. ZeroIsWhite:將BW的0視為白色
  5. BW:圖像的字節數組(Byte[]),此字節數組是原始黑白圖像的二進制數據,默認情況下,二進制位1表示白色,0表示黑色
  6. Length:字節數組長度
  7. 傳出參數:編碼后的 JBIG2 字節數組

jbig2_encode 方法實際上是調用  jbig2_encode_generic方法,為方便從其它程序調用而設,見所附源代碼。

release 方法用於釋放 jbig2_encode 方法編碼后字節數組所占用的內存,傳入參數是 jbig2_encode 方法返回的指針。

C# 的調用方法

調用 JBIG2Encoder 類的 Encode 方法即可,該方法的傳入參數為一位圖:

///<summary>JBIG2 Lossless Encoder Wrapper</summary>
public static class JBig2Encoder
{
static uint White = 0x00FFFFFF;

public static byte[] Encode (Bitmap bmp, bool zeroIsWhite) {
var bits = bmp.LockBits (new System.Drawing.Rectangle (0, 0, bmp.Width, bmp.Height), System.Drawing.Imaging.ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format1bppIndexed);
var bytes = Encode (bmp.Width, bmp.Height, bits.Stride, zeroIsWhite, bits.Scan0);
bmp.UnlockBits (bits);
return bytes;
}

private static byte[] Encode (int width, int height, int stride, bool zeroIsWhite, IntPtr b) {
int l = 0;
var r = NativeMethods.jbig2_encode (width, height, stride, zeroIsWhite, b, ref l);
byte[] result = new byte[l];
Marshal.Copy (r, result, 0, l);
NativeMethods.release (r);
return result;
}

class NativeMethods
{
[DllImport ("jbig2enc.dll")]
internal static extern IntPtr jbig2_encode (int width, int height, int stride, bool zeroIsWhite, IntPtr data, ref int length);

[DllImport ("jbig2enc.dll")]
internal static extern IntPtr release (IntPtr data);
}
}

下載(Download)

點擊此處下載 JBIG2 無損壓縮 DLL 庫

Click here to download JBIG2 Lossless Compression DLL.


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM