Patch for Exif Reader plug-in

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Patch for Exif Reader plug-in

Chris Pudney
G'day,

We noticed a small bug in the Exif Reader plugiin http://imagej.nih.gov/ij/plugins/exif-reader.html.

If the tag name or value contains a " -" character sequence then it is not correctly displayed. This can happen if values contain negative numbers, e.g. "[Exif SubIFD] Exposure Bias Value: -33/100 EV". This gets displayed as "[Exif SubIFD] Exposure Bias Value: : ".

The problem arises because of the following line in the plug-in source code:

metadata = metadata.replaceAll(" -", ":\t");

A quick fix is to replace this with (note the extra space)

metadata = metadata.replaceAll(" - ", ":\t");

However, this still leaves the the plug-in prone to the pathological case of a tag value containing the string " - ".

So, a more comprehensive fix is attached.

I don't have permission to patch the plug-in. If (either) fix is approved could a new version of the plug-in be made available.

Thanks,
Chris.

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html

import ij.*;
import ij.io.*;

import java.io.*;

import ij.plugin.*;
import ij.text.*;
import com.drew.metadata.*;
import com.drew.imaging.*;

/**
 * Display the EXIF metadata of an image.
 *
 * @author $Author: $
 * @version $Revision: 139 $
 */
public class ExifReader
        implements PlugIn
{
    // Text window dimension.
    private static final int TEXT_WINDOW_WIDTH = 450;
    private static final int TEXT_WINDOW_HEIGHT = 500;

    // Delimiters used by TextWindow.
    private static final String TEXT_WINDOW_COL_DELIMITER = "\t";
    private static final String TEXT_WINDOW_ROW_DELIMITER = "\n";

    // String buffer initialization.
    private static final int STRING_BUFFER_INIT = 1024;

    /**
     * Plug-in image processing method.
     *
     * @param arg arguments passed to the plug-in.
     */
    @Override
    public void run(final String arg)
    {
        // Get image file info.
        FileInfo fi = null;
        final ImagePlus imp = WindowManager.getCurrentImage();
        if (imp != null)
        {
            fi = imp.getOriginalFileInfo();
        }

        // Get image file name and path.
        final String directory;
        final String name;
        if (imp != null && fi != null)
        {
            directory = fi.directory;
            String fileName = fi.fileName;
            if ((fileName == null || fileName.isEmpty()) && imp.getStack().isVirtual())
            {
                fileName = imp.getStack().getSliceLabel(imp.getCurrentSlice());
            }
            name = fileName;
        }
        else
        {
            final OpenDialog od = new OpenDialog("Open JPEG...", arg);
            directory = od.getDirectory();
            name = od.getFileName();
        }

        if (name != null)
        {
            final String path = directory + name;
            try
            {
                // Display results.
                new TextWindow("EXIF metadata for " + name,
                               "Tag\tValue",
                               getMetadata(path),
                               TEXT_WINDOW_WIDTH,
                               TEXT_WINDOW_HEIGHT);
            }
            catch (Throwable e)
            {
                // Display error.
                final String msg = e.getMessage();
                IJ.showMessage("EXIF Error",
                               "Error: " + (msg == null ? String.valueOf(e) : msg) + '\n' + path);

            }
        }
    }

    /**
     * Returns the Exif metadata of the specified file,.
     *
     * @param path image file path.
     * @return the image EXIF metadata.
     * @throws ImageProcessingException if there is a problem reading the metadata.
     * @throws IOException              if there is a problem reading the image file.
     */
    private static String getMetadata(final String path) throws ImageProcessingException, IOException
    {
        // Loop through directories.
        final StringBuilder sb = new StringBuilder(STRING_BUFFER_INIT);
        for (final Directory directory : ImageMetadataReader.readMetadata(new File(path)).getDirectories())
        {
            // Loop through tags in directory.
            for (final Tag tag : directory.getTags())
            {
                sb.append(tagToString(directory, tag));
            }
        }

        return sb.toString();
    }

    /**
     * Get a string version of the tag and its description.
     *
     * @param directory the directory holding the tag.
     * @param tag       the tag.
     * @return a text version of the tag.
     */
    private static String tagToString(final Directory directory,
                                      final Tag tag)
    {
        final String name = '[' + tag.getDirectoryName() + "] " + tag.getTagName();
        String description = tag.getDescription();
        if (description == null)
        {
            description = directory.getString(tag.getTagType()) + " (unable to formulate description)";
        }

        return name.replaceAll(TEXT_WINDOW_COL_DELIMITER, " ") + ':'
               + TEXT_WINDOW_COL_DELIMITER
               + description.replaceAll(TEXT_WINDOW_COL_DELIMITER, " ")
               + TEXT_WINDOW_ROW_DELIMITER;
    }
}




--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html
Reply | Threaded
Open this post in threaded view
|

Re: Patch for Exif Reader plug-in

Chris Pudney
[ Replying with the original text for the benefit of subscribes to the mailing list, who appear to have only received the attachment ]

