1616
1717import java .io .*;
1818import java .util .*;
19+ import java .util .function .*;
1920
2021import org .eclipse .swt .*;
2122import org .eclipse .swt .internal .*;
2223import org .eclipse .swt .internal .DPIUtil .*;
2324import org .eclipse .swt .internal .cocoa .*;
2425import org .eclipse .swt .internal .graphics .*;
26+ import org .eclipse .swt .internal .image .*;
2527
2628/**
2729 * Instances of this class are graphics which have been prepared
@@ -693,8 +695,7 @@ public Image(Device device, InputStream stream) {
693695 NSAutoreleasePool pool = null ;
694696 if (!NSThread .isMainThread ()) pool = (NSAutoreleasePool ) new NSAutoreleasePool ().alloc ().init ();
695697 try {
696- // TODO: scale SVGs here too?
697- init (new ImageData (stream ));
698+ initWithSupplier (zoom -> ImageDataLoader .load (stream , FileFormat .DEFAULT_ZOOM , zoom ));
698699 init ();
699700 } finally {
700701 if (pool != null ) pool .release ();
@@ -740,8 +741,7 @@ public Image(Device device, String filename) {
740741 try {
741742 if (filename == null ) SWT .error (SWT .ERROR_NULL_ARGUMENT );
742743 initNative (filename );
743- // TODO: scale SVGs here too?
744- if (this .handle == null ) init (new ImageData (filename ));
744+ if (this .handle == null ) initWithSupplier (zoom -> ImageDataLoader .load (filename , FileFormat .DEFAULT_ZOOM , zoom ));
745745 init ();
746746 } finally {
747747 if (pool != null ) pool .release ();
@@ -781,14 +781,13 @@ public Image(Device device, ImageFileNameProvider imageFileNameProvider) {
781781 super (device );
782782 if (imageFileNameProvider == null ) SWT .error (SWT .ERROR_NULL_ARGUMENT );
783783 this .imageFileNameProvider = imageFileNameProvider ;
784- //TODO: implement fine-grained zoom handling here as well?
785784 String filename = imageFileNameProvider .getImagePath (100 );
786785 if (filename == null ) SWT .error (SWT .ERROR_INVALID_ARGUMENT );
787786 NSAutoreleasePool pool = null ;
788787 if (!NSThread .isMainThread ()) pool = (NSAutoreleasePool ) new NSAutoreleasePool ().alloc ().init ();
789788 try {
790789 initNative (filename );
791- if (this .handle == null ) init (new ImageData (filename ));
790+ if (this .handle == null ) init (ImageDataLoader . load (filename , 100 , 100 ). element ( ));
792791 init ();
793792 String filename2x = imageFileNameProvider .getImagePath (200 );
794793 if (filename2x != null ) {
@@ -800,9 +799,10 @@ public Image(Device device, ImageFileNameProvider imageFileNameProvider) {
800799 // Try to natively scale up the image (e.g. possible if it's an SVG)
801800 ElementAtZoom <ImageData > imageData2x = ImageDataLoader .load (filename , 100 , 200 );
802801 if (imageData2x .zoom () == 200 ) {
803- Image image2x = new Image ( device , imageData2x . element () );
804- NSImageRep rep = ImageUtil . createImageRep ( image2x , image2x . width , image2x . height );
802+ alphaInfo_200 = new AlphaInfo ( );
803+ NSBitmapImageRep rep = createRepresentation ( imageData2x . element (), alphaInfo_200 );
805804 handle .addRepresentation (rep );
805+ rep .release ();
806806 }
807807 }
808808 } finally {
@@ -1205,18 +1205,19 @@ NSBitmapImageRep getRepresentation (int scaleFactor) {
12051205 NSArray reps = handle .representations ();
12061206 NSSize size = handle .size ();
12071207 long count = reps .count ();
1208- int width = (int ) size .width * scaleFactor / 100 ;
1209- int height = (int ) size .height * scaleFactor / 100 ;
1208+ NSSize targetSize = new NSSize ();
1209+ targetSize .width = (int )size .width * scaleFactor / 100 ;
1210+ targetSize .height = (int )size .height * scaleFactor / 100 ;
12101211 NSBitmapImageRep rep ;
12111212 for (int i = 0 ; i < count ; i ++) {
12121213 rep = new NSBitmapImageRep (reps .objectAtIndex (i ));
1213- if (width == rep .pixelsWide () && height == rep .pixelsHigh ()) {
1214+ if (( targetSize . width == rep .pixelsWide () && targetSize . height == rep .pixelsHigh () )) {
12141215 if (rep .isKindOfClass (OS .class_NSBitmapImageRep )) {
12151216 return rep ;
12161217 }
12171218 }
12181219 }
1219- NSBitmapImageRep newRep = createImageRep (width , height );
1220+ NSBitmapImageRep newRep = createImageRep (targetSize );
12201221 for (int i = 0 ; i < count ; i ++) {
12211222 handle .removeRepresentation (new NSImageRep (handle .representations ().objectAtIndex (0 )));
12221223 }
@@ -1408,8 +1409,8 @@ NSBitmapImageRep getRepresentation () {
14081409 return getRepresentation (DPIUtil .getDeviceZoom ());
14091410}
14101411
1411- NSBitmapImageRep createImageRep (int targetWidth , int targetHeight ) {
1412- return ImageUtil .createImageRep (this , targetWidth , targetHeight );
1412+ NSBitmapImageRep createImageRep (NSSize targetSize ) {
1413+ return ImageUtil .createImageRep (this , targetSize );
14131414}
14141415
14151416/**
@@ -1476,6 +1477,25 @@ void init(ImageData image) {
14761477 handle .setCacheMode (OS .NSImageCacheNever );
14771478}
14781479
1480+ private void initWithSupplier (Function <Integer , ElementAtZoom <ImageData >> zoomToImageData ) {
1481+ ElementAtZoom <ImageData > imageData = zoomToImageData .apply (DPIUtil .getDeviceZoom ());
1482+ ImageData imageData2x = null ;
1483+ if (imageData .zoom () == 200 ) {
1484+ imageData2x = imageData .element ();
1485+ }
1486+ if (imageData .zoom () != 100 ) {
1487+ imageData = zoomToImageData .apply (DPIUtil .getDeviceZoom ());
1488+ }
1489+ init (imageData .element ());
1490+ if (imageData2x != null ) {
1491+ alphaInfo_200 = new AlphaInfo ();
1492+ NSBitmapImageRep rep = createRepresentation (imageData2x , alphaInfo_200 );
1493+ handle .addRepresentation (rep );
1494+ rep .release ();
1495+ }
1496+ }
1497+
1498+
14791499void initAlpha_200 (NSBitmapImageRep nativeRep ) {
14801500 NSAutoreleasePool pool = null ;
14811501 if (!NSThread .isMainThread ()) pool = (NSAutoreleasePool ) new NSAutoreleasePool ().alloc ().init ();
0 commit comments