org.geotoolkit.image.io
Class PaletteFactory

Object
  extended by PaletteFactory

public class PaletteFactory
extends Object

A factory for index color models created from RGB values listed in files. The palette definition files are text files containing an arbitrary number of lines, each line containing RGB components ranging from 0 to 255 inclusive. An optional fourth column may be provided for alpha components. Empty lines and lines starting with the '#' character are ignored. Example:

# RGB codes for SeaWiFs images
# (chlorophylle-a concentration)
033   000   096
032   000   097
031   000   099
030   000   101
029   000   102
028   000   104
026   000   106
025   000   107
etc...
The number of RGB codes doesn't have to match the target color map size. RGB codes will be automatically interpolated as needed. For example it is legal to declare only 2 colors, blue and red for instance. If an image needs 256 colors, then all needed colors will be interpolated from blue to red, with blue at index 0 and red at index 255.

The default instance provides the following color palettes. More details about those palettes are provided in this page.

palette palette palette palette
palette palette palette
palette palette palette
palette palette palette
palette palette palette
palette palette palette


Adding custom palettes
To add custom palettes, create a subclass of PaletteFactory like below:

public class MyPalettes extends PaletteFactory {
    public MyPalettes() {
        // The call below uses the default options for convenience,
        // but we could use an other constructor with more parameters.
        super();
    }
}

// Optionally, we could override getResourceAsStream(String) if we wanted
// to fetch the color codes from a custom source (e.g. from a database).
In the directory that contain the MyPalettes.class file, create a "colors"  sub-directory and put the palette definitions (as text files with the ".pal" suffix) in that directory. The directory and the file suffix can be changed using one of the constructors expecting arguments. Finally, declare the fully-qualified name of MyPalettes in the following file:
META-INF/services/org.geotoolkit.image.io.PaletteFactory

Since:
1.2
Version:
3.17
Author:
Martin Desruisseaux (IRD, Geomatys)
Module:
coverage/geotk-coverage (download)    View source code for this class

Field Summary
protected  WeakHashSet<Palette> palettes
          The set of palettes already created and not yet garbage-collected.
 
Constructor Summary
protected PaletteFactory()
          Constructs a default palette factory using this object class for loading palette definition files.
  PaletteFactory(File directory, String extension, Charset charset, Locale locale)
          Constructs a palette factory which will load the palette definition files from the specified directory.
  PaletteFactory(PaletteFactory fallback, Class<?> loader, File directory, String extension, Charset charset, Locale locale)
          Constructs a palette factory using an optional class for loading palette definition files.
  PaletteFactory(PaletteFactory fallback, ClassLoader loader, File directory, String extension, Charset charset, Locale locale)
          Constructs a palette factory using an optional class loader for loading palette definition files.
 
Method Summary
 Set<String> getAvailableNames()
          Returns the set of available palette names.
 Color[] getColors(String name)
          Loads colors from a definition file.
 Palette getContinuousPalette(String name, float minimum, float maximum, int dataType, int numBands, int visibleBand)
          Creates a palette suitable for floating point values.
static PaletteFactory getDefault()
          Gets the default palette factory.
 Palette getPalette(String name, int size)
          Returns the palette of the specified name and size.
 Palette getPalette(String name, int lower, int upper, int size, int numBands, int visibleBand)
          Returns the palette of the specified name and size.
 Palette getPalettePadValueFirst(String name, int size)
          Returns a palette with a pad value at index 0.
 Palette getPalettePadValueLast(String name, int size)
          Returns a palette with pad value at the last index.
protected  InputStream getResourceAsStream(String name)
          Returns an input stream for reading the specified resource.
 Locale getWarningLocale()
          Returns the locale set by the last invocation to setWarningLocale(Locale) in the current thread.
static void scanForPlugins(ClassLoader loader)
          Lookups for custom palette factories on the classpath.
 void setWarningLocale(Locale warningLocale)
          Sets the locale to use for formatting warning or error messages.
 
Methods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

palettes

protected final WeakHashSet<Palette> palettes
The set of palettes already created and not yet garbage-collected. The getPalette method implementations shall return unique Palette instances as below:
public Palette getPalette(...) {
    Palette palette = ...;
    palette = palettes.unique(palette); // Ensure that the instance is unique.
    return palette;
}
The purpose is to share existing ColorModel instances when possible, since they may be big (up to 256 kilobytes for an IndexColorModel with 16 bits integers). This mechanism works provided that Palette.createImageTypeSpecifier() lazily create the color model only when first invoked.

Since:
3.11
Constructor Detail

PaletteFactory

protected PaletteFactory()
Constructs a default palette factory using this object class for loading palette definition files. The default directory is "colors" relative to the directory of the subclass extending this class. The character encoding is ISO-8859-1 and the locale is US.

This constructor is protected because is it merely a convenience for subclasses registering themselves as a service in the following file (see class javadoc for more details):

META-INF/services/org.geotoolkit.image.io.PaletteFactory
Users should invoke getDefault() instead, which will return a shared instance of this class together with any custom factories found on the class path.

Since:
2.5

PaletteFactory

public PaletteFactory(File directory,
                      String extension,
                      Charset charset,
                      Locale locale)
Constructs a palette factory which will load the palette definition files from the specified directory. No class loader is used, i.e. the palettes are read as ordinary file relative to the current working directory (not the classpath).

Parameters:
directory - The base directory for palette definition files relative to current directory, or null for ".".
extension - File name extension, or null if there is no extension to add to filename. If non-null, this extension will be automatically appended to filename. It should starts with the '.' character.
charset - The charset to use for parsing files, or null for the default.
locale - The locale to use for parsing files, or null for the default.
Since:
2.5

PaletteFactory

public PaletteFactory(PaletteFactory fallback,
                      ClassLoader loader,
                      File directory,
                      String extension,
                      Charset charset,
                      Locale locale)
Constructs a palette factory using an optional class loader for loading palette definition files. If loader is non-null, the definitions will be read using ClassLoader.getResource(String), which imply that the directory argument is relative to the root packages on the classpath.

Parameters:
fallback - An optional fallback factory, or null if there is none. The fallback factory will be queried if a palette was not found in the current factory.
loader - An optional class loader to use for loading the palette definition files. If null, loading will occurs from the system current working directory.
directory - The base directory for palette definition files. It may be a Java package if a loader were specified. If null, then "." is assumed.
extension - File name extension, or null if there is no extension to add to filename. If non-null, this extension will be automatically appended to filename. It should starts with the '.' character.
charset - The charset to use for parsing files, or null for the default.
locale - The locale to use for parsing files, or null for the default.

PaletteFactory

public PaletteFactory(PaletteFactory fallback,
                      Class<?> loader,
                      File directory,
                      String extension,
                      Charset charset,
                      Locale locale)
Constructs a palette factory using an optional class for loading palette definition files. If loader is non-null, the definitions will be read using Class.getResource(String), which imply that the directory argument is relative to the package of the PaletteFactory subclass.

Using a class instead of a class loader can avoid security issue on some platforms (some platforms do not allow to load resources from a ClassLoader because it would make possible to load from the root package).

Parameters:
fallback - An optional fallback factory, or null if there is none. The fallback factory will be queried if a palette was not found in the current factory.
loader - An optional class to use for loading the palette definition files. If null, loading will occurs from the system current working directory.
directory - The base directory for palette definition files. It may be a Java package if a loader were specified. If null, then "." is assumed.
extension - File name extension, or null if there is no extension to add to filename. If non-null, this extension will be automatically appended to filename. It should starts with the '.' character.
charset - The charset to use for parsing files, or null for the default.
locale - The locale to use for parsing files. or null for the default.
Since:
2.2
Method Detail

getDefault

public static PaletteFactory getDefault()
Gets the default palette factory. The returned factory can provide the palettes listed in the class javadoc, together with the palettes defined by any custom factories registered in the way defined by the class javadoc.
Note: The scan for custom factories is performed only when this method is first invoked. If a new scan is desired (for example because new JAR files are added on the classpath, then scanForPlugins(null) should be invoked explicitly.
If custom factories are found and if they define some palettes of the same name than the ones provided by the build-in factory, then the custom palettes have precedence over the build-in ones.

Returns:
The default palette factory.

scanForPlugins

public static void scanForPlugins(ClassLoader loader)
Lookups for custom palette factories on the classpath. This method is automatically invoked by getDefault() and doesn't need to be invoked explicitly, unless new JAR files have been added on the classpath or unless some specific class loader needs to be used.

Custom factories shall be declared in the following file:

META-INF/services/org.geotoolkit.image.io.PaletteFactory
Newly discovered factories have precedence over the old ones.

Parameters:
loader - The class loader to use, or null for the default one.
Since:
2.4

setWarningLocale

public void setWarningLocale(Locale warningLocale)
Sets the locale to use for formatting warning or error messages. This is typically the image reader locale. This locale is informative only; there is no guarantee that this locale will be really used.

This method sets the locale for the current thread only. It is safe to use this palette factory concurrently in many threads, each with their own locale.

Parameters:
warningLocale - The locale for warning or error messages, or null for the default locale.
Since:
2.4

getWarningLocale

public Locale getWarningLocale()
Returns the locale set by the last invocation to setWarningLocale(Locale) in the current thread.

Returns:
The current locale to use for warning messages.
Since:
2.4

getResourceAsStream

protected InputStream getResourceAsStream(String name)
Returns an input stream for reading the specified resource. The default implementation delegates to either Class or ClassLoader getResourceAsStream(String) method, depending on the type of the loader argument given to the constructor. Subclasses may override this method if a more elaborated mechanism is wanted for fetching resources. This is sometime required in the context of applications using particular class loaders.

Parameters:
name - The name of the resource to load, constructed as directory + name + extension where directory and extension were specified to the constructor, while name was given to the getPalette(java.lang.String, int) method.
Returns:
The input stream, or null if the resources was not found.
Since:
2.3

getAvailableNames

public Set<String> getAvailableNames()
Returns the set of available palette names. Any item in this set can be specified as argument to the getPalette(String, int) method.

If this method can not infer the names of available palettes, then it returns null. Note that this is not the same than an empty set, which means "there is no palette". The null return value means that the set is unknown.

Returns:
The list of available palette name, or null if this method is unable to fetch this information.

getColors

public Color[] getColors(String name)
                  throws IOException
Loads colors from a definition file. If no colors were found in the current palette factory, then the fallback (if any fallback was specified to the constructor) will be queried.

Special case for single color
If the given name starts with the '#' character, then the string is decoded as a color using the Color.decode(String) method (i.e., the string is interpreted as a hexadecimal RGB value) and the decoded color is returned in an array of length 1.

Parameters:
name - The name of the palette to load.
Returns:
The set of colors, or null if no palette was found for the given name.
Throws:
IOException - if an error occurs during reading.

getPalette

public Palette getPalette(String name,
                          int size)
Returns the palette of the specified name and size. The default implementation is equivalents to:
return getPalette(name, 0, size, size, 1, 0);
This method does not test the validity of the name argument. If there is no palette for the given name, then a FileNotFoundException will be thrown by the getter methods in the returned palette.

Parameters:
name - The name of the palette to load.
size - The index color model size.
Returns:
The palette.
Since:
2.4

getPalettePadValueFirst

public Palette getPalettePadValueFirst(String name,
                                       int size)
Returns a palette with a pad value at index 0. The default implementation is equivalents to:
return getPalette(name, 1, size, size, 1, 0);
This method does not test the validity of the name argument. If there is no palette for the given name, then a FileNotFoundException will be thrown by the getter methods in the returned palette.

Parameters:
name - The name of the palette to load.
size - The index color model size.
Returns:
The palette.
Since:
2.4

getPalettePadValueLast

public Palette getPalettePadValueLast(String name,
                                      int size)
Returns a palette with pad value at the last index. The default implementation is equivalents to:
return getPalette(name, 0, size-1, size, 1, 0);
This method does not test the validity of the name argument. If there is no palette for the given name, then a FileNotFoundException will be thrown by the getter methods in the returned palette.

Parameters:
name - The name of the palette to load.
size - The index color model size.
Returns:
The palette.
Since:
2.4

getPalette

public Palette getPalette(String name,
                          int lower,
                          int upper,
                          int size,
                          int numBands,
                          int visibleBand)
Returns the palette of the specified name and size. The RGB colors will be distributed in the range lower inclusive to upper exclusive. Remaining pixel values (if any) will be left to a black or transparent color by default.

This method does not test the validity of the name argument. If there is no palette for the given name, then a FileNotFoundException will be thrown by the getter methods in the returned palette.

Parameters:
name - The name of the palette to load.
lower - Index of the first valid element (inclusive) in the index color model to be created.
upper - Index of the last valid element (exclusive) in the index color model to be created.
size - The size of the index color model to be created. This is the value to be returned by IndexColorModel.getMapSize().
numBands - The number of bands (usually 1).
visibleBand - The band to use for color computations (usually 0).
Returns:
The palette.
Since:
2.4

getContinuousPalette

public Palette getContinuousPalette(String name,
                                    float minimum,
                                    float maximum,
                                    int dataType,
                                    int numBands,
                                    int visibleBand)
Creates a palette suitable for floating point values.

Parameters:
name - The palette name.
minimum - The minimal sample value expected.
maximum - The maximal sample value expected.
dataType - The data type as a DataBuffer.TYPE_INT, DataBuffer.TYPE_FLOAT or DataBuffer.TYPE_DOUBLE constant.
numBands - The number of bands (usually 1).
visibleBand - The band to use for color computations (usually 0).
Returns:
A palette suitable for floating point values.
Since:
2.4
TODO:
Current implementation ignores the name and builds a gray scale in all cases. Future version may improve on that.


Copyright © 2009-2012 Geotoolkit.org. All Rights Reserved.