@@ -380,14 +380,6 @@ public Image(Device device, ImageData data) {
380380 this .device .registerResourceWithZoomSupport (this );
381381}
382382
383- private Image (Device device , ImageData data , int zoom ) {
384- super (device );
385- if (data == null ) SWT .error (SWT .ERROR_NULL_ARGUMENT );
386- this .imageProvider = new PlainImageDataProviderWrapper (data , zoom );
387- init ();
388- this .device .registerResourceWithZoomSupport (this );
389- }
390-
391383/**
392384 * Constructs an instance of this class, whose type is
393385 * <code>SWT.ICON</code>, from the two given <code>ImageData</code>
@@ -1957,6 +1949,46 @@ protected final ImageHandle newImageHandle(ImageData data, ZoomContext zoomConte
19571949 return init (data , zoomContext .targetZoom ());
19581950 }
19591951 }
1952+
1953+ protected final ImageHandle createHandle (int width , int height , int zoom ) {
1954+ long handle = initHandle (width , height , zoom );
1955+ ImageHandle imageHandle = new ImageHandle (handle , zoom );
1956+ zoomLevelToImageHandle .put (zoom , imageHandle );
1957+ return imageHandle ;
1958+ }
1959+
1960+ private long initHandle (int width , int height , int zoom ) {
1961+ if (isDisposed ()) SWT .error (SWT .ERROR_GRAPHIC_DISPOSED );
1962+ int scaledWidth = Win32DPIUtils .pointToPixel (width , zoom );
1963+ int scaledHeight = Win32DPIUtils .pointToPixel (height , zoom );
1964+ long hDC = device .internal_new_GC (null );
1965+ long newHandle = OS .CreateCompatibleBitmap (hDC , scaledWidth , scaledHeight );
1966+ /*
1967+ * Feature in Windows. CreateCompatibleBitmap() may fail
1968+ * for large images. The fix is to create a DIB section
1969+ * in that case.
1970+ */
1971+ if (newHandle == 0 ) {
1972+ int bits = OS .GetDeviceCaps (hDC , OS .BITSPIXEL );
1973+ int planes = OS .GetDeviceCaps (hDC , OS .PLANES );
1974+ int depth = bits * planes ;
1975+ if (depth < 16 ) depth = 16 ;
1976+ if (depth > 24 ) depth = 24 ;
1977+ newHandle = createDIB (scaledWidth , scaledHeight , depth );
1978+ }
1979+ if (newHandle != 0 ) {
1980+ long memDC = OS .CreateCompatibleDC (hDC );
1981+ long hOldBitmap = OS .SelectObject (memDC , newHandle );
1982+ OS .PatBlt (memDC , 0 , 0 , scaledWidth , scaledHeight , OS .PATCOPY );
1983+ OS .SelectObject (memDC , hOldBitmap );
1984+ OS .DeleteDC (memDC );
1985+ }
1986+ device .internal_dispose_GC (hDC , null );
1987+ if (newHandle == 0 ) {
1988+ SWT .error (SWT .ERROR_NO_HANDLES , null , device .getLastError ());
1989+ }
1990+ return newHandle ;
1991+ }
19601992}
19611993
19621994private class ExistingImageHandleProviderWrapper extends AbstractImageProviderWrapper {
@@ -2196,7 +2228,7 @@ protected ImageHandle newImageHandle(ZoomContext zoomContext) {
21962228 if (memGC .getZoom () != targetZoom ) {
21972229 GC currentGC = memGC ;
21982230 memGC = null ;
2199- createHandle (targetZoom );
2231+ createHandle (this . width , this . height , targetZoom );
22002232 currentGC .refreshFor (new DrawableWrapper (Image .this , zoomContext ));
22012233 }
22022234 return zoomLevelToImageHandle .get (targetZoom );
@@ -2205,47 +2237,7 @@ protected ImageHandle newImageHandle(ZoomContext zoomContext) {
22052237 }
22062238 private ImageHandle createBaseHandle (int zoom ) {
22072239 baseZoom = zoom ;
2208- return createHandle (zoom );
2209- }
2210-
2211- private ImageHandle createHandle (int zoom ) {
2212- long handle = initHandle (zoom );
2213- ImageHandle imageHandle = new ImageHandle (handle , zoom );
2214- zoomLevelToImageHandle .put (zoom , imageHandle );
2215- return imageHandle ;
2216- }
2217-
2218- private long initHandle (int zoom ) {
2219- if (isDisposed ()) SWT .error (SWT .ERROR_GRAPHIC_DISPOSED );
2220- int scaledWidth = Win32DPIUtils .pointToPixel (width , zoom );
2221- int scaledHeight = Win32DPIUtils .pointToPixel (height , zoom );
2222- long hDC = device .internal_new_GC (null );
2223- long newHandle = OS .CreateCompatibleBitmap (hDC , scaledWidth , scaledHeight );
2224- /*
2225- * Feature in Windows. CreateCompatibleBitmap() may fail
2226- * for large images. The fix is to create a DIB section
2227- * in that case.
2228- */
2229- if (newHandle == 0 ) {
2230- int bits = OS .GetDeviceCaps (hDC , OS .BITSPIXEL );
2231- int planes = OS .GetDeviceCaps (hDC , OS .PLANES );
2232- int depth = bits * planes ;
2233- if (depth < 16 ) depth = 16 ;
2234- if (depth > 24 ) depth = 24 ;
2235- newHandle = createDIB (scaledWidth , scaledHeight , depth );
2236- }
2237- if (newHandle != 0 ) {
2238- long memDC = OS .CreateCompatibleDC (hDC );
2239- long hOldBitmap = OS .SelectObject (memDC , newHandle );
2240- OS .PatBlt (memDC , 0 , 0 , scaledWidth , scaledHeight , OS .PATCOPY );
2241- OS .SelectObject (memDC , hOldBitmap );
2242- OS .DeleteDC (memDC );
2243- }
2244- device .internal_dispose_GC (hDC , null );
2245- if (newHandle == 0 ) {
2246- SWT .error (SWT .ERROR_NO_HANDLES , null , device .getLastError ());
2247- }
2248- return newHandle ;
2240+ return createHandle (this .width , this .height , zoom );
22492241 }
22502242
22512243 @ Override
@@ -2621,27 +2613,26 @@ protected ImageHandle newImageHandle(ZoomContext zoomContext) {
26212613 currentZoom = zoomContext ;
26222614 int targetZoom = zoomContext .targetZoom ();
26232615 int gcStyle = drawer .getGcStyle ();
2624- Image image ;
26252616 if ((gcStyle & SWT .TRANSPARENT ) != 0 ) {
26262617 int scaledHeight = Win32DPIUtils .pointToPixel (height , targetZoom );
26272618 int scaledWidth = Win32DPIUtils .pointToPixel (width , targetZoom );
26282619 /* Create a 24 bit image data with alpha channel */
26292620 final ImageData resultData = new ImageData (scaledWidth , scaledHeight , 24 , new PaletteData (0xFF , 0xFF00 , 0xFF0000 ));
26302621 resultData .alphaData = new byte [scaledWidth * scaledHeight ];
2631- image = new Image ( device , resultData , targetZoom );
2622+ init ( resultData , targetZoom );
26322623 } else {
2633- image = new Image ( device , width , height );
2624+ createHandle ( width , height , targetZoom );
26342625 }
2635- GC gc = new GC (new DrawableWrapper (image , zoomContext ), gcStyle );
2626+ GC gc = new GC (new DrawableWrapper (Image . this , zoomContext ), gcStyle );
26362627 try {
26372628 drawer .drawOn (gc , width , height );
2638- ImageData imageData = image .getImageData (targetZoom );
2629+ ImageData imageData = Image . this .getImageData (targetZoom );
26392630 drawer .postProcess (imageData );
2640- ImageData newData = adaptImageDataIfDisabledOrGray (imageData );
2641- return init (newData , targetZoom );
2631+ zoomLevelToImageHandle .get (targetZoom ).destroy ();
2632+ init (imageData , targetZoom );
2633+ return zoomLevelToImageHandle .get (targetZoom );
26422634 } finally {
26432635 gc .dispose ();
2644- image .dispose ();
26452636 }
26462637 }
26472638
0 commit comments