JavaCPP bindings for Leptonica image processing library with cross-platform support
—
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Pending
The risk profile of this skill
JavaCPP Leptonica provides comprehensive Java bindings for the Leptonica image processing library, enabling industrial-strength image processing capabilities in Java applications. The library supports morphological operations, image enhancement, analysis, format conversion, OCR support, and document processing across multiple platforms with automatic memory management.
<dependency>
<groupId>org.bytedeco</groupId>
<artifactId>leptonica-platform</artifactId>
<version>1.85.0-1.5.12</version>
</dependency>import org.bytedeco.javacpp.Loader;
import org.bytedeco.leptonica.*;
import static org.bytedeco.leptonica.global.leptonica.*;For comprehensive development:
// Core image structures
import org.bytedeco.leptonica.PIX;
import org.bytedeco.leptonica.PIXA;
import org.bytedeco.leptonica.PIXAA;
import org.bytedeco.leptonica.FPIX;
import org.bytedeco.leptonica.DPIX;
// Geometric structures
import org.bytedeco.leptonica.BOX;
import org.bytedeco.leptonica.BOXA;
import org.bytedeco.leptonica.PTA;
import org.bytedeco.leptonica.PTAA;
// Morphological and recognition
import org.bytedeco.leptonica.SEL;
import org.bytedeco.leptonica.SELA;
import org.bytedeco.leptonica.L_RECOG;
import org.bytedeco.leptonica.L_DEWARP;
import org.bytedeco.leptonica.L_DEWARPA;
// Connected components and contours
import org.bytedeco.leptonica.CCBORD;
import org.bytedeco.leptonica.CCBORDA;
// Utility structures
import org.bytedeco.leptonica.NUMA;
import org.bytedeco.leptonica.SARRAY;
import org.bytedeco.leptonica.L_STACK;
import org.bytedeco.leptonica.PIXCMAP;
// All static functions
import static org.bytedeco.leptonica.global.leptonica.*;For minimal imports:
import org.bytedeco.leptonica.PIX;
import org.bytedeco.leptonica.BOX;
import static org.bytedeco.leptonica.global.leptonica.pixRead;
import static org.bytedeco.leptonica.global.leptonica.pixWrite;import org.bytedeco.leptonica.*;
import static org.bytedeco.leptonica.global.leptonica.*;
// Initialize the library
Loader.load(org.bytedeco.leptonica.global.leptonica.class);
// Read an image
PIX pixSource = pixRead("input.png");
// Create a processed copy using morphological operations
SEL sel = selCreateBrick(5, 5, 2, 2);
PIX pixProcessed = pixDilate(null, pixSource, sel);
// Get image dimensions
int width = pixGetWidth(pixSource);
int height = pixGetHeight(pixSource);
int depth = pixGetDepth(pixSource);
// Write result
pixWrite("output.png", pixProcessed, IFF_PNG);
// Memory is automatically managed - no manual cleanup neededJavaCPP Leptonica is built around several key components:
Primary image containers and their management, supporting multiple bit depths and color models with automatic memory cleanup.
class PIX extends AbstractPIX {
static PIX create(int width, int height, int depth);
static PIX createTemplate(PIX pixs);
PIX clone();
// Dimensions and properties
int w(); // width in pixels
int h(); // height in pixels
int d(); // depth in bits per pixel
int spp(); // samples per pixel
// Data access
IntPointer data();
PIXCMAP colormap();
BytePointer text();
}
class FPIX extends AbstractFPIX {
// 32-bit float image operations
}
class DPIX extends AbstractDPIX {
// 64-bit double image operations
}Complete image reading, writing, and format conversion capabilities supporting all major image formats.
// Reading images
PIX pixRead(String filename);
PIX pixReadStream(InputStream fp, int hint);
PIX pixReadMem(BytePointer data, long size);
// Writing images
int pixWrite(String filename, PIX pix, int format);
int pixWriteStream(OutputStream fp, PIX pix, int format);
BytePointer pixWriteMem(PIX pix, long[] psize, int format);
// Format constants
static final int IFF_PNG = 1;
static final int IFF_JPEG = 2;
static final int IFF_TIFF = 3;Core image processing operations including scaling, rotation, morphological operations, and filtering.
// Scaling and geometric transforms
PIX pixScale(PIX pixs, float scalex, float scaley);
PIX pixRotate(PIX pixs, float angle, int type, int incolor, int width, int height);
PIX pixAffine(PIX pixs, FloatPointer vc, int incolor);
// Morphological operations
PIX pixDilate(PIX pixd, PIX pixs, SEL sel);
PIX pixErode(PIX pixd, PIX pixs, SEL sel);
PIX pixOpen(PIX pixd, PIX pixs, SEL sel);
PIX pixClose(PIX pixd, PIX pixs, SEL sel);
// Color space conversion
PIX pixConvertRGBToGray(PIX pixs, float rwt, float gwt, float bwt);
PIX pixConvertGrayToColormap(PIX pixs);
// Image blending and compositing
PIX pixBlend(PIX pixs1, PIX pixs2, int x, int y, float fract);
PIX pixBlendColor(PIX pixd, PIX pixs1, PIX pixs2, int x, int y, float fract, int transparent, int transpix);
// Convolution and filtering
PIX pixConvolve(PIX pixs, L_KERNEL kel, int outdepth, int normflag);
PIX pixSobelEdgeFilter(PIX pixs, int orientflag);
PIX pixUnsharpMasking(PIX pixs, int halfwidth, float fract);
// Convolution kernel type
class L_KERNEL extends Pointer {
int sy(); // kernel height
int sx(); // kernel width
int cy(); // y origin coordinate
int cx(); // x origin coordinate
}Rectangle and point operations for defining regions, measuring features, and spatial analysis.
class BOX extends Pointer {
int x(); // left coordinate
int y(); // top coordinate
int w(); // width
int h(); // height
}
class BOXA extends Pointer {
// Array of BOX rectangles
}
class PTA extends Pointer {
// Array of 2D points
}
// Box operations
BOX boxCreate(int x, int y, int w, int h);
int boxGetGeometry(BOX box, IntPointer px, IntPointer py, IntPointer pw, IntPointer ph);
BOX boxIntersection(BOX box1, BOX box2);Advanced morphological processing with custom structuring elements for shape analysis and feature extraction.
class SEL extends Pointer {
// Structuring element for morphological operations
}
// Structuring element creation
SEL selCreateBrick(int h, int w, int cy, int cx);
SEL selCreateFromString(String text, int h, int w, String name);
// Advanced morphological operations
PIX pixMorphGradient(PIX pixs, SEL sel, int op);
PIX pixTophat(PIX pixs, SEL sel, int op);
PIX pixHMT(PIX pixd, PIX pixs, SEL sel);OCR capabilities, document analysis, and text extraction with specialized structures for character recognition.
class L_RECOG extends Pointer {
// Character recognition engine
}
class L_DEWARP extends Pointer {
// Document dewarp correction
}
class L_DEWARPA extends Pointer {
// Multi-page dewarp processing
}
// Recognition functions
L_RECOG recogCreateFromRecog(L_RECOG recs, int scalew, int scaleh, int linew, int threshold, int maxyshift);
int recogTrainLabeled(L_RECOG recog, PIX pixs, BOX box, String text, int debug);Container classes for organizing and manipulating groups of images and associated metadata.
class PIXA extends Pointer {
// Array of PIX images with optional boxes
int n(); // number of images
}
class PIXAA extends Pointer {
// 2D array of PIXA structures
}
// Collection operations
PIXA pixaCreate(int n);
int pixaAddPix(PIXA pixa, PIX pix, int copyflag);
PIX pixaGetPix(PIXA pixa, int index, int accesstype);
BOXA pixaGetBoxa(PIXA pixa, int accesstype);Comprehensive connected component analysis, contour processing, and border representations for object detection and shape analysis.
class CCBORD extends Pointer {
PIX pix(); // minimally-clipped bitmap of component
BOXA boxa(); // boxes for primary component and holes
PTA start(); // initial border pixel locations
PTAA local(); // chain code for border (relative coords)
PTAA global(); // global pixel locations of border
}
class CCBORDA extends Pointer {
int n(); // number of ccbords
CCBORD getCcbord(int index);
}
// Connected component functions
CCBORD ccbordCreate(PIX pixs, int connectivity);
PIX pixConnComp(PIX pixs, PIXA ppixa, int connectivity);
int pixCountConnComp(PIX pixs, int connectivity, IntPointer pcount);
PTA ptaGetBoundaryPixels(PIX pixs, int type);Supporting data structures including arrays, stacks, queues, and utility functions for comprehensive image processing workflows.
class NUMA extends Pointer {
// Numeric array for statistics
}
class SARRAY extends Pointer {
// String array
}
class L_STACK extends Pointer {
// Stack data structure
}
// Utility functions
int pixCountPixels(PIX pix, IntPointer pcount, IntPointer tab8);
float pixGetAverageMasked(PIX pixs, PIX pixm, int x, int y, int w, int h, int factor, float[] pval);Most functions return status codes where 0 indicates success and non-zero indicates failure. For PIX creation functions, null return values indicate failure.
PIX pix = pixRead("image.jpg");
if (pix == null) {
System.err.println("Failed to read image");
return;
}
int result = pixWrite("output.png", pix, IFF_PNG);
if (result != 0) {
System.err.println("Failed to write image");
}JavaCPP provides automatic memory management through:
destroy() methodsThe library handles native memory automatically, eliminating the need for manual memory management in most cases.