fixed rendering of transparent pdfs; white overlay for very dark pdf

This commit is contained in:
Daniel Gultsch 2020-02-12 09:09:34 +01:00
parent fc2b27c3b4
commit a06eb10ed8
8 changed files with 75 additions and 3 deletions

55
art/open_pdf_white.svg Normal file
View file

@ -0,0 +1,55 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<svg
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:cc="http://creativecommons.org/ns#"
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:svg="http://www.w3.org/2000/svg"
xmlns="http://www.w3.org/2000/svg"
xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd"
xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape"
version="1.1"
width="24"
height="24"
viewBox="0 0 24 24"
id="svg4"
sodipodi:docname="open_pdf_white.svg"
inkscape:version="0.92.4 5da689c313, 2019-01-14">
<metadata
id="metadata10">
<rdf:RDF>
<cc:Work
rdf:about="">
<dc:format>image/svg+xml</dc:format>
<dc:type
rdf:resource="http://purl.org/dc/dcmitype/StillImage" />
<dc:title />
</cc:Work>
</rdf:RDF>
</metadata>
<defs
id="defs8" />
<sodipodi:namedview
pagecolor="#ffffff"
bordercolor="#666666"
borderopacity="1"
objecttolerance="10"
gridtolerance="10"
guidetolerance="10"
inkscape:pageopacity="0"
inkscape:pageshadow="2"
inkscape:window-width="3840"
inkscape:window-height="1600"
id="namedview6"
showgrid="false"
inkscape:zoom="9.8333333"
inkscape:cx="-23.084746"
inkscape:cy="11.084746"
inkscape:window-x="1920"
inkscape:window-y="0"
inkscape:window-maximized="0"
inkscape:current-layer="svg4" />
<path
d="M12,10.5H13V13.5H12V10.5M7,11.5H8V10.5H7V11.5M20,6V18A2,2 0 0,1 18,20H6A2,2 0 0,1 4,18V6A2,2 0 0,1 6,4H18A2,2 0 0,1 20,6M9.5,10.5A1.5,1.5 0 0,0 8,9H5.5V15H7V13H8A1.5,1.5 0 0,0 9.5,11.5V10.5M14.5,10.5A1.5,1.5 0 0,0 13,9H10.5V15H13A1.5,1.5 0 0,0 14.5,13.5V10.5M18.5,9H15.5V15H17V13H18.5V11.5H17V10.5H18.5V9Z"
id="path2"
style="fill:#ffffff;fill-opacity:0.69803923" />
</svg>

After

Width:  |  Height:  |  Size: 1.8 KiB

View file

@ -24,6 +24,7 @@ images = {
'play_video_black.svg' => ['play_video_black', 128],
'play_gif_black.svg' => ['play_gif_black', 128],
'open_pdf_black.svg' => ['open_pdf_black', 128],
'open_pdf_white.svg' => ['open_pdf_white', 128],
'conversations_mono.svg' => ['conversations/ic_notification', 24],
'quicksy_mono.svg' => ['quicksy/ic_notification', 24],
'ic_send_text_offline.svg' => ['ic_send_text_offline', 36],

View file

@ -825,7 +825,7 @@ public class FileBackend {
} else if (mime.startsWith("video/")) {
thumbnail = getVideoPreview(file, size);
} else {
Bitmap fullsize = getFullsizeImagePreview(file, size);
Bitmap fullsize = getFullSizeImagePreview(file, size);
if (fullsize == null) {
throw new FileNotFoundException();
}
@ -844,7 +844,7 @@ public class FileBackend {
return thumbnail;
}
private Bitmap getFullsizeImagePreview(File file, int size) {
private Bitmap getFullSizeImagePreview(File file, int size) {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = calcSampleSize(file, size);
try {
@ -886,6 +886,21 @@ public class FileBackend {
return record < 0;
}
private boolean paintOverlayBlackPdf(final Bitmap bitmap) {
final int h = bitmap.getHeight();
final int w = bitmap.getWidth();
int white = 0;
for (int y = 0; y < h; ++y) {
for (int x = 0; x < w; ++x) {
int pixel = bitmap.getPixel(x, y);
if ((Color.red(pixel) * 0.299 + Color.green(pixel) * 0.587 + Color.blue(pixel) * 0.114) > 186) {
white++;
}
}
}
return white > (h * w * 0.4f);
}
private Bitmap cropCenterSquareVideo(Uri uri, int size) {
MediaMetadataRetriever metadataRetriever = new MediaMetadataRetriever();
Bitmap frame;
@ -925,8 +940,9 @@ public class FileBackend {
final PdfRenderer.Page page = pdfRenderer.openPage(0);
Dimensions dimensions = scalePdfDimensions(new Dimensions(page.getHeight(), page.getWidth()));
final Bitmap rendered = Bitmap.createBitmap(dimensions.width, dimensions.height, Bitmap.Config.ARGB_8888);
rendered.eraseColor(0xffffffff);
page.render(rendered, null, null, PdfRenderer.Page.RENDER_MODE_FOR_DISPLAY);
drawOverlay(rendered, R.drawable.open_pdf_black, 0.75f);
drawOverlay(rendered, paintOverlayBlackPdf(rendered) ? R.drawable.open_pdf_black : R.drawable.open_pdf_white, 0.75f);
return rendered;
} catch (IOException e) {
Log.d(Config.LOGTAG, "unable to render PDF document preview", e);

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.9 KiB