-
Notifications
You must be signed in to change notification settings - Fork 151
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Respect original format by default (#171)
* respect original format by default * fix
- Loading branch information
1 parent
c824703
commit bbcd1f9
Showing
10 changed files
with
260 additions
and
94 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import 'dart:ui'; | ||
|
||
import 'package:crop_your_image/crop_your_image.dart'; | ||
import 'package:image/image.dart' as image hide ImageFormat; | ||
|
||
class MyImageCropper extends ImageCropper<image.Image> { | ||
const MyImageCropper(); | ||
@override | ||
RectValidator<image.Image> get rectValidator => defaultRectValidator; | ||
|
||
@override | ||
RectCropper<image.Image> get rectCropper => _rectCropper; | ||
|
||
@override | ||
CircleCropper<image.Image> get circleCropper => _circleCropper; | ||
} | ||
|
||
final RectCropper<image.Image> _rectCropper = ( | ||
image.Image original, { | ||
required Offset topLeft, | ||
required Size size, | ||
required ImageFormat? outputFormat, | ||
}) { | ||
/// crop image with low quality | ||
return image.encodeJpg( | ||
quality: 10, | ||
image.copyCrop( | ||
original, | ||
x: topLeft.dx.toInt(), | ||
y: topLeft.dy.toInt(), | ||
width: size.width.toInt(), | ||
height: size.height.toInt(), | ||
), | ||
); | ||
}; | ||
|
||
final CircleCropper<image.Image> _circleCropper = ( | ||
image.Image original, { | ||
required Offset center, | ||
required double radius, | ||
required ImageFormat? outputFormat, | ||
}) { | ||
/// crop image with low quality | ||
/// note: jpg can't cropped circle | ||
return image.encodeJpg( | ||
quality: 10, | ||
image.copyCropCircle( | ||
original, | ||
centerX: center.dx.toInt(), | ||
centerY: center.dy.toInt(), | ||
radius: radius.toInt(), | ||
), | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,12 @@ | ||
import 'package:crop_your_image/src/logic/cropper/image_image_cropper.dart'; | ||
import 'package:crop_your_image/src/logic/format_detector/format_detector.dart'; | ||
import 'package:crop_your_image/src/logic/cropper/legacy_image_image_cropper.dart'; | ||
import 'package:crop_your_image/src/logic/format_detector/default_format_detector.dart'; | ||
import 'package:crop_your_image/src/logic/parser/image_image_parser.dart'; | ||
|
||
export 'src/widget/widget.dart'; | ||
export 'src/logic/logic.dart'; | ||
|
||
final defaultImageParser = imageImageParser; | ||
|
||
// TODO(chooyan-eng): implement format detector if possible | ||
const FormatDetector? defaultFormatDetector = null; | ||
|
||
final defaultFormatDetector = imageFormatDetector; | ||
const defaultImageCropper = ImageImageCropper(); | ||
const legacyImageCropper = LegacyImageImageCropper(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import 'dart:ui'; | ||
|
||
import 'package:crop_your_image/crop_your_image.dart'; | ||
import 'package:crop_your_image/src/logic/cropper/errors.dart'; | ||
import 'package:image/image.dart'; | ||
|
||
/// default implementation of [RectValidator] | ||
/// this checks if the rect is inside the image, not negative, and not negative size | ||
final RectValidator<Image> defaultRectValidator = | ||
(Image original, Offset topLeft, Offset bottomRight) { | ||
if (topLeft.dx.toInt().isNegative || | ||
topLeft.dy.toInt().isNegative || | ||
bottomRight.dx.toInt().isNegative || | ||
bottomRight.dy.toInt().isNegative || | ||
topLeft.dx.toInt() > original.width || | ||
topLeft.dy.toInt() > original.height || | ||
bottomRight.dx.toInt() > original.width || | ||
bottomRight.dy.toInt() > original.height) { | ||
return InvalidRectError(topLeft: topLeft, bottomRight: bottomRight); | ||
} | ||
if (topLeft.dx > bottomRight.dx || topLeft.dy > bottomRight.dy) { | ||
return NegativeSizeError(topLeft: topLeft, bottomRight: bottomRight); | ||
} | ||
return null; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import 'dart:ui'; | ||
|
||
import 'package:crop_your_image/crop_your_image.dart'; | ||
|
||
import 'package:image/image.dart' hide ImageFormat; | ||
|
||
/// an implementation of [ImageCropper] using image package | ||
/// this implementation is legacy that behaves the same as the version 1.1.0 or earlier | ||
/// meaning that it doesn't respect the outputFormat and always encode result as png | ||
class LegacyImageImageCropper extends ImageCropper<Image> { | ||
const LegacyImageImageCropper(); | ||
|
||
@override | ||
RectCropper<Image> get rectCropper => legacyRectCropper; | ||
|
||
@override | ||
CircleCropper<Image> get circleCropper => legacyCircleCropper; | ||
|
||
@override | ||
RectValidator<Image> get rectValidator => defaultRectValidator; | ||
} | ||
|
||
/// process cropping image. | ||
/// this method is supposed to be called only via compute() | ||
final RectCropper<Image> legacyRectCropper = ( | ||
Image original, { | ||
required Offset topLeft, | ||
required Size size, | ||
required ImageFormat? outputFormat, | ||
}) { | ||
return encodePng( | ||
copyCrop( | ||
original, | ||
x: topLeft.dx.toInt(), | ||
y: topLeft.dy.toInt(), | ||
width: size.width.toInt(), | ||
height: size.height.toInt(), | ||
), | ||
); | ||
}; | ||
|
||
/// process cropping image with circle shape. | ||
/// this method is supposed to be called only via compute() | ||
final CircleCropper<Image> legacyCircleCropper = ( | ||
Image original, { | ||
required Offset center, | ||
required double radius, | ||
required ImageFormat? outputFormat, | ||
}) { | ||
return encodePng( | ||
copyCropCircle( | ||
original, | ||
centerX: center.dx.toInt(), | ||
centerY: center.dy.toInt(), | ||
radius: radius.toInt(), | ||
), | ||
); | ||
}; |
17 changes: 17 additions & 0 deletions
17
lib/src/logic/format_detector/default_format_detector.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import 'dart:typed_data'; | ||
|
||
import 'package:crop_your_image/crop_your_image.dart'; | ||
import 'package:image/image.dart' as img; | ||
|
||
final FormatDetector imageFormatDetector = (Uint8List data) { | ||
final format = img.findFormatForData(data); | ||
|
||
return switch (format) { | ||
img.ImageFormat.png => ImageFormat.png, | ||
img.ImageFormat.jpg => ImageFormat.jpeg, | ||
img.ImageFormat.webp => ImageFormat.webp, | ||
img.ImageFormat.bmp => ImageFormat.bmp, | ||
img.ImageFormat.ico => ImageFormat.ico, | ||
_ => ImageFormat.png, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.