@@ -3,7 +3,7 @@ import path from "path";
33import looksSame from "looks-same" ;
44import { CoreError } from "./core-error" ;
55import { ExistingBrowser } from "./existing-browser" ;
6- import type { Image } from "../image" ;
6+ import type { Image , RGB } from "../image" ;
77
88const DIRECTION = { FORWARD : "forward" , REVERSE : "reverse" } as const ;
99
@@ -49,7 +49,11 @@ export class Calibrator {
4949
5050 const { innerWidth, pixelRatio } = features ;
5151 const hasPixelRatio = Boolean ( pixelRatio && pixelRatio > 1.0 ) ;
52- const imageFeatures = await this . _analyzeImage ( image , { calculateColorLength : hasPixelRatio } ) ;
52+ const { width : imageWidth , height : imageHeight } = image . getSize ( ) ;
53+ const searchColor = image . hasICCPChunk
54+ ? await image . getRGB ( Math . floor ( imageWidth / 2 ) , Math . floor ( imageHeight / 2 ) )
55+ : { R : 148 , G : 250 , B : 0 } ;
56+ const imageFeatures = await this . _analyzeImage ( image , { calculateColorLength : hasPixelRatio , searchColor } ) ;
5357
5458 if ( ! imageFeatures ) {
5559 throw new CoreError (
@@ -70,9 +74,9 @@ export class Calibrator {
7074
7175 private async _analyzeImage (
7276 image : Image ,
73- params : { calculateColorLength ?: boolean } ,
77+ params : { calculateColorLength ?: boolean ; searchColor : RGB } ,
7478 ) : Promise < ImageAnalysisResult | null > {
75- const imageHeight = ( await image . getSize ( ) ) . height ;
79+ const imageHeight = image . getSize ( ) . height ;
7680
7781 for ( let y = 0 ; y < imageHeight ; y ++ ) {
7882 const result = await analyzeRow ( y , image , params ) ;
@@ -88,9 +92,9 @@ export class Calibrator {
8892async function analyzeRow (
8993 row : number ,
9094 image : Image ,
91- params : { calculateColorLength ?: boolean } = { } ,
95+ params : { calculateColorLength ?: boolean ; searchColor : RGB } ,
9296) : Promise < ImageAnalysisResult | null > {
93- const markerStart = await findMarkerInRow ( row , image , DIRECTION . FORWARD ) ;
97+ const markerStart = await findMarkerInRow ( row , image , DIRECTION . FORWARD , params . searchColor ) ;
9498
9599 if ( markerStart === - 1 ) {
96100 return null ;
@@ -102,15 +106,19 @@ async function analyzeRow(
102106 return result ;
103107 }
104108
105- const markerEnd = await findMarkerInRow ( row , image , DIRECTION . REVERSE ) ;
109+ const markerEnd = await findMarkerInRow ( row , image , DIRECTION . REVERSE , params . searchColor ) ;
106110 const colorLength = markerEnd - markerStart + 1 ;
107111
108112 return { ...result , colorLength } ;
109113}
110114
111- async function findMarkerInRow ( row : number , image : Image , searchDirection : "forward" | "reverse" ) : Promise < number > {
112- const imageWidth = ( await image . getSize ( ) ) . width ;
113- const searchColor = { R : 148 , G : 250 , B : 0 } ;
115+ async function findMarkerInRow (
116+ row : number ,
117+ image : Image ,
118+ searchDirection : "forward" | "reverse" ,
119+ searchColor : RGB ,
120+ ) : Promise < number > {
121+ const imageWidth = image . getSize ( ) . width ;
114122
115123 if ( searchDirection === DIRECTION . REVERSE ) {
116124 return searchReverse_ ( ) ;
0 commit comments