/** * Calculate the size of this IPTC directory if if were to be encoded inside a JPEG image. * * @return Returns the size in bytes. */ private int calcEncodedIPTCSize() { int size = 0; for (Iterator<Map.Entry<Integer, ImageMetaValue>> i = iterator(); i.hasNext(); ) { final Map.Entry<Integer, ImageMetaValue> me = i.next(); final ImageMetaValue imValue = me.getValue(); switch (imValue.getType()) { case META_SBYTE: case META_UBYTE: size += IPTC_ENTRY_HEADER_SIZE + 1; break; case META_DATE: size += IPTC_ENTRY_HEADER_SIZE + IPTC_DATE_SIZE; break; case META_SSHORT: case META_USHORT: size += IPTC_ENTRY_HEADER_SIZE + IPTC_SHORT_SIZE; break; case META_STRING: for (String s : imValue.getValues()) { try { final byte[] b = s.getBytes("ISO-8859-1"); size += IPTC_ENTRY_HEADER_SIZE + b.length; } catch (UnsupportedEncodingException e) { throw new IllegalStateException(e); } } break; default: throw new IllegalStateException(); } } return size; }
/** {@inheritDoc} */ public String valueToString(ImageMetaValue value) { switch (value.getOwningTagID()) { case OLYMPUS_BLUE_BALANCE: case OLYMPUS_RED_BALANCE: return TextUtil.tenths(value.getFloatValue() / 256); case OLYMPUS_CS_CUSTOM_SATURATION: { final String model = getOwningMetadata().getCameraMake(true); if (model != null && model.contains("E-1")) { final long[] n = ((LongMetaValue) value).getLongValues(); n[0] -= n[1]; n[2] -= n[1]; return n[0] + " (0," + n[2] + ')'; } // no break; } case OLYMPUS_CS_CONTRAST_SETTING: case OLYMPUS_CS_PM_CONTRAST: case OLYMPUS_CS_PM_SATURATION: case OLYMPUS_CS_PM_SHARPNESS: case OLYMPUS_CS_SHARPNESS_SETTING: { final String[] values = value.getValues(); if (values.length != 3) return null; return values[0] + " (" + values[1] + ',' + values[2] + ')'; } case OLYMPUS_CS_PANORAMA_MODE: { if (value.getValueCount() != 2) break; final int tagID = value.getOwningTagID(); return getTagValueLabelFor(tagID, value.getIntValue()) + ", " + value.getValues()[1]; } case OLYMPUS_FOCAL_PLANE_DIAGONAL: return value.getStringValue() + "mm"; // TODO: localize } return super.valueToString(value); }
/** * Encode an {@link IPTCDirectory}'s values into a {@link ByteBuffer} suitable for writing into a * JPEG image file. * * @param includePhotoshopHeader If <code>true</code>, encode the Photoshop header as is needed in * JPEG files. * @return Returns said {@link ByteBuffer}. */ private ByteBuffer encodeImpl(boolean includePhotoshopHeader) { final int encodedIPTCSize = calcEncodedIPTCSize(); int bufSize = encodedIPTCSize; ////////// Encode Photoshop IPTC header, if wanted //////////////////// if (includePhotoshopHeader) bufSize += IPTC_JPEG_HEADER_SIZE; // // Ensure the buffer size is padded to be an even number of bytes. // final ByteBuffer buf = ByteBuffer.allocate(bufSize + (bufSize & 1)); if (includePhotoshopHeader) { ByteBufferUtil.put(buf, PHOTOSHOP_3_IDENT, "ASCII"); buf.put((byte) 0); ByteBufferUtil.put(buf, PHOTOSHOP_CREATOR_CODE, "ASCII"); buf.putShort(PHOTOSHOP_IPTC_RESOURCE_ID); buf.putShort((short) 0); // resource name (empty) buf.putInt(encodedIPTCSize); } ////////// Now encode all the IPTC tags /////////////////////////////// final Integer[] tagIDs = getTagIDSet(false).toArray(new Integer[] {null}); Arrays.sort(tagIDs); for (int tagID : tagIDs) { if (!tagIsIIM(tagID)) { // // Non-IIM tags can't be encoded into binary form. // continue; } final ImageMetaValue imValue = getValue(tagID); switch (imValue.getType()) { case META_SBYTE: case META_UBYTE: { encodeTag(buf, tagID); buf.putShort((short) 1); buf.put(imValue.getStringValue().getBytes()[0]); break; } case META_DATE: { encodeTag(buf, tagID); String date = imValue.getStringValue(); date = date.substring(0, 4) + // YYYY date.substring(5, 7) + // MM date.substring(8, 10); // DD encodeString(buf, date); break; } case META_SSHORT: case META_USHORT: { encodeTag(buf, tagID); buf.putShort((short) 2); buf.putShort(imValue.getShortValue()); break; } case META_STRING: { for (String s : imValue.getValues()) { encodeTag(buf, tagID); encodeString(buf, s); } break; } default: throw new IllegalStateException(); } } if ((bufSize & 1) != 0) { // // The number of encoded bytes is odd: pad to make even. // buf.put((byte) 0); } return buf; }