Skip to content

Commit ba97568

Browse files
ShahzaibIbrahimHeikoKlare
authored andcommitted
Adding disable/gray image props in ImageData when rescaled
Disabled image do not work when rescaled. Writing a new method copyImageDataForDisabledOrGrayImages and calling it before init image in win32_getHandle
1 parent c3f7474 commit ba97568

File tree

4 files changed

+284
-120
lines changed

4 files changed

+284
-120
lines changed

bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/graphics/Image.java

Lines changed: 158 additions & 120 deletions
Original file line numberDiff line numberDiff line change
@@ -297,124 +297,13 @@ public Image(Device device, Image srcImage, int flag) {
297297
}
298298
case SWT.IMAGE_DISABLE: {
299299
ImageData data = srcImage.getImageData(srcImage.getZoom());
300-
PaletteData palette = data.palette;
301-
RGB[] rgbs = new RGB[3];
302-
rgbs[0] = device.getSystemColor(SWT.COLOR_BLACK).getRGB();
303-
rgbs[1] = device.getSystemColor(SWT.COLOR_WIDGET_NORMAL_SHADOW).getRGB();
304-
rgbs[2] = device.getSystemColor(SWT.COLOR_WIDGET_BACKGROUND).getRGB();
305-
ImageData newData = new ImageData(rect.width, rect.height, 8, new PaletteData(rgbs));
306-
newData.alpha = data.alpha;
307-
newData.alphaData = data.alphaData;
308-
newData.maskData = data.maskData;
309-
newData.maskPad = data.maskPad;
310-
if (data.transparentPixel != -1) newData.transparentPixel = 0;
311-
312-
/* Convert the pixels. */
313-
int[] scanline = new int[rect.width];
314-
int[] maskScanline = null;
315-
ImageData mask = null;
316-
if (data.maskData != null) mask = data.getTransparencyMask();
317-
if (mask != null) maskScanline = new int[rect.width];
318-
int redMask = palette.redMask;
319-
int greenMask = palette.greenMask;
320-
int blueMask = palette.blueMask;
321-
int redShift = palette.redShift;
322-
int greenShift = palette.greenShift;
323-
int blueShift = palette.blueShift;
324-
for (int y=0; y<rect.height; y++) {
325-
int offset = y * newData.bytesPerLine;
326-
data.getPixels(0, y, rect.width, scanline, 0);
327-
if (mask != null) mask.getPixels(0, y, rect.width, maskScanline, 0);
328-
for (int x=0; x<rect.width; x++) {
329-
int pixel = scanline[x];
330-
if (!((data.transparentPixel != -1 && pixel == data.transparentPixel) || (mask != null && maskScanline[x] == 0))) {
331-
int red, green, blue;
332-
if (palette.isDirect) {
333-
red = pixel & redMask;
334-
red = (redShift < 0) ? red >>> -redShift : red << redShift;
335-
green = pixel & greenMask;
336-
green = (greenShift < 0) ? green >>> -greenShift : green << greenShift;
337-
blue = pixel & blueMask;
338-
blue = (blueShift < 0) ? blue >>> -blueShift : blue << blueShift;
339-
} else {
340-
red = palette.colors[pixel].red;
341-
green = palette.colors[pixel].green;
342-
blue = palette.colors[pixel].blue;
343-
}
344-
int intensity = red * red + green * green + blue * blue;
345-
if (intensity < 98304) {
346-
newData.data[offset] = (byte)1;
347-
} else {
348-
newData.data[offset] = (byte)2;
349-
}
350-
}
351-
offset++;
352-
}
353-
}
300+
ImageData newData = applyDisableImageData(data, rect.height, rect.width);
354301
init (newData, getZoom());
355302
break;
356303
}
357304
case SWT.IMAGE_GRAY: {
358305
ImageData data = srcImage.getImageData(srcImage.getZoom());
359-
PaletteData palette = data.palette;
360-
ImageData newData = data;
361-
if (!palette.isDirect) {
362-
/* Convert the palette entries to gray. */
363-
RGB [] rgbs = palette.getRGBs();
364-
for (int i=0; i<rgbs.length; i++) {
365-
if (data.transparentPixel != i) {
366-
RGB color = rgbs [i];
367-
int red = color.red;
368-
int green = color.green;
369-
int blue = color.blue;
370-
int intensity = (red+red+green+green+green+green+green+blue) >> 3;
371-
color.red = color.green = color.blue = intensity;
372-
}
373-
}
374-
newData.palette = new PaletteData(rgbs);
375-
} else {
376-
/* Create a 8 bit depth image data with a gray palette. */
377-
RGB[] rgbs = new RGB[256];
378-
for (int i=0; i<rgbs.length; i++) {
379-
rgbs[i] = new RGB(i, i, i);
380-
}
381-
newData = new ImageData(rect.width, rect.height, 8, new PaletteData(rgbs));
382-
newData.alpha = data.alpha;
383-
newData.alphaData = data.alphaData;
384-
newData.maskData = data.maskData;
385-
newData.maskPad = data.maskPad;
386-
if (data.transparentPixel != -1) newData.transparentPixel = 254;
387-
388-
/* Convert the pixels. */
389-
int[] scanline = new int[rect.width];
390-
int redMask = palette.redMask;
391-
int greenMask = palette.greenMask;
392-
int blueMask = palette.blueMask;
393-
int redShift = palette.redShift;
394-
int greenShift = palette.greenShift;
395-
int blueShift = palette.blueShift;
396-
for (int y=0; y<rect.height; y++) {
397-
int offset = y * newData.bytesPerLine;
398-
data.getPixels(0, y, rect.width, scanline, 0);
399-
for (int x=0; x<rect.width; x++) {
400-
int pixel = scanline[x];
401-
if (pixel != data.transparentPixel) {
402-
int red = pixel & redMask;
403-
red = (redShift < 0) ? red >>> -redShift : red << redShift;
404-
int green = pixel & greenMask;
405-
green = (greenShift < 0) ? green >>> -greenShift : green << greenShift;
406-
int blue = pixel & blueMask;
407-
blue = (blueShift < 0) ? blue >>> -blueShift : blue << blueShift;
408-
int intensity = (red+red+green+green+green+green+green+blue) >> 3;
409-
if (newData.transparentPixel == intensity) intensity = 255;
410-
newData.data[offset] = (byte)intensity;
411-
} else {
412-
newData.data[offset] = (byte)254;
413-
}
414-
offset++;
415-
}
416-
}
417-
}
306+
ImageData newData = applyGrayImageData(data, rect.height, rect.width);
418307
init (newData, getZoom());
419308
break;
420309
}
@@ -738,6 +627,150 @@ public Image(Device device, ImageDataProvider imageDataProvider) {
738627
init();
739628
}
740629

