Esempio n. 1
0
 /**
  * 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;
 }
Esempio n. 2
0
  /** {@inheritDoc} */
  protected Collection<Element> toXMP(Document xmpDoc, String nsURI, String prefix) {
    Element rdfDescElement = null;
    //
    // First we have to scan through all the metadata values to see if at
    // least one of them is one of the Creator Contact Info values because
    // they need special handling.
    //
    Element cciElement = null;
    for (Iterator<Map.Entry<Integer, ImageMetaValue>> i = iterator(); i.hasNext(); ) {
      final Map.Entry<Integer, ImageMetaValue> me = i.next();
      final int tagID = me.getKey();
      if (tagIsCreatorContactInfo(tagID)) {
        rdfDescElement = XMPUtil.createRDFDescription(xmpDoc, nsURI, prefix);
        cciElement =
            XMLUtil.addElementChildTo(rdfDescElement, nsURI, prefix + ':' + "CreatorContactInfo");
        cciElement.setAttribute(XMP_RDF_PREFIX + ":parseType", "Resource");
        rdfDescElement.appendChild(cciElement);
        break;
      }
    }

    //
    // Now go through the metadata values for real and convert them to XMP.
    // If a given metadata value is one of the Creator Contact Info values,
    // make its parent the Creator Contact Info element rather than the
    // given parent.
    //
    for (Iterator<Map.Entry<Integer, ImageMetaValue>> i = iterator(); i.hasNext(); ) {
      final Map.Entry<Integer, ImageMetaValue> me = i.next();
      final int tagID = me.getKey();
      ImageMetaValue value = me.getValue();

      //
      // Only these tags are encoded as part of IPTC for XMP Core.
      //
      switch (tagID) {
        case IPTC_COUNTRY_CODE:
        case IPTC_INTELLECTUAL_GENRE:
        case IPTC_LOCATION:
        case IPTC_SCENE:
        case IPTC_SUBJECT_CODE:
          break;
        default:
          if (!tagIsCreatorContactInfo(tagID)) continue;
      }

      /*
                  //
                  // In XMP, IPTC time fields are merged into the corresponding date
                  // field.
                  //
                  switch ( tagID ) {
                      case IPTC_DATE_CREATED:
                          value = mergeDateTime( value, IPTC_TIME_CREATED );
                          break;
                      case IPTC_DATE_SENT:
                          value = mergeDateTime( value, IPTC_TIME_SENT );
                          break;
                      case IPTC_DIGITAL_CREATION_DATE:
                          value = mergeDateTime( value, IPTC_DIGITAL_CREATION_TIME );
                          break;
                      case IPTC_EXPIRATION_DATE:
                          value = mergeDateTime( value, IPTC_EXPIRATION_TIME );
                          break;
                      case IPTC_RELEASE_DATE:
                          value = mergeDateTime( value, IPTC_RELEASE_TIME );
                          break;
                      case IPTC_DIGITAL_CREATION_TIME:
                      case IPTC_EXPIRATION_TIME:
                      case IPTC_RELEASE_TIME:
                      case IPTC_TIME_CREATED:
                      case IPTC_TIME_SENT:
                          continue;
                  }

      */
      final Element valueElement = value.toXMP(xmpDoc, nsURI, prefix);
      if (valueElement != null) {
        final Element parent;
        if (tagIsCreatorContactInfo(tagID)) parent = cciElement;
        else {
          if (rdfDescElement == null)
            rdfDescElement = XMPUtil.createRDFDescription(xmpDoc, nsURI, prefix);
          parent = rdfDescElement;
        }
        //noinspection ConstantConditions
        parent.appendChild(valueElement);
      }
    }
    if (rdfDescElement != null) {
      final Collection<Element> elements = new ArrayList<Element>(1);
      elements.add(rdfDescElement);
      return elements;
    }
    return null;
  }