1 /*******************************************************************************
2  * Copyright (c) 2019 Simeon Andreev and others.
3  *
4  * This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License 2.0
6  * which accompanies this distribution, and is available at
7  * https://www.eclipse.org/legal/epl-2.0/
8  *
9  * SPDX-License-Identifier: EPL-2.0
10  *
11  * Contributors:
12  *     Simeon Andreev - initial API and implementation
13  *******************************************************************************/
14 package org.eclipse.swt.tests.gtk.snippets;
15 
16 import java.io.File;
17 
18 import org.eclipse.swt.SWT;
19 import org.eclipse.swt.graphics.Image;
20 import org.eclipse.swt.graphics.ImageData;
21 import org.eclipse.swt.graphics.ImageLoader;
22 import org.eclipse.swt.graphics.PaletteData;
23 import org.eclipse.swt.layout.FillLayout;
24 import org.eclipse.swt.widgets.Display;
25 import org.eclipse.swt.widgets.Label;
26 import org.eclipse.swt.widgets.Shell;
27 
28 /*
29  * Title: Bug 545032 - [GTK] Implement native ImageLoader
30  * How to run: run snippet and two images side-by-side.
31  * Bug description: any malfunction will be on the right side of the shell.
32  * Expected results: two RGBA patterns side-by-side, looking identical.
33  * Note that the last box may be black, due to the different alpha values.
34  * GTK Version(s): all GTK versions.
35  */
36 public class Bug545032_ImageLoaderTesting {
37 
main(String[] args)38 	public static void main(String[] args) throws Exception {
39 		File file = File.createTempFile("swt", "example");
40 
41 		Display display = new Display();
42 		Shell shell = new Shell(display);
43 		shell.setLayout(new FillLayout());
44 		shell.setSize(500, 500);
45 		shell.setText("Bug 545032: Native ImageLoader Testing");
46 
47 		shell.open();
48 
49 		int imageWidth = 200;
50 		int imageHeight = 200;
51 		int bitDepth = 32;
52 		ImageData imageData = new ImageData(imageWidth, imageHeight, bitDepth, new PaletteData(0xFF0000, 0x00FF00, 0x0000FF));
53 		int w = imageWidth / 2;
54 		int h = imageHeight / 2;
55 		for (int y = 0; y < imageHeight; ++y) {
56 			for (int x = 0; x < imageWidth; ++x) {
57 				// Setting this to 128 will make all colors half-transparent
58 				int alpha = 255;
59 				int color = 0x000000;
60 				if (x < w && y < h) {
61 					color = 0xFF0000;
62 				} else if (x < w && y >= h) {
63 					color = 0x00FF00;
64 				} else if (x >= w && y < h) {
65 					color = 0x0000FF;
66 				} else {
67 					alpha = 128;
68 				}
69 				imageData.setPixel(x, y, color);
70 				// Comment this line to test without transparency in the last box
71 				imageData.setAlpha(x, y, alpha);
72 			}
73 		}
74 
75 		Image image = new Image(display, imageData);
76 		new Label(shell, SWT.BORDER).setImage(image);
77 
78 		ImageLoader saver = new ImageLoader();
79 		saver.data = new ImageData[] { image.getImageData() };
80 		file.delete();
81 		saver.save(file.getAbsolutePath(), SWT.IMAGE_PNG);
82 
83 		ImageLoader loader = new ImageLoader();
84 		ImageData[] loaded = loader.load(file.getAbsolutePath());
85 		for (ImageData imageLoadData : loaded) {
86 			Label l = new Label(shell, SWT.BORDER);
87 			image = new Image(display, imageLoadData);
88 			l.setImage(image);
89 		}
90 		shell.pack();
91 		file.delete();
92 
93 		while (!shell.isDisposed()) {
94 			if (!display.readAndDispatch()) {
95 				display.sleep();
96 			}
97 		}
98 		display.dispose();
99 	}
100 }
101