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 ITGReader extends Reader {
final static int REGION_PADDING = 32 ;
List < Box > extraRegions = new ArrayList < > ( ) ;
static int lastJump = 0 ;
boolean failed = false ;
public ITGReader ( ) {
addRegion ( new Box ( 388 , 552 , 227 , 60 ) , NUMBER ) ; //score[0]
addRegion ( new Box ( 797 , 438 , 164 , 58 ) , NUMBER ) ; //fantastic+[1]
addRegion ( new Box ( 797 , 493 , 164 , 58 ) , NUMBER ) ; //fantastic[2]
addRegion ( new Box ( 797 , 551 , 164 , 58 ) , NUMBER ) ; //excellent[3]
addRegion ( new Box ( 797 , 618 , 164 , 58 ) , NUMBER ) ; //great[4]
addRegion ( new Box ( 797 , 673 , 164 , 58 ) , NUMBER ) ; //decent[5]
addRegion ( new Box ( 797 , 736 , 164 , 58 ) , NUMBER ) ; //miss[6]
addRegion ( new Box ( 279 , 443 , 340 , 104 ) , NUMBER ) ; //pct[7]
addRegion ( new Box ( 277 , 364 , 64 , 60 ) , NUMBER ) ; //diffnumb[8]
addRegion ( new Box ( 344 , 393 , 266 , 36 ) ) ; //playstyle[9]
addRegion ( new Box ( 277 , 174 , 324 , 171 ) ) ; //grade[10]
addRegion ( new Box ( 632 , 106 , 656 , 43 ) ) ; //songname[11]
init ( ) ;
}
void ColorFilter ( int [ ] arr , int region , int width ) {
switch ( region ) {
case 0 :
case 1 : {
process ( arr , width ,
20 , 40 , 195 , 215 , 225 , 240 ,
20 , 40 , 195 , 215 , 225 , 240 ) ;
} break ;
case 2 :
case 7 :
case 10 : {
process ( arr , width ,
240 , 255 , 240 , 255 , 240 , 255 ,
240 , 255 , 240 , 255 , 240 , 255 ) ;
} break ;
case 3 : {
process ( arr , width ,
215 , 240 , 145 , 165 , 10 , 30 ,
215 , 240 , 145 , 165 , 10 , 30 ) ;
} break ;
case 4 : {
process ( arr , width ,
90 , 110 , 190 , 210 , 75 , 100 ,
90 , 110 , 190 , 210 , 75 , 100 ) ;
} break ;
case 5 : {
process ( arr , width ,
170 , 190 , 80 , 105 , 240 , 255 ,
170 , 190 , 80 , 105 , 240 , 255 ) ;
} break ;
case 6 : {
process ( arr , width ,
240 , 255 , 40 , 55 , 40 , 55 ,
240 , 255 , 40 , 55 , 40 , 55 ) ;
} break ;
case 8 : {
process ( arr , width ,
0 , 5 , 0 , 5 , 0 , 5 ,
0 , 5 , 0 , 5 , 0 , 5 ) ;
} break ;
case 9 : {
process ( arr , width ,
170 , 255 , 170 , 255 , 170 , 255 ,
170 , 255 , 100 , 255 , 120 , 255 ) ;
} break ;
case 11 : {
process ( arr , width ,
180 , 255 , 180 , 255 , 180 , 255 ,
150 , 255 , 150 , 255 , 150 , 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 * 2 ;
}
}
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 = = 10 ) {
if ( new Color ( originalImg . getRGB ( 432 , 300 ) ) . equals ( Color . WHITE ) & &
new Color ( originalImg . getRGB ( 480 , 255 ) ) . equals ( Color . WHITE ) & &
new Color ( originalImg . getRGB ( 430 , 254 ) ) . equals ( Color . WHITE ) & &
new Color ( originalImg . getRGB ( 430 , 207 ) ) . equals ( Color . WHITE ) & &
new Color ( originalImg . getRGB ( 480 , 207 ) ) . equals ( Color . WHITE ) & &
! new Color ( originalImg . getRGB ( 480 , 304 ) ) . equals ( Color . WHITE ) ) {
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_ITG_EX ;
List < Glyph > glyphs = Glyph . split ( cutImg ) ;
if ( glyphs . size ( ) > = 3 ) {
glyphs . remove ( glyphs . size ( ) - 3 ) ;
} else {
return "" ;
}
String res = f . convertGlyphs ( glyphs ) ;
return res . substring ( 0 , res . length ( ) - 2 ) + "." + res . substring ( res . length ( ) - 2 , res . length ( ) ) ;
}
case 7 : {
Font f = Font . FONT_ITG_PCT ;
List < Glyph > glyphs = Glyph . split ( cutImg ) ;
if ( glyphs . size ( ) > = 3 ) {
glyphs . remove ( glyphs . size ( ) - 3 ) ;
} else {
return "" ;
}
String res = f . convertGlyphs ( glyphs ) ;
return res . substring ( 0 , res . length ( ) - 2 ) + "." + res . substring ( res . length ( ) - 2 , res . length ( ) ) ;
}
case 1 :
case 2 :
case 3 :
case 4 :
case 5 :
case 6 : {
Font f = Font . FONT_ITG_NOTECOUNT ;
List < Glyph > glyphs = Glyph . split ( cutImg ) ;
return f . convertGlyphs ( glyphs ) ;
}
case 8 : {
Font f = Font . FONT_ITG_DIFF ;
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 ( splitter ) ;
} break ;
case 1 : {
notes [ 0 ] = convertToInt ( splitter ) ;
} break ;
case 2 : {
notes [ 1 ] = convertToInt ( splitter ) ;
} break ;
case 3 : {
notes [ 2 ] = convertToInt ( splitter ) ;
} break ;
case 4 : {
notes [ 3 ] = convertToInt ( splitter ) ;
} break ;
case 5 : {
notes [ 4 ] = convertToInt ( splitter ) ;
} break ;
case 6 : {
notes [ 5 ] = convertToInt ( splitter ) ;
} break ;
case 7 : {
pct = Double . parseDouble ( splitter [ 0 ] ) ;
} break ;
case 8 : {
difficulty = convertToInt ( splitter ) ;
} break ;
case 9 : {
other = convertToString ( splitter ) ;
} break ;
case 10 : {
if ( failed ) {
rank = 17 ; /*F*/
} else
if ( pct > = 100 ) { rank = 0 ; /*Quad*/ } else
if ( pct > = 99 ) { rank = 1 ; /*Trip*/ } else
if ( pct > = 98 ) { rank = 2 ; /*Doub*/ } else
if ( pct > = 96 ) { rank = 3 ; /*Star*/ } else
if ( pct > = 94 ) { rank = 4 ; /*S+*/ } else
if ( pct > = 92 ) { rank = 5 ; /*S*/ } else
if ( pct > = 89 ) { rank = 6 ; /*S-*/ } else
if ( pct > = 86 ) { rank = 7 ; /*A+*/ } else
if ( pct > = 83 ) { rank = 8 ; /*A*/ } else
if ( pct > = 80 ) { rank = 9 ; /*A-*/ } else
if ( pct > = 76 ) { rank = 10 ; /*B+*/ } else
if ( pct > = 72 ) { rank = 11 ; /*B*/ } else
if ( pct > = 68 ) { rank = 12 ; /*B-*/ } else
if ( pct > = 64 ) { rank = 13 ; /*C+*/ } else
if ( pct > = 60 ) { rank = 14 ; /*C*/ } else
if ( pct > = 55 ) { rank = 15 ; /*C-*/ } else
{ rank = 16 ; /*D*/ }
}
case 11 : {
title = convertToString ( splitter ) ;
} break ;
}
}
return toString ( ) ;
}
}