X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.eclipse.swt.win32.win32.x86_64%2Fsrc%2Forg%2Feclipse%2Fswt%2Fgraphics%2FImageLoader.java;fp=bundles%2Forg.eclipse.swt.win32.win32.x86_64%2Fsrc%2Forg%2Feclipse%2Fswt%2Fgraphics%2FImageLoader.java;h=bbe5ac8d518a04b8a8c36cc74da6a82bf71bf117;hb=6b98970d0458754dd67f789afbd0a39e1e7ac6eb;hp=0000000000000000000000000000000000000000;hpb=56a61575ce0d27b340cb12438c8a7f303842095e;p=simantics%2Fplatform.git diff --git a/bundles/org.eclipse.swt.win32.win32.x86_64/src/org/eclipse/swt/graphics/ImageLoader.java b/bundles/org.eclipse.swt.win32.win32.x86_64/src/org/eclipse/swt/graphics/ImageLoader.java new file mode 100644 index 000000000..bbe5ac8d5 --- /dev/null +++ b/bundles/org.eclipse.swt.win32.win32.x86_64/src/org/eclipse/swt/graphics/ImageLoader.java @@ -0,0 +1,348 @@ +/******************************************************************************* + * Copyright (c) 2000, 2016 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.swt.graphics; + + +import java.io.*; +import java.util.*; + +import org.eclipse.swt.*; +import org.eclipse.swt.internal.image.*; + +/** + * Instances of this class are used to load images from, + * and save images to, a file or stream. + *

+ * Currently supported image formats are: + *

+ * ImageLoaders can be used to: + * + * + *

+ * NOTE: ImageLoader is implemented in Java on some platforms, which has + * certain performance implications. Performance and memory sensitive applications may + * benefit from using one of the constructors provided by Image, as these + * are implemented natively.

+ * + * @see SWT Example: ImageAnalyzer + * @see Sample code and further information + */ +public class ImageLoader { + + /** + * the array of ImageData objects in this ImageLoader. + * This array is read in when the load method is called, + * and it is written out when the save method is called + */ + public ImageData[] data; + + /** + * the width of the logical screen on which the images + * reside, in pixels (this corresponds to the GIF89a + * Logical Screen Width value) + */ + public int logicalScreenWidth; + + /** + * the height of the logical screen on which the images + * reside, in pixels (this corresponds to the GIF89a + * Logical Screen Height value) + */ + public int logicalScreenHeight; + + /** + * the background pixel for the logical screen (this + * corresponds to the GIF89a Background Color Index value). + * The default is -1 which means 'unspecified background' + * + */ + public int backgroundPixel; + + /** + * the number of times to repeat the display of a sequence + * of animated images (this corresponds to the commonly-used + * GIF application extension for "NETSCAPE 2.0 01"). + * The default is 1. A value of 0 means 'display repeatedly' + */ + public int repeatCount; + + /** + * This is the compression used when saving jpeg and png files. + *

+ * When saving jpeg files, the value is from 1 to 100, + * where 1 is very high compression but low quality, and 100 is + * no compression and high quality; default is 75. + *

+ * When saving png files, the value is from 0 to 3, but they do not impact the quality + * because PNG is lossless compression. 0 is uncompressed, 1 is low compression and fast, + * 2 is default compression, and 3 is high compression but slow. + *

+ * + * @since 3.8 + */ + public int compression; + + /* + * the set of ImageLoader event listeners, created on demand + */ + List imageLoaderListeners; + +/** + * Construct a new empty ImageLoader. + */ +public ImageLoader() { + reset(); +} + +/** + * Resets the fields of the ImageLoader, except for the + * imageLoaderListeners field. + */ +void reset() { + data = null; + logicalScreenWidth = 0; + logicalScreenHeight = 0; + backgroundPixel = -1; + repeatCount = 1; + compression = -1; +} + +/** + * Loads an array of ImageData objects from the + * specified input stream. Throws an error if either an error + * occurs while loading the images, or if the images are not + * of a supported type. Returns the loaded image data array. + * + * @param stream the input stream to load the images from + * @return an array of ImageData objects loaded from the specified input stream + * + * @exception IllegalArgumentException + * @exception SWTException + */ +public ImageData[] load(InputStream stream) { + if (stream == null) SWT.error(SWT.ERROR_NULL_ARGUMENT); + reset(); + data = FileFormat.load(stream, this); + return data; +} + +/** + * Loads an array of ImageData objects from the + * file with the specified name. Throws an error if either + * an error occurs while loading the images, or if the images are + * not of a supported type. Returns the loaded image data array. + * + * @param filename the name of the file to load the images from + * @return an array of ImageData objects loaded from the specified file + * + * @exception IllegalArgumentException + * @exception SWTException + */ +public ImageData[] load(String filename) { + if (filename == null) SWT.error(SWT.ERROR_NULL_ARGUMENT); + InputStream stream = null; + try { + stream = new FileInputStream(filename); + return load(stream); + } catch (IOException e) { + SWT.error(SWT.ERROR_IO, e); + } finally { + try { + if (stream != null) stream.close(); + } catch (IOException e) { + // Ignore error + } + } + return null; +} + +/** + * Saves the image data in this ImageLoader to the specified stream. + * The format parameter can have one of the following values: + *
+ *
IMAGE_BMP
+ *
Windows BMP file format, no compression
+ *
IMAGE_BMP_RLE
+ *
Windows BMP file format, RLE compression if appropriate
+ *
IMAGE_GIF
+ *
GIF file format
+ *
IMAGE_ICO
+ *
Windows ICO file format
+ *
IMAGE_JPEG
+ *
JPEG file format
+ *
IMAGE_PNG
+ *
PNG file format
+ *
+ * + * @param stream the output stream to write the images to + * @param format the format to write the images in + * + * @exception IllegalArgumentException + * @exception SWTException + */ +public void save(OutputStream stream, int format) { + if (stream == null) SWT.error(SWT.ERROR_NULL_ARGUMENT); + FileFormat.save(stream, format, this); +} + +/** + * Saves the image data in this ImageLoader to a file with the specified name. + * The format parameter can have one of the following values: + *
+ *
IMAGE_BMP
+ *
Windows BMP file format, no compression
+ *
IMAGE_BMP_RLE
+ *
Windows BMP file format, RLE compression if appropriate
+ *
IMAGE_GIF
+ *
GIF file format
+ *
IMAGE_ICO
+ *
Windows ICO file format
+ *
IMAGE_JPEG
+ *
JPEG file format
+ *
IMAGE_PNG
+ *
PNG file format
+ *
+ * + * @param filename the name of the file to write the images to + * @param format the format to write the images in + * + * @exception IllegalArgumentException + * @exception SWTException + */ +public void save(String filename, int format) { + if (filename == null) SWT.error(SWT.ERROR_NULL_ARGUMENT); + OutputStream stream = null; + try { + stream = new FileOutputStream(filename); + } catch (IOException e) { + SWT.error(SWT.ERROR_IO, e); + } + save(stream, format); + try { + stream.close(); + } catch (IOException e) { + } +} + +/** + * Adds the listener to the collection of listeners who will be + * notified when image data is either partially or completely loaded. + *

+ * An ImageLoaderListener should be added before invoking + * one of the receiver's load methods. The listener's + * imageDataLoaded method is called when image + * data has been partially loaded, as is supported by interlaced + * GIF/PNG or progressive JPEG images. + * + * @param listener the listener which should be notified + * + * @exception IllegalArgumentException

+ * + * @see ImageLoaderListener + * @see ImageLoaderEvent + */ +public void addImageLoaderListener(ImageLoaderListener listener) { + if (listener == null) SWT.error (SWT.ERROR_NULL_ARGUMENT); + if (imageLoaderListeners == null) { + imageLoaderListeners = new ArrayList<>(); + } + imageLoaderListeners.add(listener); +} + +/** + * Removes the listener from the collection of listeners who will be + * notified when image data is either partially or completely loaded. + * + * @param listener the listener which should no longer be notified + * + * @exception IllegalArgumentException + * + * @see #addImageLoaderListener(ImageLoaderListener) + */ +public void removeImageLoaderListener(ImageLoaderListener listener) { + if (listener == null) SWT.error (SWT.ERROR_NULL_ARGUMENT); + if (imageLoaderListeners == null) return; + imageLoaderListeners.remove(listener); +} + +/** + * Returns true if the receiver has image loader + * listeners, and false otherwise. + * + * @return true if there are ImageLoaderListeners, and false otherwise + * + * @see #addImageLoaderListener(ImageLoaderListener) + * @see #removeImageLoaderListener(ImageLoaderListener) + */ +public boolean hasListeners() { + return imageLoaderListeners != null && imageLoaderListeners.size() > 0; +} + +/** + * Notifies all image loader listeners that an image loader event + * has occurred. Pass the specified event object to each listener. + * + * @param event the ImageLoaderEvent to send to each ImageLoaderListener + */ +public void notifyListeners(ImageLoaderEvent event) { + if (!hasListeners()) return; + int size = imageLoaderListeners.size(); + for (int i = 0; i < size; i++) { + ImageLoaderListener listener = imageLoaderListeners.get(i); + listener.imageDataLoaded(event); + } +} + +}