How do you load an Image server side in dart? - dart

I've seen the answers for using an ImageElement to load an image. This only works on the browser side and I wanted to do some processing on the server.
Is there any way to load and read RGBA values from a PNG in server side dart? If not are there any server side image processing libraries planned?
Is there anyway to run Dartium "headless" so I can use the canvas api to do what I want?

There is a dart library that can read and write PNG images server side,
http://pub.dartlang.org/packages/image
The library can read and write PNG and JPG images, and provides a number of image manipulation commands.
For example, to read a PNG, resize it, and save it to disk using this library:
import 'dart:io' as Io;
import 'package:image/image.dart';
void main() {
// Read a PNG image from file.
Image image = readPng(new Io.File('foo.png').readAsBytesSync());
// Resize the image to a 120x? thumbnail (maintaining the aspect ratio).
Image thumbnail = copyResize(image, 120);
// Save the thumbnail to disk as a PNG.
new Io.File('foo-thumbnail.png').writeAsBytesSync(writePng(thumbnail));
}

There is no library out there at the moment.
I have been using ImageMagick instead. You could use Process.run() to run it. Here's an example:
Process.run('/usr/local/bin/convert', ['file', '-resize', '100x100']);

Related

Moving an image with fs causes it to corrupt

I'm using AdonisJS framework, and it has its own method move(), that uses fs.
The issue is that when making use of this method, the image that's moved is corrupted, and when oepning it, this message is shown:
Error interpreting JPEG image file (Not JPEG file: starts with 0xef 0xbf)
The documentation I based on is this, so my code is no much different than it.

find the extended file type of a specified file ( JPEG image, TIFF Image, ...) objective c

