Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Jose T. - Upgraded to Null Safety as well as latest sdk #10

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions example/bin/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import 'package:diff_image/diff_image.dart';
import 'package:image/image.dart';

void main() async {
final FIRST_IMAGE_URL =
'https://raw.githubusercontent.com/nicolashahn/diffimg/master/images/mario-circle-cs.png';
final SECOND_IMAGE_URL =
'https://raw.githubusercontent.com/nicolashahn/diffimg/master/images/mario-circle-node.png';
final FIRST_IMAGE_URL = Uri.parse(
'https://raw.githubusercontent.com/nicolashahn/diffimg/master/images/mario-circle-cs.png');
final SECOND_IMAGE_URL = Uri.parse(
'https://raw.githubusercontent.com/nicolashahn/diffimg/master/images/mario-circle-node.png');

// You need a try/catch block to handle the exceptions (http request, different size, etc)
try {
Expand Down
17 changes: 8 additions & 9 deletions lib/src/diff_image_html.dart
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import 'package:image/image.dart';
import 'package:meta/meta.dart';
import 'helper_functions.dart';
import 'models/diff_img_result.dart';

class DiffImage {
/// Returns a single number representing the difference between two RGB pixels
static num _diffBetweenPixels({
@required int firstPixel,
@required bool ignoreAlpha,
@required int secondPixel,
required int firstPixel,
required bool ignoreAlpha,
required int secondPixel,
}) {
var fRed = getRed(firstPixel);
var fGreen = getGreen(firstPixel);
Expand Down Expand Up @@ -100,9 +99,9 @@ class DiffImage {
secondPixel = secondImg.getPixel(i, j);

diffAtPixel = _diffBetweenPixels(
firstPixel: firstPixel,
firstPixel: firstPixel.toInt(),
ignoreAlpha: ignoreAlpha,
secondPixel: secondPixel,
secondPixel: secondPixel.toInt(),
);
diff += diffAtPixel;

Expand All @@ -112,8 +111,8 @@ class DiffImage {
j,
selectColor(
diffAtPixel: diffAtPixel,
firstPixel: firstPixel,
secondPixel: secondPixel,
firstPixel: firstPixel.toInt(),
secondPixel: secondPixel.toInt(),
),
);
}
Expand All @@ -132,7 +131,7 @@ class DiffImage {
/// Function to store an [Image] object as PNG in local storage.
/// Not supported on web.
static Future<void> saveDiffImg({
@required Image diffImg,
required Image diffImg,
}) async {
// TODO Define if can download image file or just show
throw UnsupportedError(
Expand Down
21 changes: 10 additions & 11 deletions lib/src/diff_image_io.dart
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
import 'dart:io' as io;

import 'package:image/image.dart';
import 'package:meta/meta.dart';
import 'helper_functions.dart';
import 'models/diff_img_result.dart';

class DiffImage {
/// Returns a single number representing the difference between two RGB pixels
static num _diffBetweenPixels({
@required int firstPixel,
@required bool ignoreAlpha,
@required int secondPixel,
required int firstPixel,
required bool ignoreAlpha,
required int secondPixel,
}) {
var fRed = getRed(firstPixel);
var fGreen = getGreen(firstPixel);
Expand Down Expand Up @@ -43,8 +42,8 @@ class DiffImage {
///
/// Can throw an [Exception].
static Future<DiffImgResult> compareFromUrl(
dynamic firstImgSrc,
dynamic secondImgSrc, {
Uri firstImgSrc,
Uri secondImgSrc, {
bool asPercentage = true,
bool ignoreAlpha = true,
}) async {
Expand Down Expand Up @@ -102,9 +101,9 @@ class DiffImage {
secondPixel = secondImg.getPixel(i, j);

diffAtPixel = _diffBetweenPixels(
firstPixel: firstPixel,
firstPixel: firstPixel.toInt(),
ignoreAlpha: ignoreAlpha,
secondPixel: secondPixel,
secondPixel: secondPixel.toInt(),
);
diff += diffAtPixel;

Expand All @@ -114,8 +113,8 @@ class DiffImage {
j,
selectColor(
diffAtPixel: diffAtPixel,
firstPixel: firstPixel,
secondPixel: secondPixel,
firstPixel: firstPixel.toInt(),
secondPixel: secondPixel.toInt(),
),
);
}
Expand All @@ -134,7 +133,7 @@ class DiffImage {
/// Function to store an [Image] object as PNG in local storage.
/// Not supported on web.
static Future<void> saveDiffImg({
@required Image diffImg,
required Image diffImg,
}) async {
await io.File('DiffImg.png').writeAsBytes(
encodePng(diffImg),
Expand Down
15 changes: 7 additions & 8 deletions lib/src/helper_functions.dart
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import 'package:http/http.dart' as http;
import 'package:image/image.dart';
import 'package:meta/meta.dart';

/// Through http get request to [imgSrc] obtains the bytes
/// that make up an image. Can throw an [Exception].
Future<Image> getImg({@required dynamic imgSrc}) async {
Image img;
Future<Image> getImg({required Uri imgSrc}) async {
Image? img;

var response = await http.get(imgSrc);
if (response.statusCode != 200) {
Expand All @@ -26,8 +25,8 @@ Future<Image> getImg({@required dynamic imgSrc}) async {

/// Check if [firstImg] and [secondImg] have the same width and height.
bool haveSameSize({
@required Image firstImg,
@required Image secondImg,
required Image firstImg,
required Image secondImg,
}) {
return firstImg.width == secondImg.width &&
firstImg.height == secondImg.height;
Expand All @@ -37,9 +36,9 @@ bool haveSameSize({
/// If one of the pixels is black, the resulting color will be the
/// other pixel but more transparent.
int selectColor({
@required num diffAtPixel,
@required int firstPixel,
@required int secondPixel,
required num diffAtPixel,
required int firstPixel,
required int secondPixel,
}) {
int result;

Expand Down
5 changes: 2 additions & 3 deletions lib/src/models/diff_img_result.dart
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import 'package:image/image.dart';
import 'package:meta/meta.dart';

/// Model to encapsulate the results of a difference between images
/// query.
class DiffImgResult {
DiffImgResult({
@required this.diffImg,
@required this.diffValue,
required this.diffImg,
required this.diffValue,
});

final Image diffImg;
Expand Down
10 changes: 4 additions & 6 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,12 @@ version: 1.0.1
homepage: https://github.com/limonadev/diff_image

environment:
sdk: '>=2.5.0 <3.0.0'
sdk: ">=2.15.0 <3.0.0"

dependencies:
http: ^0.12.2
image: ^2.1.18
meta: ^1.1.8
http: ^0.13.5
image: ^3.2.0
# path: ^1.6.0

dev_dependencies:
pedantic: ^1.8.0
test: ^1.15.4
test: ^1.21.4
15 changes: 7 additions & 8 deletions test/helper_test.dart
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
@TestOn('vm')

import 'package:test/test.dart';
import 'package:image/image.dart';

import 'package:diff_image/src/helper_functions.dart';
import 'package:image/image.dart';
import 'package:test/test.dart';

void main() {
group('Test for helper functions', () {
String flutterLogoUrl, badUrl, anotherLogoUrl;
late String flutterLogoUrl, badUrl, anotherLogoUrl;

setUp(() {
// A real image
Expand All @@ -22,13 +21,13 @@ void main() {

test('Get image from url', () async {
var first = await getImg(
imgSrc: flutterLogoUrl,
imgSrc: Uri.parse(flutterLogoUrl),
);
expect(first, isA<Image>());
expect(
() async {
await getImg(
imgSrc: badUrl,
imgSrc: Uri.parse(badUrl),
);
},
throwsException,
Expand All @@ -37,7 +36,7 @@ void main() {

test('Compare images size', () async {
var img1 = await getImg(
imgSrc: flutterLogoUrl,
imgSrc: Uri.parse(flutterLogoUrl),
);
var areEquals = haveSameSize(
firstImg: img1,
Expand All @@ -46,7 +45,7 @@ void main() {
expect(areEquals, isTrue);

var img2 = await getImg(
imgSrc: anotherLogoUrl,
imgSrc: Uri.parse(anotherLogoUrl),
);
areEquals = haveSameSize(
firstImg: img1,
Expand Down
14 changes: 7 additions & 7 deletions test/html_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,18 @@ import 'package:test/test.dart';

void main() {
group('Test when dart:html is supported', () {
String flutterLogoUrl, dartLogoUrl, androidLogoUrl;
late Uri flutterLogoUrl, dartLogoUrl, androidLogoUrl;

setUp(() {
// A real image
flutterLogoUrl =
'https://seeklogo.com/images/F/flutter-logo-5086DD11C5-seeklogo.com.png';
flutterLogoUrl = Uri.parse(
'https://seeklogo.com/images/F/flutter-logo-5086DD11C5-seeklogo.com.png');
// Image with different size with respect to flutterLogoUrl
dartLogoUrl =
'https://www.extremetech.com/wp-content/uploads/2011/10/dart-logo-banner1-348x196.jpg';
dartLogoUrl = Uri.parse(
'https://www.extremetech.com/wp-content/uploads/2011/10/dart-logo-banner1-348x196.jpg');
// Image with the same size as flutterLogoUrl
androidLogoUrl =
'https://seeklogo.com/images/A/android-western-logo-8F117A7F00-seeklogo.com.png';
androidLogoUrl = Uri.parse(
'https://seeklogo.com/images/A/android-western-logo-8F117A7F00-seeklogo.com.png');
});

test('Compare the same image', () async {
Expand Down
16 changes: 8 additions & 8 deletions test/io_test.dart
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
@TestOn('vm')

import 'package:test/test.dart';
import 'package:diff_image/diff_image.dart';
import 'package:diff_image/src/helper_functions.dart';
import 'package:test/test.dart';

void main() {
group('Test when dart:io is supported', () {
String flutterLogoUrl, dartLogoUrl, androidLogoUrl;
late Uri flutterLogoUrl, dartLogoUrl, androidLogoUrl;

setUp(() {
// A real image
flutterLogoUrl =
'https://seeklogo.com/images/F/flutter-logo-5086DD11C5-seeklogo.com.png';
flutterLogoUrl = Uri.parse(
'https://seeklogo.com/images/F/flutter-logo-5086DD11C5-seeklogo.com.png');
// Image with different size with respect to flutterLogoUrl
dartLogoUrl =
'https://www.extremetech.com/wp-content/uploads/2011/10/dart-logo-banner1-348x196.jpg';
dartLogoUrl = Uri.parse(
'https://www.extremetech.com/wp-content/uploads/2011/10/dart-logo-banner1-348x196.jpg');
// Image with the same size as flutterLogoUrl
androidLogoUrl =
'https://seeklogo.com/images/A/android-western-logo-8F117A7F00-seeklogo.com.png';
androidLogoUrl = Uri.parse(
'https://seeklogo.com/images/A/android-western-logo-8F117A7F00-seeklogo.com.png');
});

test('Compare the same image', () async {
Expand Down