Skip to content

Commit 4a6cd75

Browse files
committed
Fix crash when opening symbol feature dialog
1 parent 9f09fbb commit 4a6cd75

2 files changed

Lines changed: 84 additions & 83 deletions

File tree

gui/src/main/java/de/vorb/tesseract/gui/work/PageRecognitionProducer.java

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
import de.vorb.tesseract.tools.recognition.RecognitionProducer;
55
import de.vorb.tesseract.util.feat.Feature3D;
66

7+
import org.bytedeco.javacpp.BytePointer;
78
import org.bytedeco.javacpp.IntPointer;
8-
import org.bytedeco.javacpp.PointerPointer;
99
import org.bytedeco.javacpp.lept;
1010
import org.bytedeco.javacpp.tesseract;
1111

@@ -83,7 +83,7 @@ public void close() throws IOException {
8383
public void loadImage(Path imageFile) {
8484
if (lastPix.isPresent()) {
8585
// destroy old pix
86-
lept.pixDestroy(new PointerPointer(lastPix.get()));
86+
lept.pixDestroy(lastPix.get());
8787
}
8888

8989
final lept.PIX pix = lept.pixRead(imageFile.toString());
@@ -135,36 +135,35 @@ public List<Feature3D> getFeaturesForSymbol(BufferedImage symbol) {
135135
return new LinkedList<>();
136136
}
137137

138-
final lept.PIX pixSymb = lept.pixRead(symbolFile);
138+
try (final lept.PIX pixSymb = lept.pixRead(symbolFile);
139+
final IntPointer numFeatures = new IntPointer(1);
140+
final IntPointer outlineIndexes = new IntPointer(512);
141+
final BytePointer features = new BytePointer(4 * 512);
142+
final tesseract.INT_FEATURE_STRUCT intFeatures = new tesseract.INT_FEATURE_STRUCT(features)) {
139143

140-
final tesseract.TBLOB blob = tesseract.TessMakeTBLOB(pixSymb);
141-
lept.pixDestroy(new PointerPointer(pixSymb));
144+
final tesseract.TBLOB blob = tesseract.TessMakeTBLOB(pixSymb);
142145

143-
final IntPointer numFeatures = new IntPointer(1);
146+
lept.pixDestroy(pixSymb);
144147

145-
final IntPointer outlineIndexes = new IntPointer(512);
148+
tesseract.TessBaseAPIGetFeaturesForBlob(getHandle(), blob, intFeatures, numFeatures, outlineIndexes);
146149

147-
final tesseract.INT_FEATURE_STRUCT intFeatures = new tesseract.INT_FEATURE_STRUCT().capacity(512);
148-
tesseract.TessBaseAPIGetFeaturesForBlob(getHandle(), blob, intFeatures, numFeatures, outlineIndexes);
150+
// make a list of Features3D
151+
final ArrayList<Feature3D> featureList = new ArrayList<>(numFeatures.get());
149152

150-
// make a list of Features3D
151-
final ArrayList<Feature3D> featureList = new ArrayList<>(numFeatures.get());
153+
for (int i = 0; i < numFeatures.get(); i++) {
154+
final int x = features.get(i * 4) & 0xFF;
155+
final int y = features.get(i * 4 + 1) & 0xFF;
156+
final int theta = features.get(i * 4 + 2) & 0xFF;
157+
final byte cpMisses = features.get(i * 4 + 3);
158+
final int outlineIndex = outlineIndexes.get(i);
152159

153-
final ByteBuffer features = intFeatures.asByteBuffer();
160+
final Feature3D feat = new Feature3D(x, y, theta, cpMisses, outlineIndex);
154161

155-
for (int i = 0; i < numFeatures.get(); i++) {
156-
final int x = features.get(i * 4) & 0xFF;
157-
final int y = features.get(i * 4 + 1) & 0xFF;
158-
final int theta = features.get(i * 4 + 2) & 0xFF;
159-
final byte cpMisses = features.get(i * 4 + 3);
160-
final int outlineIndex = outlineIndexes.get(i);
162+
featureList.add(feat);
163+
}
161164

162-
final Feature3D feat = new Feature3D(x, y, theta, cpMisses, outlineIndex);
163-
164-
featureList.add(feat);
165+
return featureList;
165166
}
166-
167-
return featureList;
168167
}
169168

170169
public void setVariable(String key, String value) {

tools/src/main/java/de/vorb/tesseract/tools/recognition/RecognitionState.java

Lines changed: 62 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -35,21 +35,20 @@ public RecognitionState(tesseract.TessBaseAPI apiHandle, tesseract.ResultIterato
3535
* @return requested box
3636
*/
3737
public Box getBoundingBox(int level) {
38-
// pointers to the bounding box coordinates
39-
final IntPointer left = new IntPointer(1);
40-
final IntPointer top = new IntPointer(1);
41-
final IntPointer right = new IntPointer(1);
42-
final IntPointer bottom = new IntPointer(1);
38+
try (final IntPointer left = new IntPointer(1);
39+
final IntPointer top = new IntPointer(1);
40+
final IntPointer right = new IntPointer(1);
41+
final IntPointer bottom = new IntPointer(1)) {
4342

44-
// get bounding box
45-
tesseract.TessPageIteratorBoundingBox(pageIt, level, left, top, right, bottom);
43+
tesseract.TessPageIteratorBoundingBox(pageIt, level, left, top, right, bottom);
4644

47-
final int x = left.get();
48-
final int y = top.get();
49-
final int width = right.get() - x;
50-
final int height = bottom.get() - y;
45+
final int x = left.get();
46+
final int y = top.get();
47+
final int width = right.get() - x;
48+
final int height = bottom.get() - y;
5149

52-
return new Box(x, y, width, height);
50+
return new Box(x, y, width, height);
51+
}
5352
}
5453

5554
/**
@@ -72,22 +71,22 @@ public String getText(int level) {
7271
* @return baseline
7372
*/
7473
public Baseline getBaseline(int level) {
75-
// pointers to the baseline coordinates
76-
final IntPointer x1 = new IntPointer(1);
77-
final IntPointer y1 = new IntPointer(1);
78-
final IntPointer x2 = new IntPointer(1);
79-
final IntPointer y2 = new IntPointer(1);
74+
try (final IntPointer x1 = new IntPointer(1);
75+
final IntPointer y1 = new IntPointer(1);
76+
final IntPointer x2 = new IntPointer(1);
77+
final IntPointer y2 = new IntPointer(1)) {
8078

81-
tesseract.TessPageIteratorBaseline(pageIt, level, x1, y1, x2, y2);
79+
tesseract.TessPageIteratorBaseline(pageIt, level, x1, y1, x2, y2);
8280

83-
final int width = x2.get() - x1.get();
84-
final float height = y2.get() - y1.get();
85-
final float slope = height / width;
81+
final int width = x2.get() - x1.get();
82+
final float height = y2.get() - y1.get();
83+
final float slope = height / width;
8684

87-
final Box bbox = getBoundingBox(tesseract.RIL_WORD);
88-
final int yOffset = bbox.getY() + bbox.getHeight() - y1.get();
85+
final Box bbox = getBoundingBox(tesseract.RIL_WORD);
86+
final int yOffset = bbox.getY() + bbox.getHeight() - y1.get();
8987

90-
return new Baseline(yOffset, slope);
88+
return new Baseline(yOffset, slope);
89+
}
9190
}
9291

9392
/**
@@ -117,51 +116,54 @@ public List<AlternativeChoice> getAlternatives() {
117116
}
118117

119118
private void getSymbolFeatures(lept.PIX image) {
120-
final IntPointer left = new IntPointer(1);
121-
final IntPointer top = new IntPointer(1);
122119

123-
final lept.PIX pix = tesseract.TessPageIteratorGetImage(pageIt, tesseract.RIL_SYMBOL, 1, image, left, top);
124-
final tesseract.TBLOB blob = tesseract.TessMakeTBLOB(pix);
120+
try (final IntPointer left = new IntPointer(1);
121+
final IntPointer top = new IntPointer(1);
122+
final IntPointer numFeatures = new IntPointer(1);
123+
final IntPointer featOutlineIndex = new IntPointer(1);
124+
final tesseract.INT_FEATURE_STRUCT intFeatures = new tesseract.INT_FEATURE_STRUCT(
125+
new BytePointer(4 * 512))) {
125126

126-
final IntPointer numFeatures = new IntPointer(1);
127-
final IntPointer featOutlineIndex = new IntPointer(1);
127+
final lept.PIX pix = tesseract.TessPageIteratorGetImage(pageIt, tesseract.RIL_SYMBOL, 1, image, left, top);
128+
final tesseract.TBLOB blob = tesseract.TessMakeTBLOB(pix);
128129

129-
final tesseract.INT_FEATURE_STRUCT intFeatures = new tesseract.INT_FEATURE_STRUCT().capacity(512);
130-
131-
tesseract.TessBaseAPIGetFeaturesForBlob(apiHandle, blob, intFeatures, numFeatures, featOutlineIndex);
130+
tesseract.TessBaseAPIGetFeaturesForBlob(apiHandle, blob, intFeatures, numFeatures, featOutlineIndex);
131+
}
132132
}
133133

134134
/**
135135
* @return font attributes for the current word.
136136
*/
137137
public FontAttributes getWordFontAttributes() {
138-
// pointers to integers for every attribute
139-
final BoolPointer isBold = new BoolPointer(1);
140-
final BoolPointer isItalic = new BoolPointer(1);
141-
final BoolPointer isUnderlined = new BoolPointer(1);
142-
final BoolPointer isMonospace = new BoolPointer(1);
143-
final BoolPointer isSerif = new BoolPointer(1);
144-
final BoolPointer isSmallCaps = new BoolPointer(1);
145-
final IntPointer fontSize = new IntPointer(1);
146-
final IntPointer fontID = new IntPointer(1);
147-
148-
// set values
149-
tesseract.TessResultIteratorWordFontAttributes(resultIt, isBold, isItalic, isUnderlined, isMonospace, isSerif,
150-
isSmallCaps, fontSize, fontID);
151-
152-
// build and return FontAttributes
153-
final FontAttributes fontAttributes = new FontAttributes.Builder()
154-
.bold(isBold.get())
155-
.italic(isItalic.get())
156-
.underlined(isUnderlined.get())
157-
.monospace(isMonospace.get())
158-
.serif(isSerif.get())
159-
.smallcaps(isSmallCaps.get())
160-
.size(fontSize.get())
161-
.fontID(fontID.get())
162-
.build();
163-
164-
return fontAttributes;
138+
139+
try (final BoolPointer isBold = new BoolPointer(1);
140+
final BoolPointer isItalic = new BoolPointer(1);
141+
final BoolPointer isUnderlined = new BoolPointer(1);
142+
final BoolPointer isMonospace = new BoolPointer(1);
143+
final BoolPointer isSerif = new BoolPointer(1);
144+
final BoolPointer isSmallCaps = new BoolPointer(1);
145+
final IntPointer fontSize = new IntPointer(1);
146+
final IntPointer fontID = new IntPointer(1)) {
147+
148+
// set values
149+
tesseract.TessResultIteratorWordFontAttributes(resultIt, isBold, isItalic, isUnderlined, isMonospace,
150+
isSerif,
151+
isSmallCaps, fontSize, fontID);
152+
153+
// build and return FontAttributes
154+
final FontAttributes fontAttributes = new FontAttributes.Builder()
155+
.bold(isBold.get())
156+
.italic(isItalic.get())
157+
.underlined(isUnderlined.get())
158+
.monospace(isMonospace.get())
159+
.serif(isSerif.get())
160+
.smallcaps(isSmallCaps.get())
161+
.size(fontSize.get())
162+
.fontID(fontID.get())
163+
.build();
164+
165+
return fontAttributes;
166+
}
165167
}
166168

167169
/**

0 commit comments

Comments
 (0)