-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathDYImageView.m
597 lines (545 loc) · 16.9 KB
/
DYImageView.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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
//Copyright 2005-2023 Dominic Yu. Some rights reserved.
//This work is licensed under the Creative Commons
//Attribution-NonCommercial-ShareAlike License. To view a copy of this
//license, visit http://creativecommons.org/licenses/by-nc-sa/2.0/ or send
//a letter to Creative Commons, 559 Nathan Abbott Way, Stanford,
//California 94305, USA.
// Created by d on 2005.04.01.
#import "DYImageView.h"
@implementation DYImageViewZoomInfo
@end
@implementation DYImageView
{
NSImage *image;
NSTimer *gifTimer;
NSArray *_webpFrameInfo; int _webpCurrentFrame; NSUInteger _webpFrameCount;
int rotation;
BOOL scalesUp, showActualSize, isImageFlipped;
NSRect sourceRect;
NSSize destSize;
float zoomF;
}
// helper method to calculate appropriate zoom factor
- (float)zoomForFit {
float tmp;
NSSize imgSize = image.size; // in pixels
NSSize bSize = [self convertRect:self.bounds toView:nil].size; // in pixels (window units)
if (rotation == 90 || rotation == -90) {
tmp = bSize.width;
bSize.width = bSize.height;
bSize.height = tmp;
}
if (!scalesUp
&& imgSize.width <= bSize.width
&& imgSize.height <= bSize.height)
{
return 1;
} else {
float w_ratio, h_ratio;
w_ratio = bSize.width/imgSize.width;
h_ratio = bSize.height/imgSize.height;
if (w_ratio < h_ratio) {
return w_ratio;
} else {
return h_ratio;
}
}
}
- (void)drawRect:(NSRect)rect {
if (!image) return; //don't draw if nil
NSRect srcRect, destinationRect;
float zoom = zoomF;
NSRect boundsRect = [self convertRect:self.bounds toView:nil];
float centerX, centerY; float tmp;
centerX = (int)(boundsRect.size.width/2);
centerY = (int)(boundsRect.size.height/2);
if (zoomF) {
srcRect = sourceRect;
destinationRect.size = destSize;
} else {
if (rotation == 90 || rotation == -90) {
tmp = boundsRect.size.width;
boundsRect.size.width = boundsRect.size.height;
boundsRect.size.height = tmp;
}
srcRect.origin = NSZeroPoint;
srcRect.size = image.size;
if (!scalesUp
&& srcRect.size.width <= boundsRect.size.width
&& srcRect.size.height <= boundsRect.size.height)
{
destinationRect.size.width = (int)(srcRect.size.width);
destinationRect.size.height = (int)(srcRect.size.height);
zoom = 1;
} else {
float w_ratio, h_ratio;
w_ratio = boundsRect.size.width/srcRect.size.width;
h_ratio = boundsRect.size.height/srcRect.size.height;
if (w_ratio < h_ratio) { // the side w/ bigger ratio needs to be shrunk
destinationRect.size.height = (int)(srcRect.size.height*w_ratio);
destinationRect.size.width = (int)(boundsRect.size.width);
zoom = w_ratio;
} else {
destinationRect.size.width = (int)(srcRect.size.width*h_ratio);
destinationRect.size.height = (int)(boundsRect.size.height);
zoom = h_ratio;
}
}
}
destinationRect.origin.x = (int)(centerX - destinationRect.size.width/2);
destinationRect.origin.y = (int)(centerY - destinationRect.size.height/2);
[NSColor.whiteColor set]; // make a nice background for transparent gifs, etc.
if (rotation == 0 || rotation == 180) {
// convert destinationRect to view coords
destinationRect = [self convertRect:destinationRect fromView:nil];
[NSBezierPath fillRect:destinationRect];
// apparently doing this after you perform the transform
// will sometimes lead to white lines on the right side of 180-degree turned images.
// not sure why. so we put a call here for 180, and later on for +/-90
}
NSAffineTransform *transform = [NSAffineTransform transform];
[transform translateXBy:centerX yBy:centerY];
if (rotation != 0) [transform rotateByDegrees:rotation]; //isImageFlipped ? -rotation : rotation]; // order matters! if you switch flipping with rotation, you need to switch 90-degree rotations too
if (isImageFlipped) [transform scaleXBy:-1 yBy:1]; // [transform translateXBy:-centerX*2 yBy:0]; // move over by the screen width; don't use value of boundsRect.size.width, because it might have been switched with the height (above)
[transform translateXBy:-centerX yBy:-centerY];
[transform concat];
if (rotation == 90 || rotation == -90) { // see comment above
destinationRect = [self convertRect:destinationRect fromView:nil];
[NSBezierPath fillRect:destinationRect];
}
NSGraphicsContext *cg = NSGraphicsContext.currentContext;
NSImageInterpolation oldInterp = cg.imageInterpolation;
cg.imageInterpolation = zoom >= 4 ? NSImageInterpolationNone : NSImageInterpolationHigh;
NSImage *toDraw = image;
if (_webpImageSource) {
CGImageRef webpFrame = CGImageSourceCreateImageAtIndex((CGImageSourceRef)_webpImageSource, _webpCurrentFrame, NULL);
if (webpFrame) {
toDraw = [[NSImage alloc] initWithCGImage:webpFrame size:NSZeroSize];
if (!gifTimer || gifTimer.userInfo != _webpImageSource) {
float frameDuration = [_webpFrameInfo[_webpCurrentFrame][@"DelayTime"] floatValue];
if (frameDuration < .01) frameDuration = .01; // minimum of 10ms is apparently standard
gifTimer = [NSTimer scheduledTimerWithTimeInterval:frameDuration target:self selector:@selector(animateWebp:) userInfo:_webpImageSource repeats:NO];
gifTimer.tolerance = frameDuration*0.1;
}
}
CFRelease(webpFrame);
}
[toDraw drawInRect:destinationRect fromRect:srcRect operation:NSCompositingOperationSourceOver fraction:1.0];
cg.imageInterpolation = oldInterp;
[transform invert];
[transform concat];
if (image != toDraw) return; // animating webp, so no need to check if animating gif
id rep = image.representations[0];
if ([rep isKindOfClass:[NSBitmapImageRep class]]
&& [rep valueForProperty:NSImageFrameCount]) {
if (!gifTimer || gifTimer.userInfo != image) {
float frameDuration = [[rep valueForProperty:NSImageCurrentFrameDuration] floatValue];
gifTimer = [NSTimer scheduledTimerWithTimeInterval:frameDuration
target:self selector:@selector(animateGIF:)
userInfo:image repeats:NO];
gifTimer.tolerance = frameDuration*0.15;
}
}
}
- (void)animateGIF:(NSTimer *)t {
gifTimer = nil;
if (image != t.userInfo) return; // stop if image is changed
NSBitmapImageRep *rep = (NSBitmapImageRep *)[t.userInfo representations][0];
NSNumber *frameCount = [rep valueForProperty:NSImageFrameCount];
int n = [[rep valueForProperty:NSImageCurrentFrame] intValue];
if (++n == frameCount.intValue) n = 0;
[rep setProperty:NSImageCurrentFrame withValue:@(n)];
[self setNeedsDisplay:YES];
}
- (void)setWebpImageSource:(id)src {
_webpImageSource = nil;
CFDictionaryRef props = CGImageSourceCopyProperties((CGImageSourceRef)src, NULL);
if (props) {
NSArray *frameInfo = ((__bridge NSDictionary *)props)[@"{WebP}"][@"FrameInfo"];
if (frameInfo && (_webpFrameCount = frameInfo.count) > 1) {
_webpImageSource = src;
_webpFrameInfo = frameInfo;
}
CFRelease(props);
}
}
- (void)animateWebp:(NSTimer *)t {
gifTimer = nil;
if (_webpImageSource != t.userInfo) return;
if (++_webpCurrentFrame == _webpFrameCount) _webpCurrentFrame = 0;
[self setNeedsDisplay:YES];
}
- (void)setImage:(NSImage *)anImage {
if (anImage != image) {
image = anImage;
_webpImageSource = nil;
_webpCurrentFrame = 0;
}
zoomF = 0;
rotation = 0;
isImageFlipped = NO;
[NSCursor.arrowCursor set];
[self setNeedsDisplay:YES];
}
- (void)setImage:(NSImage *)anImage zooming:(DYImageViewZoomMode)zoomMode {
if (anImage != image) {
if (!anImage) return;
image = anImage;
NSImageRep *rep = image.representations[0]; // ** assume not corrupt
if ([rep isKindOfClass:[NSBitmapImageRep class]]) {
image.size = NSMakeSize(rep.pixelsWide, rep.pixelsHigh);
} // ** cat on nsimage?
}
if (zoomMode == DYImageViewZoomModeManual) {
return;
}
if (zoomMode == DYImageViewZoomModeActualSize) {
zoomF = 1;
} else {
float f = [self zoomForFit];
float p = 2*log2f(f);
if (zoomMode == DYImageViewZoomModeZoomIn) {
int n = floorf(p); // find closest half-power of 2
do { // in case we're off slightly, keep increasing until we find the right value
n++;
zoomF = n%2 ? ldexpf(1.5,(n-1)/2) : ldexpf(1,n/2);
} while (zoomF <= f);
} else {
int n = ceilf(p);
do {
n--;
zoomF = n%2 ? ldexpf(1.5,(n-1)/2) : ldexpf(1,n/2);
} while (zoomF >= f);
}
}
[self setZoomAndCenter:YES];
}
- (void)setZoomAndCenter:(BOOL)center {
if (!image) return;
NSSize imgSize = image.size;
float tmp;
NSSize bSize = [self convertRect:self.bounds toView:nil].size;
if (rotation == 90 || rotation == -90) {
tmp = bSize.height;
bSize.height = bSize.width;
bSize.width = tmp;
}
destSize = bSize;
NSPoint oldCenter;
if (!center) {
oldCenter.x = sourceRect.origin.x + sourceRect.size.width/2;
oldCenter.y = sourceRect.origin.y + sourceRect.size.height/2;
}
sourceRect.size.width = (int)(bSize.width/zoomF);
sourceRect.size.height = (int)(bSize.height/zoomF);
if (sourceRect.size.width > imgSize.width) {
sourceRect.size.width = imgSize.width;
destSize.width = (int)(imgSize.width*zoomF);
sourceRect.origin.x = 0;
} else if (center) {
sourceRect.origin.x = (imgSize.width - sourceRect.size.width)/2;
} else {
sourceRect.origin.x = oldCenter.x - sourceRect.size.width/2;
if (sourceRect.origin.x < 0) {
sourceRect.origin.x = 0;
} else if (sourceRect.origin.x > (tmp = imgSize.width - sourceRect.size.width)) {
sourceRect.origin.x = tmp;
}
}
if (sourceRect.size.height > imgSize.height) {
sourceRect.size.height = imgSize.height;
destSize.height = (int)(imgSize.height*zoomF);
sourceRect.origin.y = 0;
} else if (center) {
sourceRect.origin.y = (imgSize.height - sourceRect.size.height)/2;
} else {
sourceRect.origin.y = oldCenter.y - sourceRect.size.height/2;
if (sourceRect.origin.y < 0) {
sourceRect.origin.y = 0;
} else if (sourceRect.origin.y > (tmp = imgSize.height - sourceRect.size.height)) {
sourceRect.origin.y = tmp;
}
}
[self setCursor];
[self setNeedsDisplay:YES];
}
- (int)rotation { return rotation; }
- (int)addRotation:(int)r {
rotation += r;
if (rotation > 180) rotation -=360; else if (rotation < -90) rotation += 360;
if (zoomF)
[self setZoomAndCenter:NO];
else
[self setNeedsDisplay:YES];
return rotation;
}
// float tmp, tmp2; // inner, outer rect insets (tmp > tmp2)
// if (sourceRect.size.width > sourceRect.size.height) {
// tmp = sourceRect.size.width;
// tmp2 = sourceRect.size.height;
// } else {
// tmp = sourceRect.size.height;
// tmp2 = sourceRect.size.width;
// }
//
// float x,y, x2,y2, h,w, h2,w2, cx, cy, hypnus,hypnus2;
// // center
// cx = imgSize.width/2;
// cy = imgSize.height/2;
// // inner rect
// h = imgSize.height - tmp;
// w = imgSize.width - tmp;
// // outer rect
// h2 = imgSize.height - tmp2;
// w2 = imgSize.width - tmp2;
// // hypotenuse
// hypnus = sqrtf(w*w+h*h);
// hypnus2 = sqrtf(w2*w2+h2*h2);
// // original point
// x = sourceRect.origin.x/(imgSize.width-sourceRect.size.width)*w;
// y = sourceRect.origin.y/(imgSize.height-sourceRect.size.height)*h;
// // transformed point, in our criss-cross coord sys
// x2 = ((h*x/w+h+y)/2-cy)*hypnus/h;
// y2 = ((h-h*y/w+y)/2-cy)*hypnus/h;
// // stretch y
// y2 = y2*hypnus2/hypnus;
// // untransform
// // i give up!
- (void)setRotation:(int)n {
// assume from zero, don't call when zoomed
rotation = n;
[self setNeedsDisplay:YES];
}
- (void)setImageFlipped:(BOOL)b {
isImageFlipped = b;
[self setNeedsDisplay:YES];
}
- (BOOL)toggleFlip {
isImageFlipped = !isImageFlipped;
if (rotation == 90 || rotation == -90) {
rotation = -rotation;
}
[self setNeedsDisplay:YES];
return isImageFlipped;
}
- (BOOL)isImageFlipped {
return isImageFlipped;
}
//- (float)zoom { return zoom; }
- (BOOL)scalesUp { return scalesUp; }
- (void)setScalesUp:(BOOL)b {
if (scalesUp == b) return;
scalesUp = b;
[self setNeedsDisplay:YES];
}
- (BOOL)showActualSize { return showActualSize; }
- (void)setShowActualSize:(BOOL)b {
if (showActualSize == b) return;
showActualSize = b;
//[[NSCursor arrowCursor] set];
[self setNeedsDisplay:YES];
}
- (void)zoomActualSize { // toggles
if (zoomF != 1) {
zoomF = 1;
[self setZoomAndCenter:NO];
}
}
- (void)zoomIn {
if (zoomF == 0) {
[self setImage:image zooming:DYImageViewZoomModeZoomIn];
return;
}
if (zoomF >= 512) {
NSBeep();
return;
}
float oldF = zoomF;
float p = 2*log2f(zoomF);
int n = (int)floorf(p);
float f;
do {
n++;
f = n%2 ? ldexpf(1.5,(n-1)/2) : ldexpf(1,n/2);
} while (f <= oldF);
[self setZoomF:f];
}
- (void)setZoomF:(float)f {
if (!image) return;
NSSize imgSize = image.size;
float tmp;
NSSize bSize = [self convertRect:self.bounds toView:nil].size;
if (rotation == 90 || rotation == -90) {
tmp = bSize.height;
bSize.height = bSize.width;
bSize.width = tmp;
}
// new size
NSSize s = destSize = bSize;
zoomF = f < 0.002 ? 0.002 : f > 512 ? 512 : f;
s.width = (int)(s.width/zoomF); // always make dims integral; not nec origins
s.height = (int)(s.height/zoomF);
if (s.width > imgSize.width) {
s.width = imgSize.width;
destSize.width = (int)(s.width*zoomF);
}
if (s.height > imgSize.height) {
s.height = imgSize.height;
destSize.height = (int)(s.height*zoomF);
}
// new x
if (s.width < imgSize.width) {
if (sourceRect.size.width == imgSize.width)
// special case for when smaller than screen becomes larger than screen
sourceRect.origin.x = (imgSize.width - s.width)/2;
else if (sourceRect.origin.x == imgSize.width - sourceRect.size.width) // must cast to int, for edge cases
sourceRect.origin.x = imgSize.width - s.width;
else if (sourceRect.origin.x > 0)
// leave 0 if 0
sourceRect.origin.x += (sourceRect.size.width - s.width)/2.0;
} else {
sourceRect.origin.x = 0;
}
// new y
if (s.height < imgSize.height) {
if (sourceRect.size.height == imgSize.height)
sourceRect.origin.y = (imgSize.height - s.height)/2;
else if (sourceRect.origin.y == imgSize.height - sourceRect.size.height)
sourceRect.origin.y = imgSize.height - s.height;
else if (sourceRect.origin.y > 0)
sourceRect.origin.y += (sourceRect.size.height - s.height)/2.0;
} else {
sourceRect.origin.y = 0;
}
sourceRect.size = s;
[self setCursor];
[self setNeedsDisplay:YES];
}
- (void)zoomOut {
if (zoomF == 0) {
[self setImage:image zooming:DYImageViewZoomModeZoomOut];
return;
}
if (zoomF < 0.002) {
NSBeep();
return;
}
float oldF = zoomF;
float p = 2*log2f(zoomF);
int n = (int)ceilf(p);
do {
n--;
zoomF = n%2 ? ldexpf(1.5,(n-1)/2) : ldexpf(1,n/2);
} while (zoomF >= oldF);
[self setZoomAndCenter:NO];
}
- (void)fakeDragX:(float)x y:(float)y {
x /= zoomF;
y /= zoomF;
float xmax,ymax;
xmax = image.size.width - sourceRect.size.width;
ymax = image.size.height - sourceRect.size.height;
if (xmax > 0 || ymax > 0) {
switch (rotation) {
case -90:
sourceRect.origin.y -= x;
sourceRect.origin.x += y;
break;
case 90:
sourceRect.origin.y += x;
sourceRect.origin.x -= y;
break;
case 180:
sourceRect.origin.x += x;
sourceRect.origin.y += y;
break;
default:
sourceRect.origin.x -= x;
sourceRect.origin.y -= y;
break;
}
if (sourceRect.origin.x > xmax)
sourceRect.origin.x = xmax;
else if (sourceRect.origin.x < 0)
sourceRect.origin.x = 0;
if (sourceRect.origin.y > ymax)
sourceRect.origin.y = ymax;
else if (sourceRect.origin.y < 0)
sourceRect.origin.y = 0;
[self setNeedsDisplay:YES];
}
}
- (void)scrollWheel:(NSEvent *)e {
if (self.dragMode)
[self fakeDragX:e.deltaX*128 y:-e.deltaY*128];
else
[super scrollWheel:e];
}
- (void)mouseDown:(NSEvent *)e {
if (self.dragMode) {
[NSCursor.closedHandCursor push];
}
[super mouseDown:e];
}
- (void)mouseDragged:(NSEvent *)e {
if (zoomF)
[self fakeDragX:e.deltaX y:-e.deltaY]; // y is flipped?
[NSCursor.closedHandCursor set];
}
- (void)mouseUp:(NSEvent *)e {
if (self.dragMode) {
[NSCursor pop];
[self setCursor];
}
[super mouseUp:e];
}
- (void)zoomOff {
if (zoomF) {
zoomF = 0;
[NSCursor.arrowCursor set];
[self setNeedsDisplay:YES];
}
}
- (DYImageViewZoomInfo *)zoomInfo {
if (!showActualSize && zoomF == 0) return nil;
DYImageViewZoomInfo *i = [[DYImageViewZoomInfo alloc] init];
i->zoomF = zoomF;
i->sourceRect = sourceRect;
i->destSize = destSize;
return i;
}
- (void)setZoomInfo:(DYImageViewZoomInfo *)i {
zoomF = i->zoomF;
sourceRect = i->sourceRect;
destSize = i->destSize;
[self setCursor];
[self setNeedsDisplay:YES];
}
- (BOOL)zoomInfoNeedsSaving {
if (showActualSize) {
if (zoomF != 1) return YES;
NSSize imgSize = image.size;
return (sourceRect.origin.x != sourceRect.size.width > imgSize.width ? 0 : (imgSize.width - sourceRect.size.width)/2)
|| (sourceRect.origin.y != sourceRect.size.height > imgSize.height ? 0 : (imgSize.height - sourceRect.size.height)/2);
} else {
return zoomF != 0;
}
}
- (BOOL)zoomMode { return zoomF != 0; }
- (float)zoomF {return zoomF;}
- (NSImage *)image {return image;}
- (BOOL)dragMode {
if (zoomF == 0) return NO;
NSSize imgSize = image.size;
return sourceRect.size.width < imgSize.width || sourceRect.size.height < imgSize.height;
}
- (void)setCursor {
// sets hand or arrow, depending
if (self.dragMode) {
[NSCursor.openHandCursor set];
[NSCursor setHiddenUntilMouseMoves:NO]; // NOT unhide
} else {
[NSCursor.arrowCursor set];
}
}
@end