/** * Write the device independent image array stored in the specified loader to the specified output * stream using the specified file format. */ public static void save(OutputStream os, int format, ImageLoader loader) { if (format < 0 || format >= FORMATS.length) SWT.error(SWT.ERROR_UNSUPPORTED_FORMAT); if (FORMATS[format] == null) SWT.error(SWT.ERROR_UNSUPPORTED_FORMAT); if (loader.data == null || loader.data.length < 1) SWT.error(SWT.ERROR_INVALID_ARGUMENT); LEDataOutputStream stream = new LEDataOutputStream(os); FileFormat fileFormat = null; try { Class clazz = Class.forName(FORMAT_PACKAGE + '.' + FORMATS[format] + FORMAT_SUFFIX); fileFormat = (FileFormat) clazz.newInstance(); } catch (Exception e) { SWT.error(SWT.ERROR_UNSUPPORTED_FORMAT); } if (format == SWT.IMAGE_BMP_RLE) { switch (loader.data[0].depth) { case 8: fileFormat.compression = 1; break; case 4: fileFormat.compression = 2; break; } } fileFormat.unloadIntoStream(loader, stream); }
/** * Read the specified input stream using the specified loader, and return the device independent * image array represented by the stream. */ public static ImageData[] load(InputStream is, ImageLoader loader) { FileFormat fileFormat = null; LEDataInputStream stream = new LEDataInputStream(is); for (int i = 1; i < FORMATS.length; i++) { if (FORMATS[i] != null) { try { fileFormat = getFileFormat(stream, FORMATS[i]); if (fileFormat != null) break; } catch (ClassNotFoundException e) { FORMATS[i] = null; } catch (Exception e) { } } } if (fileFormat == null) SWT.error(SWT.ERROR_UNSUPPORTED_FORMAT); fileFormat.loader = loader; return fileFormat.loadFromStream(stream); }
static FileFormat getFileFormat(LEDataInputStream stream, String format) throws Exception { Class clazz = Class.forName(FORMAT_PACKAGE + '.' + format + FORMAT_SUFFIX); FileFormat fileFormat = (FileFormat) clazz.newInstance(); if (fileFormat.isFileFormat(stream)) return fileFormat; return null; }