blob: 7511b2f9fbc974fc3c21be9dc2a0b9844511f698 [file] [log] [blame]
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.
*/
package org.apache.commons.imaging.roundtrip;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Stream;
import org.apache.commons.imaging.ImageReadException;
import org.apache.commons.imaging.ImageWriteException;
import org.apache.commons.imaging.Imaging;
import org.apache.commons.imaging.ImagingConstants;
import org.apache.commons.imaging.common.RgbBufferedImageFactory;
import org.apache.commons.imaging.internal.Debug;
import org.junit.jupiter.api.io.TempDir;
import org.junit.jupiter.params.provider.Arguments;
public class RoundtripBase {
@TempDir
public File folder;
protected void roundtrip(final FormatInfo formatInfo, final BufferedImage testImage,
final String tempPrefix, final boolean imageExact) throws IOException,
ImageReadException, ImageWriteException {
final File temp1 = createTempFile(tempPrefix + ".", "."
+ formatInfo.format.getExtension());
Debug.debug("tempFile: " + temp1.getName());
final Map<String, Object> params = new HashMap<>();
Imaging.writeImage(testImage, temp1, formatInfo.format, params);
final Map<String, Object> readParams = new HashMap<>();
readParams.put(ImagingConstants.BUFFERED_IMAGE_FACTORY,
new RgbBufferedImageFactory());
final BufferedImage image2 = Imaging.getBufferedImage(temp1, readParams);
assertNotNull(image2);
if (imageExact) {
// note tolerance when comparing grayscale images
// BufferedImages of
ImageAsserts.assertEquals(testImage, image2);
}
if (formatInfo.identicalSecondWrite) {
final File temp2 = createTempFile(tempPrefix + ".", "."
+ formatInfo.format.getExtension());
// Debug.debug("tempFile: " + tempFile.getName());
Imaging.writeImage(image2, temp2, formatInfo.format, params);
ImageAsserts.assertEquals(temp1, temp2);
}
}
protected File createTempFile(final String prefix, final String suffix)
throws IOException {
return File.createTempFile(prefix, suffix, folder);
}
public static Stream<Arguments> createRoundtripArguments(BufferedImage[] images) {
return Arrays.stream(images).flatMap(i -> Arrays.stream(FormatInfo.READ_WRITE_FORMATS).map(f -> Arguments.of(i, f)));
}
}