use of javax.imageio.IIOImage in project skin-composer by raeleus.
the class TexturePacker method writeImages.
private void writeImages(File outputDir, String scaledPackFileName, Array<Page> pages) {
File packFileNoExt = new File(outputDir, scaledPackFileName);
File packDir = packFileNoExt.getParentFile();
String imageName = packFileNoExt.getName();
int fileIndex = 0;
for (Page page : pages) {
int width = page.width, height = page.height;
int paddingX = settings.paddingX;
int paddingY = settings.paddingY;
if (settings.duplicatePadding) {
paddingX /= 2;
paddingY /= 2;
}
width -= settings.paddingX;
height -= settings.paddingY;
if (settings.edgePadding) {
page.x = paddingX;
page.y = paddingY;
width += paddingX * 2;
height += paddingY * 2;
}
if (settings.pot) {
width = MathUtils.nextPowerOfTwo(width);
height = MathUtils.nextPowerOfTwo(height);
}
width = Math.max(settings.minWidth, width);
height = Math.max(settings.minHeight, height);
page.imageWidth = width;
page.imageHeight = height;
File outputFile;
while (true) {
outputFile = new File(packDir, imageName + (fileIndex++ == 0 ? "" : fileIndex) + "." + settings.outputFormat);
if (!outputFile.exists())
break;
}
new FileHandle(outputFile).parent().mkdirs();
page.imageName = outputFile.getName();
BufferedImage canvas = new BufferedImage(width, height, getBufferedImageType(settings.format));
Graphics2D g = (Graphics2D) canvas.getGraphics();
if (!settings.silent)
System.out.println("Writing " + canvas.getWidth() + "x" + canvas.getHeight() + ": " + outputFile);
for (Rect rect : page.outputRects) {
BufferedImage image = rect.getImage(imageProcessor);
int iw = image.getWidth();
int ih = image.getHeight();
int rectX = page.x + rect.x, rectY = page.y + page.height - rect.y - rect.height;
if (settings.duplicatePadding) {
int amountX = settings.paddingX / 2;
int amountY = settings.paddingY / 2;
if (rect.rotated) {
// Copy corner pixels to fill corners of the padding.
for (int i = 1; i <= amountX; i++) {
for (int j = 1; j <= amountY; j++) {
plot(canvas, rectX - j, rectY + iw - 1 + i, image.getRGB(0, 0));
plot(canvas, rectX + ih - 1 + j, rectY + iw - 1 + i, image.getRGB(0, ih - 1));
plot(canvas, rectX - j, rectY - i, image.getRGB(iw - 1, 0));
plot(canvas, rectX + ih - 1 + j, rectY - i, image.getRGB(iw - 1, ih - 1));
}
}
// Copy edge pixels into padding.
for (int i = 1; i <= amountY; i++) {
for (int j = 0; j < iw; j++) {
plot(canvas, rectX - i, rectY + iw - 1 - j, image.getRGB(j, 0));
plot(canvas, rectX + ih - 1 + i, rectY + iw - 1 - j, image.getRGB(j, ih - 1));
}
}
for (int i = 1; i <= amountX; i++) {
for (int j = 0; j < ih; j++) {
plot(canvas, rectX + j, rectY - i, image.getRGB(iw - 1, j));
plot(canvas, rectX + j, rectY + iw - 1 + i, image.getRGB(0, j));
}
}
} else {
// Copy corner pixels to fill corners of the padding.
for (int i = 1; i <= amountX; i++) {
for (int j = 1; j <= amountY; j++) {
plot(canvas, rectX - i, rectY - j, image.getRGB(0, 0));
plot(canvas, rectX - i, rectY + ih - 1 + j, image.getRGB(0, ih - 1));
plot(canvas, rectX + iw - 1 + i, rectY - j, image.getRGB(iw - 1, 0));
plot(canvas, rectX + iw - 1 + i, rectY + ih - 1 + j, image.getRGB(iw - 1, ih - 1));
}
}
// Copy edge pixels into padding.
for (int i = 1; i <= amountY; i++) {
copy(image, 0, 0, iw, 1, canvas, rectX, rectY - i, rect.rotated);
copy(image, 0, ih - 1, iw, 1, canvas, rectX, rectY + ih - 1 + i, rect.rotated);
}
for (int i = 1; i <= amountX; i++) {
copy(image, 0, 0, 1, ih, canvas, rectX - i, rectY, rect.rotated);
copy(image, iw - 1, 0, 1, ih, canvas, rectX + iw - 1 + i, rectY, rect.rotated);
}
}
}
copy(image, 0, 0, iw, ih, canvas, rectX, rectY, rect.rotated);
if (settings.debug) {
g.setColor(Color.magenta);
g.drawRect(rectX, rectY, rect.width - settings.paddingX - 1, rect.height - settings.paddingY - 1);
}
}
if (settings.bleed && !settings.premultiplyAlpha && !(settings.outputFormat.equalsIgnoreCase("jpg") || settings.outputFormat.equalsIgnoreCase("jpeg"))) {
canvas = new ColorBleedEffect().processImage(canvas, settings.bleedIterations);
g = (Graphics2D) canvas.getGraphics();
}
if (settings.debug) {
g.setColor(Color.magenta);
g.drawRect(0, 0, width - 1, height - 1);
}
ImageOutputStream ios = null;
try {
if (settings.outputFormat.equalsIgnoreCase("jpg") || settings.outputFormat.equalsIgnoreCase("jpeg")) {
BufferedImage newImage = new BufferedImage(canvas.getWidth(), canvas.getHeight(), BufferedImage.TYPE_3BYTE_BGR);
newImage.getGraphics().drawImage(canvas, 0, 0, null);
canvas = newImage;
Iterator<ImageWriter> writers = ImageIO.getImageWritersByFormatName("jpg");
ImageWriter writer = writers.next();
ImageWriteParam param = writer.getDefaultWriteParam();
param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
param.setCompressionQuality(settings.jpegQuality);
ios = ImageIO.createImageOutputStream(outputFile);
writer.setOutput(ios);
writer.write(null, new IIOImage(canvas, null, null), param);
} else {
if (settings.premultiplyAlpha)
canvas.getColorModel().coerceData(canvas.getRaster(), true);
ImageIO.write(canvas, "png", outputFile);
}
} catch (IOException ex) {
throw new RuntimeException("Error writing file: " + outputFile, ex);
} finally {
if (ios != null) {
try {
ios.close();
} catch (Exception ignored) {
}
}
}
}
}
use of javax.imageio.IIOImage in project basicv2 by EgonOlsen71.
the class Graphics method savePng.
/**
* Saves an image as PNG file.
*
* @param bi
* the image
* @param os
* the output stream. It will be closed when this method
* terminates.
*/
public static void savePng(BufferedImage bi, OutputStream os) {
try (BufferedOutputStream bos = new BufferedOutputStream(os);
ImageOutputStream ios = ImageIO.createImageOutputStream(bos)) {
Iterator<ImageWriter> itty = ImageIO.getImageWritersBySuffix("png");
if (itty.hasNext()) {
ImageWriter iw = (ImageWriter) itty.next();
ImageWriteParam iwp = iw.getDefaultWriteParam();
iw.setOutput(ios);
iw.write(null, new IIOImage((RenderedImage) bi, null, null), iwp);
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}
use of javax.imageio.IIOImage in project OpenOLAT by OpenOLAT.
the class ImageHelperImpl method writeTo.
/**
* Can change this to choose a better compression level as the default
* @param image
* @param scaledImage
* @return
*/
public static boolean writeTo(BufferedImage image, File scaledImage, Size scaledSize, String outputFormat) {
try {
if (!StringHelper.containsNonWhitespace(outputFormat)) {
outputFormat = OUTPUT_FORMAT;
}
Iterator<ImageWriter> writers = ImageIO.getImageWritersByFormatName(outputFormat);
if (writers.hasNext()) {
ImageWriter writer = writers.next();
ImageWriteParam iwp = getOptimizedImageWriteParam(writer, scaledSize);
IIOImage iiOImage = new IIOImage(image, null, null);
ImageOutputStream iOut = new FileImageOutputStream(scaledImage);
writer.setOutput(iOut);
writer.write(null, iiOImage, iwp);
writer.dispose();
iOut.flush();
iOut.close();
return true;
} else {
return ImageIO.write(image, outputFormat, scaledImage);
}
} catch (IOException e) {
return false;
}
}
use of javax.imageio.IIOImage in project imageio-ext by geosolutions-it.
the class JPEGWriterTest method writerTestRefineLayout.
@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) {
}
}
}
}
use of javax.imageio.IIOImage in project imageio-ext by geosolutions-it.
the class JPEGWriterTest method writerTest.
@Test
public void writerTest() throws IOException {
if (SKIP_TESTS) {
LOGGER.warning(ERROR_LIB_MESSAGE);
assumeTrue(!SKIP_TESTS);
return;
}
// test-data
final File input = TestData.file(this, "testmergb.png");
assertTrue("Unable to find test data", input.exists() && input.isFile() && input.canRead());
// 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);
// create write param
ImageWriteParam wParam_ = writer.getDefaultWriteParam();
assertTrue(wParam_ instanceof TurboJpegImageWriteParam);
TurboJpegImageWriteParam wParam = (TurboJpegImageWriteParam) wParam_;
wParam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
try {
wParam.setCompressionType("");
assertTrue("We should not be allowed to set an empty compression type", false);
} catch (Exception e) {
// TODO: handle exception
}
try {
wParam.setCompressionType("aaa");
assertTrue("We should not be allowed to set a generic compression type", false);
} catch (Exception e) {
// TODO: handle exception
}
wParam.setCompressionType("JPEG");
wParam.setCompressionQuality(.75f);
// create output file
final File output = TestData.temp(this, "output.jpeg", true);
writer.setOutput(output);
writer.write(null, new IIOImage(ImageIO.read(input), null, null), wParam);
assertTrue("Unable to create output file", output.exists() && output.isFile());
}
Aggregations