--- /dev/null
+/*
+ * Common
+ *
+ * Copyright (c) 2000 - 2012 Samsung Electronics Co., Ltd. All rights reserved.
+ *
+ * Contact:
+ * Changhyun Lee <changhyun1.lee@samsung.com>
+ * Hyeongseok Heo <hyeongseok.heo@samsung.com>
+ * Kangho Kim <kh5325.kim@samsung.com>
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * Contributors:
+ * - S-Core Co., Ltd
+ *
+ */
+
+package org.tizen.common;
+
+/**
+ * The original source is org.tizen.nativecpp.misc.core.ScreenDensity
+ */
+public enum ScreenDensity {
+
+ XHIGH("xhigh", "Xhigh Density", 108), HIGH("high", "High Density", 90);
+
+ private final String fName;
+ private final String fDisplayName;
+ private final int fIconSize;
+
+ private ScreenDensity(String value, String display, int size) {
+ fName = value;
+ fDisplayName = display;
+ fIconSize = size;
+ }
+
+ public int getIconSize() {
+ return fIconSize;
+ }
+
+ public String getName() {
+ return fName;
+ }
+
+ public String getDisplayName() {
+ return fDisplayName;
+ }
+
+ public String getIconDirectory() {
+ return "screen-density-" + fName;
+ }
+
+ public static ScreenDensity getEnumFromDisplayName(String disName) {
+ for (ScreenDensity sd : values()) {
+ if (sd.getDisplayName().equals(disName)) {
+ return sd;
+ }
+ }
+ return null;
+ }
+
+ public static ScreenDensity getEnum(String name) {
+ for (ScreenDensity sd : values()) {
+ if (sd.getName().equals(name)) {
+ return sd;
+ }
+ }
+ return null;
+ }
+
+}
--- /dev/null
+/*
+ * Common
+ *
+ * Copyright (c) 2000 - 2012 Samsung Electronics Co., Ltd. All rights reserved.
+ *
+ * Contact:
+ * Changhyun Lee <changhyun1.lee@samsung.com>
+ * Hyeongseok Heo <hyeongseok.heo@samsung.com>
+ * Kangho Kim <kh5325.kim@samsung.com>
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * Contributors:
+ * - S-Core Co., Ltd
+ *
+ */
+
+package org.tizen.common.ui.dialog;
+
+import java.awt.AlphaComposite;
+import java.awt.Graphics2D;
+import java.awt.image.BufferedImage;
+import java.io.File;
+import java.io.IOException;
+
+import org.eclipse.core.resources.IFile;
+import org.eclipse.core.resources.IProject;
+import org.eclipse.core.runtime.CoreException;
+import org.eclipse.core.runtime.IStatus;
+import org.eclipse.core.runtime.Status;
+import org.eclipse.jface.dialogs.MessageDialog;
+import org.eclipse.swt.SWT;
+import org.eclipse.swt.events.ModifyEvent;
+import org.eclipse.swt.events.ModifyListener;
+import org.eclipse.swt.events.SelectionAdapter;
+import org.eclipse.swt.events.SelectionEvent;
+import org.eclipse.swt.graphics.Image;
+import org.eclipse.swt.graphics.ImageData;
+import org.eclipse.swt.graphics.ImageLoader;
+import org.eclipse.swt.layout.GridData;
+import org.eclipse.swt.layout.GridLayout;
+import org.eclipse.swt.widgets.Button;
+import org.eclipse.swt.widgets.Combo;
+import org.eclipse.swt.widgets.Composite;
+import org.eclipse.swt.widgets.Control;
+import org.eclipse.swt.widgets.Display;
+import org.eclipse.swt.widgets.FileDialog;
+import org.eclipse.swt.widgets.Label;
+import org.eclipse.swt.widgets.Shell;
+import org.eclipse.swt.widgets.Text;
+import org.eclipse.ui.dialogs.SelectionStatusDialog;
+import org.tizen.common.CommonPlugin;
+import org.tizen.common.ScreenDensity;
+import org.tizen.common.util.FileUtil;
+import org.tizen.common.util.ImageUtil;
+import org.tizen.common.util.log.Logger;
+
+/**
+ * The original source is org.tizen.nativecpp.misc.editor.UiNewIconDialog
+ */
+public class NewIconDialog extends SelectionStatusDialog {
+
+ public static final String ICON_FILE_NAME = "icon.png";
+ public static final String[] ICON_FILTER_EXTENSIONS = { "*.png;*.gif;*.jpg;*.ico" };
+ public static final String MASK_IMAGE_PATH = "/icons/mask.png";
+ public static final String EFFECT_IMAGE_PATH = "/icons/effect.png";
+
+ private Text fText;
+ private Button fBrowseBtn;
+ private Combo fDensityCombo;
+ private String fPath;
+ protected Label fPreviewImage;
+ private IProject fProject; // need to refresh file tree
+ private Label lblPreview;
+ private Label lblNewLabel;
+ private Label lblImageFile;
+ private boolean fIsMenuIcon;
+ private BufferedImage fMaskImage, fEffectImage, fIconImage;
+ private Button fMenuIconCheck;
+
+ public NewIconDialog(Shell parentShell, IProject project) {
+ super(parentShell);
+ setTitle("Add Icon");
+ fProject = project;
+
+ setStatusLineAboveButtons(true);
+ }
+
+ public String getPath() {
+ return fPath;
+ }
+
+ @Override
+ protected Control createDialogArea(Composite parent) {
+ Composite grid = new Composite(parent, SWT.NONE);
+ grid.setLayout(new GridLayout(4, false));
+ GridData gd = new GridData(GridData.FILL_BOTH);
+ // gd.widthHint = 400;
+ grid.setLayoutData(gd);
+ applyDialogFont(grid);
+
+ lblImageFile = new Label(grid, SWT.NONE);
+ lblImageFile.setText("Image File:");
+ lblImageFile.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
+
+ fText = new Text(grid, SWT.BORDER);
+ fText.addModifyListener(new ModifyListener() {
+ @Override
+ public void modifyText(ModifyEvent e) {
+ validate();
+ }
+ });
+ fText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
+
+ fBrowseBtn = new Button(grid, SWT.PUSH);
+ fBrowseBtn.setText("Browse...");
+ fBrowseBtn.addSelectionListener(new SelectionAdapter() {
+ @Override
+ public void widgetSelected(SelectionEvent e) {
+ super.widgetSelected(e);
+ handleBrowse();
+ }
+ });
+ fBrowseBtn.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
+
+ lblPreview = new Label(grid, SWT.NONE);
+ lblPreview.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, false, false, 1, 1));
+ lblPreview.setText("Preview:");
+
+ // second row
+ Label label = new Label(grid, SWT.NONE);
+ label.setText("Density:");
+ label.setLayoutData(new GridData(SWT.RIGHT, SWT.TOP, false, false, 1, 1));
+
+ fDensityCombo = new Combo(grid, SWT.READ_ONLY);
+ for (ScreenDensity sd : ScreenDensity.values()) {
+ fDensityCombo.add(sd.getName());
+ }
+
+ fDensityCombo.select(0);
+ GridData gd_fDensityCombo = new GridData(SWT.LEFT, SWT.CENTER, true, false, 1, 1);
+ gd_fDensityCombo.widthHint = 250;
+ fDensityCombo.setLayoutData(gd_fDensityCombo);
+ new Label(grid, SWT.NONE);
+
+ fPreviewImage = new Label(grid, SWT.BORDER);
+ gd = new GridData(SWT.RIGHT, SWT.FILL, false, false, 1, 2);
+ gd.widthHint = 120;
+ gd.heightHint = 120;
+ fPreviewImage.setLayoutData(gd);
+
+ lblNewLabel = new Label(grid, SWT.NONE);
+ lblNewLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.TOP, false, false, 1, 1));
+ lblNewLabel.setText("Option:");
+
+ fMenuIconCheck = new Button(grid, SWT.CHECK);
+ fMenuIconCheck.setLayoutData(new GridData(SWT.FILL, SWT.TOP, false, false, 1, 1));
+ fMenuIconCheck.setText("MainMenu Icon");
+ fMenuIconCheck.addSelectionListener(new SelectionAdapter() {
+ @Override
+ public void widgetSelected(SelectionEvent e) {
+ validate();
+ super.widgetSelected(e);
+ }
+ });
+ new Label(grid, SWT.NONE);
+
+ setHelpAvailable(false);
+ validate();
+
+ return grid;
+ }
+
+ private void validate() {
+ computeResult();
+ IStatus status = null;
+
+ File file = new File(fPath);
+
+ if (fPath.length() < 3 || file.isDirectory()) {
+ status = new Status(IStatus.ERROR, CommonPlugin.PLUGIN_ID, "Enter a path");
+ } else if (!file.exists()) {
+ status = new Status(IStatus.ERROR, CommonPlugin.PLUGIN_ID, fPath + " does not exist");
+ } else {
+ status = new Status(IStatus.OK, CommonPlugin.PLUGIN_ID, null);
+ }
+
+ updateStatus(status);
+
+ if (status.isOK()) {
+ updatePreview();
+ }
+ }
+
+ @Override
+ protected void okPressed() {
+ IFile destFile = fProject.getFile(ICON_FILE_NAME);
+ if (destFile.exists()) {
+ MessageDialog dlg = new MessageDialog(getShell(),
+ "File Already Exists",
+ null,
+ destFile.getProjectRelativePath().toOSString() + " already exists.\nWould you like to replace it?",
+ MessageDialog.QUESTION,
+ new String[] { "Yes", "No" },
+ 0
+ );
+
+ if (dlg.open() == 1) {
+ return;
+ } else {
+ try {
+ destFile.delete(true, null);
+ } catch (CoreException e) {
+ Logger.log(e);
+ }
+ }
+ }
+
+ try {
+ if (fIsMenuIcon) {
+ Image image = fPreviewImage.getImage();
+ ImageLoader loader = new ImageLoader();
+ loader.data = new ImageData[] { image.getImageData() };
+ loader.save(destFile.getLocation().toString(), SWT.IMAGE_PNG);
+ } else {
+ FileUtil.copyTo(fPath, destFile.getLocation().toString(), false);
+ }
+
+ destFile.getParent().refreshLocal(1, null);
+ } catch (IOException e) {
+ Logger.log(e);
+ } catch (CoreException e1) {
+ Logger.log(e1);
+ }
+
+ super.okPressed(); // widget will be disposed
+ }
+
+ @Override
+ protected void computeResult() {
+ fPath = fText.getText();
+ fIsMenuIcon = fMenuIconCheck.getSelection();
+ }
+
+ protected void updatePreview() {
+ Image image = null;
+ if (fIsMenuIcon) {
+ int size = ScreenDensity.XHIGH.getIconSize();
+ BufferedImage srcImage = ImageUtil.getAWTImage(fPath, false);
+ fIconImage = ImageUtil.getScaledImage(srcImage, size, size);
+
+ if (fMaskImage == null) {
+ fMaskImage = ImageUtil.getAWTImage(MASK_IMAGE_PATH, true);
+ }
+ if (fEffectImage == null) {
+ fEffectImage = ImageUtil.getAWTImage(EFFECT_IMAGE_PATH, true);
+ }
+
+ BufferedImage tempImage = new BufferedImage(fMaskImage.getWidth(), fMaskImage.getHeight(), BufferedImage.TYPE_INT_ARGB);
+ Graphics2D graph = tempImage.createGraphics();
+ graph.drawImage(fMaskImage, 0, 0, null);
+ graph.setComposite(AlphaComposite.SrcAtop);
+ // graph.setComposite(AlphaComposite.SrcOver);
+ graph.drawImage(fIconImage, 0, 0, null);
+ graph.drawImage(fEffectImage, 0, 0, null);
+ graph.dispose();
+
+ image = ImageUtil.convertImageToSWT(getShell().getDisplay(), tempImage);
+ } else {
+ BufferedImage bimage = ImageUtil.getAWTImage(fPath, false);
+ image = ImageUtil.convertImageToSWT(fPreviewImage.getDisplay(), bimage);
+ }
+ fPreviewImage.setImage(image);
+ }
+
+ public void handleBrowse() {
+ FileDialog dialog = new FileDialog(Display.getDefault().getActiveShell(), SWT.OPEN);
+ dialog.setFilterExtensions(ICON_FILTER_EXTENSIONS);
+ String src = dialog.open();
+ if (src == null) {
+ return;
+ }
+ fText.setText(src);
+ }
+
+}
*/
package org.tizen.common.util;
+import java.awt.Graphics;
+import java.awt.image.BufferedImage;
+import java.awt.image.DirectColorModel;
+import java.awt.image.IndexColorModel;
+import java.awt.image.WritableRaster;
+import java.io.File;
+import java.io.IOException;
+import java.io.InputStream;
import java.net.URL;
+import javax.imageio.ImageIO;
+
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.Plugin;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
+import org.eclipse.swt.graphics.PaletteData;
+import org.eclipse.swt.graphics.RGB;
+import org.eclipse.swt.widgets.Display;
import org.osgi.framework.Bundle;
import org.tizen.common.SurrogateWithArgument;
-
+import org.tizen.common.util.log.Logger;
public class ImageUtil {
- public static class PlatformSurrogate {
- public Bundle getBundle(String pluginID) {
- return Platform.getBundle(pluginID);
- }
- }
-
- public static class PluginSurrogate {
- public Bundle getBundle(Plugin plugin) {
- return plugin.getBundle();
- }
- }
+ public static class PlatformSurrogate {
+ public Bundle getBundle(String pluginID) {
+ return Platform.getBundle(pluginID);
+ }
+ }
+
+ public static class PluginSurrogate {
+ public Bundle getBundle(Plugin plugin) {
+ return plugin.getBundle();
+ }
+ }
public static final String[] supportedImageExtension = {
"*.gif;*.png;*.jpeg;*.jpg;*.tiff;*.tif;*.ico;*.bmp;*.rle", // all supported format in SWT //$NON-NLS-1$
"*.bmp", // Windows Bitmap //$NON-NLS-1$
"*.rle" // Windows Bitmap with run-length encoding //$NON-NLS-1$
};
-
- /**
- * load image descriptor from <code>bundle</code> with <code>filePath</code>
- *
- * @param bundle {@link Bundle}
- * @param filePath image file path
- *
- * @return loaded {@link ImageDescriptor}
- */
- protected static
- ImageDescriptor
- getImageDescriptor(
- final Bundle bundle,
- final String filePath
- )
- {
- final URL url = bundle.getEntry( filePath );
- return ImageDescriptor.createFromURL( url );
- }
-
- protected static SurrogateWithArgument<Bundle, String> platformSurrogate = new SurrogateWithArgument<Bundle, String>() {
- public Bundle getAdapter( final String pluginId )
- {
- return Platform.getBundle( pluginId );
- }
- };
- protected static SurrogateWithArgument<Bundle, Plugin> pluginSurrogate = new SurrogateWithArgument<Bundle, Plugin>() {
- public Bundle getAdapter(Plugin plugin) {
- return plugin.getBundle();
- }
- };
-
- // get ImageDescriptor from Plugin ID (ID -> ImageDescriptor)
- public static ImageDescriptor getImageDescriptor(String pluginID, String filePath) {
- return getImageDescriptor( platformSurrogate.getAdapter(pluginID), filePath );
- }
-
- // get ImageDescriptor from Activator Plugin (Activator -> ImageDescriptor)
- public static ImageDescriptor getImageDescriptor(Plugin plugin, String filePath) {
- return getImageDescriptor( pluginSurrogate.getAdapter(plugin), filePath );
- }
-
- // get Image from ImageDescriptor (ImageDescriptor -> Image)
- public static Image getImage(ImageDescriptor descriptor) {
- return (descriptor != null) ? descriptor.createImage() : null;
- }
-
- // get ImageData from ImageDescriptor (ImageDescriptor -> ImageData)
- public static ImageData getImageData(ImageDescriptor descriptor) {
- return (descriptor != null) ? descriptor.getImageData() : null;
- }
-
- // get Image from Activator Plugin (Activator -> ImageDescriptor -> Image)
- public static Image getImage(Plugin plugin, String filePath) {
- ImageDescriptor descriptor = getImageDescriptor(plugin, filePath);
- return getImage(descriptor);
- }
-
- // get ImageData from Activator Plugin (Activator -> ImageDescriptor -> ImageData)
- public static ImageData getImageData(Plugin plugin, String filePath) {
- ImageDescriptor descriptor = getImageDescriptor(plugin, filePath);
- return getImageData(descriptor);
- }
-
- // get Image from Plugin ID (ID -> ImageDescriptor -> Image)
- public static Image getImage(String pluginID, String filePath) {
- ImageDescriptor descriptor = getImageDescriptor(pluginID, filePath);
- return getImage(descriptor);
- }
-
- // get Image from Plugin ID (ID -> ImageDescriptor -> ImageData)
- public static ImageData getImageData(String pluginID, String filePath) {
- ImageDescriptor descriptor = getImageDescriptor(pluginID, filePath);
- return getImageData(descriptor);
- }
+
+ /**
+ * load image descriptor from <code>bundle</code> with <code>filePath</code>
+ *
+ * @param bundle {@link Bundle}
+ * @param filePath image file path
+ *
+ * @return loaded {@link ImageDescriptor}
+ */
+ protected static
+ ImageDescriptor
+ getImageDescriptor(
+ final Bundle bundle,
+ final String filePath
+ )
+ {
+ final URL url = bundle.getEntry( filePath );
+ return ImageDescriptor.createFromURL( url );
+ }
+
+ protected static SurrogateWithArgument<Bundle, String> platformSurrogate = new SurrogateWithArgument<Bundle, String>() {
+ public Bundle getAdapter( final String pluginId )
+ {
+ return Platform.getBundle( pluginId );
+ }
+ };
+
+ protected static SurrogateWithArgument<Bundle, Plugin> pluginSurrogate = new SurrogateWithArgument<Bundle, Plugin>() {
+ public Bundle getAdapter(Plugin plugin) {
+ return plugin.getBundle();
+ }
+ };
+
+ // get ImageDescriptor from Plugin ID (ID -> ImageDescriptor)
+ public static ImageDescriptor getImageDescriptor(String pluginID, String filePath) {
+ return getImageDescriptor( platformSurrogate.getAdapter(pluginID), filePath );
+ }
+
+ // get ImageDescriptor from Activator Plugin (Activator -> ImageDescriptor)
+ public static ImageDescriptor getImageDescriptor(Plugin plugin, String filePath) {
+ return getImageDescriptor( pluginSurrogate.getAdapter(plugin), filePath );
+ }
+
+ // get Image from ImageDescriptor (ImageDescriptor -> Image)
+ public static Image getImage(ImageDescriptor descriptor) {
+ return (descriptor != null) ? descriptor.createImage() : null;
+ }
+
+ // get ImageData from ImageDescriptor (ImageDescriptor -> ImageData)
+ public static ImageData getImageData(ImageDescriptor descriptor) {
+ return (descriptor != null) ? descriptor.getImageData() : null;
+ }
+
+ // get Image from Activator Plugin (Activator -> ImageDescriptor -> Image)
+ public static Image getImage(Plugin plugin, String filePath) {
+ ImageDescriptor descriptor = getImageDescriptor(plugin, filePath);
+ return getImage(descriptor);
+ }
+
+ // get ImageData from Activator Plugin (Activator -> ImageDescriptor -> ImageData)
+ public static ImageData getImageData(Plugin plugin, String filePath) {
+ ImageDescriptor descriptor = getImageDescriptor(plugin, filePath);
+ return getImageData(descriptor);
+ }
+
+ // get Image from Plugin ID (ID -> ImageDescriptor -> Image)
+ public static Image getImage(String pluginID, String filePath) {
+ ImageDescriptor descriptor = getImageDescriptor(pluginID, filePath);
+ return getImage(descriptor);
+ }
+
+ // get Image from Plugin ID (ID -> ImageDescriptor -> ImageData)
+ public static ImageData getImageData(String pluginID, String filePath) {
+ ImageDescriptor descriptor = getImageDescriptor(pluginID, filePath);
+ return getImageData(descriptor);
+ }
+
+ public static BufferedImage getScaledImage(BufferedImage src, int width, int height) {
+ java.awt.Image scaledImage = src.getScaledInstance(width, height, java.awt.Image.SCALE_SMOOTH);
+ BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
+ Graphics g = bi.getGraphics();
+ g.drawImage(scaledImage, 0, 0, null);
+ g.dispose();
+ return bi;
+ }
+
+ /*
+ * The original source is org.tizen.nativecpp.misc.utils.ImageUtil
+ */
+ public static BufferedImage getAWTImage(String path, boolean isRelativePath) {
+ BufferedImage image = null;
+ InputStream is = null;
+
+ try {
+ if (isRelativePath) {
+ is = ImageUtil.class.getResourceAsStream(path);
+ if (is != null) {
+ image = ImageIO.read(is);
+ }
+ } else {
+ image = ImageIO.read(new File(path));
+ }
+ } catch (Exception e) {
+ Logger.log(e);
+ } finally {
+ if (is != null) {
+ try {
+ is.close();
+ } catch (IOException e) {
+ Logger.log(e);
+ }
+ }
+ }
+
+ if (image == null) {
+ image = new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB);
+ }
+
+ return image;
+ }
+
+ public static Image getSWTImage(Display display, String relativePath) {
+ Image image = null;
+ InputStream is = ImageUtil.class.getResourceAsStream(relativePath);
+
+ try {
+ image = new Image(Display.getDefault(), is);
+ } catch (RuntimeException ex) {
+ Logger.log(ex);
+ } finally {
+ try {
+ if (is != null) {
+ is.close();
+ }
+ } catch (IOException e) {
+ Logger.log(e);
+ }
+ }
+
+ if (image == null) {
+ image = new Image(display, 1, 1);
+ }
+ return image;
+ }
+
+ public static Image convertImageToSWT(Display display, BufferedImage bImage) {
+ int width = bImage.getWidth();
+ int height = bImage.getHeight();
+
+ // we need alpha
+ BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
+ Graphics graphics = bufferedImage.getGraphics();
+ graphics.drawImage(bImage, 0, 0, null);
+ graphics.dispose();
+
+ if (bufferedImage.getColorModel() instanceof DirectColorModel) {
+ DirectColorModel colorModel = (DirectColorModel) bufferedImage.getColorModel();
+ PaletteData palette = new PaletteData(colorModel.getRedMask(), colorModel.getGreenMask(), colorModel.getBlueMask());
+ ImageData data = new ImageData(bufferedImage.getWidth(), bufferedImage.getHeight(), colorModel.getPixelSize(), palette);
+ for (int y = 0; y < data.height; y++) {
+ for (int x = 0; x < data.width; x++) {
+ int rgb = bufferedImage.getRGB(x, y);
+ int pixel = palette.getPixel(new RGB((rgb >> 16) & 0xFF, (rgb >> 8) & 0xFF, rgb & 0xFF));
+ data.setPixel(x, y, pixel);
+ if (colorModel.hasAlpha()) {
+ data.setAlpha(x, y, (rgb >> 24) & 0xFF);
+ }
+ }
+ }
+ return new Image(display, data);
+
+ } else if (bufferedImage.getColorModel() instanceof IndexColorModel) {
+ IndexColorModel colorModel = (IndexColorModel) bufferedImage.getColorModel();
+ int size = colorModel.getMapSize();
+ byte[] reds = new byte[size];
+ byte[] greens = new byte[size];
+ byte[] blues = new byte[size];
+ colorModel.getReds(reds);
+ colorModel.getGreens(greens);
+ colorModel.getBlues(blues);
+ RGB[] rgbs = new RGB[size];
+ for (int i = 0; i < rgbs.length; i++) {
+ rgbs[i] = new RGB(reds[i] & 0xFF, greens[i] & 0xFF, blues[i] & 0xFF);
+ }
+ PaletteData palette = new PaletteData(rgbs);
+ ImageData data = new ImageData(bufferedImage.getWidth(), bufferedImage.getHeight(), colorModel.getPixelSize(), palette);
+ data.transparentPixel = colorModel.getTransparentPixel();
+ WritableRaster raster = bufferedImage.getRaster();
+ int[] pixelArray = new int[1];
+ for (int y = 0; y < data.height; y++) {
+ for (int x = 0; x < data.width; x++) {
+ raster.getPixel(x, y, pixelArray);
+ data.setPixel(x, y, pixelArray[0]);
+ }
+ }
+ return new Image(display, data);
+ }
+
+ return null;
+ }
+
}