Skip to content

Commit 9408d00

Browse files
committed
Move LeptoniocaWrapper out from TesseractOcrUtil to ignore it from sonarqube checks
DEVSIX-7068
1 parent 95d8a71 commit 9408d00

3 files changed

Lines changed: 154 additions & 119 deletions

File tree

pdfocr-tesseract4/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@
6868
</dependencies>
6969

7070
<properties>
71-
<sonar.exclusions>src/main/java/com/itextpdf/pdfocr/tesseract4/TesseractOcrUtil.java</sonar.exclusions>
71+
<sonar.exclusions>src/main/java/com/itextpdf/pdfocr/tesseract4/LeptonicaWrapper.java</sonar.exclusions>
7272
</properties>
7373

7474
</project>
Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
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+
}

pdfocr-tesseract4/src/main/java/com/itextpdf/pdfocr/tesseract4/TesseractOcrUtil.java

Lines changed: 0 additions & 118 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ This file is part of the iText (R) project.
3131

3232
import com.ochafik.lang.jnaerator.runtime.NativeSize;
3333
import com.ochafik.lang.jnaerator.runtime.NativeSizeByReference;
34-
import com.sun.jna.Pointer;
3534
import com.sun.jna.ptr.PointerByReference;
3635
import java.awt.image.BufferedImage;
3736
import java.io.ByteArrayInputStream;
@@ -50,8 +49,6 @@ This file is part of the iText (R) project.
5049
import java.util.List;
5150
import javax.imageio.ImageIO;
5251
import net.sourceforge.lept4j.ILeptonica;
53-
import net.sourceforge.lept4j.Leptonica;
54-
import net.sourceforge.lept4j.Leptonica1;
5552
import net.sourceforge.lept4j.Pix;
5653
import net.sourceforge.tess4j.ITesseract;
5754
import net.sourceforge.tess4j.Tesseract;
@@ -809,119 +806,4 @@ static ImageData applyRotation(final ImageData imageData) {
809806
return newImageData;
810807
}
811808
}
812-
813-
private static final class LeptonicaWrapper {
814-
private static final int LEPTONICA_NOT_SUPPORTED_JDK_VERSION = 19;
815-
private static final int JDK_MAJOR_VERSION = getJavaMajorVersion();
816-
817-
private LeptonicaWrapper() {}
818-
819-
public static int pixGetDepth(Pix pix) {
820-
if (JDK_MAJOR_VERSION < LEPTONICA_NOT_SUPPORTED_JDK_VERSION) {
821-
return Leptonica.INSTANCE.pixGetDepth(pix);
822-
}
823-
return Leptonica1.pixGetDepth(pix);
824-
}
825-
826-
public static Pix pixConvertRGBToLuminance(Pix pix) {
827-
if (JDK_MAJOR_VERSION < LEPTONICA_NOT_SUPPORTED_JDK_VERSION) {
828-
return Leptonica.INSTANCE.pixConvertRGBToLuminance(pix);
829-
}
830-
return Leptonica1.pixConvertRGBToLuminance(pix);
831-
}
832-
833-
public static Pix pixRemoveColormap(Pix pix, int option) {
834-
if (JDK_MAJOR_VERSION < LEPTONICA_NOT_SUPPORTED_JDK_VERSION) {
835-
return Leptonica.INSTANCE.pixRemoveColormap(pix, option);
836-
}
837-
return Leptonica1.pixRemoveColormap(pix, option);
838-
}
839-
840-
public static Pix pixRead(String var1) {
841-
if (JDK_MAJOR_VERSION < LEPTONICA_NOT_SUPPORTED_JDK_VERSION) {
842-
return Leptonica.INSTANCE.pixRead(var1);
843-
}
844-
return Leptonica1.pixRead(var1);
845-
}
846-
847-
public static void pixOtsuAdaptiveThreshold(Pix pix, int i, int i1, int i2, int i3, float v,
848-
PointerByReference pointerByReference, PointerByReference pointerByReference1) {
849-
if (JDK_MAJOR_VERSION < LEPTONICA_NOT_SUPPORTED_JDK_VERSION) {
850-
Leptonica.INSTANCE.pixOtsuAdaptiveThreshold(pix, i, i1, i2, i3, v, pointerByReference,
851-
pointerByReference1);
852-
return;
853-
}
854-
Leptonica1.pixOtsuAdaptiveThreshold(pix, i, i1, i2, i3, v, pointerByReference, pointerByReference1);
855-
}
856-
857-
public static void lept_free(Pointer pointer) {
858-
if (JDK_MAJOR_VERSION < LEPTONICA_NOT_SUPPORTED_JDK_VERSION) {
859-
Leptonica.INSTANCE.lept_free(pointer);
860-
return;
861-
}
862-
Leptonica1.lept_free(pointer);
863-
}
864-
865-
public static void pixWriteMem(PointerByReference pointer, NativeSizeByReference nativeSize, Pix pix, int i) {
866-
if (JDK_MAJOR_VERSION < LEPTONICA_NOT_SUPPORTED_JDK_VERSION) {
867-
Leptonica.INSTANCE.pixWriteMem(pointer, nativeSize, pix, i);
868-
return;
869-
}
870-
Leptonica1.pixWriteMem(pointer, nativeSize, pix, i);
871-
}
872-
873-
public static int pixWriteMemPng(PointerByReference pointer, NativeSizeByReference nativeSize, Pix pix, int i) {
874-
if (JDK_MAJOR_VERSION < LEPTONICA_NOT_SUPPORTED_JDK_VERSION) {
875-
return Leptonica.INSTANCE.pixWriteMemPng(pointer, nativeSize, pix, i);
876-
}
877-
return Leptonica1.pixWriteMemPng(pointer, nativeSize, pix, i);
878-
}
879-
880-
public static void pixWritePng(String s, Pix pix, float v) {
881-
if (JDK_MAJOR_VERSION < LEPTONICA_NOT_SUPPORTED_JDK_VERSION) {
882-
Leptonica.INSTANCE.pixWritePng(s, pix, v);
883-
return;
884-
}
885-
Leptonica1.pixWritePng(s, pix, v);
886-
}
887-
888-
public static Pix pixReadMem(ByteBuffer buffer, NativeSize nativeSize) {
889-
if (JDK_MAJOR_VERSION < LEPTONICA_NOT_SUPPORTED_JDK_VERSION) {
890-
return Leptonica.INSTANCE.pixReadMem(buffer, nativeSize);
891-
}
892-
return Leptonica1.pixReadMem(buffer, nativeSize);
893-
}
894-
895-
public static Pix pixRotate90(Pix pix, int i) {
896-
if (JDK_MAJOR_VERSION < LEPTONICA_NOT_SUPPORTED_JDK_VERSION) {
897-
return Leptonica.INSTANCE.pixRotate90(pix, i);
898-
}
899-
return Leptonica1.pixRotate90(pix, i);
900-
}
901-
902-
public static Pix pixRotate180(Pix pix1, Pix pix2) {
903-
if (JDK_MAJOR_VERSION < LEPTONICA_NOT_SUPPORTED_JDK_VERSION) {
904-
return Leptonica.INSTANCE.pixRotate180(pix1, pix2);
905-
}
906-
return Leptonica1.pixRotate180(pix1, pix2);
907-
}
908-
909-
/**
910-
* gets java runtime version.
911-
*
912-
* @return major version of runtime java
913-
*/
914-
private static int getJavaMajorVersion() {
915-
String version = System.getProperty("java.version");
916-
if (version.startsWith("1.")) {
917-
version = version.substring(2, 3);
918-
} else {
919-
int dot = version.indexOf('.');
920-
if(dot != -1) {
921-
version = version.substring(0, dot);
922-
}
923-
}
924-
return Integer.parseInt(version);
925-
}
926-
}
927809
}

0 commit comments

Comments
 (0)