Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
ktgw0316 committed Dec 8, 2023
1 parent 1f892da commit 34880b5
Show file tree
Hide file tree
Showing 16 changed files with 152 additions and 179 deletions.
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 @@ -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
Expand Up @@ -16,6 +16,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 +256,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 +365,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,7 @@

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

Expand Down Expand Up @@ -52,7 +54,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 @@ -296,7 +298,7 @@ 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(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
/* Copyright (C) 2005-2011 Fabio Riccardi */
/* Copyright (C) 2023- Masahiro Kitagawa */

package com.lightcrafts.image.metadata.providers;

import java.util.Date;
import java.time.ZonedDateTime;

/**
* A <code>CaptureDateTimeProvider</code> provides the capture date/time of an
Expand All @@ -18,7 +19,7 @@ public interface CaptureDateTimeProvider extends ImageMetadataProvider {
* @return Returns the date/time or <code>null</code> if no date/time is
* available.
*/
Date getCaptureDateTime();
ZonedDateTime getCaptureDateTime();

}
/* vim:set et sw=4 ts=4: */
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
/* Copyright (C) 2005-2011 Fabio Riccardi */
/* Copyright (C) 2023- Masahiro Kitagawa */

package com.lightcrafts.image.metadata.providers;

import java.util.Date;
import java.time.ZonedDateTime;

/**
* A <code>FileDateTimeProvider</code> provides the last modified date/time of
Expand All @@ -18,7 +19,7 @@ public interface FileDateTimeProvider extends ImageMetadataProvider {
* @return Returns the date/time or <code>null</code> if no date/time is
* available.
*/
Date getFileDateTime();
ZonedDateTime getFileDateTime();

}
/* vim:set et sw=4 ts=4: */
Loading

0 comments on commit 34880b5

Please sign in to comment.