Skip to content

Commit

Permalink
refactor: Migrate to Date & Time API
Browse files Browse the repository at this point in the history
  • Loading branch information
ktgw0316 committed Dec 10, 2023
1 parent de019e6 commit f64905e
Show file tree
Hide file tree
Showing 22 changed files with 235 additions and 245 deletions.
7 changes: 7 additions & 0 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.2-bin.zip
networkTimeout=10000
validateDistributionUrl=true
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
15 changes: 11 additions & 4 deletions lightcrafts/src/main/java/com/lightcrafts/image/libs/LibRaw.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
import java.awt.*;
import java.awt.image.*;
import java.io.File;
import java.time.*;
import java.util.Arrays;
import java.util.Date;

public class LibRaw extends RawDecoder {
final String filePath;
Expand Down Expand Up @@ -215,8 +215,9 @@ public float getAperture() {
return aperture;
}

public Date getCaptureDateTime() {
return new Date(1000 * timestamp);
public ZonedDateTime getCaptureDateTime() {
// TODO: Is the libraw timestamp in UTC?
return ZonedDateTime.ofInstant(Instant.ofEpochMilli(timestamp), ZoneOffset.UTC);
}

public float getFocalLength() {
Expand Down Expand Up @@ -455,7 +456,13 @@ public static void main(String[] args) {

LibRaw libRaw = new LibRaw("/Stuff/Pictures/New Raw Support/Canon 450D/IMG_1598.CR2");

System.out.println("LibRaw (p:" + libRaw.progress_flags + ", w:" + libRaw.process_warnings + ") - make: " + libRaw.make + ", model: " + libRaw.model + ", timestamp: " + new Date(1000 * libRaw.timestamp));
System.out.println(
"LibRaw (p:" + libRaw.progress_flags
+ ", w:" + libRaw.process_warnings
+ ") - make: " + libRaw.make
+ ", model: " + libRaw.model
+ ", timestamp: " + libRaw.timestamp + " (" + libRaw.getCaptureDateTime() + ")"
);
System.out.println("Filter pattern: " + libRaw.filter_pattern);

for (int i = 0; i < 4; i++)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

package com.lightcrafts.image.metadata;

import java.time.ZonedDateTime;
import java.util.HashMap;
import java.util.Map;
import java.util.Date;
import java.util.ResourceBundle;

import com.lightcrafts.image.metadata.values.DateMetaValue;
Expand Down Expand Up @@ -130,7 +130,7 @@ public String getCameraModel() {
* {@inheritDoc}
*/
@Override
public Date getCaptureDateTime() {
public ZonedDateTime getCaptureDateTime() {
final ImageMetaValue value = getValue( CIFF_CAPTURED_TIME );
return value instanceof DateMetaValue ?
((DateMetaValue)value).getDateValue() : null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,15 @@
import java.awt.*;
import java.awt.color.ICC_Profile;
import java.io.File;
import java.time.ZonedDateTime;
import java.util.*;

import static com.lightcrafts.image.metadata.CoreTags.*;
import static com.lightcrafts.image.metadata.ImageMetaType.*;
import static com.lightcrafts.image.metadata.ImageOrientation.ORIENTATION_LANDSCAPE;
import static com.lightcrafts.image.metadata.ImageOrientation.ORIENTATION_UNKNOWN;
import static com.lightcrafts.image.metadata.XMPConstants.*;
import static java.time.format.DateTimeFormatter.*;

/**
* A <code>CoreDirectory</code> is-an {@link ImageMetadataDirectory} for
Expand Down Expand Up @@ -162,7 +164,7 @@ public String getCaption() {
* {@inheritDoc}
*/
@Override
public Date getCaptureDateTime() {
public ZonedDateTime getCaptureDateTime() {
final ImageMetaValue value = getValue( CORE_CAPTURE_DATE_TIME );
return value != null ? ((DateMetaValue)value).getDateValue() : null;
}
Expand All @@ -189,7 +191,7 @@ public String getCopyright() {
* {@inheritDoc}
*/
@Override
public Date getFileDateTime() {
public ZonedDateTime getFileDateTime() {
final ImageMetaValue value = getValue( CORE_FILE_DATE_TIME );
return value != null ? ((DateMetaValue)value).getDateValue() : null;
}
Expand Down Expand Up @@ -489,9 +491,7 @@ protected Collection<Element> toXMP( Document xmpDoc, String nsURI,
);
XMLUtil.setTextContentOf(
createDateElement,
TextUtil.dateFormat(
ISO_8601_DATE_FORMAT, captureDateValue.getDateValue()
)
captureDateValue.getDateValue().format(ISO_LOCAL_DATE_TIME)
);
xapRDFDescElement.appendChild( createDateElement );
}
Expand All @@ -509,21 +509,21 @@ protected Collection<Element> toXMP( Document xmpDoc, String nsURI,

////////// MetadataDate & ModifyDate

final Date now = new Date();
final var now = ZonedDateTime.now();
final Element metadataDateElement = xmpDoc.createElementNS(
XMP_XAP_NS, XMP_XAP_PREFIX + ":MetadataDate"
);
XMLUtil.setTextContentOf(
metadataDateElement,
TextUtil.dateFormat( ISO_8601_DATE_FORMAT, now )
now.format(ISO_OFFSET_DATE_TIME)
);
xapRDFDescElement.appendChild( metadataDateElement );
final Element modifyDateElement = xmpDoc.createElementNS(
XMP_XAP_NS, XMP_XAP_PREFIX + ":ModifyDate"
);
XMLUtil.setTextContentOf(
modifyDateElement,
TextUtil.dateFormat( ISO_8601_DATE_FORMAT, now )
now.format(ISO_OFFSET_DATE_TIME)
);
xapRDFDescElement.appendChild( modifyDateElement );

Expand Down Expand Up @@ -675,7 +675,7 @@ private void addCaption( ImageMetadata metadata ) {
*/
private void addCaptureDateTime( ImageInfo imageInfo ) {
removeValue( CORE_CAPTURE_DATE_TIME );
final Date date = imageInfo.getCurrentMetadata().getCaptureDateTime();
final var date = imageInfo.getCurrentMetadata().getCaptureDateTime();
if ( date != null )
putValue( CORE_CAPTURE_DATE_TIME, new DateMetaValue( date ) );
}
Expand Down Expand Up @@ -741,8 +741,7 @@ private void addFileInfo( ImageInfo imageInfo ) {
putValue( CORE_DIR_NAME , new StringMetaValue( parentDir ) );
putValue( CORE_FILE_NAME, new StringMetaValue( file.getName() ) );
putValue( CORE_FILE_SIZE, new UnsignedLongMetaValue( file.length() ) );
final Date date = new Date( file.lastModified() );
putValue( CORE_FILE_DATE_TIME, new DateMetaValue( date ) );
putValue( CORE_FILE_DATE_TIME, new DateMetaValue(file.lastModified()) );

removeValue( CORE_ORIGINAL_IMAGE_HEIGHT );
removeValue( CORE_ORIGINAL_IMAGE_WIDTH );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,14 @@

package com.lightcrafts.image.metadata;

import java.io.IOException;
import java.util.Date;
import java.util.stream.Stream;

import com.lightcrafts.image.ImageInfo;
import com.lightcrafts.image.metadata.values.*;
import com.lightcrafts.image.types.RawImageInfo;
import com.lightcrafts.utils.LightCraftsException;
import com.lightcrafts.utils.raw.RawDecoder;

import java.io.IOException;

import static com.lightcrafts.image.metadata.EXIFTags.*;

/**
Expand Down Expand Up @@ -61,7 +59,7 @@ protected void readAllDirectories() throws IOException {
new UnsignedRationalMetaValue( (int)(aperture * 10), 10 )
);

final Date captureDateTime = dcraw.getCaptureDateTime();
final var captureDateTime = dcraw.getCaptureDateTime();
if ( captureDateTime != null )
exifDir.putValue(
EXIF_DATE_TIME, new DateMetaValue( captureDateTime )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.w3c.dom.Document;
import org.w3c.dom.Element;

import java.time.ZonedDateTime;
import java.util.*;
import java.util.stream.Stream;

Expand Down Expand Up @@ -106,7 +107,7 @@ public String getCaption() {
* {@inheritDoc}
*/
@Override
public Date getCaptureDateTime() {
public ZonedDateTime getCaptureDateTime() {
ImageMetaValue value = getValue( EXIF_DATE_TIME_ORIGINAL );
if ( value == null )
value = getValue( EXIF_DATE_TIME_DIGITIZED );
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
/* Copyright (C) 2005-2011 Fabio Riccardi */
/* Copyright (C) 2023- Masahiro Kitagawa */

package com.lightcrafts.image.metadata;

import java.io.UnsupportedEncodingException;
import java.time.ZonedDateTime;
import java.util.*;
import java.nio.ByteBuffer;

Expand Down Expand Up @@ -71,7 +73,7 @@ public String getArtist() {
* {@inheritDoc}
*/
@Override
public Date getCaptureDateTime() {
public ZonedDateTime getCaptureDateTime() {
final ImageMetaValue value = getValue( IPTC_DATE_CREATED );
return value instanceof DateMetaValue ?
((DateMetaValue)value).getDateValue() : null;
Expand Down Expand Up @@ -636,6 +638,8 @@ private static void encodeTag( ByteBuffer buf, int tagID ) {
* {@link DateMetaValue} plus the time.
*/
private ImageMetaValue mergeDateTime( ImageMetaValue date, int timeTagID ) {
// TODO: There might be a better way to do this with ZonedDateTime.

final ImageMetaValue timeValue = getValue( timeTagID );
if ( timeValue == null )
return date;
Expand Down Expand Up @@ -674,12 +678,10 @@ private ImageMetaValue mergeDateTime( ImageMetaValue date, int timeTagID ) {
return date;
}

final int delta = ((hh+zh) * 60 * 60 + (mm+zm) * 60 + ss) * 1000;
final int deltaSeconds = (hh+zh) * 60 * 60 + (mm+zm) * 60 + ss;

final DateMetaValue newDateValue = (DateMetaValue)date.clone();
final Date newDate = newDateValue.getDateValue();
newDate.setTime( newDate.getTime() + delta );
return newDateValue;
final var newDate = ((DateMetaValue)date).getDateValue().plusSeconds(deltaSeconds);
return new DateMetaValue(newDate);
}
catch ( NumberFormatException e ) {
return date;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/* Copyright (C) 2005-2011 Fabio Riccardi */
/* Copyright (C) 2023- Masahiro Kitagawa */

package com.lightcrafts.image.metadata;

Expand All @@ -16,6 +17,7 @@
import org.w3c.dom.Element;

import java.io.*;
import java.time.ZonedDateTime;
import java.util.*;

import static com.lightcrafts.image.metadata.CoreTags.*;
Expand Down Expand Up @@ -255,11 +257,11 @@ public String getCaption() {
/**
* {@inheritDoc}
*/
public Date getCaptureDateTime() {
public ZonedDateTime getCaptureDateTime() {
final Collection<ImageMetadataDirectory> dirs =
findProvidersOf( CaptureDateTimeProvider.class );
for ( ImageMetadataDirectory dir : dirs ) {
final Date date =
final var date =
((CaptureDateTimeProvider)dir).getCaptureDateTime();
if ( date != null )
return date;
Expand Down Expand Up @@ -364,7 +366,8 @@ public File getFile() {
/**
* {@inheritDoc}
*/
public Date getFileDateTime() {
@Override
public ZonedDateTime getFileDateTime() {
final CoreDirectory dir =
(CoreDirectory)getDirectoryFor( CoreDirectory.class );
return dir != null ? dir.getFileDateTime() : null;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/* Copyright (C) 2005-2011 Fabio Riccardi */
/* Copyright (C) 2023- Masahiro Kitagawa */

package com.lightcrafts.image.metadata;

Expand All @@ -15,6 +16,7 @@

import java.awt.image.RenderedImage;
import java.io.IOException;
import java.time.ZonedDateTime;
import java.util.*;

import static com.lightcrafts.image.metadata.ImageMetaType.*;
Expand Down Expand Up @@ -87,7 +89,7 @@ public String getCaption() {
/**
* {@inheritDoc}
*/
public Date getCaptureDateTime() {
public ZonedDateTime getCaptureDateTime() {
final ImageMetaValue value = getValue( TIFF_DATE_TIME );
return value instanceof DateMetaValue ?
((DateMetaValue)value).getDateValue() : null;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,17 @@
/* Copyright (C) 2005-2011 Fabio Riccardi */
/* Copyright (C) 2023- Masahiro Kitagawa */

package com.lightcrafts.image.metadata;

import com.lightcrafts.app.Application;

import java.text.SimpleDateFormat;

/**
* <code>XMPConstants</code> defines constants for XMP.
*
* @author Paul J. Lucas [[email protected]]
*/
public interface XMPConstants {

/** This is the date format required by XMP. */
SimpleDateFormat ISO_8601_DATE_FORMAT =
new SimpleDateFormat( "yyyy-MM-dd'T'HH:mm:ss" );

/** The Dublin Core namespace URI. */
String XMP_DC_NS = "http://purl.org/dc/elements/1.1/";

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/* Copyright (C) 2005-2011 Fabio Riccardi */
/* Copyright (C) 2023- Masahiro Kitagawa */

package com.lightcrafts.image.metadata.makernotes;

Expand All @@ -17,6 +18,8 @@

import java.awt.image.RenderedImage;
import java.io.IOException;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.*;
import java.util.regex.Pattern;

Expand Down Expand Up @@ -52,7 +55,7 @@ public float getAperture() {
* {@inheritDoc}
*/
@Override
public Date getCaptureDateTime() {
public ZonedDateTime getCaptureDateTime() {
final ImageMetaValue value = getValue( PENTAX_DATE );
return value instanceof DateMetaValue ?
((DateMetaValue)value).getDateValue() : null;
Expand Down Expand Up @@ -277,11 +280,11 @@ public void putValue( Integer tagID, ImageMetaValue value ) {
return;
final byte[] buf =
((UndefinedMetaValue)value).getUndefinedValue();
final Calendar cal = new GregorianCalendar(
((int)buf[0] & 0xFF) << 8 | (int)buf[1] & 0xFF,
buf[2] - 1, buf[3]
);
value = new DateMetaValue( cal.getTime() );
final int year = ((int)buf[0] & 0xFF) << 8 | (int)buf[1] & 0xFF;
final int month = buf[2];
final int day = buf[3];
final var dateTime = ZonedDateTime.of(year, month, day, 0, 0, 0, 0, ZoneId.systemDefault());
value = new DateMetaValue(dateTime);
break;
}
case PENTAX_TIME: {
Expand All @@ -296,13 +299,14 @@ public void putValue( Integer tagID, ImageMetaValue value ) {
if (dateValue == null) {
return;
}
final Date date = ((DateMetaValue)dateValue).getDateValue();
final var date = ((DateMetaValue)dateValue).getDateValue();
final byte[] buf =
((UndefinedMetaValue)value).getUndefinedValue();
date.setTime(
date.getTime() +
(buf[0] * 60 * 60 + buf[1] * 60 + buf[2]) * 1000
);
final var dateTime = date
.withHour(buf[0])
.withMinute(buf[1])
.withSecond(buf[2]);
super.putValue(PENTAX_DATE, new DateMetaValue(dateTime));
return;
}
case PENTAX_IMAGE_SIZE:
Expand Down
Loading

0 comments on commit f64905e

Please sign in to comment.