Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: resize images while exporting to fs format #8

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions core/src/main/java/studio/core/v1/utils/ImageConversion.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,41 @@ public static byte[] convertImage(byte[] data, String format) throws IOException
}
}

public static byte[] resizeAndCrop(BufferedImage img, int targetWidth, int targetHeight) throws IOException {
// BufferedImage img = ImageIO.read(new ByteArrayInputStream(data));
int width = img.getWidth();
int height = img.getHeight();

// Calculate the aspect ratio preserving scale factor
// double aspectRatio = (double) width / height;
double scaleFactor = Math.min((float) targetWidth / width, (float) targetHeight / height);

// Calculate the new dimensions
int newWidth = (int) (width * scaleFactor);
int newHeight = (int) (height * scaleFactor);

// Create a BufferedImage to hold the resized and cropped image
BufferedImage resizedImage = new BufferedImage(targetWidth, targetHeight, img.getType());
Graphics2D g = resizedImage.createGraphics();

// Set the drawing area in the center of the BufferedImage
int x = (targetWidth - newWidth) / 2;
int y = (targetHeight - newHeight) / 2;

// Draw the scaled image into the BufferedImage
g.drawImage(img, x, y, newWidth, newHeight, null);
g.dispose();

try (ByteArrayOutputStream output = new ByteArrayOutputStream()) {
ImageIO.write(resizedImage, BITMAP_FORMAT, output);
if (output.size() == 0) {
throw new IOException("Failed to convert image");
}
return output.toByteArray();
}
}


private static BufferedImage redrawImage(BufferedImage img) {
BufferedImage redrawn = new BufferedImage(img.getWidth(), img.getHeight(), BufferedImage.TYPE_INT_RGB);
Graphics2D g2d = redrawn.createGraphics();
Expand Down
10 changes: 10 additions & 0 deletions core/src/main/java/studio/core/v1/utils/PackAssetsCompression.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Function;

import java.awt.image.BufferedImage;

import javax.imageio.ImageIO;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioSystem;

Expand Down Expand Up @@ -106,6 +109,13 @@ public static void processFirmware2dot4(StoryPack pack) {
// Image
processImageAssets(pack, ImageType.BMP, ThrowingFunction.unchecked(ia -> {
byte[] imageData = ia.getRawData();
// resize image to 320x240
// we need to load the image to test its size
BufferedImage img = ImageIO.read(new ByteArrayInputStream(imageData));
if (img.getWidth() != 320 || img.getHeight() != 240) {
LOGGER.debug("Resizing image to 320x240");
imageData = ImageConversion.resizeAndCrop(img, 320, 240);
}
// Convert to 4-bits depth / RLE encoding BMP
if (ImageType.BMP != ia.getType() || imageData[28] != 0x04 || imageData[30] != 0x02) {
LOGGER.debug("Converting image asset into 4-bits/RLE BMP");
Expand Down