Skip to content

Commit

Permalink
Focal point no longer works in image service #9244
Browse files Browse the repository at this point in the history
  • Loading branch information
rymsha committed Dec 3, 2021
1 parent 40e3112 commit e31f823
Show file tree
Hide file tree
Showing 8 changed files with 121 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,8 @@ private Builder()
{
this.top = 0;
this.left = 0;
this.bottom = 0;
this.right = 0;
this.bottom = 1.0;
this.right = 1.0;
this.zoom = 1.0;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public void test()
final ReadImageParams readImageParams = ReadImageParams.newImageParams().
contentId( ContentId.from( "contentId" ) ).
binaryReference( BinaryReference.from( "binaryReference" ) ).
cropping( Cropping.create().bottom( 1 ).right( 1 ).build() ).
cropping( Cropping.create().build() ).
scaleParams( new ScaleParams( "scaleParams", null ) ).
focalPoint( FocalPoint.DEFAULT ).
scaleSize( 1 ).
Expand All @@ -36,7 +36,7 @@ public void test()
assertEquals( ContentId.from( "contentId" ), readImageParams.getContentId() );
assertEquals( BinaryReference.from( "binaryReference" ), readImageParams.getBinaryReference() );
assertEquals( BinaryReference.from( "binaryReference" ), readImageParams.getBinaryReference() );
assertEquals( Cropping.create().bottom( 1 ).right( 1 ).build(), readImageParams.getCropping() );
assertEquals( Cropping.create().build(), readImageParams.getCropping() );
assertEquals( "scaleParams", readImageParams.getScaleParams().getName() );
assertEquals( FocalPoint.DEFAULT, readImageParams.getFocalPoint() );
assertEquals( 1, readImageParams.getScaleSize() );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class NormalizedImageParams
{
this.contentId = readImageParams.getContentId();
this.binaryReference = readImageParams.getBinaryReference();
this.cropping = readImageParams.getCropping();
this.cropping = normalizeCropping( readImageParams.getCropping() );
this.scaleParams = normalizeScaleParams( readImageParams );
this.focalPoint = readImageParams.getFocalPoint();
this.filterParam = FilterSetExpr.parse( readImageParams.getFilterParam() );
Expand Down Expand Up @@ -99,6 +99,11 @@ public ImageOrientation getOrientation()
return orientation;
}

private static Cropping normalizeCropping( final Cropping cropping )
{
return cropping == null || cropping.isUnmodified() ? null : cropping;
}

private static String normalizeFormat( final ReadImageParams readImageParams )
{
// Existing should not set format parameter, but if it does all bets are off.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,9 @@ private static int centerOffset( final int value1, final int value2, final doubl
{
int diff = value1 - value2;
final int centered = (int) ( value1 * offset ) - ( value2 / 2 );
return Math.max( Math.min( centered, 0 ), diff );
return Math.min( Math.max( centered, 0 ), diff );
}


private static Values scaledSub( final int newWidth, final int newHeight, final int widthOffset, final int heightOffset,
final int viewWidth, final int viewHeight )
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,6 @@ public int estimateResolution( final int sourceWidth, final int sourceHeight )
{
final ScaleCalculator.Values values = scaleCalculator.calc( sourceWidth, sourceHeight );

return Math.multiplyExact( values.newWidth, values.newWidth );
return Math.multiplyExact( values.newWidth, values.newHeight );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import org.junit.jupiter.api.Test;

import com.enonic.xp.content.ContentId;
import com.enonic.xp.image.Cropping;
import com.enonic.xp.image.ReadImageParams;
import com.enonic.xp.image.ScaleParams;
import com.enonic.xp.util.BinaryReference;
Expand All @@ -29,76 +30,68 @@ void normalizeFormat()
@Test
void normalizeNoScaleParams()
{
final ScaleParams scaleParams = new NormalizedImageParams( someFormatTemplate().
build() ).getScaleParams();
final ScaleParams scaleParams = new NormalizedImageParams( someFormatTemplate().build() ).getScaleParams();
assertNull( scaleParams );
}

@Test
void normalizeInsignificantCropping()
{
final ScaleParams scaleParams = new NormalizedImageParams(
someFormatTemplate().cropping( Cropping.create().build() ).build() ).getScaleParams();
assertNull( scaleParams );
}

@Test
void normalizeScaleParams()
{
final ScaleParams scaleParams = new NormalizedImageParams( someFormatTemplate().
scaleParams( new ScaleParams( "block", new Object[]{10, 15} ) ).
scaleSquare( true ).
scaleWidth( true ).
scaleSize( 10 ).
build() ).getScaleParams();
final ScaleParams scaleParams = new NormalizedImageParams(
someFormatTemplate().scaleParams( new ScaleParams( "block", new Object[]{10, 15} ) )
.scaleSquare( true )
.scaleWidth( true )
.scaleSize( 10 )
.build() ).getScaleParams();
assertEquals( "block", scaleParams.getName() );
assertArrayEquals( new Object[]{10, 15}, scaleParams.getArguments() );
}

@Test
void normalizeScaleSquire()
{
final ScaleParams scaleParams = new NormalizedImageParams( someFormatTemplate().
scaleSquare( true ).
scaleWidth( true ).
scaleSize( 10 ).
build() ).getScaleParams();
final ScaleParams scaleParams = new NormalizedImageParams(
someFormatTemplate().scaleSquare( true ).scaleWidth( true ).scaleSize( 10 ).build() ).getScaleParams();
assertEquals( "square", scaleParams.getName() );
assertArrayEquals( new Object[]{10}, scaleParams.getArguments() );
}

@Test
void normalizeScaleWidth()
{
final ScaleParams scaleParams = new NormalizedImageParams( someFormatTemplate().
scaleSquare( false ).
scaleWidth( true ).
scaleSize( 10 ).
build() ).getScaleParams();
final ScaleParams scaleParams = new NormalizedImageParams(
someFormatTemplate().scaleSquare( false ).scaleWidth( true ).scaleSize( 10 ).build() ).getScaleParams();
assertEquals( "width", scaleParams.getName() );
assertArrayEquals( new Object[]{10}, scaleParams.getArguments() );
}

@Test
void normalizeScaleMax()
{
final ScaleParams scaleParams = new NormalizedImageParams( someFormatTemplate().
scaleSquare( false ).
scaleWidth( false ).
scaleSize( 10 ).
build() ).getScaleParams();
final ScaleParams scaleParams = new NormalizedImageParams(
someFormatTemplate().scaleSquare( false ).scaleWidth( false ).scaleSize( 10 ).build() ).getScaleParams();
assertEquals( "max", scaleParams.getName() );
assertArrayEquals( new Object[]{10}, scaleParams.getArguments() );
}

@Test
void normalizeBackgroundColor()
{
assertEquals( 0xFFFFFF, new NormalizedImageParams( noFormatTemplate().
mimeType( "image/jpeg" ).
build() ).getBackgroundColor() );

assertEquals( 0x00FF00, new NormalizedImageParams( noFormatTemplate().
mimeType( "image/jpeg" ).
backgroundColor( 0x00FF00 ).
build() ).getBackgroundColor() );

assertEquals( 0xFFFFFF, new NormalizedImageParams( noFormatTemplate().
mimeType( "image/png" ).
backgroundColor( 0x00FF00 ).
build() ).getBackgroundColor() );
assertEquals( 0xFFFFFF, new NormalizedImageParams( noFormatTemplate().mimeType( "image/jpeg" ).build() ).getBackgroundColor() );

assertEquals( 0x00FF00, new NormalizedImageParams(
noFormatTemplate().mimeType( "image/jpeg" ).backgroundColor( 0x00FF00 ).build() ).getBackgroundColor() );

assertEquals( 0xFFFFFF, new NormalizedImageParams(
noFormatTemplate().mimeType( "image/png" ).backgroundColor( 0x00FF00 ).build() ).getBackgroundColor() );
}

private static ReadImageParams.Builder someFormatTemplate()
Expand All @@ -108,7 +101,6 @@ private static ReadImageParams.Builder someFormatTemplate()

private static ReadImageParams.Builder noFormatTemplate()
{
return ReadImageParams.newImageParams().contentId( ContentId.from( "123" ) ).
binaryReference( BinaryReference.from( "456" ) );
return ReadImageParams.newImageParams().contentId( ContentId.from( "123" ) ).binaryReference( BinaryReference.from( "456" ) );
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package com.enonic.xp.core.impl.image.effect;

import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.assertThat;

class ScaleCalculatorTest
{
@Test
void block_upscale_focal_upper_left()
{
final ScaleCalculator.Values values = ScaleCalculator.block( 1000, 1000, 0, 0 ).calc( 100, 200 );

assertThat( values ).extracting( v -> v.newWidth, v -> v.newHeight, v -> v.widthOffset, v -> v.heightOffset, v -> v.viewWidth,
v -> v.viewHeight ).containsExactly( 1000, 2000, 0, 0, 1000, 1000 );
}

@Test
void block_focal_upper_left()
{
final ScaleCalculator.Values values = ScaleCalculator.block( 10, 10, 0, 0 ).calc( 100, 200 );

assertThat( values ).extracting( v -> v.newWidth, v -> v.newHeight, v -> v.widthOffset, v -> v.heightOffset, v -> v.viewWidth,
v -> v.viewHeight ).containsExactly( 10, 20, 0, 0, 10, 10 );
}

@Test
void block_focal_lower_left()
{
final ScaleCalculator.Values values = ScaleCalculator.block( 10, 10, 0, 1 ).calc( 100, 200 );

assertThat( values ).extracting( v -> v.newWidth, v -> v.newHeight, v -> v.widthOffset, v -> v.heightOffset, v -> v.viewWidth,
v -> v.viewHeight ).containsExactly( 10, 20, 0, 10, 10, 10 );
}

@Test
void block_mixscale_focal_lower_left()
{
final ScaleCalculator.Values values = ScaleCalculator.block( 1000, 10, 0, 1 ).calc( 100, 200 );

assertThat( values ).extracting( v -> v.newWidth, v -> v.newHeight, v -> v.widthOffset, v -> v.heightOffset, v -> v.viewWidth,
v -> v.viewHeight ).containsExactly( 1000, 2000, 0, 1990, 1000, 10 );
}

@Test
void block_focal_upper_right()
{
final ScaleCalculator.Values values = ScaleCalculator.block( 10, 10, 1, 0 ).calc( 200, 100 );

assertThat( values ).extracting( v -> v.newWidth, v -> v.newHeight, v -> v.widthOffset, v -> v.heightOffset, v -> v.viewWidth,
v -> v.viewHeight ).containsExactly( 20, 10, 10, 0, 10, 10 );
}

@Test
void block_focal_lower_right()
{
final ScaleCalculator.Values values = ScaleCalculator.block( 10, 10, 1, 1 ).calc( 200, 100 );

assertThat( values ).extracting( v -> v.newWidth, v -> v.newHeight, v -> v.widthOffset, v -> v.heightOffset, v -> v.viewWidth,
v -> v.viewHeight ).containsExactly( 20, 10, 10, 0, 10, 10 );
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.enonic.xp.core.impl.image.effect;

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;

class ScaledFunctionTest
{
@Test
void estimateResolution()
{
final ScaledFunction scaledFunction = new ScaledFunction( ScaleCalculator.block( 10, 15, 0.5, 0.5 ) );
assertEquals( 150, scaledFunction.estimateResolution( 1000, 1500 ) );

}
}

0 comments on commit e31f823

Please sign in to comment.