From dfad53d80e8bf752342ed66f057c3dae8a76ad0f Mon Sep 17 00:00:00 2001 From: Gijs van Tulder Date: Wed, 4 Sep 2024 22:51:45 +0200 Subject: [PATCH] lint: Test and improve PNG transparency check (f-019) --- se/images.py | 17 ++- .../filesystem/f-019/golden/f-019-out.txt | 8 ++ .../filesystem/f-019/in/src/epub/content.opf | 101 ++++++++++++++++++ .../in/src/epub/images/illustration-1.png | Bin 0 -> 307 bytes .../in/src/epub/images/illustration-2.png | Bin 0 -> 276 bytes .../in/src/epub/images/illustration-3.png | Bin 0 -> 381 bytes .../in/src/epub/images/illustration-4.png | Bin 0 -> 295 bytes .../in/src/epub/images/illustration-5.png | Bin 0 -> 271 bytes .../in/src/epub/images/illustration-6.png | Bin 0 -> 272 bytes .../in/src/epub/images/illustration-7.png | Bin 0 -> 284 bytes .../in/src/epub/images/illustration-8.png | Bin 0 -> 84 bytes .../f-019/in/src/epub/text/chapter-1.xhtml | 22 ++++ 12 files changed, 143 insertions(+), 5 deletions(-) create mode 100644 tests/lint/filesystem/f-019/golden/f-019-out.txt create mode 100644 tests/lint/filesystem/f-019/in/src/epub/content.opf create mode 100644 tests/lint/filesystem/f-019/in/src/epub/images/illustration-1.png create mode 100644 tests/lint/filesystem/f-019/in/src/epub/images/illustration-2.png create mode 100644 tests/lint/filesystem/f-019/in/src/epub/images/illustration-3.png create mode 100644 tests/lint/filesystem/f-019/in/src/epub/images/illustration-4.png create mode 100644 tests/lint/filesystem/f-019/in/src/epub/images/illustration-5.png create mode 100644 tests/lint/filesystem/f-019/in/src/epub/images/illustration-6.png create mode 100644 tests/lint/filesystem/f-019/in/src/epub/images/illustration-7.png create mode 100644 tests/lint/filesystem/f-019/in/src/epub/images/illustration-8.png create mode 100644 tests/lint/filesystem/f-019/in/src/epub/text/chapter-1.xhtml diff --git a/se/images.py b/se/images.py index 41ca9566..dc0f200f 100644 --- a/se/images.py +++ b/se/images.py @@ -150,12 +150,19 @@ def has_transparency(image: Image_type) -> bool: if image.mode == "P": transparent = image.info.get("transparency", -1) - for _, index in image.getcolors(): - if index == transparent: - return True - elif image.mode == "RGBA": + if isinstance(transparent, bytes): + for _, index in image.getcolors(): + if index >= len(transparent): + return False + if transparent[index] < 255: + return True + else: + for _, index in image.getcolors(): + if index == transparent: + return True + elif image.mode in ("LA", "RGBA"): extrema = image.getextrema() - if extrema[3][0] < 255: + if extrema[-1][0] < 255: return True return False diff --git a/tests/lint/filesystem/f-019/golden/f-019-out.txt b/tests/lint/filesystem/f-019/golden/f-019-out.txt new file mode 100644 index 00000000..5b333e2d --- /dev/null +++ b/tests/lint/filesystem/f-019/golden/f-019-out.txt @@ -0,0 +1,8 @@ +f-019 [Error] illustration-1.png `.png` file without transparency. Hint: If an +image doesn’t have transparency, it should be saved as a `.jpg`. +f-019 [Error] illustration-6.png `.png` file without transparency. Hint: If an +image doesn’t have transparency, it should be saved as a `.jpg`. +f-019 [Error] illustration-7.png `.png` file without transparency. Hint: If an +image doesn’t have transparency, it should be saved as a `.jpg`. +f-019 [Error] illustration-8.png `.png` file without transparency. Hint: If an +image doesn’t have transparency, it should be saved as a `.jpg`. diff --git a/tests/lint/filesystem/f-019/in/src/epub/content.opf b/tests/lint/filesystem/f-019/in/src/epub/content.opf new file mode 100644 index 00000000..cd1bc1ad --- /dev/null +++ b/tests/lint/filesystem/f-019/in/src/epub/content.opf @@ -0,0 +1,101 @@ + + + + url:https://standardebooks.org/ebooks/jane-austen/unknown-novel + 1900-01-01T00:00:00Z + 1900-01-01T00:00:00Z + The source text and artwork in this ebook are believed to be in the United States public domain; that is, they are believed to be free of copyright restrictions in the United States. They may still be copyrighted in other countries, so users located outside of the United States must check their local laws before using this ebook. The creators of, and contributors to, this ebook dedicate their contributions to the worldwide public domain via the terms in the [CC0 1.0 Universal Public Domain Dedication](https://creativecommons.org/publicdomain/zero/1.0/). + Standard Ebooks + Standard Ebooks + https://standardebooks.org + bkd + mdc + pbl + The League of Moveable Type + League of Moveable Type, The + https://www.theleagueofmoveabletype.com + tyd + + Standard Ebooks + textual + textual + readingOrder + structuralNavigation + tableOfContents + unlocked + none + This publication conforms to WCAG 2.2 Level AA. + + Unknown Novel + Unknown Novel + England--Social life and customs--19th century--Fiction + Sisters -- Fiction + LCSH + sh2008114941 + LCSH + sh2008111400 + Fiction + A short test novel for lint testing. + + <p>A short test novel for lint testing.</p> + + en-GB + https://www.gutenberg.org/ebooks/161 + https://archive.org/details/bub_gb_RtT0OLKFMHsC + WORD_COUNT + READING_EASE + https://en.wikipedia.org/wiki/Unknown_Jane_Austen_Novel + https://github.com/standardebooks/jane-austen_unknown-novel + Jane Austen + Austen, Jane + https://en.wikipedia.org/wiki/Jane_Austen + http://id.loc.gov/authorities/names/n79032879 + aut + Georg Friedrich Kersting + Kersting, Georg Friedrich + https://en.wikipedia.org/wiki/Georg_Friedrich_Kersting + http://id.loc.gov/authorities/names/n83319941 + art + Anonymous + Anonymous + trc + John Doe + Doe, John + bkp + blw + cov + mrk + pfr + tyg + wat + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/tests/lint/filesystem/f-019/in/src/epub/images/illustration-1.png b/tests/lint/filesystem/f-019/in/src/epub/images/illustration-1.png new file mode 100644 index 0000000000000000000000000000000000000000..64022b2c165ce32f604f64ae136fad5e76f4e51e GIT binary patch literal 307 zcmeAS@N?(olHy`uVBq!ia0vp^0wBx+BpCi@`0fExEa{HEjtmSN`?>!lvI6-E$sR$z z3=CCj3=9n|3=F@3LJcn%7)lKo7+xhXFj&oCU=S~uvn$XBD8ZEE?e4}RS#jR%uwb;# z$8LWgLl<%1kU$qPPZz->#f+Xa><_+Yw3!VwMzzE>q9i4;B-JXpC>2OC7#SFu=o(n+ z8kmF_T3DGHTbY<>8yHv_7?i7NOQUGW%}>cptHiB=t$Olupaup{S3j3^P6#}JM4$q5P!5|S(*wG$*m4N61|jQ$*7&%h8pn@MGA z>kgo045}rr5hW>!C8<`)MX5lF!N|bKMAyJl*T5vi(89{p*viC2+rYrez@S`BTN*_} cZhlH;S|x4`Y}J#O12r&sy85}Sb4q9e0IU#2DF6Tf literal 0 HcmV?d00001 diff --git a/tests/lint/filesystem/f-019/in/src/epub/images/illustration-3.png b/tests/lint/filesystem/f-019/in/src/epub/images/illustration-3.png new file mode 100644 index 0000000000000000000000000000000000000000..c0ac042cafeff0b069cf749b0b26b9d2e16d3d32 GIT binary patch literal 381 zcmeAS@N?(olHy`uVBq!ia0vp^0wB!63?wyl`GbKJOS+@4BLl<6e(pbstU$g(vPY0F z14ES>14Ba#1H&(%P{RubhEf9thF1v;3|2E37{m+a>21szu;sM zGS#vEC9eb&((!b043W4TdvGfwg95{W4UhQ#*Pp(}qQepB;Uv}-(5hCF$PpZINUX1* zRb77aJNCxS-19*$RxNRjC`m~yNwrEYN(E93Mg~SEx(1fI1|}hf7FMRlRwgFe1_o9J j2IXqn(kL2o^HVa@DsgLItDd|ZsDZ)L)z4*}Q$iB}f9h!i literal 0 HcmV?d00001 diff --git a/tests/lint/filesystem/f-019/in/src/epub/images/illustration-4.png b/tests/lint/filesystem/f-019/in/src/epub/images/illustration-4.png new file mode 100644 index 0000000000000000000000000000000000000000..ef281da0bb3a25034742caeb2b97871f9b353c64 GIT binary patch literal 295 zcmeAS@N?(olHy`uVBq!ia0vp^0wB!63?wyl`GbKJOS+@4BLl<6e(pbstU$g(vPY0F z14ES>14Ba#1H&(%P{RubhEf9thF1v;3|2E37{m+a> zMHowh{DK)Ap4~_Tagw~s;u=wsl30>zm0Xkxq!^40j7)S5EOiY`LJTdeOpUEf pOtcLQtPBjw)wHEiH00)|WTsW(*1%Rhc{xx6gQu&X%Q~loCIC*0NF)FN literal 0 HcmV?d00001 diff --git a/tests/lint/filesystem/f-019/in/src/epub/images/illustration-5.png b/tests/lint/filesystem/f-019/in/src/epub/images/illustration-5.png new file mode 100644 index 0000000000000000000000000000000000000000..f410fcbde1a1e051d0c9c677d6627e10d13b49c0 GIT binary patch literal 271 zcmeAS@N?(olHy`uVBq!ia0vp^0wB!61|;P_|4#%`Ea{HEjtmSN`?>!lvI6-E$sR$z z3=CCj3=9n|3=F@3LJcn%7)lKo7+xhXFj&oCU=S~uvn$XBD8ZKG?e4Jp<3b^ zQIe8al4_M)lnSI6j0}uSbPX(Z4NO7|Ev!t9txQa`4GgRd49eBCrBO8G=BH$)RpQpb TRy}z+Py>UftDnm{r-UW|6KzKL literal 0 HcmV?d00001 diff --git a/tests/lint/filesystem/f-019/in/src/epub/images/illustration-6.png b/tests/lint/filesystem/f-019/in/src/epub/images/illustration-6.png new file mode 100644 index 0000000000000000000000000000000000000000..d1e25a0e7387532e0c1ccd1fd823e155fab7817e GIT binary patch literal 272 zcmeAS@N?(olHy`uVBq!ia0vp^0wB!61|;P_|4#%`Ea{HEjtmSN`?>!lvI6-E$sR$z z3=CCj3=9n|3=F@3LJcn%7)lKo7+xhXFj&oCU=S~uvn$XBD8ZKG?e4OsSJUftDnm{r-UW|l`=~+ literal 0 HcmV?d00001 diff --git a/tests/lint/filesystem/f-019/in/src/epub/images/illustration-7.png b/tests/lint/filesystem/f-019/in/src/epub/images/illustration-7.png new file mode 100644 index 0000000000000000000000000000000000000000..959a1069884dff8c32ec01e369f931640f8eed88 GIT binary patch literal 284 zcmeAS@N?(olHy`uVBq!ia0vp^0wB!61SBU+%rFB|Ea{HEjtmSN`?>!lvI6-E$sR$z z3=CCj3=9n|3=F@3LJcn%7)lKo7+xhXFj&oCU=S~uvn$XBD8ZKG?e4&pIulSRl>$NHDN5>QCN)5S4F<9za;|NrfoH5*zV{{0=uyI88Njm_m9-vy)64!{5l*E!$tK_0oAjM#0U}U0eV5w_h5@Kj!Wom3?VxnzeU}a!X iuBI)Gq9HdwB{QuOw+6QA$;*Kn7(8A5T-G@yGywo-kV=*S literal 0 HcmV?d00001 diff --git a/tests/lint/filesystem/f-019/in/src/epub/images/illustration-8.png b/tests/lint/filesystem/f-019/in/src/epub/images/illustration-8.png new file mode 100644 index 0000000000000000000000000000000000000000..f426125379bd716ff407c312808e3cd81478283e GIT binary patch literal 84 zcmeAS@N?(olHy`uVBq!ia0vp^0wB!60wlNoGJgf6q&;06Ln`JZ|M@?kQ9q%wKfW&H hU_j7;6AlWy823*T@Cwco3 + + + I + + + + +
+

I

+

We can assume that any instance of a point can be construed as a themeless fisherman. Framed in a different way, some picked shakes are thought of simply as crabs. Nowhere is it disputed that a dinner sees a modem as a warming customer. The zeitgeist contends that we can assume that any instance of a kenneth can be construed as an entranced belgian. A rotate is a gaumless debt.

+

Not transparent.

+

Transparent.

+

Transparent.

+

Transparent.

+

Transparent.

+

Not transparent.

+

Not transparent.

+

Not transparent.

+
+ +