Skip to content

Commit

Permalink
jpeg
Browse files Browse the repository at this point in the history
  • Loading branch information
datahaki committed Jan 1, 2024
1 parent ddeedbf commit 6cd943e
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 11 deletions.
10 changes: 5 additions & 5 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.10.0</version>
<version>5.10.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<version>5.10.0</version>
<version>5.10.1</version>
<scope>test</scope>
</dependency>
</dependencies>
Expand All @@ -42,7 +42,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.11.0</version>
<version>3.12.1</version>
<configuration>
<release>21</release>
</configuration>
Expand Down Expand Up @@ -72,12 +72,12 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.1.2</version>
<version>3.2.3</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>3.1.2</version>
<version>3.2.3</version>
</plugin>

<plugin>
Expand Down
35 changes: 35 additions & 0 deletions src/main/java/ch/alpine/tensor/ext/Jpeg.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// adapted by jph
package ch.alpine.tensor.ext;

import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Iterator;

import javax.imageio.IIOImage;
import javax.imageio.ImageIO;
import javax.imageio.ImageWriteParam;
import javax.imageio.ImageWriter;
import javax.imageio.stream.ImageOutputStream;

public enum Jpeg {
;
/** Reference:
* https://stackoverflow.com/questions/13204432/java-how-to-set-jpg-quality
*
* @param bufferedImage
* @param object
* @param quality
* @throws IOException */
public static void put(BufferedImage bufferedImage, Object object, float quality) throws IOException {
try (ImageOutputStream imageOutputStream = ImageIO.createImageOutputStream(object)) {
Iterator<ImageWriter> iterator = ImageIO.getImageWritersByFormatName("jpeg");
ImageWriter imageWriter = iterator.next();
ImageWriteParam imageWriteParam = imageWriter.getDefaultWriteParam();
imageWriteParam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
imageWriteParam.setCompressionQuality(quality);
imageWriter.setOutput(imageOutputStream);
imageWriter.write(null, new IIOImage(bufferedImage, null, null), imageWriteParam);
imageWriter.dispose();
}
}
}
5 changes: 2 additions & 3 deletions src/main/java/ch/alpine/tensor/img/ImageResize.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,9 @@ public enum ImageResize {
* @param bufferedImage
* @param width of rescaled image
* @param height of rescaled image
* @return scaled instance of given buffered image with given dimensions and type
* either BufferedImage.TYPE_BYTE_GRAY or BufferedImage.TYPE_INT_ARGB */
* @return scaled instance of given buffered image with given dimensions */
public static BufferedImage of(BufferedImage bufferedImage, int width, int height) {
BufferedImage result = new BufferedImage(width, height, StaticHelper.type(bufferedImage.getType()));
BufferedImage result = new BufferedImage(width, height, bufferedImage.getType());
Graphics graphics = result.createGraphics();
// TODO TENSOR IMPL improve results
Image image = bufferedImage.getScaledInstance(width, height, Image.SCALE_AREA_AVERAGING);
Expand Down
14 changes: 14 additions & 0 deletions src/main/java/ch/alpine/tensor/img/ImageRotate.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// code by jph
package ch.alpine.tensor.img;

import java.awt.image.BufferedImage;

import ch.alpine.tensor.Tensor;
import ch.alpine.tensor.Tensors;
import ch.alpine.tensor.Unprotect;
Expand Down Expand Up @@ -41,4 +43,16 @@ public static Tensor _180(Tensor tensor) {
private static int cols(Tensor tensor) {
return Integers.requirePositive(Unprotect.dimension1(tensor));
}

/** @param bufferedImage
* @return */
public static BufferedImage cw(BufferedImage bufferedImage) {
int width = bufferedImage.getWidth();
int height = bufferedImage.getHeight();
BufferedImage result = new BufferedImage(height, width, bufferedImage.getType());
for (int i = 0; i < width; ++i)
for (int j = 0; j < height; ++j)
result.setRGB(height - 1 - j, i, bufferedImage.getRGB(i, j));
return result;
}
}
3 changes: 1 addition & 2 deletions src/main/java/ch/alpine/tensor/img/Thumbnail.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,8 @@ public static BufferedImage of(BufferedImage bufferedImage, int size) {
Dimension dimension = h <= w //
? new Dimension(s.multiply(r).number().intValue(), size)
: new Dimension(size, s.divide(r).number().intValue());
BufferedImage result = new BufferedImage(size, size, StaticHelper.type(bufferedImage.getType()));
BufferedImage result = new BufferedImage(size, size, bufferedImage.getType());
Graphics2D graphics = result.createGraphics();
// TODO TENSOR IMPL improve results
Image image = bufferedImage.getScaledInstance(dimension.width, dimension.height, Image.SCALE_AREA_AVERAGING);
graphics.drawImage(image, (size - dimension.width) / 2, (size - dimension.height) / 2, null);
graphics.dispose();
Expand Down
6 changes: 5 additions & 1 deletion src/main/java/ch/alpine/tensor/io/ExportHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import javax.imageio.ImageIO;

import ch.alpine.tensor.Tensor;
import ch.alpine.tensor.ext.Jpeg;

/* package */ enum ExportHelper {
;
Expand All @@ -28,6 +29,8 @@ public static void of(Filename filename, Tensor tensor, OutputStream outputStrea
of(extension, tensor, outputStream);
}

private static final float JPG_QUALITY = 0.98f;

/** @param extension
* @param tensor
* @param outputStream
Expand All @@ -38,7 +41,8 @@ public static void of(Extension extension, Tensor tensor, OutputStream outputStr
case CSV -> lines(XsvFormat.CSV.of(tensor), outputStream);
case TSV -> lines(XsvFormat.TSV.of(tensor), outputStream);
case VECTOR -> lines(VectorFormat.of(tensor), outputStream);
case BMP, JPG -> ImageIO.write(ImageFormat.bgr(tensor), extension.name(), outputStream);
case BMP -> ImageIO.write(ImageFormat.bgr(tensor), extension.name(), outputStream);
case JPG -> Jpeg.put(ImageFormat.bgr(tensor), outputStream, JPG_QUALITY);
case GIF, PNG, TIFF -> ImageIO.write(ImageFormat.of(tensor), extension.name(), outputStream);
case M -> lines(MatlabExport.of(tensor), outputStream);
default -> throw new UnsupportedOperationException(extension.name());
Expand Down

0 comments on commit 6cd943e

Please sign in to comment.