Skip to content
This repository has been archived by the owner on Jul 16, 2022. It is now read-only.

Commit

Permalink
ImageUtil for EPWING
Browse files Browse the repository at this point in the history
- Bump EB4j@2.3.0
- Use ImageUtil in EB4j
- Refactoring ImageUtils

Signed-off-by: Hiroshi Miura <miurahr@linux.com>
  • Loading branch information
miurahr committed Sep 27, 2021
1 parent f90278a commit ee8652f
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 143 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Expand Up @@ -68,7 +68,7 @@ repositories {
}

dependencies {
implementation("io.github.eb4j:eb4j:2.2.2")
implementation("io.github.eb4j:eb4j:2.3.0")
implementation("org.slf4j:slf4j-simple:1.7.32")

implementation("commons-io:commons-io:2.11.0")
Expand Down
Expand Up @@ -4,6 +4,7 @@
import io.github.eb4j.GraphicData;
import io.github.eb4j.SoundData;
import io.github.eb4j.SubBook;
import io.github.eb4j.ebview.utils.ImageUtils;
import io.github.eb4j.hook.Hook;
import io.github.eb4j.hook.HookAdapter;
import io.github.eb4j.util.ImageUtil;
Expand Down Expand Up @@ -305,7 +306,7 @@ public void endMonoGraphic(final long pos) {
try {
byte[] bytes = graphicData.getMonoGraphic(pos, lastWidth, lastHeight);
output.append("\" src=\"data:image/png;base64,");
output.append(Utils.convertMonoGraphic2Base64(bytes, lastWidth, lastHeight)).append("\"/>");
output.append(ImageUtils.convertMonoGraphic2Base64(bytes, lastWidth, lastHeight)).append("\"/>");
} catch (EBException | IOException e) {
e.printStackTrace();
}
Expand All @@ -317,12 +318,12 @@ public void beginInlineColorGraphic(final int format, final long pos) {
byte[] bytes = graphicData.getColorGraphic(pos);
if (format == Hook.JPEG) {
output.append("<img src=\"data:image/jpeg;base64,");
output.append(Utils.convertImage2Base64("jpeg", bytes));
output.append(ImageUtils.convertImage2Base64("jpeg", bytes));
output.append("\" alt=\"");
} else {
bytes = ImageUtil.dibToPNG(bytes);
output.append("<img src=\"data:image/png;base64,");
output.append(Utils.convertImage2Base64("png", bytes));
output.append(ImageUtils.convertImage2Base64("png", bytes));
output.append("\" alt=\"");
}
} catch (EBException | IOException e) {
Expand Down
Expand Up @@ -4,6 +4,7 @@
import io.github.eb4j.ExtFont;
import io.github.eb4j.SubAppendix;
import io.github.eb4j.SubBook;
import io.github.eb4j.ebview.utils.ImageUtils;
import io.github.eb4j.ext.UnicodeMap;
import io.github.eb4j.util.HexUtil;
import org.apache.commons.lang3.StringUtils;
Expand Down Expand Up @@ -91,7 +92,7 @@ public String getAltCode(final int code, final boolean narrow) {

private String convertImage(final byte[] data, final int width, final int height) throws IOException {
StringBuilder sb = new StringBuilder("<img src=\"data:image/png;base64,");
sb.append(Utils.convertMonoGraphic2Base64(data, width, height));
sb.append(ImageUtils.convertMonoGraphic2Base64(data, width, height));
sb.append("\"/>");
return sb.toString();
}
Expand Down
137 changes: 0 additions & 137 deletions src/main/java/io/github/eb4j/ebview/dictionary/epwing/Utils.java

This file was deleted.

49 changes: 49 additions & 0 deletions src/main/java/io/github/eb4j/ebview/utils/ImageUtils.java
@@ -0,0 +1,49 @@
package io.github.eb4j.ebview.utils;

import io.github.eb4j.util.ImageUtil;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Base64;


public final class ImageUtils {
private ImageUtils() {
}

/**
* convert image data to base64.
* @param format image format.
* @param data image data
* @return base64 string
* @throws IOException when conversion failed
*/
public static String convertImage2Base64(final String format, final byte[] data) throws IOException {
byte[] bytes;
Base64.Encoder base64Encoder = Base64.getEncoder();
try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
final BufferedImage res = ImageIO.read(new ByteArrayInputStream(data));
ImageIO.write(res, format, baos);
baos.flush();
bytes = baos.toByteArray();
}
return base64Encoder.encodeToString(bytes);
}

/**
* Convert eb_bitmap to PNG, and convert to Base64 String.
*
* @param data eb_bitmap font data
* @param width image width
* @param height image height
* @return String Base64 encoded PNG data.
* @throws IOException when the image is broken or caused error.
*/
public static String convertMonoGraphic2Base64(final byte[] data, final int width, final int height)
throws IOException {
return convertImage2Base64("png", ImageUtil.ebBitmap2BMP(data, width, height));
}
}
@@ -1,5 +1,6 @@
package io.github.eb4j.ebview.dictionary.epwing;

import io.github.eb4j.ebview.utils.ImageUtils;
import org.junit.jupiter.api.Test;

import java.io.IOException;
Expand All @@ -14,7 +15,7 @@ public void convertImageTest() throws IOException {
byte[] data = new byte[] {0x00, 0x00, 0x00, 0x00, 0x00, (byte) 0xe0, 0x03, (byte) 0x80, 0x00,
0x00, 0x00, 0x00, 0x03, (byte) 0xc0, 0x04, 0x20, 0x00, 0x20, 0x00,
(byte) 0xe0, 0x03, 0x20, 0x04, 0x20, 0x04, 0x60, 0x03, (byte) 0xa0, 0x00, 0x00, 0x00, 0x00};
String image = Utils.convertMonoGraphic2Base64(data, 16, 16);
String image = ImageUtils.convertMonoGraphic2Base64(data, 16, 16);
assertEquals("iVBORw0KGgoAAAANSUhEUgAAABAAAAAQAQMAAAAlPW0iAAAABlBMVEX///8AAABVwtN+AAAAKklEQVR4Xm"
+ "NggIAHzMz8EBYj815GFhmmPwxMDAcYmWWZGIEMB6b/DhBZAHKDBT9OJvDqAAAAAElFTkSuQmCC", image);
}
Expand Down

0 comments on commit ee8652f

Please sign in to comment.