Skip to content

Commit

Permalink
Basic usage of new Image class. Only pixDestroy is wrapped at the mom…
Browse files Browse the repository at this point in the history
…ent.

Add new methods to Image class and replace them in non-public code.
  • Loading branch information
egorpugin committed Mar 31, 2021
1 parent ce6e2f1 commit a792b67
Show file tree
Hide file tree
Showing 102 changed files with 707 additions and 685 deletions.
10 changes: 6 additions & 4 deletions src/api/baseapi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -631,7 +631,7 @@ Pix *TessBaseAPI::GetThresholdedImage() {
if (tesseract_ == nullptr || thresholder_ == nullptr) {
return nullptr;
}
if (tesseract_->pix_binary() == nullptr && !Threshold(tesseract_->mutable_pix_binary())) {
if (tesseract_->pix_binary() == nullptr && !Threshold(&tesseract_->mutable_pix_binary()->pix_)) {
return nullptr;
}
return pixClone(tesseract_->pix_binary());
Expand Down Expand Up @@ -2098,9 +2098,11 @@ bool TessBaseAPI::Threshold(Pix **pix) {
thresholder_->SetSourceYResolution(kMinCredibleResolution);
}
auto pageseg_mode = static_cast<PageSegMode>(static_cast<int>(tesseract_->tessedit_pageseg_mode));
if (!thresholder_->ThresholdToPix(pageseg_mode, pix)) {
Image im(*pix);
if (!thresholder_->ThresholdToPix(pageseg_mode, &im)) {
return false;
}
*pix = im;
thresholder_->GetImageSizes(&rect_left_, &rect_top_, &rect_width_, &rect_height_, &image_width_,
&image_height_);
if (!thresholder_->IsBinary()) {
Expand Down Expand Up @@ -2144,7 +2146,7 @@ int TessBaseAPI::FindLines() {
tesseract_->InitAdaptiveClassifier(nullptr);
#endif
}
if (tesseract_->pix_binary() == nullptr && !Threshold(tesseract_->mutable_pix_binary())) {
if (tesseract_->pix_binary() == nullptr && !Threshold(&tesseract_->mutable_pix_binary()->pix_)) {
return -1;
}

Expand Down Expand Up @@ -2270,7 +2272,7 @@ bool TessBaseAPI::DetectOS(OSResults *osr) {
return false;
}
ClearResults();
if (tesseract_->pix_binary() == nullptr && !Threshold(tesseract_->mutable_pix_binary())) {
if (tesseract_->pix_binary() == nullptr && !Threshold(&tesseract_->mutable_pix_binary()->pix_)) {
return false;
}

Expand Down
14 changes: 7 additions & 7 deletions src/ccmain/equationdetect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -583,13 +583,13 @@ void EquationDetect::IdentifySeedParts() {
}

float EquationDetect::ComputeForegroundDensity(const TBOX &tbox) {
Pix *pix_bi = lang_tesseract_->pix_binary();
Image pix_bi = lang_tesseract_->pix_binary();
const int pix_height = pixGetHeight(pix_bi);
Box *box = boxCreate(tbox.left(), pix_height - tbox.top(), tbox.width(), tbox.height());
Pix *pix_sub = pixClipRectangle(pix_bi, box, nullptr);
Image pix_sub = pixClipRectangle(pix_bi, box, nullptr);
l_float32 fract;
pixForegroundFraction(pix_sub, &fract);
pixDestroy(&pix_sub);
pix_sub.destroy();
boxDestroy(&box);

return fract;
Expand Down Expand Up @@ -1395,7 +1395,7 @@ void EquationDetect::GetOutputTiffName(const char *name, std::string &image_name
}

void EquationDetect::PaintSpecialTexts(const std::string &outfile) const {
Pix *pix = nullptr, *pixBi = lang_tesseract_->pix_binary();
Image pix = nullptr, pixBi = lang_tesseract_->pix_binary();
pix = pixConvertTo32(pixBi);
ColPartitionGridSearch gsearch(part_grid_);
ColPartition *part = nullptr;
Expand All @@ -1408,11 +1408,11 @@ void EquationDetect::PaintSpecialTexts(const std::string &outfile) const {
}

pixWrite(outfile.c_str(), pix, IFF_TIFF_LZW);
pixDestroy(&pix);
pix.destroy();
}

void EquationDetect::PaintColParts(const std::string &outfile) const {
Pix *pix = pixConvertTo32(lang_tesseract_->BestPix());
Image pix = pixConvertTo32(lang_tesseract_->BestPix());
ColPartitionGridSearch gsearch(part_grid_);
gsearch.StartFullSearch();
ColPartition *part = nullptr;
Expand All @@ -1430,7 +1430,7 @@ void EquationDetect::PaintColParts(const std::string &outfile) const {
}

pixWrite(outfile.c_str(), pix, IFF_TIFF_LZW);
pixDestroy(&pix);
pix.destroy();
}

void EquationDetect::PrintSpecialBlobsDensity(const ColPartition *part) const {
Expand Down
12 changes: 6 additions & 6 deletions src/ccmain/linerec.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ ImageData *Tesseract::GetRectImage(const TBOX &box, const BLOCK &block, int padd
}
// Now revised_box always refers to the image.
// BestPix is never colormapped, but may be of any depth.
Pix *pix = BestPix();
Image pix = BestPix();
int width = pixGetWidth(pix);
int height = pixGetHeight(pix);
TBOX image_box(0, 0, width, height);
Expand All @@ -196,22 +196,22 @@ ImageData *Tesseract::GetRectImage(const TBOX &box, const BLOCK &block, int padd
}
Box *clip_box = boxCreate(revised_box->left(), height - revised_box->top(), revised_box->width(),
revised_box->height());
Pix *box_pix = pixClipRectangle(pix, clip_box, nullptr);
Image box_pix = pixClipRectangle(pix, clip_box, nullptr);
boxDestroy(&clip_box);
if (box_pix == nullptr) {
return nullptr;
}
if (num_rotations > 0) {
Pix *rot_pix = pixRotateOrth(box_pix, num_rotations);
pixDestroy(&box_pix);
Image rot_pix = pixRotateOrth(box_pix, num_rotations);
box_pix.destroy();
box_pix = rot_pix;
}
// Convert sub-8-bit images to 8 bit.
int depth = pixGetDepth(box_pix);
if (depth < 8) {
Pix *grey;
Image grey;
grey = pixConvertTo8(box_pix, false);
pixDestroy(&box_pix);
box_pix.destroy();
box_pix = grey;
}
bool vertical_text = false;
Expand Down
6 changes: 3 additions & 3 deletions src/ccmain/osdetect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ void OSResults::accumulate(const OSResults &osr) {
// image, so that non-text blobs are removed from consideration.
static void remove_nontext_regions(tesseract::Tesseract *tess, BLOCK_LIST *blocks,
TO_BLOCK_LIST *to_blocks) {
Pix *pix = tess->pix_binary();
Image pix = tess->pix_binary();
ASSERT_HOST(pix != nullptr);
int vertical_x = 0;
int vertical_y = 1;
Expand All @@ -174,10 +174,10 @@ static void remove_nontext_regions(tesseract::Tesseract *tess, BLOCK_LIST *block

tesseract::LineFinder::FindAndRemoveLines(resolution, false, pix, &vertical_x, &vertical_y,
nullptr, &v_lines, &h_lines);
Pix *im_pix = tesseract::ImageFind::FindImages(pix, nullptr);
Image im_pix = tesseract::ImageFind::FindImages(pix, nullptr);
if (im_pix != nullptr) {
pixSubtract(pix, pix, im_pix);
pixDestroy(&im_pix);
im_pix.destroy();
}
tess->mutable_textord()->find_components(tess->pix_binary(), blocks, to_blocks);
}
Expand Down
16 changes: 8 additions & 8 deletions src/ccmain/pageiterator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -444,19 +444,19 @@ Pix *PageIterator::GetBinaryImage(PageIteratorLevel level) const {
return cblob_it_->data()->render();
}
Box *box = boxCreate(left, top, right - left, bottom - top);
Pix *pix = pixClipRectangle(tesseract_->pix_binary(), box, nullptr);
Image pix = pixClipRectangle(tesseract_->pix_binary(), box, nullptr);
boxDestroy(&box);
if (level == RIL_BLOCK || level == RIL_PARA) {
// Clip to the block polygon as well.
TBOX mask_box;
Pix *mask = it_->block()->block->render_mask(&mask_box);
Image mask = it_->block()->block->render_mask(&mask_box);
int mask_x = left - mask_box.left();
int mask_y = top - (tesseract_->ImageHeight() - mask_box.top());
// AND the mask and pix, putting the result in pix.
pixRasterop(pix, std::max(0, -mask_x), std::max(0, -mask_y), pixGetWidth(pix),
pixGetHeight(pix), PIX_SRC & PIX_DST, mask, std::max(0, mask_x),
std::max(0, mask_y));
pixDestroy(&mask);
mask.destroy();
}
return pix;
}
Expand Down Expand Up @@ -488,25 +488,25 @@ Pix *PageIterator::GetImage(PageIteratorLevel level, int padding, Pix *original_
right = std::min(right + padding, rect_width_);
bottom = std::min(bottom + padding, rect_height_);
Box *box = boxCreate(*left, *top, right - *left, bottom - *top);
Pix *grey_pix = pixClipRectangle(original_img, box, nullptr);
Image grey_pix = pixClipRectangle(original_img, box, nullptr);
boxDestroy(&box);
if (level == RIL_BLOCK || level == RIL_PARA) {
// Clip to the block polygon as well.
TBOX mask_box;
Pix *mask = it_->block()->block->render_mask(&mask_box);
Image mask = it_->block()->block->render_mask(&mask_box);
// Copy the mask registered correctly into an image the size of grey_pix.
int mask_x = *left - mask_box.left();
int mask_y = *top - (pixGetHeight(original_img) - mask_box.top());
int width = pixGetWidth(grey_pix);
int height = pixGetHeight(grey_pix);
Pix *resized_mask = pixCreate(width, height, 1);
Image resized_mask = pixCreate(width, height, 1);
pixRasterop(resized_mask, std::max(0, -mask_x), std::max(0, -mask_y), width, height, PIX_SRC,
mask, std::max(0, mask_x), std::max(0, mask_y));
pixDestroy(&mask);
mask.destroy();
pixDilateBrick(resized_mask, resized_mask, 2 * padding + 1, 2 * padding + 1);
pixInvert(resized_mask, resized_mask);
pixSetMasked(grey_pix, resized_mask, UINT32_MAX);
pixDestroy(&resized_mask);
resized_mask.destroy();
}
return grey_pix;
}
Expand Down
40 changes: 20 additions & 20 deletions src/ccmain/pagesegmain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,21 +58,21 @@ const int kMaxCircleErosions = 8;
// The returned pix must be pixDestroyed after use. nullptr may be returned
// if the image doesn't meet the trivial conditions that it uses to determine
// success.
static Pix *RemoveEnclosingCircle(Pix *pixs) {
Pix *pixsi = pixInvert(nullptr, pixs);
Pix *pixc = pixCreateTemplate(pixs);
static Image RemoveEnclosingCircle(Image pixs) {
Image pixsi = pixInvert(nullptr, pixs);
Image pixc = pixCreateTemplate(pixs);
pixSetOrClearBorder(pixc, 1, 1, 1, 1, PIX_SET);
pixSeedfillBinary(pixc, pixc, pixsi, 4);
pixInvert(pixc, pixc);
pixDestroy(&pixsi);
Pix *pixt = pixAnd(nullptr, pixs, pixc);
pixsi.destroy();
Image pixt = pixAnd(nullptr, pixs, pixc);
l_int32 max_count;
pixCountConnComp(pixt, 8, &max_count);
// The count has to go up before we start looking for the minimum.
l_int32 min_count = INT32_MAX;
Pix *pixout = nullptr;
Image pixout = nullptr;
for (int i = 1; i < kMaxCircleErosions; i++) {
pixDestroy(&pixt);
pixt.destroy();
pixErodeBrick(pixc, pixc, 3, 3);
pixt = pixAnd(nullptr, pixs, pixc);
l_int32 count;
Expand All @@ -82,14 +82,14 @@ static Pix *RemoveEnclosingCircle(Pix *pixs) {
min_count = count;
} else if (count < min_count) {
min_count = count;
pixDestroy(&pixout);
pixout.destroy();
pixout = pixCopy(nullptr, pixt); // Save the best.
} else if (count >= min_count) {
break; // We have passed by the best.
}
}
pixDestroy(&pixt);
pixDestroy(&pixc);
pixt.destroy();
pixc.destroy();
return pixout;
}

Expand Down Expand Up @@ -148,9 +148,9 @@ int Tesseract::SegmentPage(const char *input_file, BLOCK_LIST *blocks, Tesseract
deskew_ = FCOORD(1.0f, 0.0f);
reskew_ = FCOORD(1.0f, 0.0f);
if (pageseg_mode == PSM_CIRCLE_WORD) {
Pix *pixcleaned = RemoveEnclosingCircle(pix_binary_);
Image pixcleaned = RemoveEnclosingCircle(pix_binary_);
if (pixcleaned != nullptr) {
pixDestroy(&pix_binary_);
pix_binary_.destroy();
pix_binary_ = pixcleaned;
}
}
Expand Down Expand Up @@ -200,8 +200,8 @@ int Tesseract::SegmentPage(const char *input_file, BLOCK_LIST *blocks, Tesseract
*/
int Tesseract::AutoPageSeg(PageSegMode pageseg_mode, BLOCK_LIST *blocks, TO_BLOCK_LIST *to_blocks,
BLOBNBOX_LIST *diacritic_blobs, Tesseract *osd_tess, OSResults *osr) {
Pix *photomask_pix = nullptr;
Pix *musicmask_pix = nullptr;
Image photomask_pix = nullptr;
Image musicmask_pix = nullptr;
// The blocks made by the ColumnFinder. Moved to blocks before return.
BLOCK_LIST found_blocks;
TO_BLOCK_LIST temp_blocks;
Expand Down Expand Up @@ -231,8 +231,8 @@ int Tesseract::AutoPageSeg(PageSegMode pageseg_mode, BLOCK_LIST *blocks, TO_BLOC
}
delete finder;
}
pixDestroy(&photomask_pix);
pixDestroy(&musicmask_pix);
photomask_pix.destroy();
musicmask_pix.destroy();
if (result < 0) {
return result;
}
Expand Down Expand Up @@ -272,8 +272,8 @@ static void AddAllScriptsConverted(const UNICHARSET &sid_set, const UNICHARSET &
ColumnFinder *Tesseract::SetupPageSegAndDetectOrientation(PageSegMode pageseg_mode,
BLOCK_LIST *blocks, Tesseract *osd_tess,
OSResults *osr, TO_BLOCK_LIST *to_blocks,
Pix **photo_mask_pix,
Pix **music_mask_pix) {
Image *photo_mask_pix,
Image *music_mask_pix) {
int vertical_x = 0;
int vertical_y = 1;
TabVector_LIST v_lines;
Expand All @@ -293,14 +293,14 @@ ColumnFinder *Tesseract::SetupPageSegAndDetectOrientation(PageSegMode pageseg_mo
// Leptonica is used to find a mask of the photo regions in the input.
*photo_mask_pix = ImageFind::FindImages(pix_binary_, &pixa_debug_);
if (tessedit_dump_pageseg_images) {
Pix *pix_no_image_ = nullptr;
Image pix_no_image_ = nullptr;
if (*photo_mask_pix != nullptr) {
pix_no_image_ = pixSubtract(nullptr, pix_binary_, *photo_mask_pix);
} else {
pix_no_image_ = pixClone(pix_binary_);
}
pixa_debug_.AddPix(pix_no_image_, "NoImages");
pixDestroy(&pix_no_image_);
pix_no_image_.destroy();
}
if (!PSM_COL_FIND_ENABLED(pageseg_mode)) {
v_lines.clear();
Expand Down
18 changes: 9 additions & 9 deletions src/ccmain/tesseractclass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,7 @@ Tesseract::Tesseract()

Tesseract::~Tesseract() {
Clear();
pixDestroy(&pix_original_);
pix_original_.destroy();
end_tesseract();
for (auto *lang : sub_langs_) {
delete lang;
Expand All @@ -442,10 +442,10 @@ Dict &Tesseract::getDict() {
void Tesseract::Clear() {
std::string debug_name = imagebasename + "_debug.pdf";
pixa_debug_.WritePDF(debug_name.c_str());
pixDestroy(&pix_binary_);
pixDestroy(&pix_grey_);
pixDestroy(&pix_thresholds_);
pixDestroy(&scaled_color_);
pix_binary_.destroy();
pix_grey_.destroy();
pix_thresholds_.destroy();
scaled_color_.destroy();
deskew_ = FCOORD(1.0f, 0.0f);
reskew_ = FCOORD(1.0f, 0.0f);
splitter_.Clear();
Expand Down Expand Up @@ -518,7 +518,7 @@ void Tesseract::PrepareForPageseg() {
if (pageseg_strategy > max_pageseg_strategy) {
max_pageseg_strategy = pageseg_strategy;
}
pixDestroy(&sub_lang->pix_binary_);
sub_lang->pix_binary_.destroy();
sub_lang->pix_binary_ = pixClone(pix_binary());
}
// Perform shiro-rekha (top-line) splitting and replace the current image by
Expand All @@ -527,7 +527,7 @@ void Tesseract::PrepareForPageseg() {
splitter_.set_pageseg_split_strategy(max_pageseg_strategy);
if (splitter_.Split(true, &pixa_debug_)) {
ASSERT_HOST(splitter_.splitted_image());
pixDestroy(&pix_binary_);
pix_binary_.destroy();
pix_binary_ = pixClone(splitter_.splitted_image());
}
}
Expand Down Expand Up @@ -555,14 +555,14 @@ void Tesseract::PrepareForTessOCR(BLOCK_LIST *block_list, Tesseract *osd_tess, O
bool split_for_ocr = splitter_.Split(false, &pixa_debug_);
// Restore pix_binary to the binarized original pix for future reference.
ASSERT_HOST(splitter_.orig_pix());
pixDestroy(&pix_binary_);
pix_binary_.destroy();
pix_binary_ = pixClone(splitter_.orig_pix());
// If the pageseg and ocr strategies are different, refresh the block list
// (from the last SegmentImage call) with blobs from the real image to be used
// for OCR.
if (splitter_.HasDifferentSplitStrategies()) {
BLOCK block("", true, 0, 0, 0, 0, pixGetWidth(pix_binary_), pixGetHeight(pix_binary_));
Pix *pix_for_ocr = split_for_ocr ? splitter_.splitted_image() : splitter_.orig_pix();
Image pix_for_ocr = split_for_ocr ? splitter_.splitted_image() : splitter_.orig_pix();
extract_edges(pix_for_ocr, &block);
splitter_.RefreshSegmentationWithNewBlobs(block.blob_list());
}
Expand Down
Loading

0 comments on commit a792b67

Please sign in to comment.