github.com/status-im/status-go@v1.1.0/images/manipulation_test.go (about)

     1  package images
     2  
     3  import (
     4  	"bytes"
     5  	"errors"
     6  	"image"
     7  	"testing"
     8  
     9  	"github.com/stretchr/testify/require"
    10  )
    11  
    12  func TestResize(t *testing.T) {
    13  	sizes := []ResizeDimension{80, 240, 1000}
    14  
    15  	cs := []struct {
    16  		Filename string
    17  		Bounds   map[ResizeDimension]image.Rectangle
    18  	}{
    19  		{
    20  			"elephant.jpg",
    21  			map[ResizeDimension]image.Rectangle{
    22  				80: {
    23  					Min: image.Point{X: 0, Y: 0},
    24  					Max: image.Point{X: 80, Y: 80},
    25  				},
    26  				240: {
    27  					Min: image.Point{X: 0, Y: 0},
    28  					Max: image.Point{X: 240, Y: 240},
    29  				},
    30  				1000: {
    31  					Min: image.Point{X: 0, Y: 0},
    32  					Max: image.Point{X: 1000, Y: 1000},
    33  				},
    34  			},
    35  		},
    36  		{
    37  			"rose.webp",
    38  			map[ResizeDimension]image.Rectangle{
    39  				80: {
    40  					Min: image.Point{X: 0, Y: 0},
    41  					Max: image.Point{X: 107, Y: 80},
    42  				},
    43  				240: {
    44  					Min: image.Point{X: 0, Y: 0},
    45  					Max: image.Point{X: 319, Y: 240},
    46  				},
    47  				1000: {
    48  					Min: image.Point{X: 0, Y: 0},
    49  					Max: image.Point{X: 1329, Y: 1000},
    50  				},
    51  			},
    52  		},
    53  		{
    54  			"spin.gif",
    55  			map[ResizeDimension]image.Rectangle{
    56  				80: {
    57  					Min: image.Point{X: 0, Y: 0},
    58  					Max: image.Point{X: 80, Y: 80},
    59  				},
    60  				240: {
    61  					Min: image.Point{X: 0, Y: 0},
    62  					Max: image.Point{X: 240, Y: 240},
    63  				},
    64  				1000: {
    65  					Min: image.Point{X: 0, Y: 0},
    66  					Max: image.Point{X: 1000, Y: 1000},
    67  				},
    68  			},
    69  		},
    70  		{
    71  			"status.png",
    72  			map[ResizeDimension]image.Rectangle{
    73  				80: {
    74  					Min: image.Point{X: 0, Y: 0},
    75  					Max: image.Point{X: 80, Y: 80},
    76  				},
    77  				240: {
    78  					Min: image.Point{X: 0, Y: 0},
    79  					Max: image.Point{X: 240, Y: 240},
    80  				},
    81  				1000: {
    82  					Min: image.Point{X: 0, Y: 0},
    83  					Max: image.Point{X: 1000, Y: 1000},
    84  				},
    85  			},
    86  		},
    87  	}
    88  
    89  	for _, c := range cs {
    90  		img, err := Decode(path + c.Filename)
    91  		require.NoError(t, err)
    92  
    93  		for _, s := range sizes {
    94  			rsImg := Resize(s, img)
    95  			require.Exactly(t, c.Bounds[s], rsImg.Bounds())
    96  		}
    97  	}
    98  }
    99  
   100  // Requirement of ShrinkOnly
   101  func TestThatResizeResizesSmallerHeightByTheSmallestSide(t *testing.T) {
   102  	image := image.NewRGBA(image.Rect(0, 0, 4, 2))
   103  	resizedImage := Resize(ResizeDimension(1), image)
   104  	require.Exactly(t, 2, resizedImage.Bounds().Dx())
   105  	require.Exactly(t, 1, resizedImage.Bounds().Dy())
   106  }
   107  
   108  // Requirement of ShrinkOnly
   109  func TestThatResizeResizesSmallerWidthByTheSmallestSide(t *testing.T) {
   110  	image := image.NewRGBA(image.Rect(0, 0, 4, 8))
   111  	resizedImage := Resize(ResizeDimension(2), image)
   112  	require.Exactly(t, 2, resizedImage.Bounds().Dx())
   113  	require.Exactly(t, 4, resizedImage.Bounds().Dy())
   114  }
   115  
   116  func TestCrop(t *testing.T) {
   117  	type params struct {
   118  		Rectangle   image.Rectangle
   119  		OutputBound image.Rectangle
   120  		OutputSize  int
   121  		CropError   error
   122  	}
   123  
   124  	topLeftSquare := image.Rectangle{
   125  		Min: image.Point{X: 0, Y: 0},
   126  		Max: image.Point{X: 80, Y: 80},
   127  	}
   128  	offsetSquare := image.Rectangle{
   129  		Min: image.Point{X: 80, Y: 80},
   130  		Max: image.Point{X: 160, Y: 160},
   131  	}
   132  	outOfBoundsSquare := image.Rectangle{
   133  		Min: image.Point{X: 0, Y: 0},
   134  		Max: image.Point{X: 1000000, Y: 1000000},
   135  	}
   136  	rect := image.Rectangle{}
   137  	options := EncodeConfig{
   138  		Quality: 70,
   139  	}
   140  
   141  	cs := []struct {
   142  		Filename string
   143  		Params   []params
   144  	}{
   145  		{
   146  			"elephant.jpg",
   147  			[]params{
   148  				{topLeftSquare, topLeftSquare, 1447, nil},
   149  				{offsetSquare, rect, 0, errors.New("crop dimensions out of bounds of image, image width '80px' & height '80px'; crop bottom right coordinate at X '160px' Y '160px'")},
   150  				{outOfBoundsSquare, rect, 0, errors.New("crop dimensions out of bounds of image, image width '80px' & height '80px'; crop bottom right coordinate at X '1000000px' Y '1000000px'")},
   151  			},
   152  		},
   153  		{
   154  			"rose.webp",
   155  			[]params{
   156  				{topLeftSquare, topLeftSquare, 1183, nil},
   157  				{offsetSquare, offsetSquare, 1251, nil},
   158  				{outOfBoundsSquare, rect, 0, errors.New("crop dimensions out of bounds of image, image width '400px' & height '301px'; crop bottom right coordinate at X '1000000px' Y '1000000px'")},
   159  			},
   160  		},
   161  		{
   162  			"spin.gif",
   163  			[]params{
   164  				{topLeftSquare, topLeftSquare, 693, nil},
   165  				{offsetSquare, offsetSquare, 1339, nil},
   166  				{outOfBoundsSquare, rect, 0, errors.New("crop dimensions out of bounds of image, image width '256px' & height '256px'; crop bottom right coordinate at X '1000000px' Y '1000000px'")},
   167  			},
   168  		},
   169  		{
   170  			"status.png",
   171  			[]params{
   172  				{topLeftSquare, topLeftSquare, 1027, nil},
   173  				{offsetSquare, offsetSquare, 1157, nil},
   174  				{outOfBoundsSquare, rect, 0, errors.New("crop dimensions out of bounds of image, image width '256px' & height '256px'; crop bottom right coordinate at X '1000000px' Y '1000000px'")},
   175  			},
   176  		},
   177  	}
   178  
   179  	for _, c := range cs {
   180  		img, err := Decode(path + c.Filename)
   181  		require.NoError(t, err)
   182  
   183  		for _, p := range c.Params {
   184  			cImg, err := Crop(img, p.Rectangle)
   185  			if p.CropError != nil {
   186  				require.EqualError(t, err, p.CropError.Error())
   187  				continue
   188  			} else {
   189  				require.NoError(t, err)
   190  			}
   191  			require.Exactly(t, p.OutputBound.Dx(), cImg.Bounds().Dx(), c.Filename)
   192  			require.Exactly(t, p.OutputBound.Dy(), cImg.Bounds().Dy(), c.Filename)
   193  
   194  			bb := bytes.NewBuffer([]byte{})
   195  			err = Encode(bb, cImg, options)
   196  			require.NoError(t, err)
   197  			require.Exactly(t, p.OutputSize, bb.Len())
   198  		}
   199  	}
   200  }