Lossless JBIG2 encoding library (Lossless JBIG2 Encoder)

Source: Internet
Author: User

Some netizens hope to provide the JBIG2 encoding library of PDFPatcher.

The encoding library provided here comes from the open source code of agl on Github. After the code is compiled, the EXE file is output to encode the data provided by the existing bitmap file or StdIn, And the DLL library called by other applications is not provided. To add the JBIG2 encoding function to the PDF patch Ding, I modified the Code to remove the lossy compression function and the dependency between the Leptonica Image Library, reducing the file size of the encoder.

Export Function

The DLL library exports three functions (in addition to the following methods, there are also the functions provided by the original codeJbig2_encode_genericMethod. For the call method of this method, see the original code instructions ):

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_encodeEncode the byte array. There are six parameters:

  1. Width: Image Width
  2. Height: Image Height
  3. Stride: number of dual-words (DWORD) occupied by a row of pixels
  4. ZeroIsWhite: treats 0 of BW as white
  5. BW: the Byte array of the image (Byte []). This Byte array is the binary data of the original black and white image. By default, binary 1 indicates white, and 0 indicates black.
  6. Length: the Length of the byte array.
  7. Outgoing parameter: encoded JBIG2 byte array

The jbig2_encode method is actually called.Jbig2_encode_genericMethod, to facilitate calls from other programs, see the source code attached.

ReleaseThe method is used to release the memory occupied by the byte array after the jbig2_encode method encoding. The input parameter is the pointer returned by the jbig2_encode method.

C # Call Method

CallJBIG2EncoderClassEncodeMethod. The input parameter of this method is a bitmap:

///<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)

Click here to download the JBIG2 lossless compression DLL library.

Click here to download JBIG2 Lossless Compression DLL.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.