G'day,

We noticed a small bug in the Exif Reader plugiin http://imagej.nih.gov/ij/plugins/exif-reader.html.

If the tag name or value contains a " -" character sequence then it is not correctly displayed. This can happen if values contain negative numbers, e.g. "[Exif SubIFD] Exposure Bias Value: -33/100 EV". This gets displayed as "[Exif SubIFD] Exposure Bias Value: : ".

The problem arises because of the following line in the plug-in source code:

metadata = metadata.replaceAll(" -", ":\t");

A quick fix is to replace this with (note the extra space)

metadata = metadata.replaceAll(" - ", ":\t");

However, this still leaves the the plug-in prone to the pathological case of a tag value containing the string " - ".

So, a more comprehensive fix is attached.

I don't have permission to patch the plug-in. If (either) fix is approved could a new version of the plug-in be made available.

Thanks,
Chris.

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html
Reply | Threaded
Open this post in threaded view
|

Re: Starting a plugin with a plugin

Michael Schmid
Hi Michael,

Probably the cleanest solution: write a plugin that uses IJ.run with both ExtendedPlugInFilters. Between ExtendedPlugInFilter1 and ExtendedPlugInFilter2, the Plugin would ask the user whether to proceed with ExtendedPlugInFilter2.

Another possibility that is not too ugly:

Have your ExtendedPlugInFilter1 specify the 'FINAL_PROCESSING' flag if the user wants to continue. In the setup method, if arg.equals("final"), (i.e., at the 'final processing' stage, after everything else has been done), unlock the image and do ExtendedPlugInFilter2 with IJ.run.
  imp.unlock() unlocks an ImagePlus (named 'imp').

A further option, though less nice: have a public method in ExtendedPlugInFilter2 to process the image; duplicate the user interface in ExtendedPlugInFilter1 (works only if ExtendedPlugInFilter2 has no preview).


In any case, before you start, you may set Undo.setup(Undo.COMPOUND_FILTER, imp) and after finishing, Undo.setup(Undo.COMPOUND_FILTER_DONE, imp).
(To avoid unnecessary memory allocation, make sure that you always issue COMPOUND_FILTER_DONE before leaving your code).

This will give you undo capability for the full operation, i.e., both filters.

Michael
________________________________________________________________
On May 24, 2013, at 15:39, Michael Epping wrote:

> Hi,
>
> I'm developing some plugins for electron microscopy. At one point I don't know what's the best solution.
>
> Two of my plugins (Plugin1 & Plugin2) are sequential steps of processing. Both plugins implement ExtendedPlugInFilter. I want the user to decide if Plugin2 will be used automaticaly when Plugin1 finished. So for I tried two implementations:
>
> 1. Use the public methods of Plugin2
> > Plugin2 plugin = new Plugin2();
> > if (plugin.setup()!=DONE) {
> >     if (plugin.showDialog()!=DONE){
> >         plugin.run();
> >     }
> > }
> This works, but in my opinion it's ugly code.
>
> 2. Use IJ.run()
> > IJ.run("MenuItemName used at plugin.config");
> At the moment I prefer this implementation. But the JavaDoc of IJ.run() says:
> > "To avoid "image locked",errors, plugins that call this method should implement the PlugIn interface instead of PlugInFilter."
> I was not able to observe any problems by now. Maybe it's because I create new images instead of manipulating the original image.
>
> Is there another way that you can recommended?
>
> Thanks very much for your help.
>
> Best regards,
> Michael

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html
Reply | Threaded
Open this post in threaded view
|

Re: Patch for Exif Reader plug-in

Rasband, Wayne (NIH/NIMH) [E]
In reply to this post by Chris Pudney
On May 23, 2013, at 11:42 PM, Chris Pudney wrote:

> G'day,
>
> We noticed a small bug in the Exif Reader plugiin http://imagej.nih.gov/ij/plugins/exif-reader.html.
>
> If the tag name or value contains a " -" character sequence then it is not correctly displayed. This can happen if values contain negative numbers, e.g. "[Exif SubIFD] Exposure Bias Value: -33/100 EV". This gets displayed as "[Exif SubIFD] Exposure Bias Value: : ".
>
> The problem arises because of the following line in the plug-in source code:
>
> metadata = metadata.replaceAll(" -", ":\t");
>
> A quick fix is to replace this with (note the extra space)
>
> metadata = metadata.replaceAll(" - ", ":\t");

This fix is in the Exif Reader available at <http://imagej.nih.gov/ij/plugins/exif-reader.html>.

With ImageJ 1.47n or later and Exif Reader 2013/03/29 or later, you can display the Exif metadata of an open JPEG image by typing "i" (Image>Show Info).

-wayne


> However, this still leaves the the plug-in prone to the pathological case of a tag value containing the string " - ".
>
> So, a more comprehensive fix is attached.
>
> I don't have permission to patch the plug-in. If (either) fix is approved could a new version of the plug-in be made available.
>
> Thanks,
> Chris.

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html