@@ -448,111 +448,104 @@ static void checkIdentRaw(BufferedImage expectedImage, PDImageXObject actualImag
448448
449449 private void doBitmaskTransparencyTest (int imageType , String pdfFilename ) throws IOException
450450 {
451- PDDocument document = new PDDocument ();
452-
453- int width = 257 ;
454- int height = 256 ;
455-
456- // create an ARGB image
457- BufferedImage argbImage = new BufferedImage (width , height , imageType );
458-
459- // from there, create an image with Transparency.BITMASK
460- Graphics2D g = argbImage .createGraphics ();
461- GraphicsConfiguration gc = g .getDeviceConfiguration ();
462- argbImage = gc .createCompatibleImage (width , height , Transparency .BITMASK );
463- g .dispose ();
464- // create a red rectangle
465- g = argbImage .createGraphics ();
466- g .setColor (Color .red );
467- g .fillRect (0 , 0 , width , height );
468- g .dispose ();
469-
470- Random random = new Random ();
471- random .setSeed (12345 );
472- // create a transparency cross: only pixels in the
473- // interval max/2 - max/8 ... max/2 + max/8 will be visible
474- int startX = width / 2 - width / 8 ;
475- int endX = width / 2 + width / 8 ;
476- int startY = height / 2 - height / 8 ;
477- int endY = height / 2 + height / 8 ;
478- for (int x = 0 ; x < width ; ++x )
451+ File pdfFile ;
452+ try (PDDocument document = new PDDocument ())
479453 {
480- for (int y = 0 ; y < height ; ++y )
454+ int width = 257 ;
455+ int height = 256 ;
456+ // create an ARGB image
457+ BufferedImage argbImage = new BufferedImage (width , height , imageType );
458+ // from there, create an image with Transparency.BITMASK
459+ Graphics2D g = argbImage .createGraphics ();
460+ GraphicsConfiguration gc = g .getDeviceConfiguration ();
461+ argbImage = gc .createCompatibleImage (width , height , Transparency .BITMASK );
462+ g .dispose ();
463+ // create a red rectangle
464+ g = argbImage .createGraphics ();
465+ g .setColor (Color .red );
466+ g .fillRect (0 , 0 , width , height );
467+ g .dispose ();
468+ Random random = new Random ();
469+ random .setSeed (12345 );
470+ // create a transparency cross: only pixels in the
471+ // interval max/2 - max/8 ... max/2 + max/8 will be visible
472+ int startX = width / 2 - width / 8 ;
473+ int endX = width / 2 + width / 8 ;
474+ int startY = height / 2 - height / 8 ;
475+ int endY = height / 2 + height / 8 ;
476+ for (int x = 0 ; x < width ; ++x )
481477 {
482- // create pseudorandom alpha values, but those within the cross
483- // must be >= 128 and those outside must be < 128
484- int alpha ;
485- if ((x >= startX && x <= endX ) || y >= startY && y <= endY )
486- {
487- alpha = 128 + random .nextInt (128 );
488- assertTrue (alpha >= 128 );
489- argbImage .setRGB (x , y , (argbImage .getRGB (x , y ) & 0xFFFFFF ) | (alpha << 24 ));
490- assertEquals (255 , argbImage .getRGB (x , y ) >>> 24 );
491- }
492- else
478+ for (int y = 0 ; y < height ; ++y )
493479 {
494- alpha = random .nextInt (128 );
495- assertTrue (alpha < 128 );
496- argbImage .setRGB (x , y , (argbImage .getRGB (x , y ) & 0xFFFFFF ) | (alpha << 24 ));
497- assertEquals (0 , argbImage .getRGB (x , y ) >>> 24 );
480+ // create pseudorandom alpha values, but those within the cross
481+ // must be >= 128 and those outside must be < 128
482+ int alpha ;
483+ if ((x >= startX && x <= endX ) || y >= startY && y <= endY )
484+ {
485+ alpha = 128 + random .nextInt (128 );
486+ assertTrue (alpha >= 128 );
487+ argbImage .setRGB (x , y , (argbImage .getRGB (x , y ) & 0xFFFFFF ) | (alpha << 24 ));
488+ assertEquals (255 , argbImage .getRGB (x , y ) >>> 24 );
489+ }
490+ else
491+ {
492+ alpha = random .nextInt (128 );
493+ assertTrue (alpha < 128 );
494+ argbImage .setRGB (x , y , (argbImage .getRGB (x , y ) & 0xFFFFFF ) | (alpha << 24 ));
495+ assertEquals (0 , argbImage .getRGB (x , y ) >>> 24 );
496+ }
498497 }
499498 }
500- }
501-
502- PDImageXObject ximage = LosslessFactory .createFromImage (document , argbImage );
503- validate (ximage , 8 , width , height , "png" , PDDeviceRGB .INSTANCE .getName ());
504- checkIdent (argbImage , ximage .getImage ());
505- checkIdentRGB (argbImage , ximage .getOpaqueImage (null , 1 ));
506-
507- assertNotNull (ximage .getSoftMask ());
508- validate (ximage .getSoftMask (), 1 , width , height , "png" , PDDeviceGray .INSTANCE .getName ());
509- assertEquals (2 , colorCount (ximage .getSoftMask ().getImage ()));
510-
511- // check whether the mask is a b/w cross
512- BufferedImage maskImage = ximage .getSoftMask ().getImage ();
513-
514- // avoid multiple of 8 to test padding
515- assertNotEquals (0 , maskImage .getWidth () % 8 );
516-
517- assertEquals (Transparency .OPAQUE , maskImage .getTransparency ());
518- for (int x = 0 ; x < width ; ++x )
519- {
520- for (int y = 0 ; y < height ; ++y )
499+ PDImageXObject ximage = LosslessFactory .createFromImage (document , argbImage );
500+ validate (ximage , 8 , width , height , "png" , PDDeviceRGB .INSTANCE .getName ());
501+ checkIdent (argbImage , ximage .getImage ());
502+ checkIdentRGB (argbImage , ximage .getOpaqueImage (null , 1 ));
503+ assertNotNull (ximage .getSoftMask ());
504+ validate (ximage .getSoftMask (), 1 , width , height , "png" , PDDeviceGray .INSTANCE .getName ());
505+ assertEquals (2 , colorCount (ximage .getSoftMask ().getImage ()));
506+ // check whether the mask is a b/w cross
507+ BufferedImage maskImage = ximage .getSoftMask ().getImage ();
508+ // avoid multiple of 8 to test padding
509+ assertNotEquals (0 , maskImage .getWidth () % 8 );
510+ assertEquals (Transparency .OPAQUE , maskImage .getTransparency ());
511+ for (int x = 0 ; x < width ; ++x )
521512 {
522- if ((x >= startX && x <= endX ) || y >= startY && y <= endY )
523- {
524- assertEquals (0xFFFFFF , maskImage .getRGB (x , y ) & 0xFFFFFF );
525- }
526- else
513+ for (int y = 0 ; y < height ; ++y )
527514 {
528- assertEquals (0 , maskImage .getRGB (x , y ) & 0xFFFFFF );
515+ if ((x >= startX && x <= endX ) || y >= startY && y <= endY )
516+ {
517+ assertEquals (0xFFFFFF , maskImage .getRGB (x , y ) & 0xFFFFFF );
518+ }
519+ else
520+ {
521+ assertEquals (0 , maskImage .getRGB (x , y ) & 0xFFFFFF );
522+ }
529523 }
530524 }
525+ // This part isn't really needed because this test doesn't break
526+ // if the mask has the wrong colorspace (PDFBOX-2057), but it is still useful
527+ // if something goes wrong in the future and we want to have a PDF to open.
528+ // Create a rectangle
529+ BufferedImage rectImage = new BufferedImage (width , height , BufferedImage .TYPE_INT_RGB );
530+ g = rectImage .createGraphics ();
531+ g .setColor (Color .blue );
532+ g .fillRect (0 , 0 , width , height );
533+ g .dispose ();
534+ PDImageXObject ximage2 = LosslessFactory .createFromImage (document , rectImage );
535+ PDPage page = new PDPage ();
536+ document .addPage (page );
537+ try (PDPageContentStream contentStream = new PDPageContentStream (document , page , AppendMode .APPEND , false ))
538+ {
539+ contentStream .drawImage (ximage2 , 150 , 300 , ximage2 .getWidth (), ximage2 .getHeight ());
540+ contentStream .drawImage (ximage , 150 , 300 , ximage .getWidth (), ximage .getHeight ());
541+ }
542+ pdfFile = new File (TESTRESULTSDIR , pdfFilename );
543+ document .save (pdfFile );
544+ }
545+ try (PDDocument document = Loader .loadPDF (pdfFile , (String ) null ))
546+ {
547+ new PDFRenderer (document ).renderImage (0 );
531548 }
532-
533- // This part isn't really needed because this test doesn't break
534- // if the mask has the wrong colorspace (PDFBOX-2057), but it is still useful
535- // if something goes wrong in the future and we want to have a PDF to open.
536- // Create a rectangle
537- BufferedImage rectImage = new BufferedImage (width , height , BufferedImage .TYPE_INT_RGB );
538- g = rectImage .createGraphics ();
539- g .setColor (Color .blue );
540- g .fillRect (0 , 0 , width , height );
541- g .dispose ();
542- PDImageXObject ximage2 = LosslessFactory .createFromImage (document , rectImage );
543-
544- PDPage page = new PDPage ();
545- document .addPage (page );
546- PDPageContentStream contentStream = new PDPageContentStream (document , page , AppendMode .APPEND , false );
547- contentStream .drawImage (ximage2 , 150 , 300 , ximage2 .getWidth (), ximage2 .getHeight ());
548- contentStream .drawImage (ximage , 150 , 300 , ximage .getWidth (), ximage .getHeight ());
549- contentStream .close ();
550- File pdfFile = new File (TESTRESULTSDIR , pdfFilename );
551- document .save (pdfFile );
552- document .close ();
553- document = Loader .loadPDF (pdfFile , (String ) null );
554- new PDFRenderer (document ).renderImage (0 );
555- document .close ();
556549 }
557550
558551 /**
@@ -607,46 +600,52 @@ void testCreateLosslessFrom16Bit() throws IOException
607600 @ Test
608601 void testCreateLosslessFromImageINT_BGR () throws IOException
609602 {
610- PDDocument document = new PDDocument ();
611- BufferedImage image = ImageIO .read (this .getClass ().getResource ("png.png" ));
612-
613- BufferedImage imgBgr = new BufferedImage (image .getWidth (), image .getHeight (), BufferedImage .TYPE_INT_BGR );
614- Graphics2D graphics = imgBgr .createGraphics ();
615- graphics .drawImage (image , 0 , 0 , null );
616-
617- PDImageXObject ximage = LosslessFactory .createFromImage (document , imgBgr );
618- validate (ximage , 8 , imgBgr .getWidth (), imgBgr .getHeight (), "png" , PDDeviceRGB .INSTANCE .getName ());
619- checkIdent (image , ximage .getImage ());
603+ try (PDDocument document = new PDDocument ())
604+ {
605+ BufferedImage image = ImageIO .read (this .getClass ().getResource ("png.png" ));
606+
607+ BufferedImage imgBgr = new BufferedImage (image .getWidth (), image .getHeight (), BufferedImage .TYPE_INT_BGR );
608+ Graphics2D graphics = imgBgr .createGraphics ();
609+ graphics .drawImage (image , 0 , 0 , null );
610+
611+ PDImageXObject ximage = LosslessFactory .createFromImage (document , imgBgr );
612+ validate (ximage , 8 , imgBgr .getWidth (), imgBgr .getHeight (), "png" , PDDeviceRGB .INSTANCE .getName ());
613+ checkIdent (image , ximage .getImage ());
614+ }
620615 }
621616
622617 @ Test
623618 void testCreateLosslessFromImageINT_RGB () throws IOException
624619 {
625- PDDocument document = new PDDocument ();
626- BufferedImage image = ImageIO .read (this .getClass ().getResource ("png.png" ));
627-
628- BufferedImage imgRgb = new BufferedImage (image .getWidth (), image .getHeight (), BufferedImage .TYPE_INT_RGB );
629- Graphics2D graphics = imgRgb .createGraphics ();
630- graphics .drawImage (image , 0 , 0 , null );
631-
632- PDImageXObject ximage = LosslessFactory .createFromImage (document , imgRgb );
633- validate (ximage , 8 , imgRgb .getWidth (), imgRgb .getHeight (), "png" , PDDeviceRGB .INSTANCE .getName ());
634- checkIdent (image , ximage .getImage ());
620+ try (PDDocument document = new PDDocument ())
621+ {
622+ BufferedImage image = ImageIO .read (this .getClass ().getResource ("png.png" ));
623+
624+ BufferedImage imgRgb = new BufferedImage (image .getWidth (), image .getHeight (), BufferedImage .TYPE_INT_RGB );
625+ Graphics2D graphics = imgRgb .createGraphics ();
626+ graphics .drawImage (image , 0 , 0 , null );
627+
628+ PDImageXObject ximage = LosslessFactory .createFromImage (document , imgRgb );
629+ validate (ximage , 8 , imgRgb .getWidth (), imgRgb .getHeight (), "png" , PDDeviceRGB .INSTANCE .getName ());
630+ checkIdent (image , ximage .getImage ());
631+ }
635632 }
636633
637634 @ Test
638635 void testCreateLosslessFromImageBYTE_3BGR () throws IOException
639636 {
640- PDDocument document = new PDDocument ();
641- BufferedImage image = ImageIO .read (this .getClass ().getResource ("png.png" ));
642-
643- BufferedImage imgRgb = new BufferedImage (image .getWidth (), image .getHeight (), BufferedImage .TYPE_3BYTE_BGR );
644- Graphics2D graphics = imgRgb .createGraphics ();
645- graphics .drawImage (image , 0 , 0 , null );
646-
647- PDImageXObject ximage = LosslessFactory .createFromImage (document , imgRgb );
648- validate (ximage , 8 , imgRgb .getWidth (), imgRgb .getHeight (), "png" , PDDeviceRGB .INSTANCE .getName ());
649- checkIdent (image , ximage .getImage ());
637+ try (PDDocument document = new PDDocument ())
638+ {
639+ BufferedImage image = ImageIO .read (this .getClass ().getResource ("png.png" ));
640+
641+ BufferedImage imgRgb = new BufferedImage (image .getWidth (), image .getHeight (), BufferedImage .TYPE_3BYTE_BGR );
642+ Graphics2D graphics = imgRgb .createGraphics ();
643+ graphics .drawImage (image , 0 , 0 , null );
644+
645+ PDImageXObject ximage = LosslessFactory .createFromImage (document , imgRgb );
646+ validate (ximage , 8 , imgRgb .getWidth (), imgRgb .getHeight (), "png" , PDDeviceRGB .INSTANCE .getName ());
647+ checkIdent (image , ximage .getImage ());
648+ }
650649 }
651650
652651 @ Test
0 commit comments