JavaCPP bindings for Leptonica image processing library with cross-platform support
—
Supporting data structures including arrays, stacks, queues, hash maps, and utility functions for comprehensive image processing workflows.
Core data structures for organizing and manipulating data in image processing algorithms.
/**
* Stack data structure
*/
class L_STACK extends Pointer {
int n(); // number of elements
int nalloc(); // allocated capacity
Pointer auxstack(); // auxiliary stack for object pointers
}
/**
* Queue data structure
*/
class L_QUEUE extends Pointer {
int nalloc(); // allocated capacity
int nhead(); // head index
int ntail(); // tail index
int nelem(); // number of elements
Pointer array(); // element array
}
/**
* Priority heap
*/
class L_HEAP extends Pointer {
int n(); // number of elements
int nalloc(); // allocated capacity
FloatPointer array(); // value array
int direction(); // L_SORT_INCREASING or L_SORT_DECREASING
}
/**
* Hash map
*/
class L_HASHMAP extends Pointer {
int nitems(); // number of items
int nbuckets(); // number of hash buckets
L_HASHITEM array(); // hash item array
}
/**
* Hash map item
*/
class L_HASHITEM extends Pointer {
long key(); // hash key
Pointer value(); // stored value
L_HASHITEM next(); // next item in bucket
}
// Stack operations
L_STACK lstackCreate(int nalloc);
int lstackAdd(L_STACK lstack, Pointer item);
Pointer lstackRemove(L_STACK lstack);
int lstackGetCount(L_STACK lstack);
// Queue operations
L_QUEUE lqueueCreate(int nalloc);
int lqueueAdd(L_QUEUE lqueue, Pointer item);
Pointer lqueueRemove(L_QUEUE lqueue);
int lqueueGetCount(L_QUEUE lqueue);
// Heap operations
L_HEAP lheapCreate(int nalloc, int direction);
int lheapAdd(L_HEAP lh, Pointer item);
Pointer lheapRemove(L_HEAP lh);
int lheapGetCount(L_HEAP lh);
// Hash map operations
L_HASHMAP hashmapCreate(int nbuckets);
int hashmapLookup(L_HASHMAP hmap, long key, Pointer pval);
int hashmapInsert(L_HASHMAP hmap, long key, Pointer val);
int hashmapDelete(L_HASHMAP hmap, long key);Usage Examples:
import org.bytedeco.leptonica.*;
import static org.bytedeco.leptonica.global.leptonica.*;
// Stack for region processing
L_STACK regionStack = lstackCreate(100);
lstackAdd(regionStack, seedRegion);
while (lstackGetCount(regionStack) > 0) {
BOX region = (BOX)lstackRemove(regionStack);
// Process region and add neighbors...
}
// Priority queue for pathfinding
L_HEAP priorityQueue = lheapCreate(500, L_SORT_INCREASING);
lheapAdd(priorityQueue, startNode);
while (lheapGetCount(priorityQueue) > 0) {
PathNode current = (PathNode)lheapRemove(priorityQueue);
// Process current node...
}
// Hash map for caching results
L_HASHMAP cache = hashmapCreate(1024);
hashmapInsert(cache, imageHash, processedResult);
// Later lookup
Pointer cached = new Pointer();
if (hashmapLookup(cache, imageHash, cached) == 0) {
// Use cached result
}Dynamic byte arrays and buffers for I/O operations and data manipulation.
/**
* Dynamic byte array
*/
class L_BYTEA extends Pointer {
int nalloc(); // allocated size
int size(); // actual size
BytePointer data(); // byte data
}
/**
* Byte buffer for I/O
*/
class L_BBUFFER extends Pointer {
int nalloc(); // allocated size
int n(); // actual size
int nwritten(); // bytes written
BytePointer array(); // buffer data
}
/**
* Create byte array
* @param nbytes - Initial capacity
* @return L_BYTEA or null on failure
*/
L_BYTEA l_byteaCreate(int nbytes);
/**
* Append data to byte array
* @param ba - Target byte array
* @param newdata - Data to append
* @param size - Size of data
* @return 0 on success, 1 on failure
*/
int l_byteaAppendData(L_BYTEA ba, BytePointer newdata, int size);
/**
* Get data from byte array
* @param ba - Source byte array
* @param psize - Returns data size
* @return BytePointer to data or null on failure
*/
BytePointer l_byteaGetData(L_BYTEA ba, IntPointer psize);
/**
* Create byte buffer
* @param nbytes - Initial capacity
* @return L_BBUFFER or null on failure
*/
L_BBUFFER bbufferCreate(int nbytes);
/**
* Read data into buffer
* @param bb - Target buffer
* @param src - Source data
* @param nbytes - Number of bytes to read
* @return 0 on success, 1 on failure
*/
int bbufferRead(L_BBUFFER bb, BytePointer src, int nbytes);
/**
* Write data from buffer
* @param bb - Source buffer
* @param dest - Destination
* @param nbytes - Number of bytes to write
* @return 0 on success, 1 on failure
*/
int bbufferWrite(L_BBUFFER bb, BytePointer dest, int nbytes);Usage Examples:
// Dynamic byte array for accumulating data
L_BYTEA accumulator = l_byteaCreate(1024);
// Append image data
BytePointer imageData = pixGetData(pix);
int imageSize = pixGetHeight(pix) * pixGetWpl(pix) * 4;
l_byteaAppendData(accumulator, imageData, imageSize);
// Get accumulated data
IntPointer totalSize = new IntPointer(1);
BytePointer allData = l_byteaGetData(accumulator, totalSize);
System.out.println("Accumulated " + totalSize.get() + " bytes");
// Byte buffer for I/O operations
L_BBUFFER buffer = bbufferCreate(4096);
bbufferRead(buffer, inputData, dataSize);
// ... process buffer ...
bbufferWrite(buffer, outputData, processedSize);Timing utilities for performance measurement and profiling.
/**
* Timer structure
*/
class L_TIMER extends Pointer {
// Internal timer state
}
/**
* Wall clock timer
*/
class L_WALLTIMER extends Pointer {
long start_sec(); // start seconds
long start_usec(); // start microseconds
long stop_sec(); // stop seconds
long stop_usec(); // stop microseconds
}
/**
* Start timing
* @return L_TIMER or null on failure
*/
L_TIMER startTimer();
/**
* Stop timing and get elapsed time
* @param timer - Timer to stop
* @return Elapsed time in seconds
*/
float stopTimer(L_TIMER timer);
/**
* Start wall clock timer
* @return L_WALLTIMER or null on failure
*/
L_WALLTIMER startWallTimer();
/**
* Stop wall clock timer
* @param timer - Wall timer to stop
* @return Elapsed time in seconds
*/
float stopWallTimer(L_WALLTIMER timer);
/**
* Get current timestamp
* @return Current time in seconds since epoch
*/
float getCurrentTime();Usage Examples:
// Time image processing operation
L_TIMER timer = startTimer();
PIX processed = pixGaussianBlur(sourceImage, 3.0f, 3.0f);
processed = pixScale(processed, 2.0f, 2.0f);
processed = pixRotate(processed, 0.1f, L_ROTATE_AREA_MAP, L_BRING_IN_WHITE, 0, 0);
float elapsed = stopTimer(timer);
System.out.println("Processing took " + elapsed + " seconds");
// Wall clock timing for I/O operations
L_WALLTIMER wallTimer = startWallTimer();
for (int i = 0; i < 100; i++) {
PIX image = pixRead("image_" + i + ".jpg");
PIX result = pixScale(image, 0.5f, 0.5f);
pixWrite("thumb_" + i + ".jpg", result, IFF_JPEG);
}
float totalTime = stopWallTimer(wallTimer);
System.out.println("Batch processing: " + totalTime + " seconds");Miscellaneous utility functions for common operations and calculations.
/**
* Count pixels in binary image
* @param pix - Binary image
* @param pcount - Returns pixel count
* @param tab8 - Lookup table (can be null)
* @return 0 on success, 1 on failure
*/
int pixCountPixels(PIX pix, IntPointer pcount, IntPointer tab8);
/**
* Get average pixel value in region
* @param pixs - Source image
* @param pixm - Mask image (can be null)
* @param x - Region x coordinate
* @param y - Region y coordinate
* @param w - Region width
* @param h - Region height
* @param factor - Sampling factor
* @param pval - Returns average value
* @return 0 on success, 1 on failure
*/
int pixGetAverageMasked(PIX pixs, PIX pixm, int x, int y, int w, int h, int factor, FloatPointer pval);
/**
* Get pixel variance in region
* @param pixs - Source image
* @param pixm - Mask image (can be null)
* @param x - Region x coordinate
* @param y - Region y coordinate
* @param w - Region width
* @param h - Region height
* @param factor - Sampling factor
* @param pvar - Returns variance
* @return 0 on success, 1 on failure
*/
int pixGetVarianceMasked(PIX pixs, PIX pixm, int x, int y, int w, int h, int factor, FloatPointer pvar);
/**
* Find extreme pixel values
* @param pixs - Source image
* @param pminval - Returns minimum value (can be null)
* @param pmaxval - Returns maximum value (can be null)
* @param pminloc - Returns minimum location (can be null)
* @param pmaxloc - Returns maximum location (can be null)
* @return 0 on success, 1 on failure
*/
int pixGetExtremeValue(PIX pixs, IntPointer pminval, IntPointer pmaxval, IntPointer pminloc, IntPointer pmaxloc);
/**
* Check if images are equal
* @param pix1 - First image
* @param pix2 - Second image
* @param psame - Returns 1 if equal, 0 otherwise
* @return 0 on success, 1 on failure
*/
int pixEqual(PIX pix1, PIX pix2, IntPointer psame);
/**
* Calculate correlation between images
* @param pix1 - First image
* @param pix2 - Second image
* @param pcorrel - Returns correlation coefficient
* @return 0 on success, 1 on failure
*/
int pixCorrelationBinary(PIX pix1, PIX pix2, FloatPointer pcorrel);Usage Examples:
// Count foreground pixels
IntPointer pixelCount = new IntPointer(1);
pixCountPixels(binaryImage, pixelCount, null);
System.out.println("Foreground pixels: " + pixelCount.get());
// Get region statistics
FloatPointer average = new FloatPointer(1);
FloatPointer variance = new FloatPointer(1);
pixGetAverageMasked(grayImage, null, 100, 100, 50, 50, 1, average);
pixGetVarianceMasked(grayImage, null, 100, 100, 50, 50, 1, variance);
System.out.println("Region stats: mean=" + average.get() + ", var=" + variance.get());
// Find extreme values
IntPointer minVal = new IntPointer(1);
IntPointer maxVal = new IntPointer(1);
IntPointer minLoc = new IntPointer(1);
IntPointer maxLoc = new IntPointer(1);
pixGetExtremeValue(grayImage, minVal, maxVal, minLoc, maxLoc);
System.out.println("Range: " + minVal.get() + " to " + maxVal.get());
// Compare images
IntPointer same = new IntPointer(1);
pixEqual(image1, image2, same);
if (same.get() == 1) {
System.out.println("Images are identical");
}
// Calculate similarity
FloatPointer correlation = new FloatPointer(1);
pixCorrelationBinary(binary1, binary2, correlation);
System.out.println("Correlation: " + correlation.get());Memory allocation and management utilities for native operations.
/**
* Allocate memory
* @param nbytes - Number of bytes to allocate
* @return Pointer to allocated memory or null on failure
*/
Pointer lept_malloc(long nbytes);
/**
* Reallocate memory
* @param ptr - Existing pointer
* @param nbytes - New size in bytes
* @return Pointer to reallocated memory or null on failure
*/
Pointer lept_realloc(Pointer ptr, long nbytes);
/**
* Free memory
* @param ptr - Pointer to free
*/
void lept_free(Pointer ptr);
/**
* Get memory usage statistics
* @param pnalloc - Returns number of allocations
* @param pnfree - Returns number of frees
* @param pnbytes - Returns bytes allocated
* @return 0 on success, 1 on failure
*/
int lept_getAllocStats(IntPointer pnalloc, IntPointer pnfree, IntPointer pnbytes);Usage Examples:
// Manual memory management (rarely needed with JavaCPP)
Pointer buffer = lept_malloc(1024);
// ... use buffer ...
lept_free(buffer);
// Check memory statistics
IntPointer allocCount = new IntPointer(1);
IntPointer freeCount = new IntPointer(1);
IntPointer bytesAlloc = new IntPointer(1);
lept_getAllocStats(allocCount, freeCount, bytesAlloc);
System.out.println("Memory: " + allocCount.get() + " allocs, " +
freeCount.get() + " frees, " + bytesAlloc.get() + " bytes");// Direction constants
static final int L_SORT_INCREASING = 1;
static final int L_SORT_DECREASING = 2;
// Access flags
static final int L_COPY = 0;
static final int L_CLONE = 1;
static final int L_INSERT = 2;
static final int L_NOCOPY = 3;
// Buffer sizes
static final int L_DEFAULT_BUFFER_SIZE = 4096;Utility structures integrate seamlessly with image processing workflows:
// Region growing with stack
L_STACK growStack = lstackCreate(1000);
lstackAdd(growStack, seedPoint);
while (lstackGetCount(growStack) > 0) {
Point current = (Point)lstackRemove(growStack);
// Check neighbors and add to stack
if (isValidGrowthPoint(current.x + 1, current.y)) {
lstackAdd(growStack, new Point(current.x + 1, current.y));
}
// ... check other neighbors ...
}
// Performance monitoring
L_TIMER overallTimer = startTimer();
for (int i = 0; i < imageCount; i++) {
L_TIMER imageTimer = startTimer();
PIX processed = processImage(images[i]);
float imageTime = stopTimer(imageTimer);
System.out.println("Image " + i + ": " + imageTime + " seconds");
}
float totalTime = stopTimer(overallTimer);
System.out.println("Total processing: " + totalTime + " seconds");Install with Tessl CLI
npx tessl i tessl/maven-org-bytedeco--leptonica-platform