Created
October 1, 2011 07:47
-
-
Save EverythingSolution/1255741 to your computer and use it in GitHub Desktop.
Subpixel Test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static inline CGColorSpaceRef GetDeviceGrayScaleColorSpace(void) | |
{ | |
static CGColorSpaceRef deviceGrayscaleSpace = NULL; | |
if (deviceGrayscaleSpace == NULL) | |
deviceGrayscaleSpace = CGColorSpaceCreateDeviceGray(); | |
return deviceGrayscaleSpace; | |
} | |
static inline CGContextRef CreateGrayScaleCGBitmapContextForWidthAndHeight(void *data, | |
unsigned int width, | |
unsigned int height) | |
{ | |
CGColorSpaceRef colorSpace = GetDeviceGrayScaleColorSpace(); | |
CGBitmapInfo alphaInfo = kCGImageAlphaNone; | |
return CGBitmapContextCreate( | |
data, | |
width, | |
height, | |
8, | |
width, | |
colorSpace, | |
alphaInfo | |
); | |
} | |
static inline CGColorSpaceRef GetDeviceRGBColorSpace(void) | |
{ | |
static CGColorSpaceRef deviceRGBSpace = NULL; | |
if (deviceRGBSpace == NULL) | |
deviceRGBSpace = CGColorSpaceCreateDeviceRGB(); | |
return deviceRGBSpace; | |
} | |
static inline CGContextRef CreateCGBitmapContextForWidthAndHeight(void *data, | |
unsigned int width, | |
unsigned int height) | |
{ | |
CGColorSpaceRef colorSpace = GetDeviceRGBColorSpace(); | |
CGBitmapInfo alphaInfo = kCGImageAlphaNoneSkipLast | kCGBitmapByteOrder32Host; | |
return CGBitmapContextCreate( | |
data, | |
width, | |
height, | |
8, | |
width*4, | |
colorSpace, | |
alphaInfo | |
); | |
} | |
#import "WebViewController.h" | |
#import <QuartzCore/QuartzCore.h> | |
@implementation WebViewController | |
@synthesize webView=_webView; | |
@synthesize renderedWebView=_renderedWebView; | |
- (void)viewDidLoad | |
{ | |
[super viewDidLoad]; | |
CGRect rect = self.view.bounds; | |
rect.size.width /= 2; | |
self.renderedWebView = [[UIImageView alloc] initWithFrame:rect]; | |
self.renderedWebView.contentMode = UIViewContentModeTopLeft; | |
[self.view addSubview:self.renderedWebView]; | |
rect.origin.x += rect.size.width; | |
self.webView = [[UIWebView alloc] initWithFrame:rect]; | |
self.webView.delegate = self; | |
[self.view addSubview:self.webView]; | |
[self.webView loadHTMLString:@"<html><body><p style=\"font-size:500%;font-family:Baskerville-Italic;\">Lorem ipsum dolor</p></body></html>" baseURL:nil]; | |
} | |
- (void)webViewDidFinishLoad:(UIWebView *)webView | |
{ | |
// Make sure the webview is the right size | |
CGRect frame = self.webView.frame; | |
frame.size.height = 1; | |
self.webView.frame = frame; | |
CGSize size = [self.webView sizeThatFits:CGSizeZero]; | |
frame.size = size; | |
self.webView.frame = frame; | |
// Render the webview into a grayscale context that is triple wide | |
CGFloat scale = [[UIScreen mainScreen] scale]; | |
CGContextRef grayscaleContext = CreateGrayScaleCGBitmapContextForWidthAndHeight(NULL, size.width*scale * 3.0, size.height*scale); | |
CGContextTranslateCTM(grayscaleContext, 0.0, size.height*scale); | |
CGContextScaleCTM(grayscaleContext, scale, -scale); | |
CGContextScaleCTM(grayscaleContext, 3.0, 1.0); | |
[self.webView.layer renderInContext:grayscaleContext]; | |
// move the gray scale pixels into a byte array arranged for RGBX | |
void * src = CGBitmapContextGetData(grayscaleContext); | |
void * dest = malloc(size.width*scale*size.height*scale*4); | |
char* src8 = (char*)src; | |
char* dst8 = (char*)dest; | |
int count = (int)(size.width * scale * size.height * scale); | |
for (int i = 0; i < count; i++) | |
{ | |
*dst8++ = (char)0; | |
*dst8++ = *src8++; | |
*dst8++ = *src8++; | |
*dst8++ = *src8++; | |
} | |
// Get the image | |
CGContextRef rgbContext = CreateCGBitmapContextForWidthAndHeight(dest, size.width*scale, size.height*scale); | |
CGImageRef webViewImageRef = CGBitmapContextCreateImage(rgbContext); | |
UIImage* webViewImage = [UIImage imageWithCGImage:webViewImageRef scale:scale orientation:UIImageOrientationUp]; | |
self.renderedWebView.image = webViewImage; | |
// Cleanup | |
CGImageRelease(webViewImageRef); | |
CGContextRelease(rgbContext); | |
CGContextRelease(grayscaleContext); | |
free(dest); | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment