Skip to content

Commit c1233fd

Browse files
author
Samuel Huylebroeck
committed
Save Mask attribute for PNG image as an array instead of String
String resultated in a PDFLiteral object, which did not result in correctly applied transparancy in Pdf viewers Update 24-bit input file to contain transparency DEVSIX-1934
1 parent 7669c36 commit c1233fd

File tree

6 files changed

+76
-2
lines changed

6 files changed

+76
-2
lines changed

io/src/main/java/com/itextpdf/io/image/PngImageHelper.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ private static void processPng(InputStream pngStream, PngParameters png) throws
216216
png.palShades = true;
217217
png.genBWMask = (!png.palShades && (pal0 > 1 || png.transRedGray >= 0));
218218
if (!png.palShades && !png.genBWMask && pal0 == 1) {
219-
png.additional.put("Mask", MessageFormatUtil.format("[{0} {1}]", palIdx, palIdx));
219+
png.additional.put("Mask", new int[]{palIdx,palIdx});
220220
}
221221
boolean needDecode = (png.interlaceMethod == 1) || (png.bitDepth == 16) || ((png.colorType & 4) != 0) || png.palShades || png.genBWMask;
222222
switch (png.colorType) {

kernel/src/test/java/com/itextpdf/kernel/pdf/ImageFormatsTest.java

Lines changed: 75 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ This file is part of the iText (R) project.
5757
import org.junit.Test;
5858
import org.junit.experimental.categories.Category;
5959

60+
import java.io.FileNotFoundException;
6061
import java.io.IOException;
6162

6263
@Category(IntegrationTest.class)
@@ -71,7 +72,6 @@ public static void beforeClass() {
7172

7273
@Test
7374
public void imagesWithDifferentDepth() throws IOException, InterruptedException {
74-
//TODO: update after DEVSIX-1934 ticket will be fixed
7575
String outFileName = destinationFolder + "transparencyTest01.pdf";
7676
String cmpFileName = sourceFolder + "cmp_transparencyTest01.pdf";
7777
PdfDocument pdfDocument = new PdfDocument(new PdfWriter(outFileName, new WriterProperties()
@@ -148,4 +148,78 @@ public void imagesWithDifferentDepth() throws IOException, InterruptedException
148148

149149
Assert.assertNull(new CompareTool().compareByContent(outFileName, cmpFileName, destinationFolder, "diff_"));
150150
}
151+
152+
@Test
153+
public void png_imageTransparancy_8bitDepthImage() throws IOException, InterruptedException {
154+
String outFileName = destinationFolder + "png_imageTransparancy_8bitDepthImage.pdf";
155+
String cmpFileName = sourceFolder + "cmp_png_imageTransparancy_8bitDepthImage.pdf";
156+
PdfDocument pdfDocument = new PdfDocument(new PdfWriter(outFileName, new WriterProperties()
157+
.setCompressionLevel(CompressionConstants.NO_COMPRESSION)));
158+
PdfPage page = pdfDocument.addNewPage(PageSize.A4);
159+
PdfCanvas canvas = new PdfCanvas(page);
160+
canvas.setFillColor(ColorConstants.LIGHT_GRAY).fill();
161+
canvas.rectangle(80, 0, PageSize.A4.getWidth()-80, PageSize.A4.getHeight()).fill();
162+
163+
canvas
164+
.saveState()
165+
.beginText()
166+
.moveText(116, 800)
167+
.setFontAndSize(PdfFontFactory.createFont(StandardFonts.HELVETICA), 14)
168+
.setFillColor(ColorConstants.MAGENTA)
169+
.showText("8 bit depth PNG")
170+
.moveText(0,-20)
171+
.showText("This image should not have a black rectangle as background")
172+
.endText()
173+
.restoreState();
174+
ImageData img = ImageDataFactory.create(sourceFolder + "manualTransparency_8bit.png");
175+
canvas.addImage(img, 100, 450, 200, false);
176+
177+
canvas.release();
178+
pdfDocument.close();
179+
180+
Assert.assertNull(new CompareTool().compareByContent(outFileName, cmpFileName, destinationFolder, "diff_"));
181+
}
182+
183+
@Test
184+
public void png_imageTransparancy_24bitDepthImage() throws IOException, InterruptedException {
185+
String outFileName = destinationFolder + "png_imageTransparancy_24bitDepthImage.pdf";
186+
String cmpFileName = sourceFolder + "cmp_png_imageTransparancy_24bitDepthImage.pdf";
187+
PdfDocument pdfDocument = new PdfDocument(new PdfWriter(outFileName, new WriterProperties()
188+
.setCompressionLevel(CompressionConstants.NO_COMPRESSION)));
189+
PdfPage page = pdfDocument.addNewPage(PageSize.A4);
190+
PdfCanvas canvas = new PdfCanvas(page);
191+
canvas.setFillColor(ColorConstants.LIGHT_GRAY).fill();
192+
canvas.rectangle(80, 0, PageSize.A4.getWidth()-80, PageSize.A4.getHeight()).fill();
193+
194+
canvas
195+
.saveState()
196+
.beginText()
197+
.moveText(116, 800)
198+
.setFontAndSize(PdfFontFactory.createFont(StandardFonts.HELVETICA), 14)
199+
.setFillColor(ColorConstants.MAGENTA)
200+
.showText("24 bit depth PNG")
201+
.moveText(0,-20)
202+
.showText("This image should not have a white rectangle as background")
203+
.endText()
204+
.restoreState();
205+
ImageData img = ImageDataFactory.create(sourceFolder + "manualTransparency_24bit.png");
206+
canvas.addImage(img, 100, 450, 200, false);
207+
208+
canvas
209+
.saveState()
210+
.beginText()
211+
.moveText(116, 400)
212+
.setFontAndSize(PdfFontFactory.createFont(StandardFonts.HELVETICA), 14)
213+
.setFillColor(ColorConstants.MAGENTA)
214+
.showText("32 bit depth PNG")
215+
.endText()
216+
.restoreState();
217+
img = ImageDataFactory.create(sourceFolder + "manualTransparency_32bit.png");
218+
canvas.addImage(img, 116, 100, 200, false);
219+
220+
canvas.release();
221+
pdfDocument.close();
222+
223+
Assert.assertNull(new CompareTool().compareByContent(outFileName, cmpFileName, destinationFolder, "diff_"));
224+
}
151225
}
182 Bytes
Loading

0 commit comments

Comments
 (0)