brendan-duncan / image Goto Github PK
View Code? Open in Web Editor NEWDart Image Library for opening, manipulating, and saving various different image file formats.
License: MIT License
Dart Image Library for opening, manipulating, and saving various different image file formats.
License: MIT License
Hi, the copyResize can get maxWidth and maxHeight?
i use copyResize(image, 1200, 1200);
it not manteins the aspect ratio.
Hi there, could you please tag your releases with the version number so pub can figure out versions?
Hello,
We faced with issue of decoding some tiff images.
For most tiffs it works fine, but for some we have next result (see attach).
Can you please share your thoughts on this? Thank you!
original image
I got this exception at one point. I restarted my Dart server and tried again, and it worked, but figured I'd bring it up as it may be something I shouldn't ignore.
ImageException: Invalid IDAT checksum
#0 PngDecoder.decodeFrame (package:image/src/formats/png_decoder.dart:215:11)
#1 PngDecoder.decodeImage (package:image/src/formats/png_decoder.dart:304:12)
#2 decodeImage (package:image/src/formats/formats.dart:60:18)
#3 ImageUtil.resize.<resize_async_body> (package:woven/src/server/util/image_util.dart:19:19)
...
Thank you for a great lib! I had been using ImageMagick via Dart for a long time. Recently I switched to your lib and I was so happy to kill the IM dependency.
Hi Brendan:
We're trying to use this package to load some 16bit Grayscale TIFF images and display them. Using Image.dart, we can load and display RGB TIFF images (2.TIF) correctly, but we couldn't seem to load and display 16 Grayscale TIFF images (1.TIF) correctly. I'm including a couple of such TIFF images here:
https://drive.google.com/file/d/0B_y0G33KtOykcGdPcERmNThrQWc/view?usp=sharing
Could you check them out and show us how to load and display them correctly?
Thank you.
Read and write webp images.
I'm using this in a Flutter project to upload photos from a phone to cloud storage, and to reduce file size and costs I'm using copyResize
to first scale down the image. If the picture was taken in portrait, the photo is left in horizontal orientation and the EXIF data is not copied, so the final receiver of the photo just sees it as a standard horizontal photo.
The two obvious solutions I can think of are to either forward on the original EXIF data or to use the orientation data to first copyRotate
before the resize.
(PS. I love this library. It "just works." I haven't had to think much about it until this point. Thanks for your work on it.)
Looks like there is another type of problem (that was not there before your last changes? But the other ones work fine now!). Seems to be related to alpha.
My applications is only decoding the file from the psd format and encodes them as again as png.
Here is a small collection of files: https://dl.dropboxusercontent.com/u/480043/PSD-Problems.zip
Thanks!
PNG decoder currently does not support interlaced images.
dart --preview-dart-2 test/filter_test.dart
type 'int' is not a subtype of type 'double' where...
type 'int' is not a subtype of type 'double' where
int is from dart:core
double is from dart:core
../..image/lib/src/filter/smooth.dart 12:10 smooth
According to specs:
The gamma value has no effect on alpha samples, which are always a linear fraction of full opacity
Setup: Image input: PNG Grayscale+Alpha (2byte). Random gamma chunk of 0.45. Alpha channel is initially 192 (0xC0) and is changed to 224 (0xE0) due to gamma/clut tables.
Read and write gif images, including animation.
I am already working on this; adding the issue to track it.
It will support decoding to HDR images, and tone-mapping operations to convert to a regular image.
So, after loading a file into a List I try to create an image with this command:
image.Image source_image = image.decodeImage(data);
I end up getting this error:
RangeError: 4792720 must be in the range [0..4765668)
#0 _throwRangeError (dart:typed_data-patch/typed_data.dart:3638)
#1 Uint8List.
#2 InputBuffer.readByte (package:image/src/util/input_buffer.dart:137:18)
#3 JpegData._nextMarker (package:image/src/formats/jpeg/jpeg_data.dart:369:27)
#4 JpegData.validate (package:image/src/formats/jpeg/jpeg_data.dart:41:27)
#5 JpegDecoder.isValidFile (package:image/src/formats/jpeg_decoder.dart:15:35)
#6 findDecoderForData (package:image/src/formats/formats.dart:14:22)
#7 decodeImage (package:image/src/formats/formats.dart:56:39)
#8 Thumbnailer.createThumbnailForBits (package:dartlery_server/src/thumbnailer.dart:10:49)
#9 FilesModel.createFile.
4765668 is the size of the file in question here, so it seems like there's some kind of miscalculation going on here. Here's the image:
I've ported https://github.com/mapbox/pixelmatch to Dart (which is fairly trivial) to help with detecting differences between images. My goal is to incorporate this into some rendering tests I have to check for fidelity between a known good render and the latest output.
I think it could make sense to incorporate the logic into this package, perhaps as a dependency or perhaps as its own class/module. What do you think?
A primary goal I have is to eventually write a matcher
that can use it to report on how many pixels are different in a test
/expect
and perhaps optionally output the difference image for reference.
Here's the basic idea of what it can do:
I'm using my image_transformer package (https://github.com/Fox32/image_transformer) to convert a psd file to png. But I noticed some strage behavior for the following PSD file:
https://dl.dropboxusercontent.com/u/480043/grass_01_02.psd
If I use Photoshop to export the image (with export for web) I get the following file;
But using the transformer I get:
You might not see a difference on the embedded images here, but they look different on a black background, see the white border:
I'm not sure if the problem is caused by the PSD decoder or the PNG encoder.
Thanks for your great support for this package!
Including support for bitonal images.
Legacy xml package is deprecated (https://github.com/prujohn/dart-xml)
Could you update to new xml package (i.e. >=2.0.0) (https://github.com/renggli/dart-xml) ?
Hi,
i don't know why, but your demo-code fires this error. The lib is already stable?
[103255 is the length of the test file]
RangeError (index): Index out of range: index should be less than 103255: 103255
#0 Uint8List.[] (dart:typed_data-patch/typed_data.dart:981)
#1 InputBuffer.readByte (package:image/src/util/input_buffer.dart:137:18)
#2 JpegData._nextMarker (package:image/src/formats/jpeg/jpeg_data.dart:389:19)
#3 JpegData.validate (package:image/src/formats/jpeg/jpeg_data.dart:19:18)
#4 JpegDecoder.isValidFile (package:image/src/formats/jpeg_decoder.dart:15:27)
#5 findDecoderForData (package:image/src/formats/formats.dart:14:11)
#6 decodeImage (package:image/src/formats/formats.dart:56:21)
Dart VM version: 1.13.0-dev.2.0 (Tue Sep 08 05:25:02 2015) on "windows_x64"
RangeError (index): Invalid value: Not in range 0..112, inclusive: 113
#0 List.[] (dart:core-patch/array.dart:9)
#1 JpegScan._decodeMcu (package:image/src/formats/jpeg/jpeg_scan.dart:298:41)
#2 JpegScan.decode (package:image/src/formats/jpeg/jpeg_scan.dart:89:17)
#3 JpegData._readSOS (package:image/src/formats/jpeg/jpeg_data.dart:568:55)
#4 JpegData._read (package:image/src/formats/jpeg/jpeg_data.dart:338:11)
#5 JpegData.read (package:image/src/formats/jpeg/jpeg_data.dart:94:5)
#6 JpegDecoder.decodeImage (package:image/src/formats/jpeg_decoder.dart:46:10)
#7 decodeImage (package:image/src/formats/formats.dart:60:18)
Image: https://rand.darkholme.net/_images/5c0c01f531a6a7b08c83cf021faed25d/
Most of our asset source files are Photoshop files. It would be really cool to have the ability to convert them to png files using a pub transformer during build. For that PSD format support would be cool (read only?). I'm not sure about the complexity and documentation of the format, so maybe it is imposible. At the moment we plan to use ImageMagick as a format conversation tool. But with support in this library we could do it in Dart out of the box.
I have been unable to compare the colors of the getPixel method successfully to some hex numbers constants I defined.
The source is a standard PNG image without transparency.
I have just been getting false results doing the following:
const int RED = 0xff0000
int pixel = image.getPixel(5,5);
if ( (pixel >> 8).toUnsigned(32) == RED){
print ('RED PIXEL');
}
However, even black dots are printing RED PIXEL in the console with this approach.
Am I doing it the wrong way?
With the very helpful error:
Uncaught RangeError: bofyshould be a multiple ofe (native_typed_data_dart2js.dart:835)
This points to code relating to creating a Uint32List.view.
including animation
Not sure this fits this library but:
Support various GPU texture formats would be helpful for WebGL applications.
ETC, PVRTC, S3TC/DXTC and maybe crunch.
Not sure if its possible to write compression/decompression for all these formats in dart because some of them may have no public spec.
Maybe it is easier to use binaries here.
int getColor(int r, int g, int b, [int a = 255]) {
return (_clamp255(a) << 24) |
(_clamp255(b) << 16) |
(_clamp255(g) << 8) |
_clamp255(r);
}
According to wikipedia, the bit positions of b should be 0-7, bit positions of r - 16-23
http://en.wikipedia.org/wiki/RGBA_color_space
I'm trying to stream images from my websocket server to webbrowser.
the server is written in c++ with libwebp to compress images into webp format.
the web browser side is written in dart with this image package. I realized that decoding a small image 300*450 needs 350msecs, this seems to be too much for a streaming program :(
rendering also takes long time, 60msecs, with the method you showed in the document:
var imageData = canvas.context2D.createImageData(image.width, image.height);
imageData.data.setAll(0, image.getBytes());
canvas.context2D.putImageData(imageData, 0, 0,0,0,image.width,image.height);
here are some detailed numbers (unit msec):
run as javascript:
time, network:1 decode: 352 render: 58
run as dart:
time, network:2 decode: 224 render: 60
my code :
--- image: "^1.1.30"
import 'dart:io' as Io;
import 'package:image_picker/image_picker.dart';
Io.File imgFile = await ImagePicker.pickImage(source: ImageSource.gallery);
imageFun.Image image = imageFun.decodeImage(imgFile.readAsBytesSync()); // crash here..
---- error
[VERBOSE-2:dart_error.cc(16)] Unhandled exception:
type 'List' is not a subtype of type 'List' where
List is from dart:core
List is from dart:core
Int16List is from dart:typed_data
Heya. While trying to load up a particular image, it shows this error:
2014-07-06 19:12:06.147: SEVERE: (FilesResource) type 'double' is not a subtype of type 'int' of 'index'.
2014-07-06 19:12:06.147: SEVERE: (FilesResource) #0 List.[] (dart:core-patch/array.dart:12)
#1 JpegData.getData (package:image/src/formats/jpeg/jpeg_data.dart:231:47)
#2 JpegDecoder._copyToImage (package:image/src/formats/jpeg_decoder.dart:78:34)
#3 JpegDecoder.decodeImage (package:image/src/formats/jpeg_decoder.dart:56:17)
#4 decodeImage (package:image/src/formats/formats.dart:60:29)
#5 Thumbnailer.createThumbnailForBits (package:dartlery_server/src/thumbnailer.dart:14:49)
#6 FilesModel.createFile.<anonymous closure>.<anonymous closure> (package:dartlery_server/model/files_model.dart:113:43)
#7 _rootRunUnary (dart:async/zone.dart:730)
#8 _RootZone.runUnary (dart:async/zone.dart:864)
#9 _Future._propagateToListeners.handleValueCallback (dart:async/future_impl.dart:488)
#10 _Future._propagateToListeners (dart:async/future_impl.dart:571)
#11 _Future._complete (dart:async/future_impl.dart:317)
#12 Stream.forEach.<anonymous closure> (dart:async/stream.dart:623)
#13 _rootRun (dart:async/zone.dart:719)
#14 _RootZone.run (dart:async/zone.dart:862)
#15 _BaseZone.runGuarded (dart:async/zone.dart:574)
#16 _BufferingStreamSubscription._sendDone.sendDone (dart:async/stream_impl.dart:385)
#17 _BufferingStreamSubscription._sendDone (dart:async/stream_impl.dart:394)
#18 _BufferingStreamSubscription._close (dart:async/stream_impl.dart:283)
#19 _SinkTransformerStreamSubscription._close (dart:async/stream_transformers.dart:92)
#20 _EventSinkWrapper.close (dart:async/stream_transformers.dart:18)
#21 _ResultsImpl._ResultsImpl.<anonymous closure> (package:sqljocky/src/results/results_impl.dart:15:19)
#22 _HandlerEventSink.close (dart:async/stream_transformers.dart:219)
#23 _handleDone (dart:async/stream_transformers.dart:137)
#24 _rootRun (dart:async/zone.dart:719)
#25 _RootZone.run (dart:async/zone.dart:862)
#26 _BaseZone.runGuarded (dart:async/zone.dart:574)
#27 _BufferingStreamSubscription._sendDone.sendDone (dart:async/stream_impl.dart:385)
#28 _BufferingStreamSubscription._sendDone (dart:async/stream_impl.dart:394)
#29 _DelayedDone.perform (dart:async/stream_impl.dart:604)
#30 _StreamImplEvents.handleNext (dart:async/stream_impl.dart:701)
#31 _PendingEvents.schedule.<anonymous closure> (dart:async/stream_impl.dart:661)
#32 _asyncRunCallbackLoop (dart:async/schedule_microtask.dart:23)
#33 _asyncRunCallback (dart:async/schedule_microtask.dart:32)
#34 _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:128)
I'm experimenting with the JPEG decoder and there seems to be an issue with the RGB values.
I added this to the existing JPEG decode/encode test:
// Check RGB color extraction
expect(toRGB(image.getPixel(0, 0)), [48, 47, 45]);
expect(toRGB(image.getPixel(0, image.height)), [85, 81, 98]);
expect(toRGB(image.getPixel(image.width, 0)), [89, 103, 77]);
expect(toRGB(image.getPixel(image.width, image.height)), [40, 47, 39]);
where toRGB is simply defined as:
List<int> toRGB(int pixel) => [getRed(pixel), getGreen(pixel), getBlue(pixel)];
and the expected colors where extracted with GIMP.
The test fails right at the first pixel (0,0):
FAIL: JPEG decode/encode
Expected: [48, 47, 45]
Actual: [48, 46, 45]
Hi, encoding/decoding images takes a little while (especially on mobile devices). It would be nice to have an optional progress event after each line or so of encoding/decoding. Possible?
I get this error with a few images (very few out of thousands, but it keeps coming up), none of which appear to have any issues when opened in other applications: It occurs when I try to read the file.
RangeError (index): Invalid value: Only valid value is 0: 1
#0 List.[] (dart:core-patch/growable_array.dart:151)
#1 JpegScan._decodeHuffman (package:image/src/formats/jpeg/jpeg_scan.dart:134:18)
#2 JpegScan._decodeACSuccessive (package:image/src/formats/jpeg/jpeg_scan.dart:238:20)
#3 JpegScan._decodeBlock (package:image/src/formats/jpeg/jpeg_scan.dart:308:13)
#4 JpegScan.decode (package:image/src/formats/jpeg/jpeg_scan.dart:78:11)
#5 JpegData._readSOS (package:image/src/formats/jpeg/jpeg_data.dart:568:55)
#6 JpegData._read (package:image/src/formats/jpeg/jpeg_data.dart:338:11)
#7 JpegData.read (package:image/src/formats/jpeg/jpeg_data.dart:94:5)
#8 JpegDecoder.decodeImage (package:image/src/formats/jpeg_decoder.dart:46:10)
#9 decodeImage (package:image/src/formats/formats.dart:60:18)
Here is one such image:
Saw you just updated this, not sure if you caught this or if you are even experiencing this:
"transformers.$dart2js" refers to a package that's not listed in "dependencies".
In current moment image package used crypto: '>=0.9.0 <1.0.0'
It still produces a recognizable image, but it's quite off.
I noticed that drawing lines "backwards" wouldn't render. The bug is located in draw_line.dart:
// Axis-aligned lines
if (dx == 0) {
for (int y = y1; y <= y2; ++y) { <-- error here. Not checking for dy >= 0
drawPixel(image, x1, y, color);
}
return image;
} else if (dy == 0) {
for (int x = x1; x <= x2; ++x) { <-- error here too. Need check for dx >= 0
drawPixel(image, x, y1, color);
}
return image;
}
A quick fix is to check for dy >= 0 and flip from
// Axis-aligned lines
if (dx == 0) {
if (dy >= 0)
for (int y = y1; y <= y2; ++y) {
Im.drawPixel(image, x1, y, color);
}
else
for (int y = y2; y <= y1; ++y) { <-- y1 and y2 flipped
Im.drawPixel(image, x1, y, color);
}
return image;
} else if (dy == 0) {
if (dx >= 0)
for (int x = x1; x <= x2; ++x) {
Im.drawPixel(image, x, y1, color);
}
else
for (int x = x2; x <= x1; ++x) { <-- x1 and x2 flipped
Im.drawPixel(image, x, y1, color);
}
return image;
}
I am thinking to use the image library for my game framework. I do like the fact that your library allows for image editing on the go.
Question is, do you think the library is a good fit for a game framework performance-wise? (I haven't taken a dive into your lib's code).
So I discovered after building and running dart2js...The JavaScript wasn't coloring the images properly. A co-worker and I sat down today and traced things back and finally discovered it wasn't in the encoding or base64 or anything like that, it was in the reading of the data. There are shift operators being used and JavaScript has a problem with them in some cases. I found this: http://stackoverflow.com/questions/15457893/java-right-shift-on-negative-number/15457908#15457908 ...but that's for Java. It's different in JavaScript, but I think similar.
What we ended up doing was doing the math, pow(2, ShiftAmount) and dividing or multiplying depending on the shift direction. In the case of division, we also rounded the values so there were no problems there.
...and...It worked! I was missing a few replacements and kept testing and each time I tested I could see the image sorta materialize =) Was kinda fun. Once I found all the places it was being used and replaced things, the image looked perfect.
...For JPG. I'm assuming there's other areas in the package that use the shift operators and I'll try to run through them and replace them. I also want to see if I can optimize some of the math a bit so that the operations aren't more expensive than need be. The shift operators would be ideal, but I'm not sure they can be used if you plan to convert down to JavaScript using dart2js.
I absolutely will get to these, so I can submit a pull request if you like. Though I was interested in getting your thoughts (maybe you know of a better solution).
I don't know if I'm doing it wrong, but I'm trying to create thumbnails. This is my code.
if(source_image.width==source_image.height) {
new_height = SettingsModel.thumbnailMaxDimension;
new_width = SettingsModel.thumbnailMaxDimension;
} else if(source_image.width>=source_image.height) {
new_height = -1; // The image library has it's own internal auto-resize that mantains aspect ratio
new_width = SettingsModel.thumbnailMaxDimension;
} else {
new_height = SettingsModel.thumbnailMaxDimension;
double ratio = source_image.width / source_image.height;
new_width = (SettingsModel.thumbnailMaxDimension * ratio).round();
}
thumbnail = image.copyResize(source_image,new_width,new_height,image.CUBIC);
But the resized images come back looking like this (included a shot from doing a cubic resize in GIMP for comparison):
Am I not calling the CUBIC interpolation properly, or is this just how cubic is in this lib? I'm using version 1.1.21.
Would be great to have ability to trim the transparent pixels. Should probably be an async callback that gives info like the offset X,Y of the NW point where the crop occurred and the resulting image.
Calling decodeImage in a loop of images, all in PNG format.
Stack:
Unhandled exception:
Uncaught Error: RangeError: 355 must be in the range [0..355)
Stack Trace:
#0 _throwRangeError (dart:typed_data-patch/typed_data.dart:3571)
#1 Uint8List.[] (dart:typed_data-patch/typed_data.dart:790)
#2 InputBuffer.readByte (package:image/src/util/input_buffer.dart:137:18)
#3 JpegData._nextMarker (package:image/src/formats/jpeg/jpeg_data.dart:389:27)
#4 JpegData.validate (package:image/src/formats/jpeg/jpeg_data.dart:19:29)
#5 JpegDecoder.isValidFile (package:image/src/formats/jpeg_decoder.dart:15:35)
#6 findDecoderForData (package:image/src/formats/formats.dart:14:22)
#7 decodeImage (package:image/src/formats/formats.dart:56:39)
The png file is 355 bytes (19x80 pixel left arrow sprite).
Hey there,
I just implemented your image library with flutter and we are noticing very slow decoding on iOS.
This is my compression function:
static Future<File> compressImageFile(File inputFile,
{int resizePercentage = 50, int quality = 90}) async {
List<int> bytes = await inputFile.readAsBytes();
Image image = decodeImage(bytes);
if (image.height > 1500 || image.width > 1500) {
print("Resize required");
print("Image size before: ${image.height}x${image.width}");
var newWidth = (image.width / 100 * resizePercentage).toInt();
var newHeight = (image.height / 100 * resizePercentage).toInt();
Image resizedImage = copyResize(image, newWidth, newHeight);
print("Image size after: ${resizedImage.height}x${resizedImage.width}");
String originalFileName = basename(inputFile.path);
String filePath = inputFile.parent.path;
String newFile =
join(filePath, originalFileName.split('.').first + "_compressed.jpg");
File outputFile = new File(newFile);
outputFile.writeAsBytesSync(encodeJpg(resizedImage, quality: quality),
mode: FileMode.WRITE, flush: true);
return outputFile;
}
return inputFile;
}
This is the sample image we tried on both Android & iOS. Android takes around 5 seconds for the function to run (most time in "decodeImage") and the app crashed after a few minutes waiting for decodeImage on iOS.
https://www.pexels.com/photo/1-wtc-america-architecture-buildings-374710/
I can't seem to figure out how to loop my gif. The default loopCount
is already zero (forever) and i tried changing it but it doesn't seem to have any effect. Am i doing something wrong here?
The backgroundColor
property doesn't seem to do anything either but i can get around that.
main() async {
var fontFile = await new File('font.zip').readAsBytes();
var font = readFontZip(fontFile);
Animation anim = new Animation();
// anim.loopCount = 0;
String str = 'Animation';
for (var i = 0; i < 10; i++) {
Image image = new Image(480, 120);
for (var i = 0; i < str.length; i++) {
// slightly modified drawChar()
drawCharacter(image, font, 64*i, 0, str[i], i);
}
anim.addFrame(image);
}
List<int> gif = encodeGifAnimation(anim);
new File('test.gif')
..writeAsBytesSync(gif);
}
Dart has a Code interface: https://api.dartlang.org/apidocs/channels/stable/#dart-convert.Codec@id_encode
Thoughts on using it?
See also https://www.dartlang.org/articles/converters-and-codecs/
Im.Image image = Im.decodeImage(imageFile.readAsBytesSync());
fail with error:
type 'List' is not a subtype of type 'List'
Thank you for your work
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.