package com.coreoz.ppt;

import java.awt.Dimension;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;

import javax.imageio.ImageIO;

import org.apache.poi.sl.usermodel.PictureData.PictureType;

import lombok.AllArgsConstructor;
import lombok.SneakyThrows;
import net.coobird.thumbnailator.Thumbnails;
import net.coobird.thumbnailator.Thumbnails.Builder;
import net.coobird.thumbnailator.geometry.Positions;

class ImagesUtils {

	// resizing

	static byte[] resizeCrop(byte[] imageData, String targetFormat, int width, int height) {
		return resize(imageData, targetFormat, width, height, true);
	}

	static byte[] resizeOnly(byte[] imageData, String targetFormat, int width, int height) {
		return resize(imageData, targetFormat, width, height, false);
	}

	@SneakyThrows
	private static byte[] resize(byte[] imageData, String targetFormat, int width, int height, boolean crop) {
		ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
		Builder<? extends InputStream> builder = Thumbnails
			.of(new ByteArrayInputStream(imageData))
			.outputQuality(1F)
			.size(width, height);

		if(crop) {
			builder.crop(Positions.CENTER);
		}

		builder
			.outputFormat(targetFormat)
			.toOutputStream(byteArrayOutputStream);

		return byteArrayOutputStream.toByteArray();
	}

	// image size

	@SneakyThrows
	static Dimension imageDimension(byte[] pictureData) {
		BufferedImage bufferedImage = ImageIO.read(new ByteArrayInputStream(pictureData));
		return new Dimension(bufferedImage.getWidth(), bufferedImage.getHeight());
	}

	// image mime type

	static PictureType guessPictureType(byte[] pictureData) {
		for(ImageType imageType : ImageType.values()) {
			if(startsWith(pictureData, imageType.startPattern)) {
				return imageType.poiType;
			}
		}

		return null;
	}

	@AllArgsConstructor
	private enum ImageType {
		PNG(PictureType.PNG, new byte[] { (byte) 0x89, (byte) 0x50, (byte) 0x4E, (byte) 0x47, (byte) 0x0D, (byte) 0x0A, (byte) 0x1A, (byte) 0x0A }),
		GIF(PictureType.GIF, new byte[] { (byte) 0x47, (byte) 0x49, (byte) 0x46, (byte) 0x38 }),
		JPEG(PictureType.JPEG, new byte[] { (byte) 0xFF, (byte) 0xD8, (byte) 0xFF }),
		BMP(PictureType.BMP, new byte[] { (byte) 0x42, (byte) 0x4D }),
		;

		private final PictureType poiType;
		private final byte[] startPattern;
	}

	private static boolean startsWith(byte[] source, byte[] match) {
		if(match.length > source.length) {
			return false;
		}

		for(int i=0; i<match.length; i++) {
			if(source[i] != match[i]) {
				return false;
			}
		}

		return true;
	}

}

@@ -513,16 +513,70 @@
 tils %7B%0A%0A
+%09private static final int QUALITY_MULTIPLICATOR = 2;%0A%0A
 %09// resi
@@ -1230,24 +1230,72 @@
 ze(width
-, height
+ * QUALITY_MULTIPLICATOR, height * QUALITY_MULTIPLICATOR
 );%0A%0A%09%09if
@@ -1672,16 +1672,20 @@
 mension(
+%0A%09%09%09
 buffered
@@ -1704,35 +1704,89 @@
 th()
-, bufferedImage.getHeight()
+ / QUALITY_MULTIPLICATOR,%0A%09%09%09bufferedImage.getHeight() / QUALITY_MULTIPLICATOR%0A%09%09
 );%0A%09

Improve images quality