@Override public void run() { Database database = input.getDatabase(); Relation<O> relation = database.getRelation(distance.getInputTypeRestriction()); DistanceQuery<O> distanceQuery = database.getDistanceQuery(relation, distance); KNNQuery<O> knnQ = database.getKNNQuery(distanceQuery, DatabaseQuery.HINT_HEAVY_USE); // open file. try (RandomAccessFile file = new RandomAccessFile(out, "rw"); FileChannel channel = file.getChannel(); // and acquire a file write lock FileLock lock = channel.lock()) { // write magic header file.writeInt(KNN_CACHE_MAGIC); int bufsize = k * 12 * 2 + 10; // Initial size, enough for 2 kNN. ByteBuffer buffer = ByteBuffer.allocateDirect(bufsize); FiniteProgress prog = LOG.isVerbose() ? new FiniteProgress("Computing kNN", relation.size(), LOG) : null; for (DBIDIter it = relation.iterDBIDs(); it.valid(); it.advance()) { final KNNList nn = knnQ.getKNNForDBID(it, k); final int nnsize = nn.size(); // Grow the buffer when needed: if (nnsize * 12 + 10 > bufsize) { while (nnsize * 12 + 10 > bufsize) { bufsize <<= 1; } buffer = ByteBuffer.allocateDirect(bufsize); } buffer.clear(); ByteArrayUtil.writeUnsignedVarint(buffer, it.internalGetIndex()); ByteArrayUtil.writeUnsignedVarint(buffer, nnsize); int c = 0; for (DoubleDBIDListIter ni = nn.iter(); ni.valid(); ni.advance(), c++) { ByteArrayUtil.writeUnsignedVarint(buffer, ni.internalGetIndex()); buffer.putDouble(ni.doubleValue()); } if (c != nn.size()) { throw new AbortException("Sizes did not agree. Cache is invalid."); } buffer.flip(); channel.write(buffer); LOG.incrementProcessed(prog); } LOG.ensureCompleted(prog); lock.release(); } catch (IOException e) { LOG.exception(e); } // FIXME: close! }
/** * Show a "Save as" dialog. * * @param plot The plot to be exported. * @param width The width of the exported image (when export to JPEG/PNG). * @param height The height of the exported image (when export to JPEG/PNG). * @return Result from {@link JFileChooser#showSaveDialog} */ public static int showSaveDialog(SVGPlot plot, int width, int height) { double quality = 1.0; int ret = -1; JFileChooser fc = new JFileChooser(new File(".")); fc.setDialogTitle(DEFAULT_TITLE); // fc.setFileFilter(new ImageFilter()); SaveOptionsPanel optionsPanel = new SaveOptionsPanel(fc, width, height); fc.setAccessory(optionsPanel); ret = fc.showSaveDialog(null); fc.setDialogTitle("Saving... Please wait."); if (ret == JFileChooser.APPROVE_OPTION) { File file = fc.getSelectedFile(); String format = optionsPanel.getSelectedFormat(); if (format == null || automagic_format.equals(format)) { format = guessFormat(file.getName()); } try { if (format == null) { showError(fc, "Error saving image.", "File format not recognized."); } else if ("jpeg".equals(format) || "jpg".equals(format)) { quality = optionsPanel.getJPEGQuality(); plot.saveAsJPEG(file, width, height, quality); } else if ("png".equals(format)) { plot.saveAsPNG(file, width, height); } else if ("ps".equals(format)) { plot.saveAsPS(file); } else if ("eps".equals(format)) { plot.saveAsEPS(file); } else if ("pdf".equals(format)) { plot.saveAsPDF(file); } else if ("svg".equals(format)) { plot.saveAsSVG(file); } else { showError(fc, "Error saving image.", "Unsupported format: " + format); } } catch (java.lang.IncompatibleClassChangeError e) { showError( fc, "Error saving image.", "It seems that your Java version is incompatible with this version of Batik and Jpeg writing. Sorry."); } catch (ClassNotFoundException e) { showError( fc, "Error saving image.", "A class was not found when saving this image. Maybe installing Apache FOP will help (for PDF, PS and EPS output).\n" + e.toString()); } catch (IOException e) { LOG.exception(e); showError(fc, "Error saving image.", e.toString()); } catch (TranscoderException e) { LOG.exception(e); showError(fc, "Error saving image.", e.toString()); } catch (TransformerFactoryConfigurationError e) { LOG.exception(e); showError(fc, "Error saving image.", e.toString()); } catch (TransformerException e) { LOG.exception(e); showError(fc, "Error saving image.", e.toString()); } catch (Exception e) { LOG.exception(e); showError(fc, "Error saving image.", e.toString()); } } else if (ret == JFileChooser.ERROR_OPTION) { showError(fc, "Error in file dialog.", "Unknown Error."); } else if (ret == JFileChooser.CANCEL_OPTION) { // do nothing - except return result } return ret; }