Where people come together to learn, code, and play. Custom-built HTTP server, site generator, and website from scratch using no external libraries. Goal is to be as minimalistic and fun as possible. http://projectdivar.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
SigPlace/readers/Reader.java

134 lines
5.1 KiB

package readers;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import java.awt.Color;
public abstract class Reader{
int score;
int rank;
int[] notes = new int[7];
int difficulty;
String title;
int pct;
int maxcombo;
String other;
List<Box> readRegions = new ArrayList<>();
final int TRANSPARENT = new Color(0,0,0,0).getRGB();
String readAllBoxes(Path img) {
try {
Process p = Runtime.getRuntime().exec(new String[]{"python3","runocr.py","ja",img.toAbsolutePath().toString()});
while (p.isAlive());
InputStreamReader result = new InputStreamReader(p.getInputStream());
StringBuilder sb = new StringBuilder();
while (result.ready()) {
sb.append((char)result.read());
}
result.close();
sb.append("\n");
p = Runtime.getRuntime().exec(new String[]{"python3","runocr.py","en",img.toAbsolutePath().toString()});
while (p.isAlive());
result = new InputStreamReader(p.getInputStream());
while (result.ready()) {
sb.append((char)result.read());
}
return sb.toString();
} catch (IOException e) {
e.printStackTrace();
}
return "";
}
abstract String interpretOutput(String[] jp_data,String[] en_data);
void trimAllData(String[] data) {
StringBuilder sb = new StringBuilder();
for (int i=0;i<data.length;i++) {
sb.delete(0,sb.length());
for (int j=0;j<data[i].length();j++) {
if (data[i].charAt(j)!='['&&data[i].charAt(j)!='('&&data[i].charAt(j)!=')'&&data[i].charAt(j)!=']') {
sb.append(data[i].charAt(j));
}
}
data[i]=sb.toString().trim();
}
}
void seek(int[]arr,int i,ColorRange SEEKCOLOR,Color FINALCOLOR,int width) {
seek(arr,i,SEEKCOLOR,FINALCOLOR,width,0);
}
int seek(int[]arr,int i,ColorRange SEEKCOLOR,Color FINALCOLOR,int width,int farthestRight) {
arr[i]=FINALCOLOR.getRGB();
int X = i%width;
for (int x=-1;x<=1;x++) {
for (int y=-1;y<=1;y++) {
int newX=(i+x+y*width)%width;
int newY=(i+x+y*width)/width;
if (newX>=0&&newY>=0&&newX<=width&&newY<=arr.length/width) {
Color col = new Color(arr[i+x+y*width]);
if (!col.equals(Color.MAGENTA)&&SEEKCOLOR.colorInRange(col)) {
farthestRight=seek(arr,i+x+y*width,SEEKCOLOR,FINALCOLOR,width,farthestRight);
}
}
}
}
return X>farthestRight?X:farthestRight;
}
void seekThreshold(int[]arr,int i,int threshold,Color FINALCOLOR,int width) {
seekThreshold(arr,i,threshold,FINALCOLOR,width,0);
}
int seekThreshold(int[]arr,int i,int threshold,Color FINALCOLOR,int width,int farthestRight) {
arr[i]=FINALCOLOR.getRGB();
int X = i%width;
for (int x=-1;x<=1;x++) {
for (int y=-1;y<=1;y++) {
int newX=(i+x+y*width)%width;
int newY=(i+x+y*width)/width;
if (newX>=0&&newY>=0&&newX<=width&&newY<=arr.length/width) {
Color col = new Color(arr[i+x+y*width]);
if (!col.equals(Color.MAGENTA)&&colorIsBright(col, threshold)) {
farthestRight=seekThreshold(arr,i+x+y*width,threshold,FINALCOLOR,width,farthestRight);
}
}
}
}
return X>farthestRight?X:farthestRight;
}
void process(int[]arr,int width,int a,int b,int c,int d,int e,int f,int g,int h,int ii,int j,int k,int l) {
final ColorRange TARGETCOLOR = new ColorRange(a,b,c,d,e,f);
final ColorRange SEEKINGCOLOR = new ColorRange(g,h,ii,j,k,l);
final Color FINALCOLOR = Color.MAGENTA;
for (int i=0;i<arr.length;i++) {
Color col = new Color(arr[i],true);
if (TARGETCOLOR.colorInRange(col)) {
seek(arr,i,SEEKINGCOLOR,FINALCOLOR,width);
}
}
for (int i=0;i<arr.length;i++) {
Color col = new Color(arr[i],true);
if (!col.equals(Color.MAGENTA)) {
arr[i]=TRANSPARENT;
}
}
}
boolean colorIsBright(Color col,int brightnessThreshold) {
return col.getRed()+col.getBlue()+col.getGreen()>brightnessThreshold;
}
void processBrightness(int[]arr,int width,int threshold1,int threshold2) {
final Color FINALCOLOR = Color.MAGENTA;
for (int i=0;i<arr.length;i++) {
Color col = new Color(arr[i],true);
if (colorIsBright(col, threshold1)) {
seekThreshold(arr,i,threshold2,FINALCOLOR,width);
}
}
for (int i=0;i<arr.length;i++) {
Color col = new Color(arr[i],true);
if (!col.equals(Color.MAGENTA)) {
arr[i]=TRANSPARENT;
}
}
}
}