JPEG is not encoding the DPI

This commit is contained in:
Robert Grant 2014-10-24 16:16:31 -04:00
parent 27272bc58e
commit 7306d272b8
6 changed files with 23 additions and 4 deletions

View File

@ -595,13 +595,19 @@ NSString* NSImageCompressionFactor = @"NSImageCompressionFactor";
return nil;
}
int dpi = 72;
if (_size.width > 0) {
dpi = ceilf(72 * _pixelsWide / _size.width);
}
NSMutableData *result=[NSMutableData data];
// Convert the NS options to CG options - just NSImageCompressionFactor for now
NSDictionary *CGProperties = nil;
NSDictionary *CGProperties = [NSMutableDictionary dictionary];
[CGProperties setValue: [NSNumber numberWithInt: dpi] forKey: (id)kCGImageDestinationDPI];
if ([properties count]) {
id compressionFactor = [properties valueForKey:NSImageCompressionFactor];
if (compressionFactor) {
CGProperties = [NSMutableDictionary dictionary];
[CGProperties setValue:compressionFactor forKey:(id)kCGImageDestinationLossyCompressionQuality];
}
}

View File

@ -8,6 +8,7 @@ typedef struct O2ImageDestination *CGImageDestinationRef;
COREGRAPHICS_EXPORT const CFStringRef kCGImageDestinationLossyCompressionQuality;
COREGRAPHICS_EXPORT const CFStringRef kCGImageDestinationBackgroundColor;
COREGRAPHICS_EXPORT const CFStringRef kCGImageDestinationDPI;
COREGRAPHICS_EXPORT CFTypeID CGImageDestinationGetTypeID(void);

View File

@ -4,6 +4,7 @@
const CFStringRef kCGImageDestinationLossyCompressionQuality=(CFStringRef)@"kCGImageDestinationLossyCompressionQuality";
const CFStringRef kCGImageDestinationBackgroundColor=(CFStringRef)@"kCGImageDestinationBackgroundColor";
const CFStringRef kCGImageDestinationDPI=(CFStringRef)@"kCGImageDestinationDPI";
CFTypeID CGImageDestinationGetTypeID(void) {
return O2ImageDestinationGetTypeID();

View File

@ -46,8 +46,17 @@ void O2JPGEncoderWriteImage(O2JPGEncoderRef self,O2ImageRef image,CFDictionaryRe
cinfo.image_height = height;
cinfo.input_components = 3;
cinfo.in_color_space = JCS_RGB;
jpeg_set_defaults(&cinfo);
NSNumber *dpi = [properties objectForKey:(NSString *)kO2ImageDestinationDPI];
if (dpi) {
int dpiValue = [dpi intValue];
cinfo.density_unit = 1; /* DPI */
cinfo.X_density = dpiValue; /* Horizontal pixel density */
cinfo.Y_density = dpiValue; /* Vertical pixel density */
}
NSNumber *compression = [properties objectForKey:(NSString *)kO2ImageDestinationLossyCompressionQuality];
if (compression) {
jpeg_set_quality(&cinfo, 100*[compression floatValue], TRUE);

View File

@ -11,6 +11,7 @@ typedef O2ImageDestination *O2ImageDestinationRef;
extern const CFStringRef kO2ImageDestinationLossyCompressionQuality;
extern const CFStringRef kO2ImageDestinationBackgroundColor;
extern const CFStringRef kO2ImageDestinationDPI;
@interface O2ImageDestination : NSObject {
@public

View File

@ -7,7 +7,8 @@
// Using the same value as CoreGraphics - that's removing the needs for conversion
const CFStringRef kO2ImageDestinationLossyCompressionQuality = (const CFStringRef)@"kCGImageDestinationLossyCompressionQuality";
const CFStringRef kO2ImageDestinationBackgroundColor = (const CFStringRef)@"kO2ImageDestinationBackgroundColor";
const CFStringRef kO2ImageDestinationBackgroundColor = (const CFStringRef)@"kCGImageDestinationBackgroundColor";
const CFStringRef kO2ImageDestinationDPI = (const CFStringRef)@"kCGImageDestinationDPI";
@interface _O2ImageDestination : O2ImageDestination
@end