@@ -271,6 +271,15 @@ export class TestRunsService {
271271 } ) ;
272272 }
273273
274+ private scaleImageToSize ( image : PNG , width : number , height : number ) : PNG {
275+ if ( width > image . width || height > image . height ) {
276+ const preparedImage = new PNG ( { width, height, fill : true } ) ;
277+ PNG . bitblt ( image , preparedImage , 0 , 0 , image . width , image . height ) ;
278+ return preparedImage ;
279+ }
280+ return image ;
281+ }
282+
274283 getDiff ( baseline : PNG , image : PNG , testRun : TestRun ) : DiffResult {
275284 const result : DiffResult = {
276285 status : undefined ,
@@ -280,42 +289,47 @@ export class TestRunsService {
280289 isSameDimension : undefined ,
281290 } ;
282291
283- if ( baseline ) {
284- result . isSameDimension = baseline . width === image . width && baseline . height === image . height ;
285-
286- if ( result . isSameDimension ) {
287- const diff = new PNG ( {
288- width : baseline . width ,
289- height : baseline . height ,
290- } ) ;
291-
292- const ignoreAreas = this . getIgnoteAreas ( testRun ) ;
293- // compare
294- result . pixelMisMatchCount = Pixelmatch (
295- this . applyIgnoreAreas ( baseline , ignoreAreas ) ,
296- this . applyIgnoreAreas ( image , ignoreAreas ) ,
297- diff . data ,
298- baseline . width ,
299- baseline . height ,
300- {
301- includeAA : true ,
302- }
303- ) ;
304- result . diffPercent = ( result . pixelMisMatchCount * 100 ) / ( image . width * image . height ) ;
305-
306- if ( result . diffPercent > testRun . diffTollerancePercent ) {
307- // save diff
308- result . diffName = this . staticService . saveImage ( 'diff' , PNG . sync . write ( diff ) ) ;
309- result . status = TestStatus . unresolved ;
310- } else {
311- result . status = TestStatus . ok ;
312- }
313- } else {
314- // diff dimensions
315- result . status = TestStatus . unresolved ;
316- }
292+ if ( ! baseline ) {
293+ // no baseline
294+ return result ;
295+ }
296+
297+ result . isSameDimension = baseline . width === image . width && baseline . height === image . height ;
298+
299+ if ( ! result . isSameDimension && ! process . env . ALLOW_DIFF_DIMENSIONS ) {
300+ // diff dimensions
301+ result . status = TestStatus . unresolved ;
302+ return result ;
317303 }
304+ // scale image to max size
305+ const maxWidth = Math . max ( baseline . width , image . width ) ;
306+ const maxHeight = Math . max ( baseline . height , image . height ) ;
307+ const scaledBaseline = this . scaleImageToSize ( baseline , maxWidth , maxHeight ) ;
308+ const scaledImage = this . scaleImageToSize ( image , maxWidth , maxHeight ) ;
309+
310+ // apply ignore areas
311+ const ignoreAreas = this . getIgnoteAreas ( testRun ) ;
312+ const baselineData = this . applyIgnoreAreas ( scaledBaseline , ignoreAreas ) ;
313+ const imageData = this . applyIgnoreAreas ( scaledImage , ignoreAreas ) ;
314+
315+ // compare
316+ const diff = new PNG ( {
317+ width : maxWidth ,
318+ height : maxHeight ,
319+ } ) ;
320+ result . pixelMisMatchCount = Pixelmatch ( baselineData , imageData , diff . data , maxWidth , maxHeight , {
321+ includeAA : true ,
322+ } ) ;
323+ result . diffPercent = ( result . pixelMisMatchCount * 100 ) / ( scaledImage . width * scaledImage . height ) ;
318324
325+ // process result
326+ if ( result . diffPercent > testRun . diffTollerancePercent ) {
327+ // save diff
328+ result . diffName = this . staticService . saveImage ( 'diff' , PNG . sync . write ( diff ) ) ;
329+ result . status = TestStatus . unresolved ;
330+ } else {
331+ result . status = TestStatus . ok ;
332+ }
319333 return result ;
320334 }
321335
0 commit comments