public class ImageProvider extends Object
ImageIcon icon = new ImageProvider(name).setMaxSize(ImageSizes.MAP).get();
(there are more options, see below)
short form:
ImageIcon icon = ImageProvider.get(name);
Modifier and Type | Class and Description |
---|---|
static interface |
ImageProvider.ImageCallback
Callback interface for asynchronous image loading.
|
static interface |
ImageProvider.ImageResourceCallback
Callback interface for asynchronous image loading (with delayed scaling possibility).
|
static class |
ImageProvider.ImageSizes
Supported image sizes
|
static class |
ImageProvider.ImageType
Supported image types
|
static class |
ImageProvider.OverlayPosition
Position of an overlay icon
|
private static class |
ImageProvider.SAXReturnException
Quit parsing, when a certain condition is met
|
Modifier and Type | Field and Description |
---|---|
protected Collection<ClassLoader> |
additionalClassLoaders
list of class loaders to take images from
|
protected File |
archive
archive file to take image from
|
private static Map<String,ImageResource> |
cache
The icon cache
|
private static Pattern |
dataUrlPattern
data:[<mediatype>][;base64],<data> |
(package private) static double |
DEGREE_90
90 degrees in radians units
|
protected Collection<String> |
dirs
directories in which images are searched
|
protected int |
height
height of the resulting image, -1 when original image data should be used
|
private static String |
HTTP_PROTOCOL |
private static String |
HTTPS_PROTOCOL |
protected String |
id
caching identifier
|
private static ExecutorService |
IMAGE_FETCHER |
protected String |
inArchiveDir
directory inside the archive
|
protected int |
maxHeight
maximum height of the resulting image, -1 for no restriction
|
protected int |
maxWidth
maximum width of the resulting image, -1 for no restriction
|
protected String |
name
image file name
|
protected boolean |
optional
In case of errors do not throw exception but return
null for missing image |
protected List<ImageOverlay> |
overlayInfo
ordered list of overlay images
|
static String |
PROP_TRANSPARENCY_COLOR
Property set on
BufferedImage returned by read(java.io.File, boolean, boolean) if metadata is required. |
static String |
PROP_TRANSPARENCY_FORCED
Property set on
BufferedImage returned by makeImageTransparent(java.awt.image.BufferedImage, java.awt.Color) . |
private static Map<Image,Map<Long,ImageResource>> |
ROTATE_CACHE
Caches the image data for rotated versions of the same image.
|
protected String |
subdir
sub directory the image can be found in
|
protected boolean |
suppressWarnings
true if warnings should be suppressed |
private static SVGUniverse |
svgUniverse |
protected int |
width
width of the resulting image, -1 when original image data should be used
|
private static String |
WIKI_PROTOCOL |
Constructor and Description |
---|
ImageProvider(ImageProvider image)
Constructs a new
ImageProvider from an existing one. |
ImageProvider(String name)
Constructs a new
ImageProvider from a filename. |
ImageProvider(String subdir,
String name)
Constructs a new
ImageProvider from a filename in a given directory. |
Modifier and Type | Method and Description |
---|---|
ImageProvider |
addOverlay(ImageOverlay overlay)
Add an overlay over the image.
|
static Image |
createBoundedImage(Image img,
int maxSize)
Creates a scaled down version of the input image to fit maximum dimensions.
|
static BufferedImage |
createImageFromSvg(SVGDiagram svg,
Dimension dim)
Constructs an image from the given SVG data.
|
static Image |
createRotatedImage(Image img,
double rotatedAngle)
Creates a rotated version of the input image.
|
static Image |
createRotatedImage(Image img,
double rotatedAngle,
Dimension dimension)
Creates a rotated version of the input image, scaled to the given dimension.
|
ImageIcon |
get()
Execute the image request and scale result.
|
static ImageIcon |
get(OsmPrimitiveType type)
Replies the icon for an OSM primitive type
|
static ImageIcon |
get(String name)
Load an image with a given file name.
|
static ImageIcon |
get(String subdir,
String name)
Load an image with a given file name.
|
static Cursor |
getCursor(String name,
String overlay)
Load a cursor with a given file name, optionally decorated with an overlay image.
|
static ImageIcon |
getIfAvailable(String name)
Load an image with a given file name, but do not throw an exception
when the image cannot be found.
|
static ImageIcon |
getIfAvailable(String subdir,
String name)
Load an image with a given file name, but do not throw an exception
when the image cannot be found.
|
private static ImageResource |
getIfAvailableDataUrl(String url)
Internal implementation of the image request for inline images (data: urls).
|
private static ImageResource |
getIfAvailableHttp(String url,
ImageProvider.ImageType type)
Internal implementation of the image request for URL's.
|
private ImageResource |
getIfAvailableImpl(Collection<ClassLoader> additionalClassLoaders)
Internal implementation of the image request.
|
private static ImageResource |
getIfAvailableLocalURL(URL path,
ImageProvider.ImageType type)
Internal implementation of the image request for local images.
|
private static ImageResource |
getIfAvailableWiki(String name,
ImageProvider.ImageType type)
Internal implementation of the image request for wiki images.
|
private static ImageResource |
getIfAvailableZip(String fullName,
File archive,
String inArchiveDir,
ImageProvider.ImageType type)
Internal implementation of the image request for images in Zip archives.
|
static Dimension |
getImageSizes(ImageProvider.ImageSizes size)
Convert enumerated size values to real numbers
|
private static URL |
getImageUrl(String imageName,
Collection<String> dirs,
Collection<ClassLoader> additionalClassLoaders) |
private static URL |
getImageUrl(String path,
String name,
Collection<ClassLoader> additionalClassLoaders) |
private static String |
getImgUrlFromWikiInfoPage(String base,
String fn)
Reads the wiki page on a certain file in html format in order to find the real image URL.
|
void |
getInBackground(ImageProvider.ImageCallback callback)
Load the image in a background thread.
|
void |
getInBackground(ImageProvider.ImageResourceCallback callback)
Load the image in a background thread.
|
ImageResource |
getResource()
Execute the image request.
|
private static SVGUniverse |
getSvgUniverse() |
static Color |
getTransparentColor(ColorModel model,
ImageReader reader)
Returns the
TransparentColor defined in image reader metadata. |
static boolean |
hasTransparentColor(BufferedImage bi)
Determines if the given
BufferedImage has a transparent color determiend by a previous call to read(java.io.File, boolean, boolean) . |
static boolean |
isTransparencyForced(BufferedImage bi)
Determines if the transparency of the given
BufferedImage has been enforced by a previous call to makeImageTransparent(java.awt.image.BufferedImage, java.awt.Color) . |
static BufferedImage |
makeImageTransparent(BufferedImage bi,
Color color)
Returns a transparent version of the given image, based on the given transparent color.
|
static ImageIcon |
overlay(Icon ground,
Icon overlay,
ImageProvider.OverlayPosition pos)
Deprecated.
this method will be refactored
|
private static Color |
parseRGB(String[] s) |
static BufferedImage |
read(File input,
boolean readMetadata,
boolean enforceTransparency)
Returns a
BufferedImage as the result of decoding
a supplied File with an ImageReader
chosen automatically from among those currently registered. |
static BufferedImage |
read(ImageInputStream stream,
boolean readMetadata,
boolean enforceTransparency)
Returns a
BufferedImage as the result of decoding
a supplied ImageInputStream with an
ImageReader chosen automatically from among those
currently registered. |
static BufferedImage |
read(InputStream input,
boolean readMetadata,
boolean enforceTransparency)
Returns a
BufferedImage as the result of decoding
a supplied InputStream with an ImageReader
chosen automatically from among those currently registered. |
static BufferedImage |
read(URL input,
boolean readMetadata,
boolean enforceTransparency)
Returns a
BufferedImage as the result of decoding
a supplied URL with an ImageReader
chosen automatically from among those currently registered. |
ImageProvider |
resetMaxSize(Dimension maxSize)
Limit the maximum size of the image.
|
ImageProvider |
setAdditionalClassLoaders(Collection<ClassLoader> additionalClassLoaders)
Add a collection of additional class loaders to search image for.
|
ImageProvider |
setArchive(File archive)
Specify a zip file where the image is located.
|
ImageProvider |
setDirs(Collection<String> dirs)
Directories to look for the image.
|
ImageProvider |
setHeight(int height)
Set image height
|
ImageProvider |
setId(String id)
Set an id used for caching.
|
ImageProvider |
setInArchiveDir(String inArchiveDir)
Specify a base path inside the zip file.
|
ImageProvider |
setMaxHeight(int maxHeight)
Limit the maximum height of the image.
|
ImageProvider |
setMaxSize(Dimension maxSize)
Limit the maximum size of the image.
|
ImageProvider |
setMaxSize(ImageProvider.ImageSizes size)
Limit the maximum size of the image.
|
ImageProvider |
setMaxSize(int maxSize)
Convenience method, see
setMaxSize(Dimension) . |
ImageProvider |
setMaxWidth(int maxWidth)
Limit the maximum width of the image.
|
ImageProvider |
setOptional(boolean optional)
Decide, if an exception should be thrown, when the image cannot be located.
|
ImageProvider |
setSize(Dimension size)
Set the dimensions of the image.
|
ImageProvider |
setSize(ImageProvider.ImageSizes size)
Set the dimensions of the image.
|
ImageProvider |
setSuppressWarnings(boolean suppressWarnings)
Suppresses warning on the command line in case the image cannot be found.
|
ImageProvider |
setWidth(int width)
Set image width
|
private static final String HTTP_PROTOCOL
private static final String HTTPS_PROTOCOL
private static final String WIKI_PROTOCOL
public static String PROP_TRANSPARENCY_FORCED
BufferedImage
returned by makeImageTransparent(java.awt.image.BufferedImage, java.awt.Color)
.public static String PROP_TRANSPARENCY_COLOR
BufferedImage
returned by read(java.io.File, boolean, boolean)
if metadata is required.protected Collection<String> dirs
protected String inArchiveDir
protected int width
protected int height
protected int maxWidth
protected int maxHeight
protected boolean optional
null
for missing imageprotected boolean suppressWarnings
true
if warnings should be suppressedprotected Collection<ClassLoader> additionalClassLoaders
protected List<ImageOverlay> overlayInfo
private static SVGUniverse svgUniverse
private static final Map<String,ImageResource> cache
private static final Map<Image,Map<Long,ImageResource>> ROTATE_CACHE
private static final ExecutorService IMAGE_FETCHER
private static final Pattern dataUrlPattern
data:[<mediatype>][;base64],<data>
static final double DEGREE_90
public ImageProvider(String subdir, String name)
ImageProvider
from a filename in a given directory.subdir
- subdirectory the image lies inname
- the name of the image. If it does not end with '.png' or '.svg',
both extensions are tried.public ImageProvider(String name)
ImageProvider
from a filename.name
- the name of the image. If it does not end with '.png' or '.svg',
both extensions are tried.public ImageProvider(ImageProvider image)
ImageProvider
from an existing one.image
- the existing image provider to be copiedpublic ImageProvider setDirs(Collection<String> dirs)
dirs
- The directories to look for.public ImageProvider setId(String id)
id
- the id for the cached imagepublic ImageProvider setArchive(File archive)
public ImageProvider setInArchiveDir(String inArchiveDir)
inArchiveDir
- path inside the archivepublic ImageProvider addOverlay(ImageOverlay overlay)
overlay
- overlay image and placement specificationpublic static Dimension getImageSizes(ImageProvider.ImageSizes size)
size
- the size enumerationpublic ImageProvider setSize(Dimension size)
size
- final dimensions of the imagepublic ImageProvider setSize(ImageProvider.ImageSizes size)
size
- final dimensions of the imagepublic ImageProvider setWidth(int width)
width
- final width of the imagesetSize(java.awt.Dimension)
public ImageProvider setHeight(int height)
height
- final height of the imagesetSize(java.awt.Dimension)
public ImageProvider setMaxSize(Dimension maxSize)
maxSize
- maximum image sizepublic ImageProvider resetMaxSize(Dimension maxSize)
maxSize
- maximum image sizesetMaxSize(Dimension)
public ImageProvider setMaxSize(ImageProvider.ImageSizes size)
size
- maximum image sizepublic ImageProvider setMaxSize(int maxSize)
setMaxSize(Dimension)
.maxSize
- maximum image sizepublic ImageProvider setMaxWidth(int maxWidth)
maxWidth
- maximum image widthsetMaxSize(java.awt.Dimension)
public ImageProvider setMaxHeight(int maxHeight)
maxHeight
- maximum image heightsetMaxSize(java.awt.Dimension)
public ImageProvider setOptional(boolean optional)
optional
- true, if JOSM should not throw a RuntimeException
in case the image cannot be located.public ImageProvider setSuppressWarnings(boolean suppressWarnings)
suppressWarnings
- if true
warnings are suppressedpublic ImageProvider setAdditionalClassLoaders(Collection<ClassLoader> additionalClassLoaders)
additionalClassLoaders
- class loaders to add to the internal listpublic ImageIcon get()
public ImageResource getResource()
public void getInBackground(ImageProvider.ImageCallback callback)
callback
- a callback. It is called, when the image is ready.
This can happen before the call to this method returns or it may be
invoked some time (seconds) later. If no image is available, a null
value is returned to callback (just like get()
).public void getInBackground(ImageProvider.ImageResourceCallback callback)
callback
- a callback. It is called, when the image is ready.
This can happen before the call to this method returns or it may be
invoked some time (seconds) later. If no image is available, a null
value is returned to callback (just like get()
).public static ImageIcon get(String subdir, String name)
subdir
- subdirectory the image lies inname
- The icon name (base name with or without '.png' or '.svg' extension)RuntimeException
- if the image cannot be locatedpublic static ImageIcon get(String name)
name
- The icon name (base name with or without '.png' or '.svg' extension)get(String, String)
public static ImageIcon getIfAvailable(String subdir, String name)
subdir
- subdirectory the image lies inname
- The icon name (base name with or without '.png' or '.svg' extension)get(String, String)
public static ImageIcon getIfAvailable(String name)
name
- The icon name (base name with or without '.png' or '.svg' extension)getIfAvailable(String, String)
private ImageResource getIfAvailableImpl(Collection<ClassLoader> additionalClassLoaders)
additionalClassLoaders
- the list of class loaders to useprivate static ImageResource getIfAvailableHttp(String url, ImageProvider.ImageType type)
url
- URL of the imagetype
- data type of the imageprivate static ImageResource getIfAvailableDataUrl(String url)
url
- the data URL for image extractionprivate static ImageResource getIfAvailableWiki(String name, ImageProvider.ImageType type)
name
- image file nametype
- data type of the imageprivate static ImageResource getIfAvailableZip(String fullName, File archive, String inArchiveDir, ImageProvider.ImageType type)
fullName
- image file namearchive
- the archive to get image frominArchiveDir
- directory of the image inside the archive or null
type
- data type of the imageprivate static ImageResource getIfAvailableLocalURL(URL path, ImageProvider.ImageType type)
path
- image file pathtype
- data type of the imageprivate static URL getImageUrl(String path, String name, Collection<ClassLoader> additionalClassLoaders)
private static URL getImageUrl(String imageName, Collection<String> dirs, Collection<ClassLoader> additionalClassLoaders)
private static String getImgUrlFromWikiInfoPage(String base, String fn)
base
- base URL for Wiki imagefn
- filename of the Wiki imagepublic static Cursor getCursor(String name, String overlay)
name
- the cursor image filename in "cursor" directoryoverlay
- optional overlay image@Deprecated public static ImageIcon overlay(Icon ground, Icon overlay, ImageProvider.OverlayPosition pos)
ground
- the base imageoverlay
- the overlay image (can be smaller than the base image)pos
- position of the overlay image inside the base image (positioned
in one of the corners)public static Image createRotatedImage(Image img, double rotatedAngle)
img
- the image to be rotated.rotatedAngle
- the rotated angle, in degree, clockwise. It could be any double but we
will mod it with 360 before using it. More over for caching performance, it will be rounded to
an entire value between 0 and 360.public static Image createRotatedImage(Image img, double rotatedAngle, Dimension dimension)
img
- the image to be rotated.rotatedAngle
- the rotated angle, in degree, clockwise. It could be any double but we
will mod it with 360 before using it. More over for caching performance, it will be rounded to
an entire value between 0 and 360.dimension
- The requested dimensions. Use (-1,-1) for the original size
and (width, -1) to set the width, but otherwise scale the image proportionally.public static Image createBoundedImage(Image img, int maxSize)
img
- the image to be scaled down.maxSize
- the maximum size in pixels (both for width and height)public static ImageIcon get(OsmPrimitiveType type)
type
- the typepublic static BufferedImage createImageFromSvg(SVGDiagram svg, Dimension dim)
svg
- the SVG datadim
- the desired image dimensionprivate static SVGUniverse getSvgUniverse()
public static BufferedImage read(File input, boolean readMetadata, boolean enforceTransparency) throws IOException
BufferedImage
as the result of decoding
a supplied File
with an ImageReader
chosen automatically from among those currently registered.
The File
is wrapped in an
ImageInputStream
. If no registered
ImageReader
claims to be able to read the
resulting stream, null
is returned.
The current cache settings from getUseCache
and
getCacheDirectory
will be used to control caching in the
ImageInputStream
that is created.
Note that there is no read
method that takes a
filename as a String
; use this method instead after
creating a File
from the filename.
This method does not attempt to locate
ImageReader
s that can read directly from a
File
; that may be accomplished using
IIORegistry
and ImageReaderSpi
.
input
- a File
to read from.readMetadata
- if true
, makes sure to read image metadata to detect transparency color, if any.
In that case the color can be retrieved later through PROP_TRANSPARENCY_COLOR
.
Always considered true
if enforceTransparency
is also true
enforceTransparency
- if true
, makes sure to read image metadata and, if the image does not
provide an alpha channel but defines a TransparentColor
metadata node, that the resulting image
has a transparency set to TRANSLUCENT
and uses the correct transparent color.BufferedImage
containing the decoded
contents of the input, or null
.IllegalArgumentException
- if input
is null
.IOException
- if an error occurs during reading.BufferedImage.getProperty(java.lang.String, java.awt.image.ImageObserver)
public static BufferedImage read(InputStream input, boolean readMetadata, boolean enforceTransparency) throws IOException
BufferedImage
as the result of decoding
a supplied InputStream
with an ImageReader
chosen automatically from among those currently registered.
The InputStream
is wrapped in an
ImageInputStream
. If no registered
ImageReader
claims to be able to read the
resulting stream, null
is returned.
The current cache settings from getUseCache
and
getCacheDirectory
will be used to control caching in the
ImageInputStream
that is created.
This method does not attempt to locate
ImageReader
s that can read directly from an
InputStream
; that may be accomplished using
IIORegistry
and ImageReaderSpi
.
This method does not close the provided
InputStream
after the read operation has completed;
it is the responsibility of the caller to close the stream, if desired.
input
- an InputStream
to read from.readMetadata
- if true
, makes sure to read image metadata to detect transparency color for non translucent images, if any.
In that case the color can be retrieved later through PROP_TRANSPARENCY_COLOR
.
Always considered true
if enforceTransparency
is also true
enforceTransparency
- if true
, makes sure to read image metadata and, if the image does not
provide an alpha channel but defines a TransparentColor
metadata node, that the resulting image
has a transparency set to TRANSLUCENT
and uses the correct transparent color.BufferedImage
containing the decoded
contents of the input, or null
.IllegalArgumentException
- if input
is null
.IOException
- if an error occurs during reading.public static BufferedImage read(URL input, boolean readMetadata, boolean enforceTransparency) throws IOException
BufferedImage
as the result of decoding
a supplied URL
with an ImageReader
chosen automatically from among those currently registered. An
InputStream
is obtained from the URL
,
which is wrapped in an ImageInputStream
. If no
registered ImageReader
claims to be able to read
the resulting stream, null
is returned.
The current cache settings from getUseCache
and
getCacheDirectory
will be used to control caching in the
ImageInputStream
that is created.
This method does not attempt to locate
ImageReader
s that can read directly from a
URL
; that may be accomplished using
IIORegistry
and ImageReaderSpi
.
input
- a URL
to read from.readMetadata
- if true
, makes sure to read image metadata to detect transparency color for non translucent images, if any.
In that case the color can be retrieved later through PROP_TRANSPARENCY_COLOR
.
Always considered true
if enforceTransparency
is also true
enforceTransparency
- if true
, makes sure to read image metadata and, if the image does not
provide an alpha channel but defines a TransparentColor
metadata node, that the resulting image
has a transparency set to TRANSLUCENT
and uses the correct transparent color.BufferedImage
containing the decoded
contents of the input, or null
.IllegalArgumentException
- if input
is null
.IOException
- if an error occurs during reading.public static BufferedImage read(ImageInputStream stream, boolean readMetadata, boolean enforceTransparency) throws IOException
BufferedImage
as the result of decoding
a supplied ImageInputStream
with an
ImageReader
chosen automatically from among those
currently registered. If no registered
ImageReader
claims to be able to read the stream,
null
is returned.
Unlike most other methods in this class, this method does
close the provided ImageInputStream
after the read
operation has completed, unless null
is returned,
in which case this method does not close the stream.
stream
- an ImageInputStream
to read from.readMetadata
- if true
, makes sure to read image metadata to detect transparency color for non translucent images, if any.
In that case the color can be retrieved later through PROP_TRANSPARENCY_COLOR
.
Always considered true
if enforceTransparency
is also true
enforceTransparency
- if true
, makes sure to read image metadata and, if the image does not
provide an alpha channel but defines a TransparentColor
metadata node, that the resulting image
has a transparency set to TRANSLUCENT
and uses the correct transparent color.BufferedImage
containing the decoded
contents of the input, or null
.IllegalArgumentException
- if stream
is null
.IOException
- if an error occurs during reading.public static Color getTransparentColor(ColorModel model, ImageReader reader) throws IOException
TransparentColor
defined in image reader metadata.model
- The image color modelreader
- The image readerTransparentColor
defined in image reader metadata, or null
IOException
- if an error occurs during readingpublic static BufferedImage makeImageTransparent(BufferedImage bi, Color color)
bi
- The image to convertcolor
- The transparent colorbi
where all pixels of the given color are transparent.
This resulting image has also the special property PROP_TRANSPARENCY_FORCED
set to color
BufferedImage.getProperty(java.lang.String, java.awt.image.ImageObserver)
,
isTransparencyForced(java.awt.image.BufferedImage)
public static boolean isTransparencyForced(BufferedImage bi)
BufferedImage
has been enforced by a previous call to makeImageTransparent(java.awt.image.BufferedImage, java.awt.Color)
.bi
- The BufferedImage
to testtrue
if the transparency of bi
has been enforced by a previous call to makeImageTransparent
.makeImageTransparent(java.awt.image.BufferedImage, java.awt.Color)
public static boolean hasTransparentColor(BufferedImage bi)
BufferedImage
has a transparent color determiend by a previous call to read(java.io.File, boolean, boolean)
.bi
- The BufferedImage
to testtrue
if bi
has a transparent color determined by a previous call to read
.read(java.io.File, boolean, boolean)