* @return a populated JPEG 2000 ImageRecord instance
* @throws DjatokaException
*/
public final ImageRecord getMetadata(ImageRecord r) throws DjatokaException {
if (r == null) {
throw new DjatokaException("ImageRecord is null");
}
if (r.getImageFile() == null && r.getObject() != null) {
ImageRecord ir = getMetadata(getStreamFromObject(r.getObject()));
ir.setObject(r.getObject());
return ir;
}
File f = new File(r.getImageFile());
if (!f.exists()) {
throw new DjatokaException("Image Does Not Exist");
}
if (!ImageProcessingUtils.checkIfJp2(r.getImageFile())) {
throw new DjatokaException("Not a JP2 image.");
}
if (f.length() <= 4096) {
FileInputStream fis = null;
// If < 4K bytes, image may be corrupt;
// use safer pure Java Metadata gatherer.
try {
fis = new FileInputStream(f);
return getMetadata(fis);
} catch (Exception e) {
throw new DjatokaException("Invalid file.");
} finally {
info.freelibrary.util.IOUtils.closeQuietly(fis);
}
}
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Checking: java.library.path = {}", System.getProperty("java.library.path"));
}
Jpx_source inputSource = new Jpx_source();
Jp2_family_src jp2_family_in = new Jp2_family_src();
int ref_component = 0;
try {
jp2_family_in.Open(r.getImageFile(), true);
inputSource.Open(jp2_family_in, true);
Kdu_codestream codestream = new Kdu_codestream();
codestream.Create(inputSource.Access_codestream(ref_component).Open_stream());
int minLevels = codestream.Get_min_dwt_levels();
int depth = codestream.Get_bit_depth(ref_component);
int colors = codestream.Get_num_components();
int[] frames = new int[1];
inputSource.Count_compositing_layers(frames);
Kdu_dims image_dims = new Kdu_dims();
codestream.Get_dims(ref_component, image_dims);
Kdu_coords imageSize = image_dims.Access_size();
r.setWidth(imageSize.Get_x());
r.setHeight(imageSize.Get_y());
r.setDWTLevels(minLevels);
int djatokaLevels = ImageProcessingUtils.getLevelCount(r.getWidth(), r.getHeight());
r.setLevels((djatokaLevels > minLevels) ? minLevels : djatokaLevels);
r.setBitDepth(depth);
r.setNumChannels(colors);
r.setCompositingLayerCount(frames[0]);
int[] v = new int[1];
Kdu_params p = codestream.Access_siz().Access_cluster("COD");
if (p != null) {
p.Get(Kdu_global.Clayers, 0, 0, v, true, true, true);
if (v[0] > 0) {
r.setQualityLayers(v[0]);
}
}
if (codestream.Exists()) {
codestream.Destroy();
}
inputSource.Native_destroy();
jp2_family_in.Native_destroy();
} catch (KduException e) {
LOGGER.error(e.getMessage(), e);
throw new DjatokaException(e.getMessage(), e);
}
return r;
}