|
| 1 | +import 'dart:io'; |
| 2 | +import 'package:image/image.dart'; |
| 3 | +import 'package:test/test.dart'; |
| 4 | + |
| 5 | +import '../_test_util.dart'; |
| 6 | + |
| 7 | +void main() { |
| 8 | + group('Transform', () { |
| 9 | + test('resize nearest', () { |
| 10 | + final img = |
| 11 | + decodePng(File('test/_data/png/buck_24.png').readAsBytesSync())!; |
| 12 | + final i0 = resize(img, width: 64); |
| 13 | + expect(i0.width, equals(64)); |
| 14 | + expect(i0.height, equals(40)); |
| 15 | + File('$testOutputPath/transform/resize.png') |
| 16 | + ..createSync(recursive: true) |
| 17 | + ..writeAsBytesSync(encodePng(i0)); |
| 18 | + }); |
| 19 | + |
| 20 | + test('resize average', () { |
| 21 | + final img = |
| 22 | + decodePng(File('test/_data/png/buck_24.png').readAsBytesSync())!; |
| 23 | + final i0 = |
| 24 | + resize(img, width: 64, interpolation: Interpolation.average); |
| 25 | + expect(i0.width, equals(64)); |
| 26 | + expect(i0.height, equals(40)); |
| 27 | + File('$testOutputPath/transform/resize_average.png') |
| 28 | + ..createSync(recursive: true) |
| 29 | + ..writeAsBytesSync(encodePng(i0)); |
| 30 | + }); |
| 31 | + |
| 32 | + test('resize linear', () { |
| 33 | + final img = |
| 34 | + decodePng(File('test/_data/png/buck_24.png').readAsBytesSync())!; |
| 35 | + final i0 = |
| 36 | + resize(img, width: 64, interpolation: Interpolation.linear); |
| 37 | + expect(i0.width, equals(64)); |
| 38 | + expect(i0.height, equals(40)); |
| 39 | + File('$testOutputPath/transform/resize_linear.png') |
| 40 | + ..createSync(recursive: true) |
| 41 | + ..writeAsBytesSync(encodePng(i0)); |
| 42 | + }); |
| 43 | + |
| 44 | + test('resize cubic', () { |
| 45 | + final img = |
| 46 | + decodePng(File('test/_data/png/buck_24.png').readAsBytesSync())!; |
| 47 | + final i0 = resize(img, width: 64, interpolation: Interpolation.cubic); |
| 48 | + expect(i0.width, equals(64)); |
| 49 | + expect(i0.height, equals(40)); |
| 50 | + File('$testOutputPath/transform/resize_cubic.png') |
| 51 | + ..createSync(recursive: true) |
| 52 | + ..writeAsBytesSync(encodePng(i0)); |
| 53 | + }); |
| 54 | + |
| 55 | + test('resize maintainAspect', () { |
| 56 | + final img = |
| 57 | + decodePng(File('test/_data/png/buck_24.png').readAsBytesSync())!; |
| 58 | + final i0 = resize(img, |
| 59 | + width: 640, |
| 60 | + height: 640, |
| 61 | + maintainAspect: true, |
| 62 | + backgroundColor: ColorRgb8(0, 0, 255)); |
| 63 | + expect(i0.width, equals(640)); |
| 64 | + expect(i0.height, equals(640)); |
| 65 | + File('$testOutputPath/transform/resize_maintainAspect.png') |
| 66 | + ..createSync(recursive: true) |
| 67 | + ..writeAsBytesSync(encodePng(i0)); |
| 68 | + }); |
| 69 | + |
| 70 | + test('resize maintainAspect palette', () { |
| 71 | + final img = |
| 72 | + decodePng(File('test/_data/png/buck_8.png').readAsBytesSync())!; |
| 73 | + final i0 = resize(img, |
| 74 | + width: 640, |
| 75 | + height: 640, |
| 76 | + maintainAspect: true, |
| 77 | + backgroundColor: ColorRgb8(0, 0, 255)); |
| 78 | + expect(i0.width, equals(640)); |
| 79 | + expect(i0.height, equals(640)); |
| 80 | + File('$testOutputPath/transform/resize_maintainAspect_palette.png') |
| 81 | + ..createSync(recursive: true) |
| 82 | + ..writeAsBytesSync(encodePng(i0)); |
| 83 | + }); |
| 84 | + |
| 85 | + test('resize maintainAspect 2', () { |
| 86 | + final i0 = Image(width: 100, height: 50)..clear(ColorRgb8(255, 0, 0)); |
| 87 | + final i1 = resize(i0, |
| 88 | + width: 200, |
| 89 | + height: 200, |
| 90 | + maintainAspect: true, |
| 91 | + backgroundColor: ColorRgb8(0, 0, 255)); |
| 92 | + expect(i1.width, equals(200)); |
| 93 | + expect(i1.height, equals(200)); |
| 94 | + File('$testOutputPath/transform/resize_maintainAspect_2.png') |
| 95 | + ..createSync(recursive: true) |
| 96 | + ..writeAsBytesSync(encodePng(i1)); |
| 97 | + }); |
| 98 | + |
| 99 | + test('resize maintainAspect 3', () { |
| 100 | + final i0 = Image(width: 50, height: 100)..clear(ColorRgb8(0, 255, 0)); |
| 101 | + final i1 = resize(i0, |
| 102 | + width: 200, |
| 103 | + height: 200, |
| 104 | + maintainAspect: true, |
| 105 | + backgroundColor: ColorRgb8(0, 0, 255)); |
| 106 | + expect(i1.width, equals(200)); |
| 107 | + expect(i1.height, equals(200)); |
| 108 | + File('$testOutputPath/transform/resize_maintainAspect_3.png') |
| 109 | + ..createSync(recursive: true) |
| 110 | + ..writeAsBytesSync(encodePng(i1)); |
| 111 | + }); |
| 112 | + |
| 113 | + test('resize maintainAspect 4', () { |
| 114 | + final i0 = Image(width: 100, height: 50)..clear(ColorRgb8(255, 0, 0)); |
| 115 | + final i1 = resize(i0, |
| 116 | + width: 50, |
| 117 | + height: 100, |
| 118 | + maintainAspect: true, |
| 119 | + backgroundColor: ColorRgb8(0, 0, 255)); |
| 120 | + expect(i1.width, equals(50)); |
| 121 | + expect(i1.height, equals(100)); |
| 122 | + File('$testOutputPath/transform/resize_maintainAspect_4.png') |
| 123 | + ..createSync(recursive: true) |
| 124 | + ..writeAsBytesSync(encodePng(i1)); |
| 125 | + }); |
| 126 | + |
| 127 | + test('resize maintainAspect 5', () { |
| 128 | + final i0 = Image(width: 50, height: 100)..clear(ColorRgb8(0, 255, 0)); |
| 129 | + final i1 = resize(i0, |
| 130 | + width: 100, |
| 131 | + height: 50, |
| 132 | + maintainAspect: true, |
| 133 | + backgroundColor: ColorRgb8(0, 0, 255)); |
| 134 | + expect(i1.width, equals(100)); |
| 135 | + expect(i1.height, equals(50)); |
| 136 | + File('$testOutputPath/transform/resize_maintainAspect_5.png') |
| 137 | + ..createSync(recursive: true) |
| 138 | + ..writeAsBytesSync(encodePng(i1)); |
| 139 | + }); |
| 140 | + |
| 141 | + test('resize maintainAspect 5', () { |
| 142 | + final i0 = Image(width: 50, height: 100)..clear(ColorRgb8(0, 255, 0)); |
| 143 | + final i1 = resize(i0, |
| 144 | + width: 100, |
| 145 | + height: 500, |
| 146 | + maintainAspect: true, |
| 147 | + backgroundColor: ColorRgb8(0, 0, 255)); |
| 148 | + expect(i1.width, equals(100)); |
| 149 | + expect(i1.height, equals(500)); |
| 150 | + File('$testOutputPath/transform/resize_maintainAspect_5.png') |
| 151 | + ..createSync(recursive: true) |
| 152 | + ..writeAsBytesSync(encodePng(i1)); |
| 153 | + }); |
| 154 | + |
| 155 | + test('resize maintainAspect 6', () { |
| 156 | + final i0 = Image(width: 100, height: 50)..clear(ColorRgb8(0, 255, 0)); |
| 157 | + final i1 = resize(i0, |
| 158 | + width: 500, |
| 159 | + height: 100, |
| 160 | + maintainAspect: true, |
| 161 | + backgroundColor: ColorRgb8(0, 0, 255)); |
| 162 | + expect(i1.width, equals(500)); |
| 163 | + expect(i1.height, equals(100)); |
| 164 | + File('$testOutputPath/transform/resize_maintainAspect_6.png') |
| 165 | + ..createSync(recursive: true) |
| 166 | + ..writeAsBytesSync(encodePng(i1)); |
| 167 | + }); |
| 168 | + |
| 169 | + test('resize palette', () async { |
| 170 | + final img = await decodePngFile('test/_data/png/test.png'); |
| 171 | + final i0 = |
| 172 | + resize(img!, width: 64, interpolation: Interpolation.cubic); |
| 173 | + await encodePngFile( |
| 174 | + '$testOutputPath/transform/resize_palette.png', i0); |
| 175 | + }); |
| 176 | + }); |
| 177 | +} |
0 commit comments