Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.

Bug 342869

Summary: Image doesn't scale the contained SWT Image on resize
Product: [Modeling] Graphiti Reporter: Veit Hoffmann <vhoff>
Component: CoreAssignee: Michael Wenz <michael.wenz>
Status: CLOSED FIXED QA Contact:
Severity: enhancement    
Priority: P4 CC: juergen.pasch, michael.wenz
Version: 0.7.0Flags: michael.wenz: juno+
Target Milestone: 0.9.0   
Hardware: All   
OS: All   
Whiteboard: Juno M5 Theme_round_offs
Attachments:
Description Flags
Images Example
none
Scaling GFImageFigure Patch against 0.9.0 michael.wenz: iplog+

Description Veit Hoffmann CLA 2011-04-14 12:59:09 EDT
Build Identifier: 

Graphity Images never scale the contained org.eclipse.swt.graphics.Image to fit the given Bounds of the Graphiti Image. Instead the org.eclipse.swt.graphics.Image is clipped at the Graphiti Image's bounds.
This is even the case if StretchH and/or StretchV of the Image is set to true

Reproducible: Always

Steps to Reproduce:
1. Create an Image with StretchH and StretchV = true
2. Resize the Image
3.
Comment 1 Juergen Pasch CLA 2011-05-27 08:37:02 EDT
Resizing of images was never requested before, that might be the reason for the absence of an implementation in class PictogramElementDelegate. Unfortunately the SWT class Image itself offers no solution, but there are articles with workarounds: http://www.eclipse.org/articles/Article-SWT-images/graphics-resources.html#Scaling 
http://www.eclipse.org/articles/Article-Image-Viewer/Image_viewer.html
http://aniszczyk.org/2007/08/09/resizing-images-using-swt/
They are all based on newly created (scaled) images. Ambitious solutions should avoid nasty flickering, which is discussed as well.
Comment 2 Michael Wenz CLA 2011-07-19 04:00:08 EDT
Out-of-scope for Juno, unless there are contributions in this area
Comment 3 Veit Hoffmann CLA 2012-01-13 08:21:59 EST
I had a little time.
Following this http://www.eclipse.org/articles/Article-SWT-images/graphics-resources.html#Scaling I was able to create a resizing Image.

I don't think it is wise to provide my solution as a patch because I made it work without patching the graphiti api by using a subclass of PlatformGraphicsAlgorithm to insert ScalingGFImageFigure  as a Subclass of GFImageFigure.
Currently the ScalingGFImageFigure always scales proportionally. But I can come up wit a version that respects Strech and Proportional setting easily if needed.

Currently ScalingGFImageFigure looks like that:

import org.eclipse.graphiti.mm.algorithms.GraphicsAlgorithm;
import org.eclipse.graphiti.platform.ga.IGraphicsAlgorithmRenderer;
import org.eclipse.graphiti.ui.internal.figures.GFImageFigure;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.widgets.Display;

public class ScalingGFImageFigure extends GFImageFigure implements
		IGraphicsAlgorithmRenderer {

	private GraphicsAlgorithm localGA;

	public ScalingGFImageFigure(GraphicsAlgorithm graphicsAlgorithm) {
		super(graphicsAlgorithm);
		// copy because ga is private in super class
		this.localGA = graphicsAlgorithm;
	}

	@Override
	public void setImage(Image image) {
		if (image != null) {
			// get image data form default image 
			ImageData fullImageData = image.getImageData();
			//scale according to size of ga 
			int width = fullImageData.width;
			int height = fullImageData.height;
			double scalefactorX = (double) localGA.getWidth() / (double) width;
			double scalefactorY = (double) localGA.getHeight()
					/ (double) height;
			double actualScalefactor = 0;
			if (scalefactorX < scalefactorY) {
				actualScalefactor = scalefactorX;
			} else {
				actualScalefactor = scalefactorY;
			}
			// create scaled image
			Image fillImage = new Image(Display.getCurrent(),
					fullImageData.scaledTo((int) (width * actualScalefactor),
							(int) (height * actualScalefactor)));
			super.setImage(fillImage);
		} else {
			super.setImage(image);
		}
	}
}
Comment 4 Michael Wenz CLA 2012-01-17 11:14:02 EST
(In reply to comment #3)
> I had a little time.
> Following this
> http://www.eclipse.org/articles/Article-SWT-images/graphics-resources.html#Scaling
> I was able to create a resizing Image.
> 
> I don't think it is wise to provide my solution as a patch because I made it
> work without patching the graphiti api by using a subclass of
> PlatformGraphicsAlgorithm to insert ScalingGFImageFigure  as a Subclass of
> GFImageFigure.
> Currently the ScalingGFImageFigure always scales proportionally. But I can come
> up wit a version that respects Strech and Proportional setting easily if
> needed.
> 

Veit,
this looks really good from having a first look. Would anything speak against building that directly into GFImageFigure, so all users would benefit? If not, would you provide that as a patch to GFIMageFigure (maybe extended with stretch and proportional)?

Thanks,
Michael
Comment 5 Veit Hoffmann CLA 2012-01-20 05:42:15 EST
Created attachment 209798 [details]
Images Example
Comment 6 Veit Hoffmann CLA 2012-01-20 05:43:15 EST
Created attachment 209799 [details]
Scaling GFImageFigure Patch against 0.9.0
Comment 7 Veit Hoffmann CLA 2012-01-20 05:45:36 EST
Hi Michael,
here's the requested Patch. The GFImageFigure now respects the settings in strech and proportional.
The Example tries to indicate the behavior for different settings.
Feel free to come back to me for any questions.

Cheers,

Veit
Comment 8 Michael Wenz CLA 2012-01-26 08:32:28 EST
Veit, thanks for this contribution!

I have check-in the changes to master and pushed it to Eclipse.
- Patch for the new functionality:
commit 8771b53919d45a6cf9de2d71dd8901a5ceec29ea
Author: mwenz <michael.wenz@sap.com> 2012-01-20 16:08:58
Committer: mwenz <michael.wenz@sap.com> 2012-01-20 16:08:58
Parent: e7801ef2bcd27a2b0d7b24e91975f51485a5345a (Bug 368146: Exception during SVG export - enable rotating an ellipse)
Child: 9601eca7fc0b9f40dba54d6f6de24ecb2d402ffe (Bug 342869 - Added sample in Sketch)
Branches: origin/master, master

- Test functionality in Sketch tool:
commit 9601eca7fc0b9f40dba54d6f6de24ecb2d402ffe
Author: mwenz <michael.wenz@sap.com> 2012-01-23 16:51:29
Committer: mwenz <michael.wenz@sap.com> 2012-01-23 16:51:29
Parent: 8771b53919d45a6cf9de2d71dd8901a5ceec29ea (Bug 342869 - Applied patch)
Child: a6f98cbd47f586cc5802c63ee665af272bb0b77e (Bug 342869 - Updated headers)
Branches: origin/master, master
Comment 9 Juergen Pasch CLA 2012-01-26 08:43:12 EST
Is O.K.
Comment 10 Michael Wenz CLA 2012-04-11 10:52:23 EDT
Bookkeeping: Set target release
Comment 11 Michael Wenz CLA 2012-06-29 04:18:41 EDT
Part of Graphiti 0.9.0 (Eclipse Juno)