if ((jfifSegments != null) && (!jfifSegments.isEmpty())) {
jfifSegment = (JfifSegment) jfifSegments.get(0);
}
final List<Segment> app14Segments = readSegments(byteSource, new int[] { JpegConstants.JPEG_APP14_MARKER}, true);
App14Segment app14Segment = null;
if (app14Segments != null && !app14Segments.isEmpty()) {
app14Segment = (App14Segment) app14Segments.get(0);
}
// JfifSegment fTheJFIFSegment = (JfifSegment) findSegment(segments,
// kJFIFMarker);
double xDensity = -1.0;
double yDensity = -1.0;
double unitsPerInch = -1.0;
// int JFIF_major_version;
// int JFIF_minor_version;
String formatDetails;
if (jfifSegment != null) {
xDensity = jfifSegment.xDensity;
yDensity = jfifSegment.yDensity;
final int densityUnits = jfifSegment.densityUnits;
// JFIF_major_version = fTheJFIFSegment.JFIF_major_version;
// JFIF_minor_version = fTheJFIFSegment.JFIF_minor_version;
formatDetails = "Jpeg/JFIF v." + jfifSegment.jfifMajorVersion + "."
+ jfifSegment.jfifMinorVersion;
switch (densityUnits) {
case 0:
break;
case 1: // inches
unitsPerInch = 1.0;
break;
case 2: // cms
unitsPerInch = 2.54;
break;
default:
break;
}
} else {
final JpegImageMetadata metadata = (JpegImageMetadata) getMetadata(
byteSource, params);
if (metadata != null) {
{
final TiffField field = metadata
.findEXIFValue(TiffTagConstants.TIFF_TAG_XRESOLUTION);
if (field != null) {
xDensity = ((Number) field.getValue()).doubleValue();
}
}
{
final TiffField field = metadata
.findEXIFValue(TiffTagConstants.TIFF_TAG_YRESOLUTION);
if (field != null) {
yDensity = ((Number) field.getValue()).doubleValue();
}
}
{
final TiffField field = metadata
.findEXIFValue(TiffTagConstants.TIFF_TAG_RESOLUTION_UNIT);
if (field != null) {
final int densityUnits = ((Number) field.getValue()).intValue();
switch (densityUnits) {
case 1:
break;
case 2: // inches
unitsPerInch = 1.0;
break;
case 3: // cms
unitsPerInch = 2.54;
break;
default:
break;
}
}
}
}
formatDetails = "Jpeg/DCM";
}
int physicalHeightDpi = -1;
float physicalHeightInch = -1;
int physicalWidthDpi = -1;
float physicalWidthInch = -1;
if (unitsPerInch > 0) {
physicalWidthDpi = (int) Math.round(xDensity * unitsPerInch);
physicalWidthInch = (float) (width / (xDensity * unitsPerInch));
physicalHeightDpi = (int) Math.round(yDensity * unitsPerInch);
physicalHeightInch = (float) (height / (yDensity * unitsPerInch));
}
final List<String> comments = new ArrayList<String>();
final List<Segment> commentSegments = readSegments(byteSource,
new int[] { JpegConstants.COM_MARKER}, false);
for (Segment commentSegment : commentSegments) {
final ComSegment comSegment = (ComSegment) commentSegment;
String comment = "";
try {
comment = new String(comSegment.getComment(), "UTF-8");
} catch (final UnsupportedEncodingException cannotHappen) { // NOPMD - can't happen
}
comments.add(comment);
}
final int numberOfComponents = fSOFNSegment.numberOfComponents;
final int precision = fSOFNSegment.precision;
final int bitsPerPixel = numberOfComponents * precision;
final ImageFormat format = ImageFormats.JPEG;
final String formatName = "JPEG (Joint Photographic Experts Group) Format";
final String mimeType = "image/jpeg";
// we ought to count images, but don't yet.
final int numberOfImages = 1;
// not accurate ... only reflects first
final boolean progressive = fSOFNSegment.marker == JpegConstants.SOF2_MARKER;
boolean transparent = false;
final boolean usesPalette = false; // TODO: inaccurate.
// See http://docs.oracle.com/javase/6/docs/api/javax/imageio/metadata/doc-files/jpeg_metadata.html#color
int colorType = ImageInfo.COLOR_TYPE_UNKNOWN;
// Some images have both JFIF/APP0 and APP14.
// JFIF is meant to win but in them APP14 is clearly right, so make it win.
if (app14Segment != null && app14Segment.isAdobeJpegSegment()) {
final int colorTransform = app14Segment.getAdobeColorTransform();
if (colorTransform == App14Segment.ADOBE_COLOR_TRANSFORM_UNKNOWN) {
if (numberOfComponents == 3) {
colorType = ImageInfo.COLOR_TYPE_RGB;
} else if (numberOfComponents == 4) {
colorType = ImageInfo.COLOR_TYPE_CMYK;