+ /**
+ * Decode an image with OpenIMAJ (which handles CMYK JPEGs and other
+ * exotic variants that plain JDK ImageIO chokes on).
+ *
+ * Files larger than {@link #OPENIMAJ_STREAM_MARK_LIMIT} are decoded
+ * directly with JDK {@link ImageIO#read(File)}, because OpenIMAJ cannot
+ * handle them: its {@code ExtendedImageIO.read()} marks the input stream
+ * with a 100 MB read-ahead limit, Apache Sanselan then scans the entire
+ * file while sniffing the format, the mark gets invalidated and every
+ * subsequent {@code reset()} fails with
+ * {@code IOException: Resetting to invalid mark}.
+ * {@code ImageIO.read(File)} uses a random-access
+ * {@code FileImageInputStream} instead of mark/reset on the raw stream,
+ * so it decodes such files without trouble.
+ *
+ * For smaller files OpenIMAJ stays the primary decoder, with the same
+ * JDK ImageIO fallback in case it still fails.
+ */
+ private static BufferedImage loadWithOpenImaj(final File file)
+ throws IOException {
+ if (file.length() > OPENIMAJ_STREAM_MARK_LIMIT)
+ return ImageIO.read(file);
+
+ try {
+ return createBufferedImage(readMBF(file));
+ } catch (final IOException e) {
+ System.out.println("OpenIMAJ failed to decode \""
+ + file.getName() + "\" (" + e.getMessage()
+ + "), retrying with JDK ImageIO.");
+ return ImageIO.read(file);
+ }
+ }
+