diff --git a/.classpath b/.classpath index e29121a32..988565a6d 100644 --- a/.classpath +++ b/.classpath @@ -1,102 +1,119 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/build.xml b/build.xml index 95d4ff99f..6c94fe8f4 100644 --- a/build.xml +++ b/build.xml @@ -182,6 +182,7 @@ + diff --git a/lib/imageio-bmp-3.2.jar b/lib/imageio-bmp-3.2.jar new file mode 100755 index 000000000..1946caefe Binary files /dev/null and b/lib/imageio-bmp-3.2.jar differ diff --git a/pom.xml b/pom.xml index a6522d868..ce47d1935 100644 --- a/pom.xml +++ b/pom.xml @@ -430,6 +430,11 @@ icu4j 56.1 + + com.twelvemonkeys.imageio + imageio-bmp + 3.2 + com.twelvemonkeys.imageio imageio-tiff diff --git a/source/net/yacy/document/ImageParser.java b/source/net/yacy/document/ImageParser.java index aaff10437..b66518a37 100644 --- a/source/net/yacy/document/ImageParser.java +++ b/source/net/yacy/document/ImageParser.java @@ -20,82 +20,43 @@ package net.yacy.document; -import java.awt.Container; import java.awt.Image; -import java.awt.MediaTracker; +import java.awt.image.BufferedImage; import java.io.ByteArrayInputStream; import java.io.IOException; import javax.imageio.ImageIO; import net.yacy.cora.util.ConcurrentLog; -import net.yacy.document.parser.images.bmpParser; -import net.yacy.document.parser.images.icoParser; public class ImageParser { - public static final Image parse(final String filename, final byte[] source) { - final MediaTracker mediaTracker = new MediaTracker(new Container()); - Image image = null; - if (((filename.endsWith(".ico")) || (filename.endsWith(".bmp"))) && (bmpParser.isBMP(source))) { - // parse image with BMP parser - image = bmpParser.parse(source).getImage(); - if (image == null) { - if (ConcurrentLog.isFine("IMAGEPARSER")) { - ConcurrentLog.fine("IMAGEPARSER", "IMAGEPARSER.parse : bmpParser failed for " + filename); - } - return null; - } - } else if ((filename.endsWith(".ico")) && (icoParser.isICO(source))) { - // parse image with ICO parser - icoParser icoparser; - try { - icoparser = new icoParser(source); - image = icoparser.getImage(0); - } catch (final Throwable e) { - if (ConcurrentLog.isFine("IMAGEPARSER")) { - ConcurrentLog.fine("IMAGEPARSER", "IMAGEPARSER.parse : could not parse image " + filename, e); - } - } - if (image == null) { - if (ConcurrentLog.isFine("IMAGEPARSER")) { - ConcurrentLog.fine("IMAGEPARSER", "IMAGEPARSER.parse : icoParser failed for " + filename); - } - return null; - } - } else { - try { - image = ImageIO.read(new ByteArrayInputStream(source)); - } catch(IOException e) { - if (ConcurrentLog.isFine("IMAGEPARSER")) { - ConcurrentLog.fine("IMAGEPARSER", "IMAGEPARSER.parse : could not parse image " + filename, e); - } - } - if (image == null) { - if (ConcurrentLog.isFine("IMAGEPARSER")) { - ConcurrentLog.fine("IMAGEPARSER", "IMAGEPARSER.parse : ImageIO failed for " + filename); - } - return null; - } - } - if (image == null) { - return null; - } - - final int handle = image.hashCode(); - mediaTracker.addImage(image, handle); - try { - mediaTracker.waitForID(handle); - - if (mediaTracker.isErrorID(handle)) { // true if status ERRORD during loading (happens on not supported formats too) - mediaTracker.removeImage(image, handle); - image = null; // return null to indicate source not handled - } - } catch (final InterruptedException e) { - return null; - } - - return image; - } + /** + * @param filename source image file url + * @param source image content as bytes + * @return an Image instance parsed from image content bytes, or null if no parser can handle image format or an error occured + */ + public static final Image parse(final String filename, final byte[] source) { + BufferedImage image = null; + try { + image = ImageIO.read(new ByteArrayInputStream(source)); + /* + * With ImageIO.read, image is already loaded as a complete BufferedImage, no need to wait + * full loading with a MediaTracker + */ + } catch (IOException e) { + if (ConcurrentLog.isFine("IMAGEPARSER")) { + ConcurrentLog.fine("IMAGEPARSER", "IMAGEPARSER.parse : could not parse image " + filename, e); + } + } + if (image == null) { + if (ConcurrentLog.isFine("IMAGEPARSER")) { + ConcurrentLog.fine("IMAGEPARSER", "IMAGEPARSER.parse : ImageIO failed for " + filename); + } + return null; + } + + return image; + } } diff --git a/source/net/yacy/document/parser/images/bmpParser.java b/source/net/yacy/document/parser/images/bmpParser.java index 33a3fc9cf..694a83f99 100644 --- a/source/net/yacy/document/parser/images/bmpParser.java +++ b/source/net/yacy/document/parser/images/bmpParser.java @@ -32,8 +32,17 @@ import java.io.IOException; import javax.imageio.ImageIO; +import com.twelvemonkeys.imageio.plugins.bmp.BMPImageReader; + import net.yacy.cora.util.ConcurrentLog; +/** + * + * @deprecated use ImageIO {@link BMPImageReader} from github.com/haraldk/TwelveMonkeys + * library (imageio-bmp-3.2.jar), which as better BMP format + * variants support + */ +@Deprecated public class bmpParser { // this is a implementation of http://de.wikipedia.org/wiki/Windows_Bitmap diff --git a/source/net/yacy/document/parser/images/genericImageParser.java b/source/net/yacy/document/parser/images/genericImageParser.java index 74e04f779..78d645fd9 100644 --- a/source/net/yacy/document/parser/images/genericImageParser.java +++ b/source/net/yacy/document/parser/images/genericImageParser.java @@ -46,6 +46,13 @@ import java.util.Set; import javax.imageio.ImageIO; +import com.drew.imaging.jpeg.JpegMetadataReader; +import com.drew.lang.GeoLocation; +import com.drew.metadata.Directory; +import com.drew.metadata.Metadata; +import com.drew.metadata.Tag; +import com.drew.metadata.exif.GpsDirectory; + import net.yacy.cora.document.id.AnchorURL; import net.yacy.cora.document.id.DigestURL; import net.yacy.cora.document.id.MultiProtocolURL; @@ -55,16 +62,8 @@ import net.yacy.document.Document; import net.yacy.document.Parser; import net.yacy.document.VocabularyScraper; import net.yacy.document.parser.html.ImageEntry; -import net.yacy.document.parser.images.bmpParser.IMAGEMAP; import net.yacy.kelondro.util.FileUtils; -import com.drew.imaging.jpeg.JpegMetadataReader; -import com.drew.lang.GeoLocation; -import com.drew.metadata.Directory; -import com.drew.metadata.Metadata; -import com.drew.metadata.Tag; -import com.drew.metadata.exif.GpsDirectory; - /** * Parser for images, bmp and jpeg and all supported by the Java Image I/O API * by default java ImageIO supports bmp, gif, jpg, jpeg, png, wbmp (tif if jai-imageio is in classpath/registered) @@ -75,11 +74,9 @@ public class genericImageParser extends AbstractParser implements Parser { public genericImageParser() { super("Generic Image Parser"); - SUPPORTED_EXTENSIONS.add("bmp"); SUPPORTED_EXTENSIONS.add("jpe"); // not listed in ImageIO extension but sometimes uses for jpeg SUPPORTED_EXTENSIONS.addAll(Arrays.asList(ImageIO.getReaderFileSuffixes())); - SUPPORTED_MIME_TYPES.add("image/bmp"); SUPPORTED_MIME_TYPES.add("image/jpg"); // this is in fact a 'wrong' mime type. We leave it here because that is a common error that occurs in the internet frequently SUPPORTED_MIME_TYPES.addAll(Arrays.asList(ImageIO.getReaderMIMETypes())); } @@ -102,21 +99,7 @@ public class genericImageParser extends AbstractParser implements Parser { String ext = MultiProtocolURL.getFileExtension(filename); double gpslat = 0; double gpslon = 0; - if (mimeType.equals("image/bmp") || ext.equals("bmp")) { - byte[] b; - try { - b = FileUtils.read(source); - } catch (final IOException e) { - ConcurrentLog.logException(e); - throw new Parser.Failure(e.getMessage(), location); - } - if (bmpParser.isBMP(b)) { - final IMAGEMAP imap = bmpParser.parse(b); - ii = parseJavaImage(location, imap.getImage()); - } else { - throw new Parser.Failure("Not supported by bmpParser", location); - } - } else if (mimeType.equals("image/jpeg") || ext.equals("jpg") || ext.equals("jpeg") || ext.equals("jpe")) { + if (mimeType.equals("image/jpeg") || ext.equals("jpg") || ext.equals("jpeg") || ext.equals("jpe")) { // use the exif parser from // http://www.drewnoakes.com/drewnoakes.com/code/exif/ // javadoc is at: http://www.drewnoakes.com/drewnoakes.com/code/exif/javadoc/ diff --git a/source/net/yacy/document/parser/images/icoParser.java b/source/net/yacy/document/parser/images/icoParser.java index 4ff965387..3036a0219 100644 --- a/source/net/yacy/document/parser/images/icoParser.java +++ b/source/net/yacy/document/parser/images/icoParser.java @@ -32,9 +32,17 @@ import java.io.IOException; import javax.imageio.ImageIO; -import net.yacy.cora.util.ConcurrentLog; +import com.twelvemonkeys.imageio.plugins.bmp.ICOImageReader; +import net.yacy.cora.util.ConcurrentLog; +/** +* +* @deprecated use ImageIO {@link ICOImageReader} from github.com/haraldk/TwelveMonkeys +* library (imageio-bmp-3.2.jar), which as better BMP format +* variants support, and support PNG encoded icons. +*/ +@Deprecated public class icoParser { // this is a implementation of http://msdn2.microsoft.com/en-us/library/ms997538(d=printer).aspx