Is it possible convert anNSData *dataImage witch was a format jpeg or png and convert it to PNG interlaced? i know about compression image UIImagePNGRepresentation but i think it convert just to non-interlaced PNG. So, how should i set option for an UIImage or NSData to interlaced PNG?
UIImagePNGRepresentation makes only non-interlaced png.
Nice question, but i think it is impossible using UIKit.
I think you should use libpng to create interlaced png.
Look at this article , there you can find Minimal Example of writing a PNG File
When you set png header in this method
png_set_IHDR(png_ptr, info_ptr, width, height,
bit_depth, color_type, PNG_INTERLACE_NONE,
PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
try to change PNG_INTERLACE_NONE to PNG_INTERLACE_ADAM7_PASSES
Related
I am using the extract_mvs.c from ffmpeg:
https://ffmpeg.org/doxygen/2.5/extract__mvs_8c_source.html
I added opencv to imwrite the image.
cv::Mat img(frame->height,frame->width,CV_8UC1,frame->data[0]);
imwrite( "pic.jpg", img );
That works because the image in the frame is in grayscale. The camera is a color camera however, and I dont know why I am getting grayscale. If I cange the above to CV_8UC3, I get segmentation fault.
I tried to save the image with ppm_save function and I still get a black and white frame when there should be a color frame. Any ideas?
Thanks,
Chris
Just read about graphics file formats and such. JPG requires BGR24 format. The raw frame buffer format YUV420P needs to be converted to BGR24 using swscale. Then the output frames height and width needs to be manually set before calling :
cv::Mat img(out_frame->height,out_frame->width,CV_8UC3,out_frame->data[0]);
imwrite( "pic.jpg", img );
Likewise,PPM file format requires RGB24 and the raw format needs to be converted to this before saving the ppm file.
Thanks,
Chris
How can I convert binary data to image ?
I have binary file which content of RGB 565 bitmap image. i want converted .bin file in to image.
Thank's in advance.
UIImage *retrieved = [UIImage imageWithData:
[NSData dataWithBytes:data length:length]];
After you get the data, you have to first convert it to a native format of iOS (RGBA8888, ARGB8888 or RGB888). This method should help you with that. Now all you have to do is get the data inside of a vBuffer. After you get the data out as RGBA8888, you need to make a CGBitmapContext with the data and get a CGImage out of that. So, in the end, I suggest you switch formats if you can. You won't save any performance by doing it this way.
Currently I am dealing with the problem that I have pdf file with lots of 1024x768 images in it and am trying to optimize the pdf's file size, but the only solution that I thought is good enough for now is compressing the images with jpeg compression. The problem is that I did not find any way to do that with iOS APIs. Am I missing something, is there a way?
I`m welcome to suggestions on how to optimize the pdf with other means (lowering the resolution of the images is not a good solution for me).
Edit: If someone knows another API to use for pdf generation on iOS - links would be much appreciated.
Thanks in advance.
Actually you can use a UIImageJPEGRepresentation. But there's another step, to then use a JPEG data provider to draw the image:
NSData *jpegData = UIImageJPEGRepresentation(sourceImage, 0.75);
CGDataProviderRef dp = CGDataProviderCreateWithCFData((__bridge CFDataRef)jpegData);
CGImageRef cgImage = CGImageCreateWithJPEGDataProvider(dp, NULL, true, kCGRenderingIntentDefault);
[[UIImage imageWithCGImage:cgImage] drawInRect:drawRect];
This worked well for me when drawing to a PDF context. 0.75 compression quality reduced the size of my image-laden PDF by about 75% and it still looked fine.
If anyone is interested I found a solution to my problem by using a free pdf library - libHaru. It gave me the needed functionality to add a JPEG compressed image to the generated pdf file.
If you'd like to compress images you can use UIImageJPEGRepresentation
NSData * UIImageJPEGRepresentation (
UIImage *image,
CGFloat compressionQuality
);
you could jpeg convert/compress a PNG image with
NSData *someImageData = UIImageJPEGRepresentation(pngImage, 0.6); // quality 0.6 (60%) (from 0.0-1.0).
UIImage *newCompressedJPG = [UIImage imageWithData:someImageData];
But i don't think so this will reduce your PDF size. Because when you place the UIImage to your pdf the RAW image get's placed (as far as i know).
Update 1:
its compressibility varies from 0.0 to 1.0 (thanks to Leena)
Is there anyway to reduce the Image file size or Raw RGB buffer ?
Actually I have RGB buffer which it has 500KB with 320X420 size.I tried to save it to disk using UIimage and it comes to 240 KB.
As per the image size, I want it to have less than 50KB or so.(loosing quality is OK)
Is it possible ?
Thanks,
Raghu
See Trevor Harmon's excellent post on the subject.
The 240KB size sounds exactly like the raw RGB image data, uncompressed (320x420x3). Doesn't the iPhone have a PNG or JPEG exporter? The internet says to use UIImageJPEGRepresentation or UIImagePNGRepresentation and NSData writeToFile.
the quality of saved image will lose when i'm using this method ...
UIImage *img=imageview1.image;
UIImageWriteToSavedPhotosAlbum(img,nil,nil,nil);
You need to wrap the image in a PNG representation, so that it's saved to the Photo Library in PNG format, rather than JPG format.
I used the following code based on the code from Ben Weiss: UIImageWriteToSavedPhotosAlbum saves to wrong size and quality for a side by side comparison.
// Image contains a non-compressed image stored within my Documents directory
UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);
NSData* imdata = UIImagePNGRepresentation ( image );
UIImage* im2 = [UIImage imageWithData:imdata];
UIImageWriteToSavedPhotosAlbum(im2, nil, nil, nil);
The image will be saved in the PNG format in the Photo Library so that it can be accessed/shared later in full quality.
The function
NSData * UIImageJPEGRepresentation (
UIImage *image,
CGFloat compressionQuality
);
enables you to get the data representation of your image for the expected compression quality (1 is best).
Then you just have to write your NSData to the file system to get the JPEG file (using writeToURL:atomically: on NSData for instance).
The same can be applied to .PNG with the UIImagePNGRepresentation.
The original doc :
http://developer.apple.com/iphone/library/documentation/UIKit/Reference/UIKitFunctionReference/Reference/reference.html#//apple_ref/c/func/UIImageJPEGRepresentation
I don't know how to do it in the SDK, but though the UI, if you copy + paste the image into a message, it keeps its full size. Perhaps there's some mechanism like that in the SDK (copy to clipboard)? Dunno. Hope it gives you an idea.
When using:
UIImageWriteToSavedPhotosAlbum(img,nil,nil,nil);
The compression quality is about 0.75
image?.jpegData(compressionQuality: 0.75)
Try it yourself for proof. 🙂