Skip to content

Commit

Permalink
remove alpha channel from png: issue #1914
Browse files Browse the repository at this point in the history
  • Loading branch information
zdenop committed Sep 27, 2018
1 parent 971fe50 commit 5fe1390
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 8 deletions.
7 changes: 7 additions & 0 deletions src/api/baseapi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -611,6 +611,13 @@ void TessBaseAPI::SetSourceResolution(int ppi) {
*/
void TessBaseAPI::SetImage(Pix* pix) {
if (InternalSetImage()) {
if (pixGetSpp(pix) == 4 && pixGetInputFormat(pix) == IFF_PNG) {
// remove alpha channel from png
PIX* p1 = pixRemoveAlpha(pix);
pixSetSpp(p1, 3);
pix = pixCopy(nullptr, p1);

This comment has been minimized.

Copy link
@amitdo

amitdo Oct 7, 2018

Collaborator

pix = pixCopy(nullptr, p1); Is unneeded here.
pixDestroy(&p1); will handle fine.

This comment has been minimized.

Copy link
@zdenop

zdenop Oct 8, 2018

Author Contributor

pixAlphaBlendUniform: This is a convenience function that renders 32 bpp RGBA images (with an alpha channel) over a uniform background of value %color. To render over a white background, use %color = 0xffffff00. The result is an RGB image.
pixRemoveAlpha: Removal of alpha component by blending with white background. Notes: This is a wrapper on pixAlphaBlendUniform()

So I decided to use more readable function.

This comment has been minimized.

Copy link
@amitdo

amitdo Oct 8, 2018

Collaborator

Thanks for clarifying this to me.

What about my my remark regarding pixCopy(...) ?

This comment has been minimized.

Copy link
@stweil

stweil Nov 18, 2018

Member

pixCopy is indeed not needed. But I stumbled over that code lines while searching for memory leaks in osd_test. We get such a leak for each call of the new code. This cannot be solved locally.

Maybe we should revert the change here and handle the alpha channel earlier in the call stack. If we use pixAlphaBlendUniform, it would even be possible to add a user option which sets the background color (white could still be the default).

This comment has been minimized.

Copy link
@stweil

stweil Jan 23, 2019

Member

@zdenop, there is still a memory leak here. stweil@9e6e3a0 would fix it, but changes the input pix. Is this acceptable?

This comment has been minimized.

Copy link
@zdenop

zdenop Jan 24, 2019

Author Contributor

I think yes.

pixDestroy(&p1);
}
thresholder_->SetImage(pix);
SetInputImage(thresholder_->GetPixRect());
}
Expand Down
9 changes: 1 addition & 8 deletions src/api/pdfrenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -716,14 +716,7 @@ bool TessPDFRenderer::imageToPDFObj(Pix *pix,
const int kJpegQuality = jpg_quality;

int format, sad;
if (filename) {
findFileFormat(filename, &format);
if (pixGetSpp(pix) == 4 && format == IFF_PNG) {
Pix *p1 = pixAlphaBlendUniform(pix, 0xffffff00);
sad = pixGenerateCIData(p1, L_FLATE_ENCODE, 0, 0, &cid);
pixDestroy(&p1);
}
}
sad = pixGenerateCIData(pix, L_FLATE_ENCODE, 0, 0, &cid);
if (!cid) {
sad = l_generateCIDataForPdf(filename, pix, kJpegQuality, &cid);
}
Expand Down

0 comments on commit 5fe1390

Please sign in to comment.