diff --git a/CHANGELOG.md b/CHANGELOG.md index 849c91e40d6..447c32b05ae 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,5 @@ + * Add helper for `PIX`, `FPIX`, and `DPIX` of Leptonica, facilitating access to image data of Tesseract ([issue #517](https://github.com/bytedeco/javacpp-presets/issues/517)) * Add presets for the NVBLAS, NVGRAPH, NVRTC, and NVML modules of CUDA ([issue deeplearning4j/nd4j#2895](https://github.com/deeplearning4j/nd4j/issues/2895)) * Link OpenBLAS with `-Wl,-z,noexecstack` on `linux-armhf` as required by the JDK ([issue deeplearning4j/libnd4j#700](https://github.com/deeplearning4j/libnd4j/issues/700)) * Include `textDetector.hpp` from the `opencv_text` module diff --git a/leptonica/src/main/java/org/bytedeco/javacpp/helper/lept.java b/leptonica/src/main/java/org/bytedeco/javacpp/helper/lept.java new file mode 100644 index 00000000000..25aeb906164 --- /dev/null +++ b/leptonica/src/main/java/org/bytedeco/javacpp/helper/lept.java @@ -0,0 +1,342 @@ +/* + * Copyright (C) 2018 Samuel Audet + * + * Licensed either under the Apache License, Version 2.0, or (at your option) + * under the terms of the GNU General Public License as published by + * the Free Software Foundation (subject to the "Classpath" exception), + * either version 2, or any later version (collectively, the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * http://www.gnu.org/licenses/ + * http://www.gnu.org/software/classpath/license.html + * + * or as provided in the LICENSE.txt file that accompanied this code. + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.bytedeco.javacpp.helper; + +import java.nio.ByteBuffer; +import java.nio.DoubleBuffer; +import java.nio.FloatBuffer; +import org.bytedeco.javacpp.BytePointer; +import org.bytedeco.javacpp.DoublePointer; +import org.bytedeco.javacpp.FloatPointer; +import org.bytedeco.javacpp.Pointer; +import org.bytedeco.javacpp.indexer.DoubleIndexer; +import org.bytedeco.javacpp.indexer.FloatIndexer; +import org.bytedeco.javacpp.indexer.Indexable; +import org.bytedeco.javacpp.indexer.UByteIndexer; + +// required by javac to resolve circular dependencies +import org.bytedeco.javacpp.lept.*; +import static org.bytedeco.javacpp.lept.dpixClone; +import static org.bytedeco.javacpp.lept.dpixCreate; +import static org.bytedeco.javacpp.lept.dpixCreateTemplate; +import static org.bytedeco.javacpp.lept.dpixDestroy; +import static org.bytedeco.javacpp.lept.dpixGetData; +import static org.bytedeco.javacpp.lept.dpixGetDimensions; +import static org.bytedeco.javacpp.lept.dpixGetWpl; +import static org.bytedeco.javacpp.lept.fpixClone; +import static org.bytedeco.javacpp.lept.fpixCreate; +import static org.bytedeco.javacpp.lept.fpixCreateTemplate; +import static org.bytedeco.javacpp.lept.fpixDestroy; +import static org.bytedeco.javacpp.lept.fpixGetData; +import static org.bytedeco.javacpp.lept.fpixGetDimensions; +import static org.bytedeco.javacpp.lept.fpixGetWpl; +import static org.bytedeco.javacpp.lept.pixClone; +import static org.bytedeco.javacpp.lept.pixCreate; +import static org.bytedeco.javacpp.lept.pixCreateHeader; +import static org.bytedeco.javacpp.lept.pixCreateNoInit; +import static org.bytedeco.javacpp.lept.pixCreateTemplate; +import static org.bytedeco.javacpp.lept.pixCreateTemplateNoInit; +import static org.bytedeco.javacpp.lept.pixDestroy; +import static org.bytedeco.javacpp.lept.pixGetData; +import static org.bytedeco.javacpp.lept.pixGetDepth; +import static org.bytedeco.javacpp.lept.pixGetHeight; +import static org.bytedeco.javacpp.lept.pixGetWidth; +import static org.bytedeco.javacpp.lept.pixGetWpl; + +public class lept extends org.bytedeco.javacpp.presets.lept { + + public static abstract class AbstractPIX extends Pointer implements Indexable { + protected static class DestroyDeallocator extends PIX implements Pointer.Deallocator { + boolean header = false; + DestroyDeallocator(PIX p) { this(p, false); } + DestroyDeallocator(PIX p, boolean header) { super(p); this.header = header; } + @Override public void deallocate() { if (header) this.data(null); pixDestroy(this); } + } + + public AbstractPIX(Pointer p) { super(p); } + + /** + * Calls pixCreate(), and registers a deallocator. + * @return PIX created. Do not call pixDestroy() on it. + */ + public static PIX create(int width, int height, int depth) { + PIX p = pixCreate(width, height, depth); + if (p != null) { + p.deallocator(new DestroyDeallocator(p)); + } + return p; + } + + /** + * Calls pixCreateNoInit(), and registers a deallocator. + * @return PIX created. Do not call pixDestroy() on it. + */ + public static PIX createNoInit(int width, int height, int depth) { + PIX p = pixCreateNoInit(width, height, depth); + if (p != null) { + p.deallocator(new DestroyDeallocator(p)); + } + return p; + } + + /** + * Calls pixCreateTemplate(), and registers a deallocator. + * @return PIX created. Do not call pixDestroy() on it. + */ + public static PIX createTemplate(PIX pixs) { + PIX p = pixCreateTemplate(pixs); + if (p != null) { + p.deallocator(new DestroyDeallocator(p)); + } + return p; + } + + /** + * Calls pixCreateTemplateNoInit(), and registers a deallocator. + * @return PIX created. Do not call pixDestroy() on it. + */ + public static PIX createTemplateNoInit(PIX pixs) { + PIX p = pixCreateTemplateNoInit(pixs); + if (p != null) { + p.deallocator(new DestroyDeallocator(p)); + } + return p; + } + + /** + * Calls pixCreateHeader(), and registers a deallocator. + * @return PIX created. Do not call pixDestroy() on it. + */ + public static PIX createHeader(int width, int height, int depth) { + PIX p = pixCreateHeader(width, height, depth); + if (p != null) { + p.deallocator(new DestroyDeallocator(p, true)); + } + return p; + } + + /** + * Calls pixClone(), and registers a deallocator. + * @return PIX cloned. Do not call pixDestroy() on it. + */ + @Override public PIX clone() { + PIX p = pixClone((PIX)this); + if (p != null) { + p.deallocator(new DestroyDeallocator(p)); + } + return p; + } + + /** @return {@code createBuffer(0)} */ + public ByteBuffer createBuffer() { + return createBuffer(0); + } + /** @return {@link PIX#data()} wrapped in a {@link ByteBuffer} starting at given byte index. */ + public ByteBuffer createBuffer(int index) { + int h = pixGetHeight((PIX)this); + int wpl = pixGetWpl((PIX)this); + BytePointer data = new BytePointer(pixGetData((PIX)this)).position(index).capacity(h * wpl * 4); + return data.asByteBuffer(); + } + + /** @return {@code createIndexer(true)} */ + public UByteIndexer createIndexer() { + return createIndexer(true); + } + @Override public UByteIndexer createIndexer(boolean direct) { + int w = pixGetWidth((PIX)this); + int h = pixGetHeight((PIX)this); + int d = pixGetDepth((PIX)this); + int wpl = pixGetWpl((PIX)this); + long[] sizes = {h, w, d / 8}; + long[] strides = {wpl * 4, d / 8, 1}; + BytePointer data = new BytePointer(pixGetData((PIX)this)).capacity(h * wpl * 4); + return UByteIndexer.create(data, sizes, strides, direct); + } + + /** + * Calls the deallocator, if registered, otherwise has no effect. + */ + public void destroy() { + deallocate(); + } + } + + public static abstract class AbstractFPIX extends Pointer implements Indexable { + protected static class DestroyDeallocator extends FPIX implements Pointer.Deallocator { + DestroyDeallocator(FPIX p) { super(p); } + @Override public void deallocate() { fpixDestroy(this); } + } + + public AbstractFPIX(Pointer p) { super(p); } + + /** + * Calls fpixCreate(), and registers a deallocator. + * @return FPIX created. Do not call fpixDestroy() on it. + */ + public static FPIX create(int width, int height) { + FPIX p = fpixCreate(width, height); + if (p != null) { + p.deallocator(new DestroyDeallocator(p)); + } + return p; + } + + /** + * Calls fpixCreateTemplate(), and registers a deallocator. + * @return FPIX created. Do not call fpixDestroy() on it. + */ + public static FPIX createTemplate(FPIX fpixs) { + FPIX p = fpixCreateTemplate(fpixs); + if (p != null) { + p.deallocator(new DestroyDeallocator(p)); + } + return p; + } + + /** + * Calls fpixClone(), and registers a deallocator. + * @return FPIX cloned. Do not call fpixDestroy() on it. + */ + @Override public FPIX clone() { + FPIX p = fpixClone((FPIX)this); + if (p != null) { + p.deallocator(new DestroyDeallocator(p)); + } + return p; + } + + /** @return {@code createBuffer(0)} */ + public FloatBuffer createBuffer() { + return createBuffer(0); + } + /** @return {@link FPIX#data()} wrapped in a {@link FloatBuffer} starting at given byte index. */ + public FloatBuffer createBuffer(int index) { + int[] w = {0}, h = {0}; + fpixGetDimensions((FPIX)this, w, h); + int wpl = fpixGetWpl((FPIX)this); + FloatPointer data = new FloatPointer(fpixGetData((FPIX)this)).position(index).capacity(h[0] * wpl); + return data.asBuffer(); + } + + /** @return {@code createIndexer(true)} */ + public FloatIndexer createIndexer() { + return createIndexer(true); + } + @Override public FloatIndexer createIndexer(boolean direct) { + int[] w = {0}, h = {0}; + fpixGetDimensions((FPIX)this, w, h); + int wpl = fpixGetWpl((FPIX)this); + long[] sizes = {h[0], w[0], wpl / w[0]}; + long[] strides = {wpl, wpl / w[0], 1}; + FloatPointer data = new FloatPointer(fpixGetData((FPIX)this)).capacity(h[0] * wpl); + return FloatIndexer.create(data, sizes, strides, direct); + } + + /** + * Calls the deallocator, if registered, otherwise has no effect. + */ + public void destroy() { + deallocate(); + } + } + + public static abstract class AbstractDPIX extends Pointer implements Indexable { + protected static class DestroyDeallocator extends DPIX implements Pointer.Deallocator { + DestroyDeallocator(DPIX p) { super(p); } + @Override public void deallocate() { dpixDestroy(this); } + } + + public AbstractDPIX(Pointer p) { super(p); } + + /** + * Calls dpixCreate(), and registers a deallocator. + * @return DPIX created. Do not call dpixDestroy() on it. + */ + public static DPIX create(int width, int height) { + DPIX p = dpixCreate(width, height); + if (p != null) { + p.deallocator(new DestroyDeallocator(p)); + } + return p; + } + + /** + * Calls dpixCreateTemplate(), and registers a deallocator. + * @return DPIX created. Do not call dpixDestroy() on it. + */ + public static DPIX createTemplate(DPIX dpixs) { + DPIX p = dpixCreateTemplate(dpixs); + if (p != null) { + p.deallocator(new DestroyDeallocator(p)); + } + return p; + } + + /** + * Calls dpixClone(), and registers a deallocator. + * @return DPIX cloned. Do not call dpixDestroy() on it. + */ + @Override public DPIX clone() { + DPIX p = dpixClone((DPIX)this); + if (p != null) { + p.deallocator(new DestroyDeallocator(p)); + } + return p; + } + + /** @return {@code createBuffer(0)} */ + public DoubleBuffer createBuffer() { + return createBuffer(0); + } + /** @return {@link DPIX#data()} wrapped in a {@link DoubleBuffer} starting at given byte index. */ + public DoubleBuffer createBuffer(int index) { + int[] w = {0}, h = {0}; + dpixGetDimensions((DPIX)this, w, h); + int wpl = dpixGetWpl((DPIX)this); + DoublePointer data = new DoublePointer(dpixGetData((DPIX)this)).position(index).capacity(h[0] * wpl); + return data.asBuffer(); + } + + /** @return {@code createIndexer(true)} */ + public DoubleIndexer createIndexer() { + return createIndexer(true); + } + @Override public DoubleIndexer createIndexer(boolean direct) { + int[] w = {0}, h = {0}; + dpixGetDimensions((DPIX)this, w, h); + int wpl = dpixGetWpl((DPIX)this); + long[] sizes = {h[0], w[0], wpl / w[0]}; + long[] strides = {wpl, wpl / w[0], 1}; + DoublePointer data = new DoublePointer(dpixGetData((DPIX)this)).capacity(h[0] * wpl); + return DoubleIndexer.create(data, sizes, strides, direct); + } + + /** + * Calls the deallocator, if registered, otherwise has no effect. + */ + public void destroy() { + deallocate(); + } + } +} diff --git a/leptonica/src/main/java/org/bytedeco/javacpp/lept.java b/leptonica/src/main/java/org/bytedeco/javacpp/lept.java index d29292c54af..a195ba64910 100644 --- a/leptonica/src/main/java/org/bytedeco/javacpp/lept.java +++ b/leptonica/src/main/java/org/bytedeco/javacpp/lept.java @@ -6,7 +6,7 @@ import org.bytedeco.javacpp.*; import org.bytedeco.javacpp.annotation.*; -public class lept extends org.bytedeco.javacpp.presets.lept { +public class lept extends org.bytedeco.javacpp.helper.lept { static { Loader.load(); } // Parsed from leptonica/alltypes.h @@ -3507,7 +3507,7 @@ public class lept extends org.bytedeco.javacpp.presets.lept { * that direct non-default actions, e.g., in png and jpeg I/O. */ /** Basic Pix */ -@Name("Pix") public static class PIX extends Pointer { +@Name("Pix") public static class PIX extends AbstractPIX { static { Loader.load(); } /** Default native constructor. */ public PIX() { super((Pointer)null); allocate(); } @@ -4190,7 +4190,7 @@ public class lept extends org.bytedeco.javacpp.presets.lept { public static final int FPIX_VERSION_NUMBER = 2; /** Pix with float array */ -@Name("FPix") public static class FPIX extends Pointer { +@Name("FPix") public static class FPIX extends AbstractFPIX { static { Loader.load(); } /** Default native constructor. */ public FPIX() { super((Pointer)null); allocate(); } @@ -4256,7 +4256,7 @@ public class lept extends org.bytedeco.javacpp.presets.lept { public static final int DPIX_VERSION_NUMBER = 2; /** Pix with double array */ -@Name("DPix") public static class DPIX extends Pointer { +@Name("DPix") public static class DPIX extends AbstractDPIX { static { Loader.load(); } /** Default native constructor. */ public DPIX() { super((Pointer)null); allocate(); } diff --git a/leptonica/src/main/java/org/bytedeco/javacpp/presets/lept.java b/leptonica/src/main/java/org/bytedeco/javacpp/presets/lept.java index bb2c81243d9..7fab406f83c 100644 --- a/leptonica/src/main/java/org/bytedeco/javacpp/presets/lept.java +++ b/leptonica/src/main/java/org/bytedeco/javacpp/presets/lept.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2014-2015 Samuel Audet + * Copyright (C) 2014-2018 Samuel Audet * * Licensed either under the Apache License, Version 2.0, or (at your option) * under the terms of the GNU General Public License as published by @@ -33,7 +33,7 @@ * * @author Samuel Audet */ -@Properties(target = "org.bytedeco.javacpp.lept", value = { +@Properties(target = "org.bytedeco.javacpp.lept", helper = "org.bytedeco.javacpp.helper.lept", value = { @Platform(include = {"leptonica/alltypes.h", "leptonica/environ.h", "leptonica/array.h", "leptonica/bbuffer.h", "leptonica/heap.h", "leptonica/list.h", "leptonica/ptra.h", "leptonica/queue.h", "leptonica/rbtree.h", "leptonica/stack.h", "leptonica/arrayaccess.h", "leptonica/bmf.h", "leptonica/ccbord.h", "leptonica/dewarp.h", "leptonica/gplot.h", "leptonica/imageio.h", "leptonica/jbclass.h", "leptonica/morph.h", "leptonica/pix.h", @@ -82,7 +82,7 @@ public void map(InfoMap infoMap) { .put(new Info("Sel").pointerTypes("SEL")) .put(new Info("Sela").pointerTypes("SELA")) .put(new Info("L_Kernel").pointerTypes("L_KERNEL")) - .put(new Info("Pix").pointerTypes("PIX")) + .put(new Info("Pix").pointerTypes("PIX").base("AbstractPIX")) .put(new Info("PixColormap").pointerTypes("PIXCMAP")) .put(new Info("RGBA_Quad").pointerTypes("RGBA_QUAD")) .put(new Info("Pixa").pointerTypes("PIXA")) @@ -94,9 +94,9 @@ public void map(InfoMap infoMap) { .put(new Info("Ptaa").pointerTypes("PTAA")) .put(new Info("Pixacc").pointerTypes("PIXACC")) .put(new Info("PixTiling").pointerTypes("PIXTILING")) - .put(new Info("FPix").pointerTypes("FPIX")) + .put(new Info("FPix").pointerTypes("FPIX").base("AbstractFPIX")) .put(new Info("FPixa").pointerTypes("FPIXA")) - .put(new Info("DPix").pointerTypes("DPIX")) + .put(new Info("DPix").pointerTypes("DPIX").base("AbstractDPIX")) .put(new Info("PixComp").pointerTypes("PIXC")) .put(new Info("PixaComp").pointerTypes("PIXAC")) .put(new Info("L_Recoga").pointerTypes("L_RECOGA"))