Skip to content

Commit

Permalink
Add ImageScaler to make smaller animated gifs possible
Browse files Browse the repository at this point in the history
  • Loading branch information
rtyley committed Feb 22, 2012
1 parent 50f7db8 commit b2f90d0
Show file tree
Hide file tree
Showing 5 changed files with 124 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.github.rtyley.android.screenshot.paparazzo.processors;

import com.madgag.gif.fmsware.AnimatedGifEncoder;

import java.awt.image.BufferedImage;
import java.io.File;
import java.util.Map;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
package com.github.rtyley.android.screenshot.paparazzo.processors;

import static java.lang.String.format;
import static java.lang.System.currentTimeMillis;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.text.NumberFormat;
import java.util.Map;

import static java.lang.String.format;

public class ImageSaver implements ScreenshotProcessor {

private final File screenshotDirectory;
Expand All @@ -21,7 +19,7 @@ public ImageSaver(File screenshotDirectory) {

@Override
public void process(BufferedImage image, Map<String, String> request) {
String name=request.containsKey("name")?request.get("name"):format("%04d", screenshotCount++);
String name = request.containsKey("name") ? request.get("name") : format("%04d", screenshotCount++);

File screenshotFile = new File(screenshotDirectory, name + ".png");
try {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.github.rtyley.android.screenshot.paparazzo.processors;

import com.github.rtyley.android.screenshot.paparazzo.processors.util.Dimensions;

import java.awt.image.BufferedImage;
import java.util.Map;

import static com.github.rtyley.android.screenshot.paparazzo.processors.util.Images.imageToBufferedImage;
import static com.github.rtyley.android.screenshot.paparazzo.processors.util.Images.scaleTo;

public class ImageScaler implements ScreenshotProcessor {

private final ScreenshotProcessor delegate;
private final Dimensions maxBounds;

public ImageScaler(ScreenshotProcessor delegate, Dimensions maxBounds) {
this.delegate = delegate;
this.maxBounds = maxBounds;
}

@Override
public void process(BufferedImage image, Map<String, String> request) {
delegate.process(ensureWithinBounds(image), request);
}

private BufferedImage ensureWithinBounds(BufferedImage image) {
Dimensions imageDimensions = new Dimensions(image.getWidth(), image.getHeight());

if (maxBounds.contains(imageDimensions)) {
return image;
}

return imageToBufferedImage(scaleTo(imageDimensions.scaledPreservingAspectRatioToFitWithin(maxBounds), image));
}

@Override
public void finish() {
delegate.finish();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package com.github.rtyley.android.screenshot.paparazzo.processors.util;


import static java.lang.Math.round;

public class Dimensions {

public final int width, height;
public final double aspectRatio;

public Dimensions(int width, int height) {
this.width = width;
this.height = height;
aspectRatio = (double) width / height;
}

public static Dimensions square(int size) {
return new Dimensions(size, size);
}

public Dimensions scaledPreservingAspectRatioToFitWithin(Dimensions bounds) {
return bounds.aspectRatio > aspectRatio ? scaleToHeight(bounds.height) : scaleToWidth(bounds.width);
}

private Dimensions scaleToWidth(int targetWidth) {
return new Dimensions(targetWidth, (int) round(targetWidth / aspectRatio));
}

private Dimensions scaleToHeight(int targetHeight) {
return new Dimensions((int) round(targetHeight * aspectRatio), targetHeight);
}

public boolean contains(int w, int h) {
return width >= w && height >= h;
}

public boolean contains(Dimensions otherDimensions) {
return contains(otherDimensions.width, otherDimensions.height);
}

@Override
public String toString() {
return getClass().getSimpleName() + "[" + toSimpleString() + "]";
}

public String toSimpleString() {
return "" + width + "x" + height;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.github.rtyley.android.screenshot.paparazzo.processors.util;

import java.awt.*;
import java.awt.image.BufferedImage;

import static java.awt.Image.SCALE_SMOOTH;
import static java.awt.image.BufferedImage.TYPE_INT_RGB;

public class Images {

public static BufferedImage imageToBufferedImage(Image image) {
if (image instanceof BufferedImage) {
return (BufferedImage) image;
}
BufferedImage target = newImage(image.getWidth(null), image.getHeight(null));
Graphics2D graphics = target.createGraphics();
graphics.drawImage(image, 0, 0, null);
graphics.dispose();
return target;
}

public static BufferedImage newImage(int width, int height) {
return new BufferedImage(width, height, TYPE_INT_RGB);
}

public static Image scaleTo(Dimensions scaledDimensions, BufferedImage image) {
return image.getScaledInstance(scaledDimensions.width, scaledDimensions.height, SCALE_SMOOTH);
}

}

0 comments on commit b2f90d0

Please sign in to comment.