Re: How to incrementally compress jpeg?
Re: How to incrementally compress jpeg?
- Subject: Re: How to incrementally compress jpeg?
- From: douglas welton <email@hidden>
- Date: Fri, 20 Nov 2009 17:02:13 -0500
<insert pure speculation here>
Is what you want to do (multiple pass compressions) actually something that is valid to do?
Let's say that you compress an image A (using JPEG). If you try to re-compress (JPEG again) the resultant data A , the compression effort will not be very useful because the clues that JPEG uses to compress an image are probably no longer available in the resultant data A. I would suspect that multiple passes of any image-specific compression will not produce any significant additional size reduction.
My question: what are you really trying to do? If you want to make sure that an image is as small as possible there are other ways to do that (can you say "lossy"?).
regards,
douglas
On Nov 20, 2009, at 4:47 PM, Phi Le wrote:
> I posted this question at
> http://stackoverflow.com/questions/1773360/how-to-compress-jpeg-image-with-cocoa
> .
> Copy and paste:
> I have an jpeg image and I want to be able to incrementally compress
> it using Cocoa/Core Image/Core Graphics. For example, I have A.jpg
> (3MB), I compress A and get B.jpg (1MB), compress B and get C.jpg
> (400KB), and so on till the image can't be compressed anymore.
>
> I am trying to use NSBitmapImageRep representationUsingType:properties
> without much success. I use an image rep created from compressed
> image data to try and compress further, but this doesn't work well, if
> I use a compression factor that is the same factor used to create the
> initial compressed data.
>
> Here is the code that I have been wrangling with
> <code>
> NSString * fileName = @"largeImage.jpg";
>
> NSLog(@"== Initializing ==\n");
>
> NSData * imageData = [NSData dataWithContentsOfFile:fileName];
> NSLog(@"imageData length: %d\n", [imageData length]);
>
> NSImage * image = [[NSImage alloc] initWithData:imageData];
> NSBitmapImageRep * bitmapRep = [[image representations] objectAtIndex:0];
>
> NSData * data = [bitmapRep representationUsingType:NSJPEGFileType
> properties:nil];
> NSLog(@"data length: %d\n", [data length]);
>
> NSDictionary * dict = [NSDictionary dictionaryWithObject: [NSNumber
> numberWithFloat:0.5] forKey:NSImageCompressionFactor];
> NSData * data5 = [bitmapRep representationUsingType:NSJPEGFileType
> properties:dict];
> NSLog(@"data5 length: %d\n", [data5 length]);
>
> NSLog(@"== End ==\n\n");
>
> NSLog(@"== Image from Data5 ==\n");
> NSImage * imageFromData5 = [[NSImage alloc] initWithData:data5];
> NSBitmapImageRep * bitmapRepFromData5 = [[imageFromData5
> representations] objectAtIndex:0];
>
> NSData * dataFromImageFromData5 = [bitmapRepFromData5
> representationUsingType:NSJPEGFileType properties:nil];
> NSLog(@"dataFromImageFromData5 length: %d\n", [dataFromImageFromData5 length]);
>
> dict = [NSDictionary dictionaryWithObject: [NSNumber
> numberWithFloat:0.5] forKey:NSImageCompressionFactor];
> NSData * dataFiveFromImageWithData5 = [bitmapRepFromData5
> representationUsingType:NSJPEGFileType properties:dict];
> NSLog(@"dataFiveFromImageWithData5 length: %d\n",
> [dataFiveFromImageWithData5 length]);
> NSLog(@"== End ==\n\n");
> </code>
>
> This is my result
> <output>
>> == Initializing ==
>> imageData length: 1882694
>> data length: 1888365
>> data5 length: 1102461
>> == End ==
>
>> == Image from Data5 ==
>> dataFromImageFromData5 length: 1646137
>> dataFiveFromImageWithData5 length: 1102115
>> == End ==
>
>
> Try a different image
>> == Initializing ==
>> imageData length: 3620257
>> data length: 2889458
>> data5 length: 1404750
>> == End ==
>
>> == Image from Data5 ==
>> dataFromImageFromData5 length: 2007951
>> dataFiveFromImageWithData5 length: 1405398
>> == End ==
> </output>
> _______________________________________________
>
> Cocoa-dev mailing list (email@hidden)
>
> Please do not post admin requests or moderator comments to the list.
> Contact the moderators at cocoa-dev-admins(at)lists.apple.com
>
> Help/Unsubscribe/Update your Subscription:
>
> This email sent to email@hidden
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden