|
| 1 | +/* |
| 2 | + This file is part of the iText (R) project. |
| 3 | + Copyright (c) 1998-2022 iText Group NV |
| 4 | + Authors: iText Software. |
| 5 | +
|
| 6 | + This program is offered under a commercial and under the AGPL license. |
| 7 | + For commercial licensing, contact us at https://itextpdf.com/sales. For AGPL licensing, see below. |
| 8 | +
|
| 9 | + AGPL licensing: |
| 10 | + This program is free software: you can redistribute it and/or modify |
| 11 | + it under the terms of the GNU Affero General Public License as published by |
| 12 | + the Free Software Foundation, either version 3 of the License, or |
| 13 | + (at your option) any later version. |
| 14 | +
|
| 15 | + This program is distributed in the hope that it will be useful, |
| 16 | + but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | + GNU Affero General Public License for more details. |
| 19 | +
|
| 20 | + You should have received a copy of the GNU Affero General Public License |
| 21 | + along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 22 | + */ |
| 23 | +package com.itextpdf.pdfocr.tesseract4; |
| 24 | + |
| 25 | +import com.ochafik.lang.jnaerator.runtime.NativeSize; |
| 26 | +import com.ochafik.lang.jnaerator.runtime.NativeSizeByReference; |
| 27 | +import com.sun.jna.Pointer; |
| 28 | +import com.sun.jna.ptr.PointerByReference; |
| 29 | +import java.nio.ByteBuffer; |
| 30 | +import net.sourceforge.lept4j.Leptonica; |
| 31 | +import net.sourceforge.lept4j.Leptonica1; |
| 32 | +import net.sourceforge.lept4j.Pix; |
| 33 | + |
| 34 | +/** |
| 35 | + * Wrapper class around {@link net.sourceforge.lept4j.Leptonica} and {@link net.sourceforge.lept4j.Leptonica1}. |
| 36 | + * It uses one of the above classes based on JDK version. {@link net.sourceforge.lept4j.Leptonica} |
| 37 | + * is not supported for JDK 19 and above. But {@link net.sourceforge.lept4j.Leptonica1} requires a fresh version |
| 38 | + * of leptonica native library on Linux. |
| 39 | + */ |
| 40 | +final class LeptonicaWrapper { |
| 41 | + private static final int LEPTONICA_NOT_SUPPORTED_JDK_VERSION = 19; |
| 42 | + private static final int JDK_MAJOR_VERSION = getJavaMajorVersion(); |
| 43 | + |
| 44 | + private LeptonicaWrapper() {} |
| 45 | + |
| 46 | + public static int pixGetDepth(Pix pix) { |
| 47 | + if (JDK_MAJOR_VERSION < LEPTONICA_NOT_SUPPORTED_JDK_VERSION) { |
| 48 | + return Leptonica.INSTANCE.pixGetDepth(pix); |
| 49 | + } |
| 50 | + return Leptonica1.pixGetDepth(pix); |
| 51 | + } |
| 52 | + |
| 53 | + public static Pix pixConvertRGBToLuminance(Pix pix) { |
| 54 | + if (JDK_MAJOR_VERSION < LEPTONICA_NOT_SUPPORTED_JDK_VERSION) { |
| 55 | + return Leptonica.INSTANCE.pixConvertRGBToLuminance(pix); |
| 56 | + } |
| 57 | + return Leptonica1.pixConvertRGBToLuminance(pix); |
| 58 | + } |
| 59 | + |
| 60 | + public static Pix pixRemoveColormap(Pix pix, int option) { |
| 61 | + if (JDK_MAJOR_VERSION < LEPTONICA_NOT_SUPPORTED_JDK_VERSION) { |
| 62 | + return Leptonica.INSTANCE.pixRemoveColormap(pix, option); |
| 63 | + } |
| 64 | + return Leptonica1.pixRemoveColormap(pix, option); |
| 65 | + } |
| 66 | + |
| 67 | + public static Pix pixRead(String var1) { |
| 68 | + if (JDK_MAJOR_VERSION < LEPTONICA_NOT_SUPPORTED_JDK_VERSION) { |
| 69 | + return Leptonica.INSTANCE.pixRead(var1); |
| 70 | + } |
| 71 | + return Leptonica1.pixRead(var1); |
| 72 | + } |
| 73 | + |
| 74 | + public static void pixOtsuAdaptiveThreshold(Pix pix, int i, int i1, int i2, int i3, float v, |
| 75 | + PointerByReference pointerByReference, PointerByReference pointerByReference1) { |
| 76 | + if (JDK_MAJOR_VERSION < LEPTONICA_NOT_SUPPORTED_JDK_VERSION) { |
| 77 | + Leptonica.INSTANCE.pixOtsuAdaptiveThreshold(pix, i, i1, i2, i3, v, pointerByReference, |
| 78 | + pointerByReference1); |
| 79 | + return; |
| 80 | + } |
| 81 | + Leptonica1.pixOtsuAdaptiveThreshold(pix, i, i1, i2, i3, v, pointerByReference, pointerByReference1); |
| 82 | + } |
| 83 | + |
| 84 | + public static void lept_free(Pointer pointer) { |
| 85 | + if (JDK_MAJOR_VERSION < LEPTONICA_NOT_SUPPORTED_JDK_VERSION) { |
| 86 | + Leptonica.INSTANCE.lept_free(pointer); |
| 87 | + return; |
| 88 | + } |
| 89 | + Leptonica1.lept_free(pointer); |
| 90 | + } |
| 91 | + |
| 92 | + public static void pixWriteMem(PointerByReference pointer, NativeSizeByReference nativeSize, Pix pix, int i) { |
| 93 | + if (JDK_MAJOR_VERSION < LEPTONICA_NOT_SUPPORTED_JDK_VERSION) { |
| 94 | + Leptonica.INSTANCE.pixWriteMem(pointer, nativeSize, pix, i); |
| 95 | + return; |
| 96 | + } |
| 97 | + Leptonica1.pixWriteMem(pointer, nativeSize, pix, i); |
| 98 | + } |
| 99 | + |
| 100 | + public static int pixWriteMemPng(PointerByReference pointer, NativeSizeByReference nativeSize, Pix pix, int i) { |
| 101 | + if (JDK_MAJOR_VERSION < LEPTONICA_NOT_SUPPORTED_JDK_VERSION) { |
| 102 | + return Leptonica.INSTANCE.pixWriteMemPng(pointer, nativeSize, pix, i); |
| 103 | + } |
| 104 | + return Leptonica1.pixWriteMemPng(pointer, nativeSize, pix, i); |
| 105 | + } |
| 106 | + |
| 107 | + public static void pixWritePng(String s, Pix pix, float v) { |
| 108 | + if (JDK_MAJOR_VERSION < LEPTONICA_NOT_SUPPORTED_JDK_VERSION) { |
| 109 | + Leptonica.INSTANCE.pixWritePng(s, pix, v); |
| 110 | + return; |
| 111 | + } |
| 112 | + Leptonica1.pixWritePng(s, pix, v); |
| 113 | + } |
| 114 | + |
| 115 | + public static Pix pixReadMem(ByteBuffer buffer, NativeSize nativeSize) { |
| 116 | + if (JDK_MAJOR_VERSION < LEPTONICA_NOT_SUPPORTED_JDK_VERSION) { |
| 117 | + return Leptonica.INSTANCE.pixReadMem(buffer, nativeSize); |
| 118 | + } |
| 119 | + return Leptonica1.pixReadMem(buffer, nativeSize); |
| 120 | + } |
| 121 | + |
| 122 | + public static Pix pixRotate90(Pix pix, int i) { |
| 123 | + if (JDK_MAJOR_VERSION < LEPTONICA_NOT_SUPPORTED_JDK_VERSION) { |
| 124 | + return Leptonica.INSTANCE.pixRotate90(pix, i); |
| 125 | + } |
| 126 | + return Leptonica1.pixRotate90(pix, i); |
| 127 | + } |
| 128 | + |
| 129 | + public static Pix pixRotate180(Pix pix1, Pix pix2) { |
| 130 | + if (JDK_MAJOR_VERSION < LEPTONICA_NOT_SUPPORTED_JDK_VERSION) { |
| 131 | + return Leptonica.INSTANCE.pixRotate180(pix1, pix2); |
| 132 | + } |
| 133 | + return Leptonica1.pixRotate180(pix1, pix2); |
| 134 | + } |
| 135 | + |
| 136 | + /** |
| 137 | + * gets java runtime version. |
| 138 | + * |
| 139 | + * @return major version of runtime java |
| 140 | + */ |
| 141 | + private static int getJavaMajorVersion() { |
| 142 | + String version = System.getProperty("java.version"); |
| 143 | + if (version.startsWith("1.")) { |
| 144 | + version = version.substring(2, 3); |
| 145 | + } else { |
| 146 | + int dot = version.indexOf('.'); |
| 147 | + if(dot != -1) { |
| 148 | + version = version.substring(0, dot); |
| 149 | + } |
| 150 | + } |
| 151 | + return Integer.parseInt(version); |
| 152 | + } |
| 153 | +} |
0 commit comments