630+
private ImageData adaptImageDataIfDisabledOrGray(ImageData data) {
631+
ImageData returnImageData = null;
632+
switch (this.styleFlag) {
633+
case SWT.IMAGE_DISABLE: {
634+
ImageData newData = applyDisableImageData(data, data.height, data.width);
635+
returnImageData = newData;
636+
break;
637+
}
638+
case SWT.IMAGE_GRAY: {
639+
ImageData newData = applyGrayImageData(data, data.height, data.width);
640+
returnImageData = newData;
641+
break;
642+
}
643+
default: {
644+
returnImageData = data;
645+
break;
646+
}
647+
}
648+
649+
return returnImageData;
650+
}
651+
652+
private ImageData applyDisableImageData(ImageData data, int height, int width) {
653+
PaletteData palette = data.palette;
654+
RGB[] rgbs = new RGB[3];
655+
rgbs[0] = this.device.getSystemColor(SWT.COLOR_BLACK).getRGB();
656+
rgbs[1] = this.device.getSystemColor(SWT.COLOR_WIDGET_NORMAL_SHADOW).getRGB();
657+
rgbs[2] = this.device.getSystemColor(SWT.COLOR_WIDGET_BACKGROUND).getRGB();
658+
ImageData newData = new ImageData(width, height, 8, new PaletteData(rgbs));
659+
newData.alpha = data.alpha;
660+
newData.alphaData = data.alphaData;
661+
newData.maskData = data.maskData;
662+
newData.maskPad = data.maskPad;
663+
if (data.transparentPixel != -1) newData.transparentPixel = 0;
664+
665+
/* Convert the pixels. */
666+
int[] scanline = new int[width];
667+
int[] maskScanline = null;
668+
ImageData mask = null;
669+
if (data.maskData != null) mask = data.getTransparencyMask();
670+
if (mask != null) maskScanline = new int[width];
671+
int redMask = palette.redMask;
672+
int greenMask = palette.greenMask;
673+
int blueMask = palette.blueMask;
674+
int redShift = palette.redShift;
675+
int greenShift = palette.greenShift;
676+
int blueShift = palette.blueShift;
677+
for (int y=0; y<height; y++) {
678+
int offset = y * newData.bytesPerLine;
679+
data.getPixels(0, y, width, scanline, 0);
680+
if (mask != null) mask.getPixels(0, y, width, maskScanline, 0);
681+
for (int x=0; x<width; x++) {
682+
int pixel = scanline[x];
683+
if (!((data.transparentPixel != -1 && pixel == data.transparentPixel) || (mask != null && maskScanline[x] == 0))) {
684+
int red, green, blue;
685+
if (palette.isDirect) {
686+
red = pixel & redMask;
687+
red = (redShift < 0) ? red >>> -redShift : red << redShift;
688+
green = pixel & greenMask;
689+
green = (greenShift < 0) ? green >>> -greenShift : green << greenShift;
690+
blue = pixel & blueMask;
691+
blue = (blueShift < 0) ? blue >>> -blueShift : blue << blueShift;
692+
} else {
693+
red = palette.colors[pixel].red;
694+
green = palette.colors[pixel].green;
695+
blue = palette.colors[pixel].blue;
696+
}
697+
int intensity = red * red + green * green + blue * blue;
698+
if (intensity < 98304) {
699+
newData.data[offset] = (byte)1;
700+
} else {
701+
newData.data[offset] = (byte)2;
702+
}
703+
}
704+
offset++;
705+
}
706+
}
707+
return newData;
708+
}
709+
710+
private ImageData applyGrayImageData(ImageData data, int pHeight, int pWidth) {
711+
PaletteData palette = data.palette;
712+
ImageData newData = data;
713+
if (!palette.isDirect) {
714+
/* Convert the palette entries to gray. */
715+
RGB [] rgbs = palette.getRGBs();
716+
for (int i=0; i<rgbs.length; i++) {
717+
if (data.transparentPixel != i) {
718+
RGB color = rgbs [i];
719+
int red = color.red;
720+
int green = color.green;
721+
int blue = color.blue;
722+
int intensity = (red+red+green+green+green+green+green+blue) >> 3;
723+
color.red = color.green = color.blue = intensity;
724+
}
725+
}
726+
newData.palette = new PaletteData(rgbs);
727+
} else {
728+
/* Create a 8 bit depth image data with a gray palette. */
729+
RGB[] rgbs = new RGB[256];
730+
for (int i=0; i<rgbs.length; i++) {
731+
rgbs[i] = new RGB(i, i, i);
732+
}
733+
newData = new ImageData(pWidth, pHeight, 8, new PaletteData(rgbs));
734+
newData.alpha = data.alpha;
735+
newData.alphaData = data.alphaData;
736+
newData.maskData = data.maskData;
737+
newData.maskPad = data.maskPad;
738+
if (data.transparentPixel != -1) newData.transparentPixel = 254;
739+
740+
/* Convert the pixels. */
741+
int[] scanline = new int[pWidth];
742+
int redMask = palette.redMask;
743+
int greenMask = palette.greenMask;
744+
int blueMask = palette.blueMask;
745+
int redShift = palette.redShift;
746+
int greenShift = palette.greenShift;
747+
int blueShift = palette.blueShift;
748+
for (int y=0; y<pHeight; y++) {
749+
int offset = y * newData.bytesPerLine;
750+
data.getPixels(0, y, pWidth, scanline, 0);
751+
for (int x=0; x<pWidth; x++) {
752+
int pixel = scanline[x];
753+
if (pixel != data.transparentPixel) {
754+
int red = pixel & redMask;
755+
red = (redShift < 0) ? red >>> -redShift : red << redShift;
756+
int green = pixel & greenMask;
757+
green = (greenShift < 0) ? green >>> -greenShift : green << greenShift;
758+
int blue = pixel & blueMask;
759+
blue = (blueShift < 0) ? blue >>> -blueShift : blue << blueShift;
760+
int intensity = (red+red+green+green+green+green+green+blue) >> 3;
761+
if (newData.transparentPixel == intensity) intensity = 255;
762+
newData.data[offset] = (byte)intensity;
763+
} else {
764+
newData.data[offset] = (byte)254;
765+
}
766+
offset++;
767+
}
768+
}
769+
}
770+
return newData;
771+
}
772+
773+
741774
/**
742775
* <b>IMPORTANT:</b> This method is not part of the public
743776
* API for Image. It is marked public only so that it
@@ -764,20 +797,22 @@ public static Long win32_getHandle (Image image, int zoom) {
764797

765798
if (image.imageFileNameProvider != null) {
766799
ElementAtZoom<String> imageCandidate = DPIUtil.validateAndGetImagePathAtZoom (image.imageFileNameProvider, zoom);
800+
ImageData imageData = new ImageData (imageCandidate.element());
767801
if (imageCandidate.zoom() == zoom) {
768802
/* Release current native resources */
769803
long handle = image.initNative(imageCandidate.element(), zoom);
770-
if (handle == 0) image.init(new ImageData (imageCandidate.element()), zoom);
804+
if (handle == 0) image.init(imageData, zoom);
771805
image.init();
772806
} else {
773-
ImageData resizedData = DPIUtil.scaleImageData (image.device, new ImageData (imageCandidate.element()), zoom, imageCandidate.zoom());
774-
image.init(resizedData, zoom);
775-
image.init ();
807+
ImageData resizedData = DPIUtil.scaleImageData(image.device, imageData, zoom, imageCandidate.zoom());
808+
ImageData newData = image.adaptImageDataIfDisabledOrGray(resizedData);
809+
image.init(newData, zoom);
776810
}
777811
} else if (image.imageDataProvider != null) {
778812
ElementAtZoom<ImageData> imageCandidate = DPIUtil.validateAndGetImageDataAtZoom (image.imageDataProvider, zoom);
779813
ImageData resizedData = DPIUtil.scaleImageData (image.device, imageCandidate.element(), zoom, imageCandidate.zoom());
780-
image.init(resizedData, zoom);
814+
ImageData newData = image.adaptImageDataIfDisabledOrGray(resizedData);
815+
image.init(newData, zoom);
781816
image.init();
782817
} else {
783818
if (image.dataAtBaseZoom == null && image.memGC == null) {
@@ -786,7 +821,8 @@ public static Long win32_getHandle (Image image, int zoom) {
786821
}
787822
if (image.dataAtBaseZoom != null) {
788823
ImageData resizedData = image.getImageData(zoom);
789-
image.init(resizedData, zoom);
824+
ImageData newData = image.adaptImageDataIfDisabledOrGray(resizedData);
825+
image.init(newData, zoom);
790826
image.init();
791827
}
792828
}
@@ -944,7 +980,9 @@ long initNative(String filename, int zoom) {
944980
ImageData img = new ImageData(width, height, depth, paletteData, scanlinePad, data);
945981
img.transparentPixel = transparentPixel;
946982
img.alphaData = alphaData;
947-
init(img, zoom);
983+
984+
ImageData newData = adaptImageDataIfDisabledOrGray(img);
985+
init(newData, zoom);
948986
handle = zoomLevelToHandle.get(zoom);
949987
}
950988
Gdip.Bitmap_UnlockBits(bitmap, lockedBitmapData);

examples/org.eclipse.swt.snippets/Snippets.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,7 @@ To contribute a new snippet, [create a snippet contribution as a pull request](h
213213
- [draw a reflection of an image](https://github.com/eclipse-platform/eclipse.platform.swt/tree/master/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet279.java)[(preview)](https://github.com/eclipse-platform/eclipse.platform.swt/blob/master/examples/org.eclipse.swt.snippets/previews/Snippet279.png "Preview for Snippet 279")
214214
- [draw an image scaled to half size and double size](https://github.com/eclipse-platform/eclipse.platform.swt/tree/master/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet355.java)[(preview)](https://github.com/eclipse-platform/eclipse.platform.swt/blob/master/examples/org.eclipse.swt.snippets/previews/Snippet355.png "Preview for Snippet 355")
215215
- [draw an image at various zoom/dpi levels](https://github.com/eclipse-platform/eclipse.platform.swt/tree/master/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet367.java)[(preview)](https://github.com/eclipse-platform/eclipse.platform.swt/blob/master/examples/org.eclipse.swt.snippets/previews/Snippet367.png "Preview for Snippet 367")
216+
- [draw a disabled/grayed image at various zoom levels](https://github.com/eclipse-platform/eclipse.platform.swt/tree/master/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet382.java)[(preview)](https://github.com/eclipse-platform/eclipse.platform.swt/blob/master/examples/org.eclipse.swt.snippets/previews/Snippet382.png "Preview for Snippet 382")
216217

217218
### **ImageData**
218219
- [display an animated GIF](https://github.com/eclipse-platform/eclipse.platform.swt/tree/master/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet141.java)
25.4 KB
Loading

0 commit comments

Comments
 (0)