Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: b93bc85332143b44acd3c22d8d75e43ccdc319cc (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
/*******************************************************************************
 * Copyright (c) 2018 Red Hat 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:
 *     Red Hat - initial API and implementation
 *******************************************************************************/
package org.eclipse.swt.tests.gtk.snippets;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;

public class Bug103691_TransparentPixel {

	private static byte LO = 0;
	private static byte MID = 1;
	private static byte HI = 2;

	// Change to path pointing to image.
	private static String IMG_PATH = "/home/xiyan/Downloads/smallfish.gif";

	private static float markOrigColor(byte orig[]) {
		int foo[] = new int[3];
		for (int i = 0; i < 3; i++)
			foo[i] = orig[i] < 0 ? orig[i] + 256 : orig[i];

		int hi, lo, mid;
		hi = lo = mid = foo[0];
		for (int i = 1; i < 3; i++)
			if (foo[i] > hi) {
				mid = hi;
				hi = foo[i];
			} else if (foo[i] < lo) {
				mid = lo;
				lo = foo[i];
			} else
				mid = foo[i];

		for (int i = 0; i < 3; i++)
			if (foo[i] == hi)
				orig[i] = HI;
			else if (foo[i] == lo)
				orig[i] = LO;
			else
				orig[i] = MID;

		return (mid - lo) / (float) (hi - lo);
	}

	private static void colorize(ImageData data, byte orig[], float midratio) {
		for (int y = 0; y < data.height; y++)
			for (int x = 0; x < data.width; x++) {
				int pixel = data.getPixel(x, y);
				if (pixel == data.transparentPixel) {
					data.setPixel(x, y, data.palette.getPixel(new RGB(0, 0, 255)));
					continue;
				}

				RGB pixelValue = data.palette.getRGB(data.getPixel(x, y));
				int foo[] = new int[] { pixelValue.red, pixelValue.green, pixelValue.blue };
				int hi, lo, mid;
				hi = lo = mid = foo[0];
				for (int i = 1; i < 3; i++)
					if (foo[i] > hi) {
						mid = hi;
						hi = foo[i];
					} else if (foo[i] < lo) {
						mid = lo;
						lo = foo[i];
					} else
						mid = foo[i];

				float mul = ((hi + mid + lo) / 765.0f * 255 - hi) / hi;
				hi += mul * hi;
				lo += mul * lo;

				int sat = 100 * (255 - lo) / (hi + 1);
				float dif = (255 - (hi - lo)) / (float) 255;
				hi *= 1 + dif;
				if (hi > 255)
					hi = 255;
				lo -= (sat + 0 * dif);
				if (lo < 0)
					lo = 0;

				mid = (int) (midratio * (hi - lo) + lo);

				for (int i = 0; i < 3; i++)
					if (orig[i] == HI)
						foo[i] = hi;
					else if (orig[i] == LO)
						foo[i] = lo;
					else
						foo[i] = mid;
				data.setPixel(x, y, data.palette.getPixel(new RGB(foo[0], foo[1], foo[2])));
			}
	}

	public static void printArray(byte bites[]) {
		System.out.print("[");
		for (int i = 0; i < 2; i++)
			System.out.print((bites[i] < 0 ? bites[i] + 256 : bites[i]) + ", ");
		System.out.println((bites[2] < 0 ? bites[2] + 256 : bites[2]) + "]");
	}

	public static void main(String[] args) {
		byte orig[] = { (byte) 255, (byte) 64, (byte) 0 };
		float midratio = markOrigColor(orig);

		Display display = new Display();
		final Shell shell = new Shell(display);

		final Label label = new Label(shell, SWT.BORDER);
		Image image = new Image(display, IMG_PATH);
		ImageData data = image.getImageData();

		colorize(data, orig, midratio);

		image = new Image(display, data);

		label.setBounds(image.getBounds());
		shell.pack();

		label.setImage(image);

		shell.open();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();
		}
		display.dispose();
	}

}

Back to the top