Skip to content

Instantly share code, notes, and snippets.

@m-damien
Created December 5, 2018 09:40
Show Gist options
  • Save m-damien/75617b3eba1b125aac1be4af7444acad to your computer and use it in GitHub Desktop.
Save m-damien/75617b3eba1b125aac1be4af7444acad to your computer and use it in GitHub Desktop.
Difference between EDID vs CGDisplayScreenSize
#import <Cocoa/Cocoa.h>
#import <IOKit/graphics/IOGraphicsLib.h>
char* getEDID(CGDirectDisplayID displayId) {
char* result = NULL;
io_registry_entry_t displayport = CGDisplayIOServicePort(displayId);
NSDictionary* dict = (NSDictionary*) IODisplayCreateInfoDictionary(displayport, kIODisplayOnlyPreferredName);
NSData* edid = [dict objectForKey: [NSString stringWithUTF8String: kIODisplayEDIDKey]];
if (edid) {
result = (char*)[edid bytes];
}
return result;
}
int main() {
for(NSScreen* screen in [NSScreen screens]) {
NSDictionary *description = [screen deviceDescription];
NSSize displayPixelSize = [[description objectForKey:NSDeviceSize] sizeValue];
CGDirectDisplayID displayId = (CGDirectDisplayID) [[description objectForKey:@"NSScreenNumber"] unsignedIntValue];
CGSize displayPhysicalSize = CGDisplayScreenSize(displayId);
char* edid = getEDID(displayId);
// See https://en.wikipedia.org/wiki/Extended_Display_Identification_Data
// The image sizes are on 12bits so we have to recompose them
int horizontalImgSize = (((edid[54 + 14] >> 4) & 0x0F) << 8) | ((unsigned char) edid[54 + 12]);
int verticalImgSize = ((edid[54 + 14] & 0x0F) << 8) | ((unsigned char) edid[54 + 13]);
NSLog(@"PPI using CGDisplayScreenSize : is %0.2fx%0.2f", (displayPixelSize.width / displayPhysicalSize.width) * 25.4f, (displayPixelSize.height / displayPhysicalSize.height) * 25.4f);
NSLog(@"PPI using EDID : is %0.2fx%0.2f", (displayPixelSize.width / horizontalImgSize) * 25.4f, (displayPixelSize.height / verticalImgSize) * 25.4f);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment