<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml" lang="en"><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/><link rel="stylesheet" href="../../jacoco-resources/report.css" type="text/css"/><link rel="shortcut icon" href="../../jacoco-resources/report.gif" type="image/gif"/><title>EXIF.java</title><link rel="stylesheet" href="../../jacoco-resources/prettify.css" type="text/css"/><script type="text/javascript" src="../../jacoco-resources/prettify.js"></script></head><body onload="window['PR_TAB_WIDTH']=4;prettyPrint()"><div class="breadcrumb" id="breadcrumb"><span class="info"><a href="../../jacoco-sessions.html" class="el_session">Sessions</a></span><a href="../../index.html" class="el_report">Mistral Examples HistogramViewer</a> > <a href="../index.html" class="el_bundle">image-core</a> > <a href="index.source.html" class="el_package">it.tidalwave.image.metadata</a> > <span class="el_source">EXIF.java</span></div><h1>EXIF.java</h1><pre class="source lang-java linenums">/*
* *********************************************************************************************************************
*
* Mistral: open source imaging engine
* http://tidalwave.it/projects/mistral
*
* Copyright (C) 2003 - 2023 by Tidalwave s.a.s. (http://tidalwave.it)
*
* *********************************************************************************************************************
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*
* *********************************************************************************************************************
*
* git clone https://bitbucket.org/tidalwave/mistral-src
* git clone https://github.com/tidalwave-it/mistral-src
*
* *********************************************************************************************************************
*/
package it.tidalwave.image.metadata;
import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;
import java.time.Instant;
import java.time.temporal.ChronoUnit;
import java.util.Optional;
import java.awt.color.ICC_Profile;
import lombok.extern.slf4j.Slf4j;
/***********************************************************************************************************************
*
* @author Fabrizio Giudici
*
**********************************************************************************************************************/
<span class="fc" id="L42">@Slf4j</span>
public class EXIF extends EXIFDirectoryGenerated
{
private static final long serialVersionUID = 3088068666726854799L;
private static final String ASCII_PREFIX = "ASCII\u0000\u0000\u0000";
/*******************************************************************************************************************
*
*
******************************************************************************************************************/
public EXIF()
<span class="fc" id="L54"> {</span>
<span class="fc" id="L55"> }</span>
/*******************************************************************************************************************
*
*
******************************************************************************************************************/
public EXIF (final Instant latestModificationTime)
{
<span class="nc" id="L63"> super(latestModificationTime);</span>
<span class="nc" id="L64"> }</span>
/*******************************************************************************************************************
*
* FIXME: this conversion could be generically be implemented in getObject().
this conversion could be generically be implemented in getObject().
*
******************************************************************************************************************/
@Override @Nonnull
public Optional<int[]> getBitsPerSample()
{
<span class="nc" id="L74"> Object object = getRaw(C_BITS_PER_SAMPLE);</span>
<span class="nc bnc" id="L76" title="All 2 branches missed."> if (object instanceof short[])</span>
{
<span class="nc" id="L78"> final var shorts = (short[])object;</span>
<span class="nc" id="L79"> final var result = new int[shorts.length];</span>
<span class="nc bnc" id="L81" title="All 2 branches missed."> for (var i = 0; i < shorts.length; i++)</span>
{
<span class="nc" id="L83"> result[i] = shorts[i];</span>
}
<span class="nc" id="L86"> object = result;</span>
}
<span class="nc" id="L89"> return Optional.ofNullable((int[])object);</span>
}
/*******************************************************************************************************************
*
* The specification says it's an UNDEFINED[1], so some implementations
* return an array of one byte instead of a single byte. This would cause
* a ClassCastException in the generated code.
*
******************************************************************************************************************/
@Override @Nonnull
public Optional<FileSource> getFileSource()
{
<span class="fc" id="L102"> Object object = getRaw(C_FILE_SOURCE);</span>
<span class="pc bpc" id="L104" title="1 of 2 branches missed."> if (object instanceof byte[])</span>
{
<span class="fc" id="L106"> object = FileSource.fromInteger(((byte[])object)[0]);</span>
}
<span class="nc bnc" id="L109" title="All 2 branches missed."> else if (object instanceof Integer)</span>
{
<span class="nc" id="L111"> object = FileSource.fromInteger(((Integer)object));</span>
}
<span class="fc" id="L114"> return Optional.ofNullable((FileSource)object);</span>
}
/*******************************************************************************************************************
*
* The specification says it's an UNDEFINED[1], so some implementations
* return an array of one byte instead of a single byte. This would cause
* a ClassCastException in the generated code.
*
******************************************************************************************************************/
@Override @Nonnull
public Optional<SceneType> getSceneType()
{
<span class="fc" id="L127"> Object object = getRaw(C_SCENE_TYPE);</span>
<span class="pc bpc" id="L129" title="1 of 2 branches missed."> if (object instanceof byte[])</span>
{
<span class="fc" id="L131"> object = SceneType.fromInteger(((byte[])object)[0]);</span>
}
<span class="nc bnc" id="L134" title="All 2 branches missed."> else if (object instanceof Integer)</span>
{
<span class="nc" id="L136"> object = SceneType.fromInteger(((Integer)object));</span>
}
<span class="fc" id="L139"> return Optional.ofNullable((SceneType)object);</span>
}
/*******************************************************************************************************************
*
* {@inheritDoc}
*
******************************************************************************************************************/
@Override @Nonnull
public Optional<byte[]> getUserComment()
{
try
{
<span class="fc" id="L152"> return super.getUserComment();</span>
}
<span class="nc" id="L154"> catch (Exception e)</span>
{
<span class="nc" id="L156"> final int i = (Integer)getRaw(37510); // flowers.jpeg does this strange thing</span>
<span class="nc" id="L158"> return Optional.ofNullable(("" + i).getBytes());</span>
}
}
/*******************************************************************************************************************
*
*
******************************************************************************************************************/
@Nonnull
public Optional<String> getUserCommentAsString()
{
<span class="nc" id="L169"> String string = null;</span>
<span class="nc" id="L170"> final var bytes = getUserComment().orElse(null);</span>
<span class="nc bnc" id="L172" title="All 2 branches missed."> if (bytes != null)</span>
{
<span class="nc" id="L174"> string = new String(bytes);</span>
<span class="nc bnc" id="L176" title="All 2 branches missed."> if (string.startsWith(ASCII_PREFIX))</span>
{
<span class="nc" id="L178"> return Optional.of(string.substring(ASCII_PREFIX.length()));</span>
}
}
<span class="nc" id="L182"> return Optional.ofNullable(string);</span>
}
/*******************************************************************************************************************
*
*
******************************************************************************************************************/
public void setUserCommentAsString (@Nonnull final String string)
{
<span class="nc bnc" id="L191" title="All 2 branches missed."> setUserComment((string == null) ? null : (ASCII_PREFIX + string).getBytes());</span>
<span class="nc" id="L192"> }</span>
/*******************************************************************************************************************
*
* {@inheritDoc}
*
******************************************************************************************************************/
@Override @Nonnull
public Optional<String> getOriginalRawFileName()
{
<span class="nc" id="L202"> var value = getRaw(C_ORIGINAL_RAW_FILE_NAME);</span>
<span class="nc bnc" id="L204" title="All 2 branches missed."> if (value instanceof byte[])</span>
{
<span class="nc" id="L206"> value = new String((byte[])value);</span>
}
<span class="nc" id="L209"> return Optional.ofNullable((String)value);</span>
}
/*******************************************************************************************************************
*
* @return
*
******************************************************************************************************************/
@Nonnull
public Optional<ICC_Profile> getICCProfile()
{
<span class="nc" id="L220"> return getInterColourProfile().map(ICC_Profile::getInstance);</span>
}
/*******************************************************************************************************************
*
* @return
*
******************************************************************************************************************/
@Nonnull
public Optional<Instant> getDateTimeAsDate()
{
<span class="pc" id="L231"> return getDateTime().map(EXIF::parseDateTime).flatMap(d -> getSubsecTime().map(s -> adjust(d, s)));</span>
}
/*******************************************************************************************************************
*
******************************************************************************************************************/
public void setDateTimeAsDate (@Nonnull final Instant date)
{
<span class="nc bnc" id="L239" title="All 2 branches missed."> setDateTime((date == null) ? null : formatDateTime(date));</span>
<span class="nc" id="L240"> }</span>
/*******************************************************************************************************************
*
* @return
*
******************************************************************************************************************/
@Nonnull
public Optional<Instant> getDateTimeOriginalAsDate()
{
<span class="fc" id="L250"> return getDateTimeOriginal().map(EXIF::parseDateTime)</span>
<span class="fc" id="L251"> .flatMap(d -> getSubsecTimeOriginal().map(s -> adjust(d, s)));</span>
}
/*******************************************************************************************************************
*
******************************************************************************************************************/
public void setDateTimeOriginalAsDate (@Nonnull final Instant date)
{
<span class="nc bnc" id="L259" title="All 2 branches missed."> setDateTimeOriginal((date == null) ? null : formatDateTime(date));</span>
<span class="nc" id="L260"> }</span>
/*******************************************************************************************************************
*
* @return
*
******************************************************************************************************************/
@Nonnull
public Optional<Instant> getDateTimeDigitizedAsDate()
{
<span class="fc" id="L270"> return getDateTimeDigitized().map(EXIF::parseDateTime).flatMap(d -> getSubsecTimeDigitized().map(s -> adjust(d,</span>
s)));
}
/*******************************************************************************************************************
*
******************************************************************************************************************/
public void setDateTimeDigitizedAsDate (@Nonnull final Instant date)
{
<span class="nc bnc" id="L279" title="All 2 branches missed."> setDateTimeDigitized((date == null) ? null : formatDateTime(date));</span>
<span class="nc" id="L280"> }</span>
/*******************************************************************************************************************
*
******************************************************************************************************************/
@Override
public void setDateTime (@Nonnull final String dateTime)
{
<span class="nc" id="L288"> super.setDateTime(dateTime);</span>
<span class="nc" id="L289"> }</span>
/*******************************************************************************************************************
*
******************************************************************************************************************/
@Override
public void setDateTimeDigitized (@Nonnull final String dateTimeDigitized)
{
<span class="nc" id="L297"> super.setDateTimeDigitized(dateTimeDigitized);</span>
<span class="nc" id="L298"> }</span>
/*******************************************************************************************************************
*
******************************************************************************************************************/
@Override
public void setDateTimeOriginal (@Nonnull final String dateTimeOriginal)
{
<span class="nc" id="L306"> super.setDateTimeOriginal(dateTimeOriginal);</span>
<span class="nc" id="L307"> }</span>
/*******************************************************************************************************************
*
* @param instant
* @param subsec
*
******************************************************************************************************************/
@Nonnull
private Instant adjust (@CheckForNull final Instant instant, @Nonnull final String subsec)
{
<span class="pc bpc" id="L318" title="1 of 2 branches missed."> if (instant == null)</span>
{
<span class="nc" id="L320"> return null;</span>
}
<span class="fc" id="L323"> return instant.plus(Integer.parseInt(subsec) * 10, ChronoUnit.MILLIS);</span>
}
}
</pre><div class="footer"><span class="right">Created with <a href="http://www.jacoco.org/jacoco">JaCoCo</a> 0.8.7.202105040129</span></div></body></html>