Created loading and saving of glyphs (#1)

Co-authored-by: sigonasr2 <sigonasr2@gmail.com>
main
sigonasr2 2 years ago
parent 4037757857
commit 8300b450e7
  1. 133
      readers/fonts/Font.java
  2. 6
      readers/fonts/Glyph.java
  3. 30
      readers/fonts/testFont
  4. 12
      sigPlace.java
  5. BIN
      test.png

@ -5,17 +5,40 @@ import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.List;
import java.awt.Color;
public class Font {
Glyph[] data = new Glyph[9];
Glyph[] data = new Glyph[10];
final static int TRANSPARENT = new Color(0,0,0,0).getRGB();
public static Font LoadFont(String fontName) {
Path f = Paths.get("readers","fonts",fontName);
if (Files.exists(f)) {
//TODO Read the file.
return new Font();
Font font = new Font();
try {
List<String> data = Files.readAllLines(f);
for (int i=0;i<data.size();i+=3) {
Glyph g = new Glyph();
g.width=Integer.parseInt(data.get(i));
g.height=Integer.parseInt(data.get(i+1));
String dataArr = data.get(i+2);
g.data=new boolean[dataArr.length()];
for (int j=0;j<dataArr.length();j++) {
g.data[j]=(dataArr.charAt(j)-'0')=='1'?true:false;
}
}
} catch (IOException e) {
e.printStackTrace();
}
return font;
} else {
try {
Files.createFile(f);
} catch (IOException e) {
e.printStackTrace();
}
}
return new Font();
}
@ -23,54 +46,76 @@ public class Font {
public static void TrainFont(String fontName,String expectedGlyphs,BufferedImage data) {
Path f = Paths.get("readers","fonts",fontName);
Font font = LoadFont(fontName);
try {
Files.createFile(f);
int startX=-1;
int endX=-1;
outer:
for (int x=0;x<data.getWidth();x++) {
for (int y=0;y<data.getHeight();y++) {
if (data.getRGB(x, y)!=TRANSPARENT) {
if (startX==-1) {
startX=x;
}
continue outer;
int startX=-1;
int endX=-1;
outer:
for (int x=0;x<data.getWidth();x++) {
for (int y=0;y<data.getHeight();y++) {
if (data.getRGB(x, y)!=TRANSPARENT) {
if (startX==-1) {
startX=x;
}
continue outer;
}
//If we got to here it means we found transparent on everything.
if (startX!=-1) {
endX=x;
//Create a Glyph out of this.
Glyph g = new Glyph();
g.width=endX-startX;
g.height=data.getHeight();
int[] arr = data.getRGB(startX,0,g.width,g.height,null,0,g.width);
//Find the min and max Y.
int startY=g.height;
int endY=0;
for (int X=0;X<g.width;X++) {
for (int Y=0;Y<g.height;Y++) {
if (arr[Y*g.width+X]!=TRANSPARENT&&Y<startY) {
startY=Y;
}
if (arr[Y*g.width+X]!=TRANSPARENT&&Y>endY) {
endY=Y;
}
}
//If we got to here it means we found transparent on everything.
if (startX!=-1) {
endX=x;
//Create a Glyph out of this.
Glyph g = new Glyph();
g.width=endX-startX;
g.height=data.getHeight();
int[] arr = data.getRGB(startX,0,g.width,g.height,null,0,g.width);
//Find the min and max Y.
int startY=g.height;
int endY=0;
for (int X=0;X<g.width;X++) {
for (int Y=0;Y<g.height;Y++) {
if (arr[Y*g.width+X]!=TRANSPARENT&&Y<startY) {
startY=Y;
}
}
g.height=endY-startY;
g.data = new boolean[g.width*g.height];
for (int X=0;X<g.width;X++) {
for (int Y=0;Y<g.height;Y++) {
g.data[Y*g.width+X]=arr[(Y+startY)*g.width+X]!=TRANSPARENT;
if (arr[Y*g.width+X]!=TRANSPARENT&&Y>endY) {
endY=Y;
}
}
int index = expectedGlyphs.charAt(0)-'0';
font.data[index]=g;
System.out.println("Glyph for number "+index+" has been saved.");
expectedGlyphs=expectedGlyphs.substring(1);
}
g.height=endY-startY;
g.data = new boolean[g.width*g.height];
for (int X=0;X<g.width;X++) {
for (int Y=0;Y<g.height;Y++) {
g.data[Y*g.width+X]=arr[(Y+startY)*g.width+X]!=TRANSPARENT;
}
}
int index = expectedGlyphs.charAt(0)-'0';
font.data[index]=g;
System.out.println("Glyph for number "+index+" has been saved.");
expectedGlyphs=expectedGlyphs.substring(1);
startX=-1;
endX=-1;
}
}
saveFont(font,fontName);
}
private static void saveFont(Font f,String name) {
StringBuilder sb = new StringBuilder();
for (int i=0;i<f.data.length;i++) {
Glyph g = f.data[i];
if (g==null) {
g=new Glyph();
}
sb
.append(g.width)
.append('\n')
.append(g.height)
.append('\n');
for (int j=0;j<g.data.length;j++) {
sb.append(g.data[j]?"1":"0");
}
sb.append('\n');
}
try {
Files.write(Paths.get("readers","fonts",name),sb.toString().getBytes(),StandardOpenOption.TRUNCATE_EXISTING);
} catch (IOException e) {
e.printStackTrace();
}

@ -1,7 +1,7 @@
package readers.fonts;
public class Glyph {
int width;
int height;
boolean[] data;
int width=0;
int height=0;
boolean[] data = new boolean[]{};
}

@ -0,0 +1,30 @@
54
51
000000000000000000001111111111111111111111111110000000000000000000000000111111111111111111111111111100000000000000000000000001111111111111111111111111111111111100000000000000000011111111111111111111111111111111111100000000000000000111111111111111111111111111111111111100000000000000001111111111111111111111111111111111111100000000000000011111111111111111111111111111111111111100000000000000111111111111111111111111111111111111111100000000000000111111111111111111111111111111111111111110000000000000111111111111111111111111111111111111111110000000000111111111111111111111111111111111111111111110000000000111111111111111111111111111111111111111111110000000001111111111111111111111111111111111111111111111000000011111111111111111111111111111111111111111111111000000011111111111111111111111111111111111111111111111000000111111100000000000000000000000000111111111111111000001111111100000000000000000000000000111111111111111000011111111100000000000000000000000000111111111111111000111111111100000000000000000000000000111111111111111000111111111000000000000000000000000000111111111111111001111111111000000000000000000000000000111111111111111111111111111000000000000000000000000000111111111111111111111111111000000000000000000000000000111111111111111111111111111000000000000000000000000000111111111111111111111111111000000000000000000000000000111111111111111111111111111000000000000000000000000000111111111111111111111111111000000000000000000000000000111111111111111111111111111000000000000000000000000000111111111111111111111111111000000000000000000000000000111111111111111111111111111000000000000000000000000000111111111111111111111111111000000000000000000000000000111111111111111111111111111000000000000000000000000000111111111111111111111111111000000000000000000000000000111111111111110111111111111000000000000000000000000000111111111111110111111111111000000000000000000000000000111111111111100111111111111000000000000000000000000000111111111111000111111111111000000000000000000000000000111111111110000111111111111000000000000000000000000000111111111100000111111111111000000000000000000000000000111111111000000111111111111000000000000000000000111111111111110000000111111111111111111111111111111111111111111111100000000111111111111111111111111111111111111111111111000000000111111111111111111111111111111111111111111110000000000111111111111111111111111111111111111111111100000000000111111111111111111111111111111111111111100000000000000111111111111111111111111111111111111111100000000000000111111111111111111111111111111111111110000000000000000111111111111111111111111111111111111100000000000000000111111111111111111111111111111111111000000000000000000111111111111111111111111111111111110000000000000000000111111111111111111111111111111111100000000000000000000
0
0
0
0
51
51
011111111111111111111111111111111111100000000000000111111111111111111111111111111111111111000000000000111111111111111111111111111111111111111000000000000111111111111111111111111111111111111111000000000000111111111111111111111111111111111111111000000000000111111111111111111111111111111111111111000000000000111111111111111111111111111111111111111000000000000011111111111111111111111111111111111111000000000000011111111111111111111111111111111111111000000000000011111111111111111111111111111111111111000000000000011111111111111111111111111111111111111000000000000011111111111111111111111111111111111111000000000000011111111111111111111111111111111111111100000000000011111111111111111111111111111111111111110000000000000000001111111111111111111111111111111111100000000000000000000000000000000000000000000111111111000000000000000000000000000000000000000000111111111110000000000000000000000000000000000000000111111111111000000000000000000000000000000000000000111111111111110000000000000000001111111111111111001111111111111110000000000000001111111111111111111111111111111111111000000000000001111111111111111111111111111111111111000000000000011111111111111111111111111111111111111000000000000111111111111111111111111111111111111111000000000001111111111111111111111111111111111111111000000000011111111111111111111111111111111111111111000000000111111111111111111111111110111111111111111000000001111111111111111111111111100111111111111111000000011111111111111111111111111000111111111111111000000111111111111111111111111110000111111111111111000001111111111111111111111111100000111111111111111000011111111111111111111111111000000111111111111111000011111111111111111111111110000000111111111111111000000000000000000000000000000000000111111111111111000000000000000000000000000000000000111111111111111000000000000000000000000000000000000111111111111111000000000000000000000000000000000000111111111111111000000000000000000000000000000000000111111111111111000000000000000000000000000000000000111111111111111000000000000000000000000000000000111111111111111111011111111111111111111111111111111111111111111111111011111111111111111111111111111111111111111111111110011111111111111111111111111111111111110011111111000011111111111111111111111111111111110000000000000000011111111111111111111111111111111100000000000000000011111111111111111111111111111111000000000000000000011111111111111111111111111111110000000000000000000011111111111111111111111111111100000000000000000000011111111111111111111111111111000000000000000000000011111111111111111111111111110000000000000000000000011111111111111111111111111100000000000000000000000
0
0
0
0
0
0
0
0
0
0
54
51
000000000000000000000011111111111111111110000000000000000000000000000000000111111111111111111111100000000000000000000000000000011111111111111111111111110000000000000000000000000000111111111111111111111111111000000000000000000000000001111111111111111111111111111100000000000000000000000011111111111111111111111111111110000000000000000000000111111111111111111111111111111111000000000000000000001111111111111111111111111111111111100000000000000000011111111111111111111111111111111111110000000000000000111111111111111111111111111111111111111000000000000001111111111111111111111111111111111111111100000000000011111111111111111111111111111111111111111110000000000011111111111111111111111111111111111111111110000000000111111111111111111111111111111111111111111111000000001111111111111111111111111111111111111111111111000000001111100000000000000000000000000111111111111111000000011111100000000000000000000000000111111111111111000000011111100000000000000000000000000111111111111111000000111111100000000000000000000000000111111111111111000001111111000000000000000000000000000111111111111111000011111111000000000000000000000000000111111111111111000111111111000000000000000000000000000111111111111111001111111111000000000000000000000000000111111111111111011111111111000001111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111110111111111111111111111111111111111111111111111111111100111111111111111111111111111111111111111111111111111000111111111111111111111111111111111111111111111111110000111111111111111111111111111111111111111111111111100000111111111111111111111111111111111111111111111110000000111111111111111000000000000000000000000000000100000000111111111111111000000000000000000000000000000000000000111111111111111000000000000000000000000000000000000000111111111111111000000000000000000000000000000000111111111111111111111000000000001111111111111111111111111111111111111111111000000000001111111111111111111111111111111111111111111000000000001111111111111111111111111111111111111111110000000000001111111111111111111111111111111111111111100000000000001111111111111111111111111111111111111111000000000000001111111111111111111111111111111111111110000000000000001111111111111111111111111111111111111100000000000000001111111111111111111111111111111111111000000000000000001111111111111111111111111111111111100000000000000000001111111111111111111111111111111111000000000000000000001111111111111111111111111111111100000000000

@ -1,3 +1,4 @@
import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.CopyOption;
@ -88,8 +89,10 @@ public class sigPlace {
int[] arr = img.getRGB(scoreBox.x, scoreBox.y, scoreBox.w, scoreBox.h, null, 0, scoreBox.w);
final ColorRange TARGETCOLOR = new ColorRange(240,255,240,255,240,255);
final ColorRange SEEKINGCOLOR = new ColorRange(100,255,100,255,100,255);
BufferedImage newImg = new BufferedImage(scoreBox.w,scoreBox.h,BufferedImage.TYPE_INT_ARGB);
final ColorRange TARGETCOLOR = new ColorRange(160,255,170,255,190,255);
final ColorRange SEEKINGCOLOR = new ColorRange(26,255,53,255,80,255);
final Color FINALCOLOR = Color.MAGENTA;
for (int i=0;i<arr.length;i++) {
Color col = new Color(arr[i],true);
@ -103,8 +106,9 @@ public class sigPlace {
arr[i]=TRANSPARENT;
}
}
img.setRGB(0,0,scoreBox.w,scoreBox.h,arr,0,scoreBox.w);
Font.TrainFont("testFont","930",img);
newImg.setRGB(0,0,scoreBox.w,scoreBox.h,arr,0,scoreBox.w);
ImageIO.write(newImg,"png",new File("test.png"));
Font.TrainFont("testFont","930",newImg);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1013 B

After

Width:  |  Height:  |  Size: 1.2 KiB

Loading…
Cancel
Save