Skip to content

Commit

Permalink
Initial implementation of android.graphics.BitmapFactory
Browse files Browse the repository at this point in the history
Only what was needed is implemented, compression method is still untested.
  • Loading branch information
animeavi committed Dec 5, 2022
1 parent 07314ef commit fed0c72
Show file tree
Hide file tree
Showing 3 changed files with 170 additions and 0 deletions.
5 changes: 5 additions & 0 deletions AndroidCompat/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,9 @@ dependencies {

// Android version of SimpleDateFormat
implementation("com.ibm.icu:icu4j:72.1")

// OpenJDK lacks a native JPEG encoder
implementation("com.twelvemonkeys.common:common-lang:3.9.4")
implementation("com.twelvemonkeys.imageio:imageio-core:3.9.4")
implementation("com.twelvemonkeys.imageio:imageio-jpeg:3.4.1")
}
129 changes: 129 additions & 0 deletions AndroidCompat/src/main/java/android/graphics/Bitmap.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
package android.graphics;

import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.OutputStream;
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 final class Bitmap {
private int width;
private int height;
private BufferedImage img;

public Bitmap(BufferedImage img) {
this.img = img;
this.width = img.getWidth();
this.height = img.getHeight();
}

public BufferedImage getImg() {
return img;
}

public int getHeight() {
return height;
}

public int getWidth() {
return width;
}

public enum CompressFormat {
JPEG (0),
PNG (1),
WEBP (2),
WEBP_LOSSY (3),
WEBP_LOSSLESS (4);

CompressFormat(int nativeInt) {
this.nativeInt = nativeInt;
}

final int nativeInt;
}

public enum Config {
ALPHA_8(1),
RGB_565(3),
ARGB_4444(4),
ARGB_8888(5),
RGBA_F16(6),
HARDWARE(7),
RGBA_1010102(8);

final int nativeInt;

private static Config sConfigs[] = {
null, ALPHA_8, null, RGB_565, ARGB_4444, ARGB_8888, RGBA_F16, HARDWARE, RGBA_1010102
};

Config(int ni) {
this.nativeInt = ni;
}

static Config nativeToConfig(int ni) {
return sConfigs[ni];
}
}

public static Bitmap createBitmap(int width, int height, Config config) {
BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
return new Bitmap(img);
}

public boolean compress(CompressFormat format, int quality, OutputStream stream) {
if (stream == null) {
throw new NullPointerException();
}

if (quality < 0 || quality > 100) {
throw new IllegalArgumentException("quality must be 0..100");
}
float qlt = ((float) quality) / 100;

String fmt = "";
if (format == Bitmap.CompressFormat.PNG) {
fmt = "png";
} else if (format == Bitmap.CompressFormat.JPEG) {
fmt = "jpg";
} else {
throw new IllegalArgumentException("unsupported compression format!");
}

Iterator<ImageWriter> writers = ImageIO.getImageWritersByFormatName(fmt);
if (!writers.hasNext()) {
throw new IllegalStateException("no image writers found for this format!");
}
ImageWriter writer = (ImageWriter) writers.next();

ImageOutputStream ios;
try {
ios = ImageIO.createImageOutputStream(stream);
} catch (IOException ex) {
throw new RuntimeException(ex);
}
writer.setOutput(ios);

ImageWriteParam param = writer.getDefaultWriteParam();
if (fmt == "jpg") {
param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
param.setCompressionQuality(qlt);
}

try {
writer.write(null, new IIOImage(img, null, null), param);
ios.close();
writer.dispose();
} catch (IOException ex) {
throw new RuntimeException(ex);
}

return true;
}
}
36 changes: 36 additions & 0 deletions AndroidCompat/src/main/java/android/graphics/BitmapFactory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package android.graphics;

import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.IOException;
import javax.imageio.ImageIO;

public class BitmapFactory {
public static Bitmap decodeStream(InputStream is) {
Bitmap bm = null;

try {
BufferedImage bf = ImageIO.read(is);
bm = new Bitmap(bf);
} catch (IOException ex) {
throw new RuntimeException(ex);
}

return bm;
}

public static Bitmap decodeByteArray(byte[] data, int offset, int length) {
Bitmap bm = null;

ByteArrayInputStream bais = new ByteArrayInputStream(data);
try {
BufferedImage bf = ImageIO.read(bais);
bm = new Bitmap(bf);
} catch (IOException ex) {
throw new RuntimeException(ex);
}

return bm;
}
}

0 comments on commit fed0c72

Please sign in to comment.