Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bilevel BW png inverted in PDF output #2059

Open
pit65 opened this issue Nov 16, 2018 · 41 comments
Open

Bilevel BW png inverted in PDF output #2059

pit65 opened this issue Nov 16, 2018 · 41 comments
Labels
leptonica output issues related output formats PDF

Comments

@pit65
Copy link

pit65 commented Nov 16, 2018

Linux64 Slackware Tesseract 4.0.0

Tesseract PDF output from BW /bilevel type/ png is white on black.

001

Png is generated from IM or ghostscript .
From BW type grayscale is OK.

@jbreiden
Copy link
Contributor

Attach the input PNG please.

@pit65
Copy link
Author

pit65 commented Nov 16, 2018

Here it isfrom ghostscript

gs -sDEVICE=pngmono -dDOINTERPOLATE -sOutputFile=%03d.png -dSAFER -dBATCH -dNOPAUSE -r300 input.pdf

019

@zdenop zdenop added PDF output issues related output formats labels Nov 24, 2018
@zdenop
Copy link
Contributor

zdenop commented Nov 24, 2018

interesting: tesseract i2059.png i2059 pdf get.image dumpes internal pix as tessinput.tif which is correct...

@jbreiden
Copy link
Contributor

jbreiden commented Dec 14, 2018 via email

@nguyenq
Copy link
Contributor

nguyenq commented Jun 22, 2019

I observe the same color inverted problem in PDF output using the latest code.

Version:
tesseract 5.0.0
leptonica-1.78.0 (Mar 23 2019, 10:29:44) [MSC v.1916 DLL Release x86]
libgif 5.1.4 : libjpeg 9c : libpng 1.6.36 : libtiff 4.0.10 : zlib 1.2.11 : libwebp 1.0.1 : libopenjp2 2.3.0

eurotext
out.pdf

@jbreiden
Copy link
Contributor

jbreiden commented Jul 9, 2019 via email

@DanBloomberg
Copy link

I don't remember what we did. I can't work on this for 2 weeks. Jeff, can you figure out what needs to be done to fix the problem?

@zdenop
Copy link
Contributor

zdenop commented Jul 27, 2019

@jbreiden @DanBloomberg : issue is still valid for the latest code of tesseract and leptonica.

@jbreiden
Copy link
Contributor

jbreiden commented Aug 7, 2019 via email

@DanBloomberg
Copy link

the colormap is opposite to the standard photometry for 1 bpp.

So it's likely that we're just ignoring the fact that there is a colormap, which would give us a video-inverted image. If so, the fix is to check for the colormap and remove it first.

@jbreiden
Copy link
Contributor

jbreiden commented Aug 8, 2019 via email

@DanBloomberg
Copy link

Looked at the code, both in pngio.c and pdfio2.c.

Leptonica appears to be doing the right thing. When
identify -verbose
indicates a colormap in a b/w png, there is an ambiguity. It may in fact have a colormap, or it may not, in which case 'identify' pretends that it does. For a b/w pix without a colormap, png stores the values inverted, so the png reader must invert the pixel value. However, if the 1 bpp png actually has a colormap, the reader removes it, and in doing so, it compares the color components of index 0 and 1. For a b/w colormap, it inverts if 1 is 255 and 0 is 0. (Removal of the colormap must be lossless -- i.e., not change the values of the pixels)

So much for png. For pdf generation, as Jeff mentioned, a 1 bpp image will be transcoded. The first step is reading the image. As described above, a b/w pix (with or without a colormap), after being written to png and read back to a pix, is unchanged. The pdf generator doesn't change the polarity either.

If someone can find a 1 bpp (b/w) image, as a png file, with or without a colormap, that gets inverted as a pdf (e.g., using convertToPdf), please attach it to this issue.

@DanBloomberg
Copy link

Perhaps the problem is in tesseract. Are the 1 bpp images being transcoded?

@zdenop
Copy link
Contributor

zdenop commented Sep 11, 2019

@jbreiden: convertToPdf with above mentioned images create "not inverted" pdf.
tessinput.tif created during
tesseract 48639675-31a07680-e9cc-11e8-9e47-00130ea102c1.png a get.images pdf is correct...

@zdenop
Copy link
Contributor

zdenop commented Oct 18, 2019

@jbreiden: did you have a chance to have a look at this?

@jbreiden
Copy link
Contributor

jbreiden commented Oct 25, 2019 via email

@jbreiden
Copy link
Contributor

jbreiden commented Oct 25, 2019 via email

@DanBloomberg
Copy link

DanBloomberg commented Oct 25, 2019

Thank you for looking at this, Jeff.

I haven't been able to prove that the change you found is responsible for the inversion problem.
Someone needs to substitute l_generateCIDataForPdf() for pixGenerateCIData() to see if the problem is fixed.

I did a little experimentation with that image. The test program is attached (I had to call it ".txt" to upload it).

Calling pixGenerateCIData() on a 1 bpp pix with a colormap generates a proper pdf with the colormap embedded.

Calling l_generateCIDataForPdf() from a png file of 1 bpp image with a colormap: causes the colormap to be removed, and then generates a proper pdf.

pdftest_invert.txt

@jbreiden
Copy link
Contributor

jbreiden commented Oct 26, 2019 via email

@DanBloomberg
Copy link

The attached pdf with a colormap is generated in lines 35-38 of pdftest_invert (attached above).

bitinvert1.pdf

@zdenop
Copy link
Contributor

zdenop commented Oct 26, 2019