My question is a very simple one. How can i get the extension type of a UIImage? I can only get the image as UIImage and not as its name. The image can be static, or can be taken from the phone gallery or even from a file path. If someone could do a little help in that, it would be grateful.
-(returnType)functionName : (argument)UIImage
{
// code to get the extension//
return extension(as string)
}
this is how i wish to have it. I tried passing NSData. But either i need to do the JPEG representation or PNG representation. In both case i will either receive a jpeg extension or a png extension. Even if i pass a gif, it will be shown either jpeg or png. :( ... Please help
Once you have a UIImage you no longer have any concept of an image type.
If you read encoded image data from a file (or other source) into an NSData object you can inspect the data to determine the image's type. But once you have a UIImage you have an abstract image bitmap that has no type.
Remember, a UIImage can be created in memory. It never started out as a file and it never had a specific type. It's just an image. Even if you load a UIImage from a file, any association with the original file is lost. It's not until you choose to convert the UIImage to a specific format do you get a type.

Extract OpenGL raw RGB(A) texture data from png data stored in NSData using libpng on iOS

Unfortunately, there appears to be no way to using a built-in method on iOS to extract 32 bit RGBA data from a PNG file without losing the alpha channel reference. Therefore, some people have been using libpng to extract their OpenGL textures. However, all the examples have required the png file to be loaded from a file. Assuming these textures are imported over a network connection, they would have to be saved to files from NSData and then read. What is the best way to extract raw PNG data into raw OpenGL RGBA texture data?
Ended up writing a category which solves this problem using the customization capabilities of libpng. Posted a gist here: https://gist.github.com/joshcodes/5681512
Hopefully this helps someone else who needs to know how this is done. The essential part is creating a method
void user_read_data(png_structp png_ptr, png_bytep data, png_size_t length)
{
void *nsDataPtr = png_get_io_ptr(png_ptr);
ReadStream *readStream = (ReadStream*)nsDataPtr;
memcpy(data, readStream->source + readStream->index, length);
readStream->index += length;
}
and using
// init png reading
png_set_read_fn(png_ptr, &readStream, user_read_data);
as a custom read method.

When should I use UIImageJPEGRepresentation and UIImagePNGRepresentation for uploading different image formats to the server?

In my application I have to send images of different formats to the server (it must be all file formats that can be read by the UIImage class) https://developer.apple.com/library/ios/#documentation/uikit/reference/UIImage_Class/Reference/Reference.html
And the problem is: I don't know when I should use each of this methods. Of course it's obvious that for .png images I need to use UIImagePNGRepresentation and for .jpg/.jpeg UIImageJPEGRepresentation. But what about other formats (.tiff,.gif , etc.)? There are only two methods for image manipulations and so many formats.
You say:
Of course it's obvious that for .png images I need to use UIImagePNGRepresentation and for .jpg/.jpeg UIImageJPEGRepresentation.
No, that's not necessarily the case. If you have some original "digital asset", rather than creating a UIImage and then using one of those two functions to create the NSData that you'll upload, you will often just load the NSData from the original asset and bypass the round-trip to a UIImage at all. If you do this, you don't risk any loss of data that converting to a UIImage, and then back again, can cause.
There are some additional considerations, though:
Meta data:
These UIImageXXXRepresentation functions strip the image of its meta data. Sometimes that's a good thing (e.g. you don't want to upload photos of your children or expensive gadgets the include the GPS locations where malcontents could identify where the shot was taken). In other cases, you don't want the meta data to be thrown away (e.g. date of the original shot, which camera, etc.).
You should make an explicit decision as to whether you want meta data stripped or not. If not, don't round-trip your image through a UIImage, but rather use the original asset.
Image quality loss and/or file size considerations:
I'm particularly not crazy about UIImageJPEGRepresentation because it a lossy compression. Thus, if you use a compressionQuality value smaller than 1.0, you can lose some image quality (modest quality loss for values close to 1.0, more significant quality loss with lower compressionQuality values). And if you use a compressionQuality of 1.0, you mitigate much of the JPEG image quality loss, but the resulting NSData can often be bigger than the original asset (at least if the original was, itself, a compressed JPEG or PNG), resulting in slower uploads.
UIImagePNGRepresentation doesn't introduce compression-based data loss, but depending upon the image, you may still lose data (e.g. if the original file was a 48-bit TIFF or used a colorspace other than sRGB).
It's a question of whether you are ok with some image quality loss and/or larger file size during the upload process.
Image size:
Sometimes you don't want to upload the full resolution image. For example, you might be using a web service that wants images no bigger than 800px per side. Or if you're uploading a thumbnail, they might want something even smaller (e.g. 32px x 32px). By resizing images, you can make the upload much smaller and thus much faster (though with obvious quality loss). But if you use an image resizing algorithm, then creating a PNG or JPEG using these UIImageXXXRepresentation functions would be quite common.
In short, if I'm trying to minimize the data/quality loss, I would upload the original asset if it's in a format that the server accepts, and I'd use UIImagePNGRepresentation (or UIImageJPGRepresentation with quality setting of 1.0) if the original asset was not in a format accepted by the server. But the choice of using these UIImageXXXRepresentation functions is a question of your business requirements and what the server accepts.
Rob points out a lot of very good things to consider when working with images (+1), however here is an example of how to create tiff's and gif's as you asked:
First, you need to link to the ImageIO library (under the Build Phases of your app).
Next you need to #import <ImageIO/ImageIO.h> at the top of your file.
Then, the following code will convert the image for you:
// Get a reference to the image that you already have stored on disk somehow.
// If it isn't stored on disk, then you can use CGImageSourceCreateWithData() to create it from an NSData representation of your image.
NSURL *url = [[NSBundle mainBundle] URLForResource:#"01" withExtension:#"jpg"];
CGImageSourceRef src = CGImageSourceCreateWithURL((__bridge CFURLRef)(url), NULL);
// Create a URL referencing the Application Support Directory. We will save the new image there.
NSFileManager *fm = [NSFileManager defaultManager];
NSURL *suppurl = [fm URLForDirectory:NSApplicationSupportDirectory
inDomain:NSUserDomainMask
appropriateForURL:nil
create:YES
error:NULL];
// Append the name of the output file to the app support directory
// For tiff change the extension in the next line to .tiff
NSURL *gifURL = [suppurl URLByAppendingPathComponent:#"mytiff.gif"];
// Create the destination for the new image
// For tiff, use #"public.tiff" for the second argument of the next line (instead of #com.compuserve.gif".
CGImageDestinationRef dest = CGImageDestinationCreateWithURL((__bridge CFURLRef)gifURL,
(CFStringRef)#"com.compuserve.gif",
1,
NULL);
CGImageDestinationAddImageFromSource(dest, src, 0, NULL);
// Write the image data to the URL.
bool ok = CGImageDestinationFinalize(dest);
if (!ok)
NSLog(#"Unable to create gif file.");
// Cleanup
CFRelease(src);
CFRelease(dest);
This was adapted from the code in this book.

How I can load a Png image from file which have another extension?

I'm using a TImage component to load some png images, but some of them have the .imp extension. I add the Vcl.Imaging.pngimage unit to my code and I'm using this code to load the images
if OpenDialog1.Execute then
Image1.Picture.LoadFromFile(OpenDialog1.FileName);
But when the LoadFromFile procedure is executed a exception is raised
Unknown picture file extension (.imp)
these images (.imp) are png files generated by an extenal app and are located in a read-only folder, so rename these files is not a option, the question is How I can load a Png image in a TImage component from a file which have another extension?
you must register the file format first using the TPicture.RegisterFileFormat method
Try this
TPicture.RegisterFileFormat('imp','imp (png) image file',TPngImage);

Resources