Skip to content

Commit

Permalink
JPEG Writing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dromagnoli committed Mar 14, 2013
1 parent b56aba7 commit ca1a84e
Showing 1 changed file with 121 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@
*/
package it.geosolutions.imageio.plugins.turbojpeg;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assume.assumeTrue;
import it.geosolutions.imageio.plugins.exif.EXIFMetadata;
import it.geosolutions.imageio.plugins.exif.EXIFTags;
import it.geosolutions.imageio.plugins.exif.EXIFTags.Type;
Expand All @@ -25,57 +29,64 @@
import it.geosolutions.imageio.stream.input.FileImageInputStreamExtImpl;
import it.geosolutions.resources.TestData;

import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.awt.image.RenderedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.imageio.IIOImage;
import javax.imageio.ImageIO;
import javax.imageio.ImageReader;
import javax.imageio.ImageWriteParam;
import javax.imageio.ImageWriter;
import javax.imageio.spi.ImageWriterSpi;

import javax.imageio.stream.FileImageInputStream;
import javax.media.jai.ImageLayout;
import javax.media.jai.JAI;
import javax.media.jai.operator.BandSelectDescriptor;

import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import static org.junit.Assume.*;
import static org.junit.Assert.*;

import org.libjpegturbo.turbojpeg.TJ;

import com.sun.media.jai.operator.ImageReadDescriptor;

public class JPEGWriterTest extends BaseTest {

private static final Logger LOGGER = Logger.getLogger(JPEGWriterTest.class.toString());

// @Before
// public void setup() {
// SKIP_TESTS = !TurboJpegUtilities.isTurboJpegAvailable();
// }
@Before
public void setup() {
SKIP_TESTS = !TurboJpegUtilities.isTurboJpegAvailable();
}

// static {
// try {
// JAI.getDefaultInstance().getTileCache().setMemoryCapacity(128 * 1024 * 1024);
// if (!(INPUT_FILE.exists() && INPUT_FILE.canRead())) {
// SKIP_TESTS = true;
// LOGGER.warning(ERROR_FILE_MESSAGE);
// } else {
// FileImageInputStream fis = new FileImageInputStream(INPUT_FILE);
// ImageReader reader = ImageIO.getImageReaders(fis).next();
// reader.setInput(fis);
// SAMPLE_IMAGE = ImageReadDescriptor.create(fis, 0, false, false, false, null, null,
// null, reader, null);
// }
//
// } catch (IOException e) {
// if (LOGGER.isLoggable(Level.SEVERE)) {
// LOGGER.severe(e.getLocalizedMessage());
// }
// }
// }
static {
try {
JAI.getDefaultInstance().getTileCache().setMemoryCapacity(128 * 1024 * 1024);
if (!(INPUT_FILE.exists() && INPUT_FILE.canRead())) {
SKIP_TESTS = true;
LOGGER.warning(ERROR_FILE_MESSAGE);
} else {
FileImageInputStream fis = new FileImageInputStream(INPUT_FILE);
ImageReader reader = ImageIO.getImageReaders(fis).next();
reader.setInput(fis);
SAMPLE_IMAGE = ImageReadDescriptor.create(fis, 0, false, false, false, null, null,
null, reader, null);
}

} catch (IOException e) {
if (LOGGER.isLoggable(Level.SEVERE)) {
LOGGER.severe(e.getLocalizedMessage());
}
}
}


public static EXIFMetadata getDefaultInstance() {
Expand Down Expand Up @@ -148,9 +159,8 @@ public void basicWriterTest() throws IOException{
final File output = TestData.temp(this, "output.jpeg", false);
writer.setOutput(output);
writer.write(ImageIO.read(input));
LOGGER.warning("Writing output to " + output);
LOGGER.info("Writing output to " + output);
assertTrue("Unable to create output file", output.exists() && output.isFile());

}

@Test
Expand Down Expand Up @@ -252,7 +262,7 @@ public void writerTestComponentsSubsampling() throws IOException {

// create output file
final File output = TestData.temp(this, "output.jpeg", false);
LOGGER.warning("output file is " + output);
LOGGER.info("output file is " + output);
writer.setOutput(output);
writer.write(null, image, wParam);
writer.dispose();
Expand All @@ -262,9 +272,86 @@ public void writerTestComponentsSubsampling() throws IOException {
// output.delete();
}

assertEquals(lengths[0], 11604);
assertEquals(lengths[1], 9376);
assertEquals(lengths[2], 8209);
assertEquals(lengths[0], 11604l);
assertEquals(lengths[1], 9376l);
assertEquals(lengths[2], 8209l);
}

@Test
public void writerTestRefineLayout() throws IOException {
if (SKIP_TESTS){
LOGGER.warning(ERROR_LIB_MESSAGE);
assumeTrue(!SKIP_TESTS);
return;
}

// test-data
final File input = TestData.file(this, "testme.jpg");
FileImageInputStream stream = null;
ImageReader reader = null;
try {

stream = new FileImageInputStream(input);
ImageLayout layout = new ImageLayout();
layout.setTileGridXOffset(-2);
layout.setTileGridYOffset(-2);
layout.setTileWidth(228);
layout.setTileHeight(104);
reader = ImageIO.getImageReaders(stream).next();
RenderingHints hints = new RenderingHints(JAI.KEY_IMAGE_LAYOUT, layout);
BufferedImage sourceImage = ImageIO.read(input);
sourceImage.getWidth();
RenderedImage inputImage = BandSelectDescriptor.create(sourceImage, new int[]{0}, hints);

// get the SPI for writer\
final Iterator<ImageWriter> it = ImageIO
.getImageWritersByFormatName(TurboJpegImageWriterSpi.formatNames[0]);
assertTrue(it.hasNext());
TurboJpegImageWriter writer = null;
while (it.hasNext()) {
ImageWriterSpi writer_ = it.next().getOriginatingProvider();
if (writer_ instanceof TurboJpegImageWriterSpi) {
writer = (TurboJpegImageWriter) writer_.createWriterInstance();
break;
}
}
assertNotNull("Unable to find TurboJpegImageWriter", writer);

IIOImage image = new IIOImage(inputImage, null, null);

// create write param
ImageWriteParam wParam_ = writer.getDefaultWriteParam();
assertTrue(wParam_ instanceof TurboJpegImageWriteParam);
TurboJpegImageWriteParam wParam = (TurboJpegImageWriteParam) wParam_;
wParam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
wParam.setCompressionType("JPEG");
wParam.setCompressionQuality(.75f);

// create output file
final File output = TestData.temp(this, "output.jpeg", false);
LOGGER.info("output file is " + output);
writer.setOutput(output);
writer.write(null, image, wParam);
writer.dispose();

assertTrue("Unable to create output file", output.exists() && output.isFile());

} catch (Throwable t) {

} finally {
if (stream != null) {
try {
stream.close();
} catch (Throwable t) {
}
}
if (reader != null) {
try {
reader.dispose();
} catch (Throwable t) {
}
}
}
}

}

0 comments on commit ca1a84e

Please sign in to comment.