@jbreiden : I am afraid problem is somewhere else: regardless I used l_generateCIDataForPdf or pixGenerateCIData I got inverted image in pdf.

So I made simple test: create pdf from cid with:

  l_uint8 *test_data;
  size_t test_nbytes;
  cidConvertToPdfData(cid, "testing issue 2059", &test_data, &test_nbytes);
  l_binaryWrite("test_i2059.pdf", "w", test_data, test_nbytes);
  delete test_data;
  printf("test finished.\n");

after

int sad = 0;
if (pixGetInputFormat(pix) == IFF_PNG)
sad = pixGenerateCIData(pix, L_FLATE_ENCODE, 0, 0, &cid);
if (!cid) {
sad = l_generateCIDataForPdf(filename, pix, jpg_quality, &cid);
}

and test_i2059.pdf has not inverted image...

@zdenop
Copy link
Contributor

zdenop commented Oct 26, 2019

@jbreiden: when I add to result pdf /Decode [1 0] to PDF object metadata after /ColorSpace /DeviceGray - result image is shown correctly. See i2059_org.pdf and i2059_fixed.pdf.

@jbreiden
Copy link
Contributor

jbreiden commented Oct 26, 2019 via email

@zdenop
Copy link
Contributor

zdenop commented Oct 27, 2019

fixed with 4a37cde.

@zdenop
Copy link
Contributor

zdenop commented Oct 27, 2019

(4) - is it possible to create such png? I tried convert 3_colors_no_bw.png -colors 2 +dither image.bmp (irfanview) reports is has 2 colors (1 BitsPerPixel). When I tried convert 3_colors_no_bw.png -colors 2 +dither image.png irfanview reports is has 16,7 Million (24 BitsPerPixel)...
3_colors_no_bw

@DanBloomberg
Copy link

DanBloomberg commented Oct 27, 2019 via email

@zdenop
Copy link
Contributor

zdenop commented Oct 27, 2019

Jeff wants in (4) png "1 bit per pixel, indexed with 2 colors that are not black or white" image

@DanBloomberg
Copy link

DanBloomberg commented Oct 27, 2019 via email

@zdenop
Copy link
Contributor

zdenop commented Oct 27, 2019

It would be great Dan. BTW: text is begin of fairy tale CAT AND MOUSE IN PARTNERSHIP By The Brothers Grimm.

@jbreiden
Copy link
Contributor

jbreiden commented Oct 27, 2019 via email

@DanBloomberg
Copy link

DanBloomberg commented Oct 28, 2019

@zdenop

Took your cat-and-mouse example image (antialiased, with 16 colors) and converted it to 1 bpp with colors similar to those in the original, using:

pix1 = pixRead("cat-and-mouse-orig.png");
pix2 = pixConvertTo32(pix1);
pix3 = pixConvertTo1(pix2, 225);
cmap = pixcmapCreate(1);
pixcmapAddColor(cmap, 254, 240, 185);
pixcmapAddColor(cmap, 50, 50, 130);
pixSetColormap(pix3, cmap);
pixWrite("cat-and-mouse.png", pix3, IFF_PNG);  

The output image is attached.
cat-and-mouse

@DanBloomberg
Copy link

I've incorporated some tests into the leptonica test prog/pdfio1_reg.c

Namely, lines 221 - 272. It has a new function I wrote that converts an RGB image to a colormapped one, if the number of colors <= 256.
The reason for this function is that the image cat-and-mouse.png has a colormap, but when it is read in to make a pix, it is converted into a 32 bpp RGB. This function converts it back to a 1 bpp pix.
pdftest.txt

The input image is
cat-and-mouse

@zdenop
Copy link
Contributor

zdenop commented Nov 4, 2019

@jbreiden : will you be able to create unittests for this?

@ShinjiLE
Copy link

ShinjiLE commented Nov 4, 2019

Ahoi !
Since commit 4a37cde the output is inverted for my images. The images are created by scantailor .

inverted result.pdf

MSR - messen steuern regeln 5 1984 0018_1L.tar.gz

@zdenop
Copy link
Contributor

zdenop commented Nov 4, 2019

@ShinjiLE : thanks for example. Your files are missing part for this puzzle. In this case there should be /Decode [0 1] instead of /Decode [1 0].
I wonder if this can not be handled by leptonica instead for coding it in tesseract...

@jbreiden
Copy link
Contributor

jbreiden commented Nov 6, 2019 via email

@zdenop
Copy link
Contributor

zdenop commented Nov 7, 2019

@DanBloomberg @jbreiden: what about create function at leptonica for this? It would take cid as argument that will return string with all needed information? I feel like we will copy this part of leptonica to tesseract step by step and I think it would be better to manage (possible) problem in place (in leptonica).

@DanBloomberg
Copy link

DanBloomberg commented Nov 7, 2019 via email

@jbreiden
Copy link
Contributor

jbreiden commented Nov 8, 2019 via email

@zdenop
Copy link
Contributor

zdenop commented Nov 10, 2019

understand. done. 2d6f38e

@stweil stweil changed the title Bilevel BW png Bilevel BW png inverted in PDF output Nov 20, 2019
@stweil
Copy link
Member

stweil commented Nov 20, 2019

There was a new issue #2771 reported which has a similar problem, but with TIFF instead of PNG. It claims that 4.1 worked correctly, so that might be different.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
leptonica output issues related output formats PDF
Projects
None yet
Development

No branches or pull requests

8 participants