[ 
https://issues.apache.org/jira/browse/IMAGING-319?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17836866#comment-17836866
 ] 

Bruno P. Kinoshita edited comment on IMAGING-319 at 4/13/24 9:05 PM:
---------------------------------------------------------------------

Couldn't find any images on my local disk, nor on public domain images. But 
just to record the test code somewhere:
{noformat}
 package org.apache.commons.imaging;

import org.apache.commons.imaging.common.ImageMetadata;
import org.apache.commons.imaging.formats.jpeg.JpegImageMetadata;
import org.apache.commons.imaging.formats.jpeg.exif.ExifRewriter;
import org.apache.commons.imaging.formats.tiff.TiffImageMetadata;
import org.apache.commons.imaging.formats.tiff.write.TiffOutputSet;
import org.apache.commons.io.IOUtils;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.util.Objects;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Search for images that cause the bug in IMAGING-319.
 */
public class LibraryTest {

    public static void main(String[] args) throws ImagingException, 
IOException, InterruptedException {
        File path = new File("/home/kinow/Desktop/haystack/");
        String needle = "Offset Time\\s+:\\s+";
        Pattern pattern = Pattern.compile(String.format("^%s$", needle), 
Pattern.MULTILINE);
        for (File source : Objects.requireNonNull(path.listFiles(s -> 
s.getName().toLowerCase().endsWith(".jpg")))) {
            File result = Files.createTempFile("test_", ".jpg").toFile();
            try {
                final ImageMetadata metadata = Imaging.getMetadata(source);
                final JpegImageMetadata jpegMetadata = (JpegImageMetadata) 
metadata;
                if (jpegMetadata != null) {
                    final TiffImageMetadata exif = jpegMetadata.getExif();
                    if (exif != null) {
                        TiffOutputSet outputSet = exif.getOutputSet();
                        BufferedOutputStream bufferedOutputStream = new 
BufferedOutputStream(Files.newOutputStream(result.toPath()));
                        new ExifRewriter().updateExifMetadataLossless(source, 
bufferedOutputStream, outputSet);

                        String[] cmd = {
                                "/bin/sh",
                                "-c",
                                String.format("exiftool %s | grep \"Offset 
Time\"", result.getAbsolutePath())
                        };
                        Process process = Runtime.getRuntime().exec(cmd);
                        process.waitFor();
                        String output = 
IOUtils.toString(process.getInputStream(), Charset.defaultCharset());
                        // System.out.println(output);

                        Matcher m = pattern.matcher(output);
                        if (m.find()) {
                            System.out.printf("Bug detected in %s%n", 
source.getAbsolutePath());
                        }
                    }
                }
            } catch (RuntimeException|ImagingException ignore) {
                ignore.printStackTrace();
            } // invalid metadata or bad format
        }
    }
}{noformat}
Looks like the best option will be to craft a test image (or mock some test 
code...)


was (Author: kinow):
Couldn't find any images on my local disk, nor on public domain images. But 
just to record the test code somewhere:
{noformat}
 package org.apache.commons.imaging;

import org.apache.commons.imaging.common.ImageMetadata;
import org.apache.commons.imaging.formats.jpeg.JpegImageMetadata;
import org.apache.commons.imaging.formats.jpeg.exif.ExifRewriter;
import org.apache.commons.imaging.formats.tiff.TiffImageMetadata;
import org.apache.commons.imaging.formats.tiff.write.TiffOutputSet;
import org.apache.commons.io.IOUtils;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.util.Objects;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Search for images that cause the bug in IMAGING-319.
 */
public class LibraryTest {

