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 ;
public class SoundVoltexReader extends Reader {
final static int REGION_PADDING = 32 ;
List < Box > extraRegions = new ArrayList < > ( ) ;
static int lastJump = 0 ;
public SoundVoltexReader ( ) {
readRegions . add ( new Box ( 430 , 1006 , 454 , 29 ) ) ; //title[0]
readRegions . add ( new Box ( 458 , 1075 , 240 , 57 ) ) ; //bigscore[1]
readRegions . add ( new Box ( 697 , 1096 , 146 , 34 ) ) ; //score[2]
readRegions . add ( new Box ( 630 , 1142 , 110 , 16 ) ) ; //exscore[3]
readRegions . add ( new Box ( 932 , 1194 , 55 , 19 ) ) ; //health percentage[4]
readRegions . add ( new Box ( 617 , 1237 , 70 , 23 ) ) ; //early error[5]
readRegions . add ( new Box ( 617 , 1268 , 70 , 23 ) ) ; //early near[6]
readRegions . add ( new Box ( 617 , 1300 , 70 , 23 ) ) ; //early critical[7]
readRegions . add ( new Box ( 617 , 1330 , 70 , 23 ) ) ; //s-critical[8]
readRegions . add ( new Box ( 617 , 1362 , 70 , 23 ) ) ; //late critical[9]
readRegions . add ( new Box ( 617 , 1393 , 70 , 23 ) ) ; //late near[10]
readRegions . add ( new Box ( 617 , 1424 , 70 , 23 ) ) ; //late error[11]
readRegions . add ( new Box ( 188 , 1231 , 70 , 23 ) ) ; //chip s-critical[12]
readRegions . add ( new Box ( 188 , 1262 , 70 , 23 ) ) ; //chip critical[13]
readRegions . add ( new Box ( 188 , 1294 , 70 , 23 ) ) ; //chip near[14]
readRegions . add ( new Box ( 188 , 1324 , 70 , 23 ) ) ; //chip error[15]
readRegions . add ( new Box ( 266 , 1231 , 70 , 23 ) ) ; //long s-critical[16]
readRegions . add ( new Box ( 266 , 1324 , 70 , 23 ) ) ; //long error[17]
readRegions . add ( new Box ( 345 , 1231 , 70 , 23 ) ) ; //vol s-critical[18]
readRegions . add ( new Box ( 345 , 1324 , 70 , 23 ) ) ; //vol error[19]
readRegions . add ( new Box ( 304 , 1364 , 70 , 23 ) ) ; //max combo[20]
readRegions . add ( new Box ( 233 , 880 , 84 , 20 ) ) ; //difficulty[21]
readRegions . add ( new Box ( 875 , 1046 , 125 , 82 ) ) ; //rating[22]
extraRegions . add ( new Box ( 531 , 1152 , 75 , 16 ) ) ; //EX Score text[0]
extraRegions . add ( new Box ( 753 , 1197 , 150 , 16 ) ) ; //effective rate text[1]
extraRegions . add ( new Box ( 499 , 1238 , 78 , 23 ) ) ; //early error text[2]
extraRegions . add ( new Box ( 499 , 1268 , 78 , 23 ) ) ; //early near text[3]
extraRegions . add ( new Box ( 499 , 1300 , 93 , 23 ) ) ; //early critical text[4]
extraRegions . add ( new Box ( 499 , 1332 , 108 , 23 ) ) ; //s-critical text[5]
extraRegions . add ( new Box ( 499 , 1362 , 93 , 23 ) ) ; //late critical text[6]
extraRegions . add ( new Box ( 499 , 1393 , 78 , 23 ) ) ; //late near text[7]
extraRegions . add ( new Box ( 499 , 1424 , 78 , 23 ) ) ; //late error text[8]
extraRegions . add ( new Box ( 62 , 1230 , 119 , 24 ) ) ; //s-critical extra text[9]
extraRegions . add ( new Box ( 62 , 1262 , 119 , 24 ) ) ; //critical extra text[10]
extraRegions . add ( new Box ( 62 , 1294 , 119 , 24 ) ) ; //near extra text[11]
extraRegions . add ( new Box ( 62 , 1325 , 119 , 24 ) ) ; //error extra text[12]
extraRegions . add ( new Box ( 62 , 1230 , 119 , 24 ) ) ; //s-critical extra text[13]
extraRegions . add ( new Box ( 62 , 1325 , 119 , 24 ) ) ; //error extra text[14]
extraRegions . add ( new Box ( 62 , 1230 , 119 , 24 ) ) ; //s-critical extra text[15]
extraRegions . add ( new Box ( 62 , 1325 , 119 , 24 ) ) ; //error extra text[16]
extraRegions . add ( new Box ( 71 , 1365 , 171 , 22 ) ) ; //maximum chain text[17]
}
void ColorFilter ( int [ ] arr , int region , int width ) {
switch ( region ) {
case 0 : {
final ColorRange TARGETCOLOR = new ColorRange ( 240 , 255 , 240 , 255 , 240 , 255 ) ;
final ColorRange SEEKINGCOLOR = new ColorRange ( 100 , 255 , 100 , 255 , 100 , 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 1 :
case 2 : {
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 ) ;
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 3 : {
final ColorRange TARGETCOLOR = new ColorRange ( 230 , 255 , 180 , 255 , 20 , 255 ) ;
final ColorRange SEEKINGCOLOR = new ColorRange ( 130 , 255 , 100 , 255 , 15 , 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 4 :
case 5 :
case 6 :
case 7 :
case 8 :
case 9 :
case 10 :
case 11 :
case 12 :
case 13 :
case 14 :
case 15 :
case 16 :
case 17 :
case 18 :
case 19 :
case 20 : {
final ColorRange TARGETCOLOR = new ColorRange ( 200 , 255 , 200 , 255 , 200 , 255 ) ;
final ColorRange SEEKINGCOLOR = new ColorRange ( 120 , 255 , 120 , 255 , 120 , 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 21 : {
final ColorRange TARGETCOLOR = new ColorRange ( 200 , 255 , 200 , 255 , 200 , 255 ) ;
final ColorRange SEEKINGCOLOR = new ColorRange ( 100 , 255 , 100 , 255 , 100 , 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 400 :
{
final ColorRange TARGETCOLOR = new ColorRange ( 200 , 255 , 150 , 255 , 0 , 50 ) ;
final ColorRange SEEKINGCOLOR = new ColorRange ( 100 , 255 , 60 , 255 , 0 , 50 ) ;
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 401 :
{
final ColorRange TARGETCOLOR = new ColorRange ( 0 , 60 , 0 , 100 , 0 , 100 ) ;
final ColorRange SEEKINGCOLOR = new ColorRange ( 0 , 100 , 0 , 180 , 0 , 180 ) ;
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 :
case 403 :
case 407 :
case 408 :
case 411 :
case 412 :
case 414 :
case 416 : //Error/Near
{
final ColorRange TARGETCOLOR = new ColorRange ( 240 , 255 , 240 , 255 , 240 , 255 ) ;
final ColorRange SEEKINGCOLOR = new ColorRange ( 120 , 255 , 120 , 255 , 120 , 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 404 :
case 406 :
case 410 : //Critical
{
final ColorRange TARGETCOLOR = new ColorRange ( 200 , 255 , 190 , 255 , 140 , 255 ) ;
final ColorRange SEEKINGCOLOR = new ColorRange ( 120 , 255 , 120 , 255 , 120 , 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 405 :
case 409 :
case 413 :
case 415 : //S-Critical
{
final ColorRange TARGETCOLOR = new ColorRange ( 200 , 255 , 200 , 255 , 0 , 50 ) ;
final ColorRange SEEKINGCOLOR = new ColorRange ( 120 , 255 , 120 , 255 , 120 , 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 417 :
{
final ColorRange TARGETCOLOR = new ColorRange ( 230 , 255 , 230 , 255 , 230 , 255 ) ;
final ColorRange SEEKINGCOLOR = new ColorRange ( 120 , 255 , 120 , 255 , 120 , 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 ;
}
}
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 ;
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 + + ) {
int [ ] arr = originalImg . getRGB ( readRegions . get ( i ) . x , readRegions . get ( i ) . y , 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 ) ;
//g.drawImage(originalImg, 0,currentHeight,readRegions.get(i).w,readRegions.get(i).h+currentHeight,readRegions.get(i).x, readRegions.get(i).y, readRegions.get(i).x+readRegions.get(i).w, readRegions.get(i).y+readRegions.get(i).h, null);
int leftMost = readRegions . get ( i ) . w ;
for ( int j = 0 ; j < arr . length ; j + + ) {
if ( arr [ j ] = = Color . MAGENTA . getRGB ( ) & & j % readRegions . get ( i ) . w < leftMost ) {
leftMost = j % readRegions . get ( i ) . w ;
}
}
if ( i = = 21 ) {
//Squish the difficulty together.
int pixelGap = 0 ;
int pixelGapStart = 0 ;
int reachedPoint = 0 ;
boolean found = false ;
for ( int x = 0 ; x < readRegions . get ( i ) . w ; x + + ) {
for ( int y = 0 ; y < readRegions . get ( i ) . h ; y + + ) {
if ( arr [ y * readRegions . get ( i ) . w + x ] ! = TRANSPARENT ) {
if ( pixelGap > 10 ) {
found = true ;
reachedPoint = x ;
System . out . println ( reachedPoint + "-" + pixelGapStart ) ;
break ;
}
pixelGap = 0 ;
pixelGapStart = x ;
break ;
}
}
if ( found ) {
break ;
}
pixelGap + + ;
}
final int PADDING = 14 ;
//From the pixelGapStart X, cut down the pixels to reach the point next to the previous pixels.
for ( int x = reachedPoint ; x < readRegions . get ( i ) . w ; x + + ) {
for ( int y = 0 ; y < readRegions . get ( i ) . h ; y + + ) {
arr [ y * readRegions . get ( i ) . w + x - pixelGapStart + PADDING ] = arr [ y * readRegions . get ( i ) . w + x ] ;
arr [ y * readRegions . get ( i ) . w + x ] = TRANSPARENT ;
}
}
}
if ( i > = 3 & & i < = 20 ) {
int [ ] arr2 = originalImg . getRGB ( extraRegions . get ( i - 3 ) . x , extraRegions . get ( i - 3 ) . y , extraRegions . get ( i - 3 ) . w , extraRegions . get ( i - 3 ) . h , null , 0 , extraRegions . get ( i - 3 ) . w ) ;
int rightMost = 0 ;
ColorFilter ( arr2 , 400 + i - 3 , extraRegions . get ( i - 3 ) . w ) ;
for ( int j = 0 ; j < arr2 . length ; j + + ) {
if ( arr2 [ j ] = = Color . MAGENTA . getRGB ( ) & & j % extraRegions . get ( i - 3 ) . w > rightMost ) {
rightMost = j % extraRegions . get ( i - 3 ) . w ;
}
}
//cutImg.setRGB(rightMost-leftMost,currentHeight,readRegions.get(i).w,readRegions.get(i).h,arr,0,readRegions.get(i).w);
cutImg . setRGB ( 0 , currentHeight , extraRegions . get ( i - 3 ) . w , extraRegions . get ( i - 3 ) . h , arr2 , 0 , extraRegions . get ( i - 3 ) . w ) ;
final int PADDING = 4 ;
for ( int x = leftMost ; x < readRegions . get ( i ) . w ; x + + ) {
for ( int y = 0 ; y < readRegions . get ( i ) . h ; y + + ) {
cutImg . setRGB ( x + rightMost - leftMost + PADDING , y + currentHeight , arr [ y * readRegions . get ( i ) . w + x ] ) ;
}
}
} else {
cutImg . setRGB ( 0 , currentHeight , readRegions . get ( i ) . w , readRegions . get ( i ) . h , arr , 0 , readRegions . get ( i ) . w ) ;
}
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 = 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 ) ) ;
g . dispose ( ) ;
} catch ( IOException e ) {
e . printStackTrace ( ) ;
}
//System.out.println(data[0]);
//System.out.println(data[2]);
}
}