{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Extending ImageJ: Data I/O" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This notebook demonstrates how to write an `IOPlugin`, which handles reading and/or writing of external data to and from Java data structures." ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "ImageJ is ready to go." ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#@ImageJ ij\n", "\n", "// Behind a firewall? Configure your proxy settings here.\n", "//System.setProperty(\"http.proxyHost\",\"myproxy.domain\")\n", "//System.setProperty(\"http.proxyPort\",\"8080\")\n", "\n", "\"ImageJ is ready to go.\"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Creating a data reader" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Suppose we have data stored in a file format, `.char-table`, which represents a table as a sequence of characters, along with metadata defining the number of rows and columns. We would like to write a plugin so that ImageJ can import these files via its _File ▶ Open_ command. \n", "\n", "The format of a `.char-table` file is key-value pairs, one per line. Valid keys are:\n", "\n", "* `cols` to define the column count\n", "* `rows` to define the row count\n", "* `data` to specify the actual character data: a sequence of $cols \\times rows$ characters enumerated in row-major order, and bracketed by `<` and `>`.\n", "\n", "Let's start by writing out an example `.char-table` file, which we will use for testing:" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "Wrote 78 bytes to '/Users/curtis/Desktop/example.char-table'" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import java.io.File\n", "import org.scijava.util.DigestUtils\n", "import org.scijava.util.FileUtils\n", "data = \"\"\"\n", "cols = 7\n", "rows = 7\n", "data = <@@@@@@@@ @ @ @ @@ @ @ @@ @ @ @@ @ @ @@@ >\n", "\"\"\"\n", "desktop = System.getProperty(\"user.home\") + \"/Desktop/\"\n", "tablePath = desktop + \"example.char-table\"\n", "outFile = new File(tablePath)\n", "FileUtils.writeFile(outFile, DigestUtils.bytes(data))\n", "\"Wrote ${outFile.length()} bytes to '$outFile'\"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Without further ado, here is the `IOPlugin` implementation:" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "priority=0.0, enabled=true, pluginType=IOPlugin" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import java.io.File\n", "import net.imagej.table.Table\n", "import net.imagej.table.DefaultGenericTable\n", "import org.scijava.io.AbstractIOPlugin\n", "import org.scijava.io.IOPlugin\n", "import org.scijava.plugin.Plugin\n", "import org.scijava.util.DigestUtils\n", "import org.scijava.util.FileUtils\n", "\n", "@Plugin(type = IOPlugin.class)\n", "public class CharTableReader extends AbstractIOPlugin
@ | @ | @ | @ | @ | @ | @ | |
@ | |||||||
@ | @ | @ | @ | ||||
@ | @ | @ | @ | ||||
@ | @ | @ | @ | ||||
@ | @ | ||||||
@ | @ | @ | @ |