/** * Return the closest compatible {@link IcyColorModel} supported by writer from the specified * image description.<br> * That means the writer is able to save the data described by the returned {@link IcyColorModel} * without any loss or conversion.<br> * * @param writer IFormatWriter we want to test compatibility * @param numChannel number of channel of the image * @param dataType image data type */ public static IcyColorModel getCompatibleColorModel( IFormatWriter writer, int numChannel, DataType dataType) { final DataType outDataType; final int outNumChannel; if (writer instanceof OMETiffWriter) { // TIFF supports all formats outDataType = dataType; outNumChannel = numChannel; } else if (writer instanceof APNGWriter) { // PNG only supports byte and short data type if (dataType.getSize() > 2) outDataType = DataType.USHORT; else outDataType = dataType; // PNG supports a maximum of 4 channels outNumChannel = Math.min(numChannel, 4); } else { // JPG, AVI, default only supports byte data type if (dataType.getSize() > 1) outDataType = DataType.UBYTE; else outDataType = dataType; // 3 channels at max if (numChannel > 3) outNumChannel = 3; else { // special case of 2 channels if (numChannel == 2) // convert to RGB outNumChannel = 3; else outNumChannel = numChannel; } } return IcyColorModel.createInstance(outNumChannel, outDataType); }
/** @deprecated use {@link OMEUtil#generateMetaData(int, int, int, DataType, boolean)} instead */ @Deprecated public static OMEXMLMetadata generateMetaData( int sizeX, int sizeY, int sizeC, int dataType, boolean signedDataType) throws ServiceException { return OMEUtil.generateMetaData( sizeX, sizeY, sizeC, DataType.getDataType(dataType, signedDataType), false); }
/** @deprecated Use {@link #saveImage(byte[], int, int, int, DataType, File, boolean)} instead */ @Deprecated public static void saveImage( byte[] data, int width, int height, int numChannel, int dataType, boolean signedDataType, File file, boolean force) throws FormatException, IOException { saveImage( data, width, height, numChannel, DataType.getDataType(dataType, signedDataType), file, force); }
/** Save a single image from bytes buffer to the specified file. */ private static void saveImage( IFormatWriter formatWriter, byte[] data, int width, int height, int numChannel, boolean separateChannel, DataType dataType, File file, boolean force) throws FormatException, IOException { final String filePath = FileUtil.cleanPath(FileUtil.getGenericPath(file.getAbsolutePath())); if (FileUtil.exists(filePath)) { // forced ? first delete the file else LOCI won't save it if (force) FileUtil.delete(filePath, true); else throw new IOException("File already exists"); } // ensure parent directory exist FileUtil.ensureParentDirExist(filePath); final IFormatWriter writer; final boolean separateCh; if (formatWriter == null) { // get the writer writer = getWriter(FileUtil.getFileExtension(filePath, false), ImageFileFormat.TIFF); // prepare the metadata try { separateCh = getSeparateChannelFlag(writer, numChannel, dataType); writer.setMetadataRetrieve( MetaDataUtil.generateMetaData(width, height, numChannel, dataType, separateCh)); } catch (ServiceException e) { System.err.println("Saver.saveImage(...) error :"); IcyExceptionHandler.showErrorMessage(e, true); } } else { // ready to use writer (metadata already prepared) writer = formatWriter; separateCh = separateChannel; } // we never interleaved data even if some image viewer need it to correctly read image (win XP // viewer) writer.setInterleaved(false); writer.setId(filePath); writer.setSeries(0); // usually give better save performance writer.setWriteSequentially(true); try { // separated channel data if (separateChannel) { final int pitch = width * height * dataType.getSize(); final byte[] dataChannel = new byte[pitch]; int offset = 0; for (int c = 0; c < numChannel; c++) { System.arraycopy(data, offset, dataChannel, 0, pitch); writer.saveBytes(c, dataChannel); offset += pitch; } } else // save all data at once writer.saveBytes(0, data); } catch (Exception e) { System.err.println("Saver.saveImage(...) error :"); IcyExceptionHandler.showErrorMessage(e, true); } writer.close(); }