Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: fabcc9c815a74de6b2643ad78115c70c35b90b82 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/*******************************************************************************
 * Copyright (c) 2000, 2011 IBM Corporation and others.
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.compare.internal;

import org.eclipse.jface.resource.CompositeImageDescriptor;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.graphics.Point;

/**
 * Combines an image with an overlay.
 */
public class DiffImageDescriptor extends CompositeImageDescriptor {

	static final int HEIGHT= 16;

	private final ImageData fBaseImageData;
	private final ImageDescriptor fOverlayImage;
	private final int fWidth;
	private final boolean fLeft;
	private final int hashCode;

	public DiffImageDescriptor(Image base, ImageDescriptor overlay, int w, boolean onLeft) {
		ImageData data = null;
		if (base != null) {
			data = base.getImageData();
			if (data == null)
				data = DEFAULT_IMAGE_DATA;
		}
		fBaseImageData = data;
		fOverlayImage= overlay;
		fWidth= w;
		fLeft= onLeft;
		hashCode = calculateHashCode();
	}

	private int calculateHashCode() {
		int h1 = 0;
		int h2 = 0;
		if (fBaseImageData != null) {
			h1 = calculateHash(fBaseImageData);
		}
		if (fOverlayImage != null) {
			h2 = fOverlayImage.hashCode();
		}
		return h1 + h2 + fWidth;
	}

	private int calculateHash(ImageData baseImageData) {
		byte[] data = baseImageData.data;
		int hash = baseImageData.width + baseImageData.height;
		for (int i = 0; i < data.length; i++) {
			byte b = data[i];
			hash >>>= 1;
			hash ^= b;
		}
		return hash;
	}

	@Override
	protected Point getSize() {
		return new Point(fWidth, HEIGHT);
	}

	@Override
	protected void drawCompositeImage(int width, int height) {
		if (fLeft) {
			if (fBaseImageData != null) {
				drawImage(fBaseImageData, fWidth - fBaseImageData.width, 0);
			}

			if (fOverlayImage != null) {
				ImageData overlay= fOverlayImage.getImageData();
				if (overlay == null)
					overlay= DEFAULT_IMAGE_DATA;
				drawImage(overlay, 0, (HEIGHT - overlay.height) / 2);
			}
		} else {
			if (fBaseImageData != null) {
				drawImage(fBaseImageData, 0, 0);
			}

			if (fOverlayImage != null) {
				ImageData overlay= fOverlayImage.getImageData();
				if (overlay == null)
					overlay= DEFAULT_IMAGE_DATA;
				drawImage(overlay, fWidth - overlay.width, (HEIGHT - overlay.height) / 2);
			}
		}
	}

	@Override
	public int hashCode() {
		return hashCode;
	}

	@Override
	public boolean equals(Object obj) {
		if (obj == this)
			return true;
		if (obj instanceof DiffImageDescriptor) {
			DiffImageDescriptor other = (DiffImageDescriptor) obj;
			return (other.hashCode == hashCode
					&& isEqual(other.fOverlayImage, fOverlayImage)
					&& other.fWidth == fWidth
					&& other.fLeft == fLeft
					&& isEqual(other.fBaseImageData, fBaseImageData));
		}
		return false;
	}

	private boolean isEqual(ImageData i1, ImageData i2) {
		if (isEqual((Object) i1, (Object) i2)) {
			return true;
		}
		if (i1 == null || i2 == null)
			return false;
		return (i1.width == i2.width && i1.height == i2.height
				&& i1.depth == i2.depth && i1.scanlinePad == i2.scanlinePad
				&& i1.bytesPerLine == i2.bytesPerLine
				/* && i1.palette == i2.palette */
				&& i1.transparentPixel == i2.transparentPixel
				&& i1.maskPad == i2.maskPad
	            && i1.alpha == i2.alpha
				&& i1.type == i2.type && i1.x == i2.x && i1.y == i2.y
				&& i1.disposalMethod == i2.disposalMethod && i1.delayTime == i2.delayTime
				&& equals(i1.data,i2.data) && equals(i1.maskData, i2.maskData)
				&& equals(i1.alphaData, i2.alphaData));
	}

	private boolean equals(byte[] data, byte[] data2) {
		if (isEqual(data, data2))
			return true;
		if (data == null || data2 == null)
			return false;
		if (data.length != data2.length)
			return false;
		for (int i = 0; i < data2.length; i++) {
			if (data[i] != data2[i])
				return false;
		}
		return true;
	}

	private boolean isEqual(Object o1, Object o2) {
		if (o1 == o2)
			return true;
		if (o1 == null || o2 == null)
			return false;
		return o1.equals(o2);
	}
}

Back to the top