    public static void main(String[] args) throws ImagingException, 
IOException, InterruptedException {
        File path = new File("/home/kinow/Desktop/haystack/");
        String needle = "Offset Time\\s+:\\s+";
        Pattern pattern = Pattern.compile(String.format("^%s$", needle), 
Pattern.MULTILINE);
        for (File source : Objects.requireNonNull(path.listFiles(s -> 
s.getName().toLowerCase().endsWith(".jpg")))) {
            File result = Files.createTempFile("test_", ".jpg").toFile();
            try {
                final ImageMetadata metadata = Imaging.getMetadata(source);
                final JpegImageMetadata jpegMetadata = (JpegImageMetadata) 
metadata;
                if (jpegMetadata != null) {
                    final TiffImageMetadata exif = jpegMetadata.getExif();
                    if (exif != null) {
                        TiffOutputSet outputSet = exif.getOutputSet();
                        BufferedOutputStream bufferedOutputStream = new 
BufferedOutputStream(Files.newOutputStream(result.toPath()));
                        new ExifRewriter().updateExifMetadataLossless(source, 
bufferedOutputStream, outputSet);

                        String[] cmd = {
                                "/bin/sh",
                                "-c",
                                String.format("exiftool %s | grep \"Offset 
Time\"", result.getAbsolutePath())
                        };
                        Process process = Runtime.getRuntime().exec(cmd);
                        process.waitFor();
                        String output = 
IOUtils.toString(process.getInputStream(), Charset.defaultCharset());
                        // System.out.println(output);

                        Matcher m = pattern.matcher(output);
                        if (m.find()) {
                            System.out.printf("Bug detected in %s%n", 
source.getAbsolutePath());
                        }
                    }
                }
            } catch (RuntimeException|ImagingException ignore) {
                ignore.printStackTrace();
            } // invalid metadata or bad format
        }
    }
}{noformat}

> updateExifMetadataLossless lost the first character of a String
> ---------------------------------------------------------------
>
>                 Key: IMAGING-319
>                 URL: https://issues.apache.org/jira/browse/IMAGING-319
>             Project: Commons Imaging
>          Issue Type: Bug
>          Components: Format: JPEG
>    Affects Versions: 1.0-alpha2
>            Reporter: Sicheng Yang
>            Assignee: Bruno P. Kinoshita
>            Priority: Major
>         Attachments: Screen Shot 2021-11-26 at 4.01.06 PM-1.png, Screen Shot 
> 2021-11-26 at 4.01.21 PM-1.png, iPhone12-geotag.JPG
>
>          Time Spent: 40m
>  Remaining Estimate: 0h
>
> I try to use TiffOutputSet to generate a new image. However, if a tag that 
> contains String, the program may miss the first character of the String.
> {noformat}
> package org.apache.commons.imaging;
> import org.apache.commons.imaging.common.ImageMetadata;
> import org.apache.commons.imaging.formats.jpeg.JpegImageMetadata;
> import org.apache.commons.imaging.formats.jpeg.exif.ExifRewriter;
> import org.apache.commons.imaging.formats.tiff.TiffImageMetadata;
> import org.apache.commons.imaging.formats.tiff.write.TiffOutputSet;
> import java.io.BufferedOutputStream;
> import java.io.File;
> import java.io.FileOutputStream;
> import java.io.IOException;
> public class LibraryTest {
>     public static void main(String[] args) throws ImagingException, 
> IOException {
>         File source = new File("/home/kinow/Desktop/iPhone12-geotag.JPG");
>         File result = new 
> File("/home/kinow/Desktop/editted-iPhone12-geotag.JPG");
>         final ImageMetadata metadata = Imaging.getMetadata(source);
>         final JpegImageMetadata jpegMetadata = (JpegImageMetadata) metadata;
>         final TiffImageMetadata exif = jpegMetadata.getExif();
>         TiffOutputSet outputSet = exif.getOutputSet();
>         BufferedOutputStream bufferedOutputStream = new 
> BufferedOutputStream(new FileOutputStream(result));
>         new ExifRewriter().updateExifMetadataLossless(source, 
> bufferedOutputStream, outputSet);
>     }
> }{noformat}
> This is the sample code.
> Tag value in original image
> !image-2021-11-26-16-01-58-645.png!
> Tag value in output image
> !image-2021-11-26-16-04-12-185.png!



--
This message was sent by Atlassian Jira
(v8.20.10#820010)

Reply via email to