-
Notifications
You must be signed in to change notification settings - Fork 1
/
ImageDownloaderBlockingPatch.m
320 lines (274 loc) · 12.4 KB
/
ImageDownloaderBlockingPatch.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
#import "ImageDownloaderBlockingPatch.h"
#import <OpenGL/CGLMacro.h>
#import <CommonCrypto/CommonDigest.h>
@implementation ImageDownloaderBlockingPatch : QCPatch
+ (QCPatchExecutionMode)executionModeWithIdentifier:(id)fp8
{
return kQCPatchExecutionModeProcessor;
}
+ (BOOL)allowsSubpatchesWithIdentifier:(id)fp8
{
return NO;
}
+ (QCPatchTimeMode)timeModeWithIdentifier:(id)fp8
{
return kQCPatchTimeModeNone;
}
- (id)initWithIdentifier:(id)fp8
{
if( self = [super initWithIdentifier:fp8])
{
[inputInterpolation setMaxIndexValue: 1];
}
return self;
}
- (BOOL)execute:(id)glContext time:(double)time arguments:(id)arguments
{
NSDictionary *args = [NSDictionary dictionaryWithObjectsAndKeys:[NSURL URLWithString:[inputURL stringValue]], @"URL",
glContext, @"context",
[NSNumber numberWithInt:[inputProxyThresholdWidth indexValue] * [inputUseProxyCache booleanValue]], @"width",
[NSNumber numberWithInt:[inputInterpolation indexValue] * [inputUseProxyCache booleanValue]], @"interpolate", nil];
[outputFinished setBooleanValue: FALSE];
if([inputAsynchronous booleanValue])
{
[NSThread detachNewThreadSelector:@selector(_asyncLoadThread:) toTarget:self withObject:args];
}
else // synchronous load
[self _asyncLoadThread:args];
return YES;
}
- (QCImage *)requestImage:(NSURL *)url proxyWidth:(int)width interpolation:(int)interpolate properties:(NSMutableDictionary*)properties
{
QCImage *outImage = nil;
BOOL usingProxy = FALSE; // we track this for enabling/disabling colorcorrection (cached versions are already corrected, otherwise they aren't)
NSError *error=nil;
NSData *data;
if(width) // proxy stuff
{
//NSLog(@"ImageDownloaderBlockingCache::requestImageUsingCache() Proxy path");
// look for on-disk proxy
NSData *urlData = [[url absoluteString] dataUsingEncoding: NSUTF8StringEncoding];
unsigned char digest[20]; // 160 bits of sha digest
CC_SHA1([urlData bytes], [urlData length], digest);
NSString *proxyFile = [NSString stringWithFormat:
@"%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x-%i-%i.png",
//@"%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x-%i.jpg",
digest[0],digest[1],digest[2],digest[3],digest[4],
digest[5],digest[6],digest[7],digest[8],digest[9],
digest[10],digest[11],digest[12],digest[13],digest[14],
digest[15],digest[16],digest[17],digest[18],digest[19], width, interpolate];
NSString *proxyExifFile = [NSString stringWithFormat:@"%@.exif", proxyFile]; // blahblah-width-int.png.exif
NSString *cachePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
cachePath = [cachePath stringByAppendingPathComponent:
[[NSBundle bundleForClass:[self class]] bundleIdentifier]];
// typically, this only happens once ever -- so we can reliably expect if to fail all of the time.
// tell GCC about it for slightly better reordering/hinting
NSFileManager *defaultManager = [NSFileManager defaultManager];
if( __builtin_expect([defaultManager fileExistsAtPath: cachePath] == NO, 0) )
{ // no cache folder, make it
//NSLog(@"ImageDownloaderBlockingCache::requestImageUsingCache() Creating Cache folder [%@]", cachePath);
[defaultManager createDirectoryAtPath: cachePath attributes:nil];
}
NSString *cacheImagePath = [cachePath stringByAppendingPathComponent: proxyFile];
NSString *cacheExifPath = [cachePath stringByAppendingPathComponent: proxyExifFile];
//NSLog(@"ImageDownloaderBlockingCache::requestImageUsingCache() Proxy file: %@", cacheImagePath);
//NSLog(@"ImageDownloaderBlockingCache::requestImageUsingCache() Proxy EXIF file: %@", cacheExifPath);
if([defaultManager fileExistsAtPath: cacheImagePath] == NO)
{
//NSLog(@"ImageDownloaderBlockingCache::requestImageUsingCache() No Proxy file... inspecting size...");
data = [NSData dataWithContentsOfURL: url];
// get EXIF info stuff
{
if([data length])
{
CGImageSourceRef isr = CGImageSourceCreateWithData((CFDataRef)data, nil);
if(isr)
{
//NSLog(@"isr exists!");
CFDictionaryRef d = CGImageSourceCopyPropertiesAtIndex(isr, 0, nil);
[properties addEntriesFromDictionary:(NSDictionary *)d];
if (d)
CFRelease(d);
CFRelease(isr);
}
else
{
//NSLog(@"isr fail!");
}
}
//NSLog(@"ImageDownloaderBlockingCache::requestImageUsingCache() EXIF Properties: %@", properties);
NSString *error;
NSData *exifData = [NSPropertyListSerialization
dataFromPropertyList: properties
format: kCFPropertyListBinaryFormat_v1_0
errorDescription: &error];
[exifData writeToFile: cacheExifPath atomically: NO];
}
CIImage *sourceImage = [[CIImage alloc] initWithData: data];
CIFilter *scale;
//if([sourceImage size].width > width) // resize if it's big enough
if([sourceImage extent].size.width > width) // resize if it's big enough
{
//float aspect = [sourceImage extent].size.width / [sourceImage extent].size.height;
if(interpolate) // lanczos scaling -- nice, but slower (also introduces a 1-pixel border sometimes?)
{
scale = [CIFilter filterWithName:@"CILanczosScaleTransform"];
[scale setValue: sourceImage forKey:@"inputImage"];
[scale setValue: [NSNumber numberWithDouble:width/[sourceImage extent].size.width] forKey:@"inputScale"];
[scale setValue: [NSNumber numberWithDouble: 1.0] forKey:@"inputAspectRatio"];
//outImage = [[QCImage alloc] initWithCIImage: [scale valueForKey:@"outputImage"] options:nil];
}
else // affine scaling -- bilinear maybe?
{
NSAffineTransform *transform = [NSAffineTransform transform];
[transform scaleBy: width/[sourceImage extent].size.width ];
scale = [CIFilter filterWithName:@"CIAffineTransform"];
[scale setValue: sourceImage forKey:@"inputImage"];
[scale setValue: transform forKey:@"inputTransform"];
//outImage = [[QCImage alloc] initWithCIImage:[scale valueForKey:@"outputImage"] options:nil];
}
outImage = [[QCImage alloc] initWithCIImage:[scale valueForKey:@"outputImage"] options:nil];
[outImage setMetadata:url forKey:@"dataURL" shouldForward:NO];
[outImage setMetadata:[NSNumber numberWithUnsignedInt:[data length]] forKey:@"dataSize" shouldForward:NO];
// Passing the image through CoreImage cooks the colorspace -- go figure. This makes it match again...
[outImage setMetadata:[NSNumber numberWithBool: FALSE] forKey:@"disableColorMatching" shouldForward:NO];
[outImage setMetadata:[NSNumber numberWithInt:0] forKey:@"textureTarget" shouldForward:YES];
[outImage setMetadata:[NSNumber numberWithInt:0] forKey:@"textureLevels" shouldForward:YES];
[self saveCIImage: [scale valueForKey:@"outputImage"] toFile: cacheImagePath];
[sourceImage release];
return outImage;
}
[sourceImage release];
//NSLog(@"ImageDownloaderBlockingCache::requestImageUsingCache() Proxy Handling Completed.");
}
else // proxy file exists, just load that.
{
//NSLog(@"ImageDownloaderBlockingCache::requestImageUsingCache() Found proxy file. Using as data source...");
data = [NSData dataWithContentsOfFile: cacheImagePath];
// load cached exif info, if any
NSString *error;
NSData *exifFileData = [[NSData alloc] initWithContentsOfFile: cacheExifPath];
[properties addEntriesFromDictionary:
[NSPropertyListSerialization
propertyListFromData: exifFileData
mutabilityOption: NSPropertyListImmutable
format: NULL
errorDescription: &error]];
[exifFileData release];
usingProxy = TRUE;
}
}
else // non-proxied version. use raw url data
{
//NSLog(@"ImageDownloaderBlockingCache::requestImageUsingCache() Non-Proxy Path");
data = [NSData dataWithContentsOfURL: url];
if([data length])
{
// read EXIF info
CGImageSourceRef isr = CGImageSourceCreateWithData((CFDataRef)data, nil);
if(isr)
{
if(CGImageSourceGetCount(isr)>0)
{
//NSLog(@"isr2 exists!");
CFDictionaryRef d = CGImageSourceCopyPropertiesAtIndex(isr, 0, nil);
[properties addEntriesFromDictionary: (NSDictionary*)d];
if (d)
CFRelease(d);
}
CFRelease(isr);
}
}
else
{
//NSLog(@"isr2 fail");
}
}
if(![data length])
{
// NSLog(@"ImageDownloaderBlockingCache::requestImageUsingCache() NSData: len %08x",[data length]);
// NSLog(@"ImageDownloaderBlockingCache::requestImageUsingCache() NSError: %08x",error);
NSBundle *thisBundle=[NSBundle bundleForClass:[self class]];
NSString *path=[thisBundle pathForResource:@"bad" ofType:@"png"];
if(!path)
{
NSLog(@"ImageDownloaderBlockingCache::requestImageUsingCache() can't find bad.png");
return NO;
}
//NSURL *errorURL=[NSURL fileURLWithPath:path];
data=[NSData dataWithContentsOfFile: path options:0 error:&error];
}
{
//NSAssert(data,@"ImageDownloaderBlockingCache::requestImageUsingCache() data == 0");
//NSLog(@"ImageDownloaderBlockingCache::requestImageUsingCache() Creating QCImage...");
outImage=[[QCImage alloc] initWithData:data options:nil];
if(!outImage)
{
NSString *path=[[NSBundle bundleForClass:[self class]] pathForResource:@"bad" ofType:@"png"];
data=[NSData dataWithContentsOfFile: path options:0 error:&error];
outImage=[[QCImage alloc] initWithData:data options:nil];
}
[outImage setMetadata:url forKey:@"dataURL" shouldForward:NO];
[outImage setMetadata:[NSNumber numberWithUnsignedInt:[data length]] forKey:@"dataSize" shouldForward:NO];
if(usingProxy) // cached versions don't need color matching
[outImage setMetadata:[NSNumber numberWithBool:TRUE] forKey:@"disableColorMatching" shouldForward:NO];
[outImage setMetadata:[NSNumber numberWithInt:0] forKey:@"textureTarget" shouldForward:YES];
[outImage setMetadata:[NSNumber numberWithInt:0] forKey:@"textureLevels" shouldForward:YES];
//NSAssert(outImage,@"ImageDownloaderBlockingCache::requestImageUsingCache() outImage == 0");
//NSLog(@"ImageDownloaderBlockingCache::requestImageUsingCache() QCImage Created (0x%08x)",outImage);
// cast is to silence a gcc warning -- initWithImage conflicts with a CoreImage message
//NSLog(@"ImageDownloaderBlockingCache::requestImageUsingCache(%@) estimated size: %lu",url,[c estimatedSize]);
}
return outImage;
}
- (void) saveCIImage:(CIImage*)img toFile:(NSString*)file
{
NSBitmapImageRep *bmp = [[NSBitmapImageRep alloc]
// initWithCIImage: img]; // leopard-only. kills rest of this function :)
initWithBitmapDataPlanes: NULL
pixelsWide: [img extent].size.width//newSize.width
pixelsHigh: [img extent].size.height//newSize.height
bitsPerSample: 8
samplesPerPixel: 4
hasAlpha: YES
isPlanar: NO
colorSpaceName: NSDeviceRGBColorSpace
bytesPerRow: 0
bitsPerPixel: 0];
// Create an NSGraphicsContext that draws into the NSBitmapImageRep. (This capability is new in Tiger.)
NSGraphicsContext *nsContext = [NSGraphicsContext
graphicsContextWithBitmapImageRep: bmp];
// Save the previous graphics context and state, and make our bitmap context current.
[NSGraphicsContext saveGraphicsState];
[NSGraphicsContext setCurrentContext: nsContext];
// Get a CIContext from the NSGraphicsContext, and use it to draw the CIImage into the NSBitmapImageRep.
CGPoint point;
point.x = 0;
point.y = 0;
[[nsContext CIContext] drawImage: img atPoint: point fromRect: [img extent]];
// Restore the previous graphics context and state.
[NSGraphicsContext restoreGraphicsState];
[[bmp representationUsingType: NSPNGFileType properties: nil]
writeToFile: file atomically:NO];
[bmp release];
}
-(void)_asyncLoadThread:(NSDictionary*)context
{
NSAutoreleasePool *pool = [NSAutoreleasePool new];
NSURL *url = [context objectForKey:@"URL"];
NSMutableDictionary *properties = [[NSMutableDictionary alloc] init];
QCImage *image;
image = [self requestImage: url
proxyWidth: [[context objectForKey:@"width"] intValue]
interpolation: [[context objectForKey:@"interpolate"] intValue]
properties: properties];
QCStructure *exif = [[QCStructure alloc] initWithDictionary: properties];
[outputEXIFData setStructureValue: exif];
[exif release];
[properties release];
[outputImage setImageValue:image];
[image release];
[outputFinished setBooleanValue: TRUE];
[pool drain];
}
@end