package readers ;
import java.awt.Color ;
import java.awt.Graphics2D ;
import java.awt.image.BufferedImage ;
import java.io.File ;
import java.io.IOException ;
import java.nio.file.Path ;
import java.nio.file.Paths ;
import java.util.ArrayList ;
import java.util.Arrays ;
import java.util.List ;
import java.util.regex.Pattern ;
import javax.imageio.ImageIO ;
import readers.fonts.Font ;
import readers.fonts.Glyph ;
public class LoveLiveReader extends Reader {
List < Box > extraRegions = new ArrayList < > ( ) ;
static int lastJump = 0 ;
public LoveLiveReader ( ) {
addRegion ( new Box ( 713 , 401 , 232 , 50 ) , NUMBER ) ; //score[0]
addRegion ( new Box ( 867 , 710 , 286 , 156 ) ) ; //rank[1]
extraRegions . add ( new Box ( 65 , 604 , 250 , 53 ) ) ; //perfect outline[0]
extraRegions . add ( new Box ( 65 , 680 , 250 , 53 ) ) ; //great outline[1]
extraRegions . add ( new Box ( 65 , 760 , 250 , 53 ) ) ; //good outline[2]
extraRegions . add ( new Box ( 65 , 840 , 250 , 53 ) ) ; //bad outline[3]
extraRegions . add ( new Box ( 65 , 920 , 250 , 53 ) ) ; //miss outline[4]
addRegion ( new Box ( 509 , 604 , 190 , 54 ) , NUMBER ) ; //notes[2]
addRegion ( new Box ( 509 , 680 , 190 , 54 ) , NUMBER ) ; //notes[3]
addRegion ( new Box ( 509 , 760 , 190 , 54 ) , NUMBER ) ; //notes[4]
addRegion ( new Box ( 509 , 840 , 190 , 54 ) , NUMBER ) ; //notes[5]
addRegion ( new Box ( 509 , 920 , 190 , 54 ) , NUMBER ) ; //notes[6]
addRegion ( new Box ( 26 , 374 , 265 , 36 ) ) ; //difficulty[7]
addRegion ( new Box ( 277 , 165 , 572 , 40 ) ) ; //title[8]
addRegion ( new Box ( 716 , 502 , 226 , 45 ) , NUMBER ) ; //pct[9]
addRegion ( new Box ( 782 , 452 , 158 , 50 ) , NUMBER ) ; //maxcombo[10]
addRegion ( new Box ( 100 , 470 , 84 , 42 ) ) ; //difficultylv[11]
init ( ) ;
}
void ColorFilter ( int [ ] arr , int region , int width ) {
switch ( region ) {
case 0 : {
final ColorRange TARGETCOLOR = new ColorRange ( 240 , 255 , 130 , 150 , 0 , 10 ) ;
final ColorRange SEEKINGCOLOR = new ColorRange ( 140 , 255 , 110 , 255 , 0 , 200 ) ;
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 ;
}
}
} break ;
case 2 :
case 3 :
case 4 :
case 5 :
case 6 :
case 8 :
case 11 : {
final ColorRange TARGETCOLOR = new ColorRange ( 255 , 255 , 255 , 255 , 255 , 255 ) ;
final ColorRange SEEKINGCOLOR = new ColorRange ( 240 , 255 , 240 , 255 , 240 , 255 ) ;
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 ;
}
}
} break ;
case 9 : {
final ColorRange TARGETCOLOR = new ColorRange ( 0 , 80 , 135 , 190 , 75 , 145 ) ;
final ColorRange SEEKINGCOLOR = new ColorRange ( 0 , 200 , 135 , 240 , 75 , 240 ) ;
final Color FINALCOLOR = Color . RED ;
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 ) ;
}
}
final ColorRange SEEKINGCOLOR2 = new ColorRange ( 255 , 255 , 0 , 0 , 0 , 0 ) ;
final Color FINALCOLOR2 = new Color ( TRANSPARENT , true ) ;
for ( int x = 0 ; x < width ; x + + ) {
//30 pixels from top.
if ( arr [ 30 * width + x ] = = Color . RED . getRGB ( ) ) {
System . out . println ( "Start Jump: " + x ) ;
x = seek ( arr , 30 * width + x , SEEKINGCOLOR2 , FINALCOLOR2 , width , x ) ;
System . out . println ( "End Jump: " + x ) ;
}
}
for ( int i = 0 ; i < arr . length ; i + + ) {
Color col = new Color ( arr [ i ] , true ) ;
if ( ! col . equals ( Color . RED ) ) {
arr [ i ] = TRANSPARENT ;
} else {
arr [ i ] = Color . MAGENTA . getRGB ( ) ;
}
}
} break ;
case 10 : {
final ColorRange TARGETCOLOR = new ColorRange ( 240 , 255 , 30 , 50 , 0 , 5 ) ;
final ColorRange SEEKINGCOLOR = new ColorRange ( 180 , 255 , 10 , 120 , 0 , 120 ) ;
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 ;
}
}
} break ;
case 402 : {
final ColorRange TARGETCOLOR = new ColorRange ( 80 , 120 , 100 , 130 , 100 , 140 ) ;
final ColorRange SEEKINGCOLOR = new ColorRange ( 220 , 255 , 220 , 255 , 220 , 255 ) ;
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 - width , 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 ;
}
}
} break ;
case 400 :
case 401 :
case 403 :
case 404 : {
final ColorRange TARGETCOLOR = new ColorRange ( 255 , 255 , 255 , 255 , 255 , 255 ) ;
final ColorRange SEEKINGCOLOR = new ColorRange ( 220 , 255 , 220 , 255 , 220 , 255 ) ;
final Color FINALCOLOR = Color . RED ;
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 ) ;
}
}
final ColorRange SEEKINGCOLOR2 = new ColorRange ( 255 , 255 , 0 , 0 , 0 , 0 ) ;
final Color FINALCOLOR2 = new Color ( TRANSPARENT , true ) ;
for ( int x = 0 ; x < width ; x + + ) {
//30 pixels from top.
if ( arr [ 30 * width + x ] = = Color . RED . getRGB ( ) ) {
System . out . println ( "Start Jump: " + x ) ;
x = seek ( arr , 30 * width + x , SEEKINGCOLOR2 , FINALCOLOR2 , width , x ) ;
System . out . println ( "End Jump: " + x ) ;
}
}
for ( int i = 0 ; i < arr . length ; i + + ) {
Color col = new Color ( arr [ i ] , true ) ;
if ( ! col . equals ( Color . RED ) ) {
arr [ i ] = TRANSPARENT ;
} else {
arr [ i ] = Color . MAGENTA . getRGB ( ) ;
}
}
} break ;
}
}
boolean checkRank ( ColorRange range , int [ ] arr ) {
return checkRank ( range , arr , 4096 ) ;
}
boolean checkRank ( ColorRange range , int [ ] arr , int amt ) {
int colorCount = 0 ;
for ( int j = 0 ; j < arr . length ; j + + ) {
Color col = new Color ( arr [ j ] , true ) ;
if ( range . colorInRange ( col ) ) {
colorCount + + ;
}
}
System . out . println ( "Found " + colorCount + " pixels." ) ;
return colorCount > amt ;
}
public void interpretBoxes ( Path img ) {
/ * String dataString = readAllBoxes ( img ) ;
String [ ] data = dataString . split ( Pattern . quote ( "\n" ) ) ;
String [ ] ja_data = data [ 0 ] . split ( Pattern . quote ( ")" ) ) ;
String [ ] en_data = data [ 2 ] . split ( Pattern . quote ( ")" ) ) ;
trimAllData ( ja_data ) ;
trimAllData ( en_data ) ;
System . out . println ( Arrays . toString ( ja_data ) ) ;
System . out . println ( Arrays . toString ( en_data ) ) ; * /
int regionHeights = 0 ;
int maxWidth = 0 ;
int counter = 0 ;
for ( int i = 0 ; i < readRegions . size ( ) ; i + + ) {
regionHeights + = readRegions . get ( i ) . h + REGION_PADDING ;
if ( readRegions . get ( i ) . w > maxWidth ) {
maxWidth = readRegions . get ( i ) . w ;
}
}
try {
BufferedImage originalImg = ImageIO . read ( img . toFile ( ) ) ;
BufferedImage cutImg = new BufferedImage ( maxWidth , regionHeights , BufferedImage . TYPE_INT_ARGB ) ;
Graphics2D g = cutImg . createGraphics ( ) ;
int currentHeight = 0 ;
for ( int i = 0 ; i < readRegions . size ( ) ; i + + ) {
BufferedImage subRegion = new BufferedImage ( readRegions . get ( i ) . w , readRegions . get ( i ) . h , BufferedImage . TYPE_INT_ARGB ) ;
subRegion . setRGB ( 0 , 0 , readRegions . get ( i ) . w , readRegions . get ( i ) . h , originalImg . getRGB ( readRegions . get ( i ) . x , readRegions . get ( i ) . y , readRegions . get ( i ) . w , readRegions . get ( i ) . h , null , 0 , readRegions . get ( i ) . w ) , 0 , readRegions . get ( i ) . w ) ;
int [ ] arr = subRegion . getRGB ( 0 , 0 , readRegions . get ( i ) . w , readRegions . get ( i ) . h , null , 0 , readRegions . get ( i ) . w ) ;
//System.out.println(Arrays.toString(arr));
//System.out.println(i);
//ImageIO.write(originalImg.getSubimage(readRegions.get(i).x, readRegions.get(i).y, readRegions.get(i).w, readRegions.get(i).h),"png",new File("cut.png"));
ColorFilter ( arr , i , readRegions . get ( i ) . w ) ;
subRegion . setRGB ( 0 , 0 , readRegions . get ( i ) . w , readRegions . get ( i ) . h , arr , 0 , readRegions . get ( i ) . w ) ;
ImageIO . write ( subRegion , "png" , new File ( "sub.png" ) ) ;
cutImg . setRGB ( 0 , currentHeight , readRegions . get ( i ) . w , readRegions . get ( i ) . h , arr , 0 , readRegions . get ( i ) . w ) ;
if ( i = = 1 ) {
final ColorRange dRange = new ColorRange ( 130 , 160 , 130 , 190 , 200 , 250 ) ;
final ColorRange cRange = new ColorRange ( 50 , 200 , 180 , 230 , 10 , 115 ) ;
final ColorRange bRange = new ColorRange ( 240 , 252 , 0 , 140 , 150 , 200 ) ;
final ColorRange aRange = new ColorRange ( 180 , 230 , 40 , 200 , 230 , 250 ) ;
final ColorRange sRange = new ColorRange ( 245 , 255 , 150 , 255 , 0 , 100 ) ;
Color col = new Color ( subRegion . getRGB ( 269 , 19 ) , true ) ;
System . out . println ( col ) ;
final ColorRange sssRange = new ColorRange ( 245 , 255 , 60 , 190 , 0 , 150 ) ;
Color col2 = new Color ( subRegion . getRGB ( 228 , 19 ) , true ) ;
final ColorRange ssRange = new ColorRange ( 245 , 255 , 60 , 190 , 0 , 150 ) ;
System . out . println ( col2 ) ;
if ( checkRank ( dRange , arr ) ) {
rank = 6 ; /*D*/
} else
if ( checkRank ( cRange , arr ) ) {
rank = 5 ; /*C*/
} else
if ( checkRank ( bRange , arr ) ) {
rank = 4 ; /*B*/
} else
if ( checkRank ( aRange , arr ) ) {
rank = 3 ; /*A*/
} else
if ( sssRange . colorInRange ( col ) ) {
rank = 0 ; /*SSS*/
} else
if ( ssRange . colorInRange ( col2 ) ) {
rank = 1 ; /*SS*/
} else
if ( checkRank ( sRange , arr ) ) {
rank = 2 ; /*S*/
}
}
String val = interpretImage ( subRegion , i ) ;
sig_data [ counter + + ] = val ;
//System.out.println(i+": "+currentHeight+"-"+(currentHeight+readRegions.get(i).h+REGION_PADDING));
currentHeight + = readRegions . get ( i ) . h + REGION_PADDING ;
}
Path output = Paths . get ( "result.png" ) ;
ImageIO . write ( cutImg , "png" , output . toFile ( ) ) ;
String dataString = readAllBoxes ( output ) ;
String [ ] data = dataString . split ( Pattern . quote ( "\n" ) ) ;
String [ ] ja_data = parseOutCommas ( data [ 0 ] ) . split ( Pattern . quote ( "," ) ) ;
String [ ] en_data = parseOutCommas ( data [ 2 ] ) . split ( Pattern . quote ( "," ) ) ;
trimAllData ( ja_data ) ;
trimAllData ( en_data ) ;
System . out . println ( Arrays . toString ( ja_data ) ) ;
System . out . println ( Arrays . toString ( en_data ) ) ;
System . out . println ( Arrays . toString ( sig_data ) ) ;
interpretOutput ( ja_data , en_data , sig_data ) ;
g . dispose ( ) ;
} catch ( IOException e ) {
e . printStackTrace ( ) ;
}
//System.out.println(data[0]);
//System.out.println(data[2]);
}
private String interpretImage ( BufferedImage cutImg , int i ) {
switch ( i ) {
case 0 :
case 10 : {
Font f = Font . FONT_LOVELIVE_SCORE ;
List < Glyph > glyphs = Glyph . split ( cutImg ) ;
return f . convertGlyphs ( glyphs ) ;
}
case 2 :
case 3 :
case 4 :
case 5 :
case 6 : {
Font f = Font . FONT_LOVELIVE_NOTECOUNT ;
List < Glyph > glyphs = Glyph . split ( cutImg ) ;
return f . convertGlyphs ( glyphs ) ;
}
case 9 : {
Font f = Font . FONT_LOVELIVE_PCT ;
List < Glyph > glyphs = Glyph . split ( cutImg ) ;
glyphs . remove ( glyphs . size ( ) - 3 ) ;
glyphs . remove ( glyphs . size ( ) - 1 ) ;
String res = f . convertGlyphs ( glyphs , true ) ;
return res . substring ( 0 , glyphs . size ( ) - 1 ) + "." + res . substring ( glyphs . size ( ) - 1 , glyphs . size ( ) ) ;
}
}
return "" ;
}
void interpretResults ( String [ ] finalData ) {
for ( int i = 0 ; i < finalData . length ; i + + ) {
String [ ] splitter = finalData [ i ] . split ( Pattern . quote ( "\n" ) ) ;
switch ( i ) {
case 0 : {
score = convertToInt ( splitter ) ;
} break ;
case 1 : { } break ; //We will handle the grade with another system.
case 2 : {
notes [ 0 ] = convertToInt ( "PERFECT" , splitter ) ;
} break ;
case 3 : {
notes [ 1 ] = convertToInt ( "GREAT" , splitter ) ;
} break ;
case 4 : {
notes [ 2 ] = convertToInt ( "GOOD" , splitter ) ;
} break ;
case 5 : {
notes [ 3 ] = convertToInt ( "BAD" , splitter ) ;
} break ;
case 6 : {
notes [ 4 ] = convertToInt ( "MISS" , splitter ) ;
} break ;
case 7 : {
//difficulty=getDifficulty(convertToString(splitter).toLowerCase()); //I think we'll use the number instead. Assume CHALLENGE.
} break ;
case 8 : {
title = convertToString ( splitter ) ;
} break ;
case 9 : {
//pct=convertToPct(splitter); //No longer used.
pct = convertToDouble ( splitter ) ;
} break ;
case 10 : {
maxcombo = convertToInt ( splitter ) ;
} break ;
case 11 : {
difficulty = convertToInt ( splitter ) ;
} break ;
}
}
System . out . println ( this ) ;
}
int getDifficulty ( String str ) {
final String [ ] diffs = { "easy" , "normal" , "hard" , "master" , "challenge" } ;
for ( int i = 0 ; i < diffs . length ; i + + ) {
String diff = diffs [ i ] . toLowerCase ( ) ;
if ( str . contains ( diff ) ) {
return i ;
}
}
return - 1 ;
}
}