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 PopnReader extends Reader {
List < Box > extraRegions = new ArrayList < > ( ) ;
static int lastJump = 0 ;
boolean failed = false ;
public PopnReader ( ) {
addRegion ( new Box ( 941 , 609 , 275 , 54 ) , NUMBER ) ; //score[0]
addRegion ( new Box ( 1060 , 684 , 157 , 40 ) , NUMBER ) ; //cool[1]
addRegion ( new Box ( 1060 , 724 , 157 , 39 ) , NUMBER ) ; //great[2]
addRegion ( new Box ( 1060 , 761 , 157 , 40 ) , NUMBER ) ; //good[3]
addRegion ( new Box ( 1060 , 800 , 157 , 40 ) , NUMBER ) ; //bad[4]
addRegion ( new Box ( 1060 , 858 , 157 , 42 ) , NUMBER ) ; //combo[5]
addRegion ( new Box ( 970 , 288 , 271 , 50 ) ) ; //diff[6]
addRegion ( new Box ( 689 , 345 , 547 , 128 ) ) ; //song[7]
addRegion ( new Box ( 502 , 23 , 314 , 288 ) ) ; //rating[8]
addRegion ( new Box ( 692 , 525 , 532 , 16 ) ) ; //lifebar[9]
extraRegions . add ( new Box ( 699 , 616 , 179 , 44 ) ) ; //score text[0]
extraRegions . add ( new Box ( 712 , 680 , 164 , 40 ) ) ; //cool text[1]
extraRegions . add ( new Box ( 712 , 721 , 168 , 38 ) ) ; //great text[2]
extraRegions . add ( new Box ( 712 , 759 , 150 , 40 ) ) ; //good text[3]
extraRegions . add ( new Box ( 712 , 800 , 101 , 38 ) ) ; //bad text[4]
extraRegions . add ( new Box ( 708 , 861 , 170 , 39 ) ) ; //combo text[5]
extraRegions . add ( new Box ( 970 , 290 , 194 , 47 ) ) ; //diff text[6]
init ( ) ;
}
void ColorFilter ( int [ ] arr , int region , int width ) {
switch ( region ) {
case 0 : {
process ( arr , width ,
240 , 255 , 100 , 130 , 0 , 10 ,
240 , 255 , 100 , 255 , 0 , 100 ) ;
} break ;
case 1 : {
process ( arr , width ,
230 , 255 , 0 , 10 , 230 , 255 ,
230 , 255 , 0 , 150 , 230 , 255 ) ;
} break ;
case 2 : {
process ( arr , width ,
240 , 255 , 180 , 225 , 0 , 10 ,
240 , 255 , 180 , 255 , 0 , 140 ) ;
} break ;
case 3 : {
process ( arr , width ,
240 , 244 , 0 , 40 , 0 , 10 ,
240 , 255 , 0 , 180 , 0 , 255 ) ;
} break ;
case 4 : {
process ( arr , width ,
90 , 120 , 190 , 220 , 240 , 255 ,
90 , 255 , 190 , 255 , 0 , 255 ) ;
} break ;
case 5 : {
process ( arr , width ,
0 , 10 , 0 , 60 , 240 , 255 ,
0 , 20 , 0 , 100 , 240 , 255 ) ;
} break ;
case 6 : {
process ( arr , width ,
240 , 255 , 230 , 255 , 0 , 40 ,
240 , 255 , 230 , 255 , 0 , 240 ) ;
} break ;
case 400 : {
process ( arr , width ,
240 , 255 , 0 , 10 , 0 , 10 ,
240 , 255 , 0 , 10 , 0 , 10 ) ;
} break ;
case 401 : {
process ( arr , width ,
230 , 255 , 0 , 10 , 240 , 255 ,
230 , 255 , 0 , 10 , 240 , 255 ) ;
} break ;
case 402 : {
process ( arr , width ,
240 , 255 , 200 , 255 , 0 , 10 ,
240 , 255 , 200 , 255 , 0 , 10 ) ;
} break ;
case 403 : {
process ( arr , width ,
240 , 255 , 0 , 50 , 0 , 10 ,
240 , 255 , 0 , 50 , 0 , 10 ) ;
} break ;
case 404 : {
process ( arr , width ,
90 , 120 , 180 , 220 , 240 , 255 ,
85 , 120 , 160 , 220 , 180 , 255 ) ;
} break ;
case 405 : {
process ( arr , width ,
0 , 20 , 0 , 80 , 240 , 255 ,
0 , 20 , 0 , 80 , 240 , 255 ) ;
} break ;
case 406 : {
process ( arr , width ,
240 , 255 , 240 , 255 , 240 , 255 ,
210 , 255 , 210 , 255 , 210 , 255 ) ;
} break ;
}
}
public void interpretBoxes ( Path img , boolean testingMode ) {
/ * 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 = = 9 ) {
Color col = new Color ( subRegion . getRGB ( 365 , 5 ) , true ) ;
ColorRange range = new ColorRange ( 230 , 255 , 70 , 90 , 80 , 105 ) ;
if ( ! range . colorInRange ( col ) ) {
failed = true ;
}
}
String val = interpretImage ( subRegion , i ) ;
sig_data [ counter + + ] = val ;
currentHeight + = readRegions . get ( i ) . h + REGION_PADDING ;
}
Path output = Paths . get ( "result.png" ) ;
ImageIO . write ( cutImg , "png" , output . toFile ( ) ) ;
if ( testingMode ) {
interpretOutput ( new String [ ] { } , new String [ ] { } , sig_data ) ;
} else {
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 : {
Font f = Font . FONT_POPN_SCORE ;
List < Glyph > glyphs = Glyph . split ( cutImg ) ;
return f . convertGlyphs ( glyphs ) ;
}
case 1 :
case 2 :
case 3 :
case 4 : {
Font f = Font . FONT_POPN_NOTECOUNT ;
List < Glyph > glyphs = Glyph . split ( cutImg ) ;
return f . convertGlyphs ( glyphs , true ) ;
}
case 5 : {
Font f = Font . FONT_POPN_COMBO ;
List < Glyph > glyphs = Glyph . split ( cutImg ) ;
return f . convertGlyphs ( glyphs ) ;
}
}
return "" ;
}
String 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 ( "SCORE" , splitter ) ;
} break ;
case 1 : {
notes [ 0 ] = convertToInt ( "COOL" , splitter ) ;
} break ;
case 2 : {
notes [ 1 ] = convertToInt ( "GREAT" , splitter ) ;
} break ;
case 3 : {
notes [ 2 ] = convertToInt ( "GOOD" , splitter ) ;
} break ;
case 4 : {
notes [ 3 ] = convertToInt ( "BAD" , splitter ) ;
} break ;
case 5 : {
maxcombo = convertToInt ( "COMBO" , splitter ) ;
} break ;
case 6 : {
difficulty = convertToInt ( splitter ) ;
} break ; //We'll handle the level in a special way.
case 7 : {
title = convertToString ( splitter ) ;
} break ;
case 9 : {
other = "{failed:" + failed + "}" ;
if ( score > = 98000 ) { rank = 0 ; /*S*/ } else
if ( score > = 95000 ) { rank = 1 ; /*AAA*/ } else
if ( score > = 90000 ) { rank = 2 ; /*AA*/ } else
if ( score > = 82000 ) { rank = 3 ; /*A*/ } else
if ( score > = 72000 ) { rank = 4 ; /*B*/ } else
if ( score > = 62000 ) { rank = 5 ; /*C*/ } else
if ( score > = 50000 ) { rank = 6 ; /*D*/ } else
{ rank = 7 ; /*E*/ }
if ( failed ) {
rank = Math . max ( rank , 3 ) ; /*F*/
}
} break ;
}
}
return toString ( ) ;
}
}