Added #1858 PDF export - JPEG with alpha channel exported as is

This commit is contained in:
Jindra Petřík
2022-12-22 22:09:21 +01:00
parent 23404f85b6
commit 355df5899d
5 changed files with 54 additions and 21 deletions

View File

@@ -15,10 +15,16 @@ import java.util.zip.DeflaterOutputStream;
*/
public class PDFMask extends PDFStream implements ImageObserver {
private final Image img;
private byte[] alphaChannel;
private Image img;
private int width;
private int height;
public PDFMask(byte alphaChannel[], int width, int height) {
this.alphaChannel = alphaChannel;
this.width = width;
this.height = height;
}
public PDFMask(Image img) {
super("/XObject");
this.img = img;
@@ -51,23 +57,34 @@ public class PDFMask extends PDFStream implements ImageObserver {
int h = height;
int x = 0;
int y = 0;
int[] pixels = new int[w * h];
PixelGrabber pg = new PixelGrabber(img, x, y, w, h, pixels, 0, w);
try {
pg.grabPixels();
} catch (InterruptedException e) {
System.err.println("interrupted waiting for pixels!");
return;
}
if ((pg.getStatus() & ImageObserver.ABORT) != 0) {
System.err.println("image fetch aborted or errored");
return;
}
for (int j = 0; j < h; j++) {
for (int i = 0; i < w; i++) {
int p = pixels[j * w + i];
int alpha = 255 - (p >> 24) & 0xff;
dos.write(alpha);
if (img != null) {
int[] pixels = new int[w * h];
PixelGrabber pg = new PixelGrabber(img, x, y, w, h, pixels, 0, w);
try {
pg.grabPixels();
} catch (InterruptedException e) {
System.err.println("interrupted waiting for pixels!");
return;
}
if ((pg.getStatus() & ImageObserver.ABORT) != 0) {
System.err.println("image fetch aborted or errored");
return;
}
for (int j = 0; j < h; j++) {
for (int i = 0; i < w; i++) {
int p = pixels[j * w + i];
int alpha = 255 - (p >> 24) & 0xff;
dos.write(alpha);
}
}
} else {
for (int j = 0; j < h; j++) {
for (int i = 0; i < w; i++) {
int p = alphaChannel[j * w + i] & 0xff;
int alpha = 255 - p;
dos.write(alpha);
}
}
}