Artifact
62bd8f0ea5324f08b06ec76ab435f931c0eeec57 :
File
lib/jedit/img_orientation.tcl
— part of check-in
[d9d2e18858]
at
2009-08-12 02:54:38
on branch trunk
— Switched orientation controller to use futures in its API, and updated the places using it
(user:
aku
size: 5489)
0000: 23 23 20 2d 2a 2d 20 74 63 6c 20 2d 2a 2d 0a 23 ## -*- tcl -*-.#
0010: 20 23 23 23 20 23 23 23 20 23 23 23 20 23 23 23 ### ### ### ###
0020: 23 23 23 23 23 23 20 23 23 23 23 23 23 23 23 23 ###### #########
0030: 20 23 23 23 23 23 23 23 23 23 0a 0a 23 23 20 4d #########..## M
0040: 61 6e 69 70 75 6c 61 74 65 20 69 6d 61 67 65 20 anipulate image
0050: 6f 72 69 65 6e 74 61 74 69 6f 6e 20 28 72 6f 74 orientation (rot
0060: 61 74 69 6f 6e 2c 20 66 6c 69 70 73 20 61 6c 6f ation, flips alo
0070: 6e 67 20 76 61 72 69 6f 75 73 20 61 78 65 73 29 ng various axes)
0080: 2e 0a 23 23 20 43 50 53 20 73 74 79 6c 65 20 28 ..## CPS style (
0090: 72 65 71 75 65 73 74 20 2b 20 63 6f 6d 70 6c 65 request + comple
00a0: 74 69 6f 6e 20 63 61 6c 6c 62 61 63 6b 29 2e 0a tion callback)..
00b0: 23 23 20 46 75 6c 6c 79 20 70 61 72 61 6c 6c 65 ## Fully paralle
00c0: 6c 20 77 69 74 68 20 74 68 65 20 6d 61 69 6e 20 l with the main
00d0: 74 61 73 6b 73 20 64 75 65 20 74 6f 20 75 73 65 tasks due to use
00e0: 20 6f 66 20 54 68 72 65 61 64 73 20 28 70 6f 6f of Threads (poo
00f0: 6c 29 2e 0a 0a 23 20 23 23 23 20 23 23 23 20 23 l)...# ### ### #
0100: 23 23 20 23 23 23 23 23 23 23 23 23 20 23 23 23 ## ######### ###
0110: 23 23 23 23 23 23 20 23 23 23 23 23 23 23 23 23 ###### #########
0120: 0a 23 23 20 52 65 71 75 69 73 69 74 65 73 0a 0a .## Requisites..
0130: 70 61 63 6b 61 67 65 20 72 65 71 75 69 72 65 20 package require
0140: 54 63 6c 20 38 2e 35 0a 70 61 63 6b 61 67 65 20 Tcl 8.5.package
0150: 72 65 71 75 69 72 65 20 44 65 62 75 67 0a 70 61 require Debug.pa
0160: 63 6b 61 67 65 20 72 65 71 75 69 72 65 20 73 6e ckage require sn
0170: 69 74 0a 70 61 63 6b 61 67 65 20 72 65 71 75 69 it.package requi
0180: 72 65 20 74 68 72 65 61 64 70 6f 6f 6c 0a 70 61 re threadpool.pa
0190: 63 6b 61 67 65 20 72 65 71 75 69 72 65 20 75 65 ckage require ue
01a0: 76 65 6e 74 3a 3a 6a 6f 62 3a 3a 71 75 65 75 65 vent::job::queue
01b0: 0a 0a 23 20 23 23 23 20 23 23 23 20 23 23 23 20 ..# ### ### ###
01c0: 23 23 23 23 23 23 23 23 23 20 23 23 23 23 23 23 ######### ######
01d0: 23 23 23 20 23 23 23 23 23 23 23 23 23 0a 23 23 ### #########.##
01e0: 20 54 72 61 63 69 6e 67 0a 0a 44 65 62 75 67 20 Tracing..Debug
01f0: 6f 66 66 20 69 6d 67 2f 6f 72 69 65 6e 74 61 74 off img/orientat
0200: 69 6f 6e 0a 0a 23 20 23 23 23 20 23 23 23 20 23 ion..# ### ### #
0210: 23 23 20 23 23 23 23 23 23 23 23 23 20 23 23 23 ## ######### ###
0220: 23 23 23 23 23 23 20 23 23 23 23 23 23 23 23 23 ###### #########
0230: 0a 23 23 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 .## Implementati
0240: 6f 6e 0a 0a 73 6e 69 74 3a 3a 74 79 70 65 20 69 on..snit::type i
0250: 6d 67 3a 3a 6f 72 69 65 6e 74 61 74 69 6f 6e 20 mg::orientation
0260: 7b 0a 20 20 20 20 23 20 23 23 23 20 23 23 23 20 {. # ### ###
0270: 23 23 23 20 23 23 23 23 23 23 23 23 23 20 23 23 ### ######### ##
0280: 23 23 23 23 23 23 23 20 23 23 23 23 23 23 23 23 ####### ########
0290: 23 0a 20 20 20 20 23 23 20 41 50 49 0a 0a 20 20 #. ## API..
02a0: 20 20 74 79 70 65 6d 65 74 68 6f 64 20 6c 69 6d typemethod lim
02b0: 69 74 20 7b 6e 7d 20 7b 0a 09 24 6f 75 72 70 6f it {n} {..$ourpo
02c0: 6f 6c 20 63 6f 6e 66 69 67 75 72 65 20 2d 6c 69 ol configure -li
02d0: 6d 69 74 20 24 6e 0a 09 72 65 74 75 72 6e 0a 20 mit $n..return.
02e0: 20 20 20 7d 0a 0a 20 20 20 20 64 65 6c 65 67 61 }.. delega
02f0: 74 65 20 74 79 70 65 6d 65 74 68 6f 64 20 7b 72 te typemethod {r
0300: 6f 74 61 74 65 20 63 63 77 7d 20 20 20 20 20 20 otate ccw}
0310: 74 6f 20 6f 75 72 73 65 6c 76 65 73 20 61 73 20 to ourselves as
0320: 7b 53 63 68 65 64 75 6c 65 20 6c 65 66 74 7d 0a {Schedule left}.
0330: 20 20 20 20 64 65 6c 65 67 61 74 65 20 74 79 70 delegate typ
0340: 65 6d 65 74 68 6f 64 20 7b 72 6f 74 61 74 65 20 emethod {rotate
0350: 63 77 7d 20 20 20 20 20 20 20 74 6f 20 6f 75 72 cw} to our
0360: 73 65 6c 76 65 73 20 61 73 20 7b 53 63 68 65 64 selves as {Sched
0370: 75 6c 65 20 72 69 67 68 74 7d 0a 20 20 20 20 64 ule right}. d
0380: 65 6c 65 67 61 74 65 20 74 79 70 65 6d 65 74 68 elegate typemeth
0390: 6f 64 20 7b 72 6f 74 61 74 65 20 75 70 7d 20 20 od {rotate up}
03a0: 20 20 20 20 20 74 6f 20 6f 75 72 73 65 6c 76 65 to ourselve
03b0: 73 20 61 73 20 7b 53 63 68 65 64 75 6c 65 20 75 s as {Schedule u
03c0: 70 7d 0a 20 20 20 20 64 65 6c 65 67 61 74 65 20 p}. delegate
03d0: 74 79 70 65 6d 65 74 68 6f 64 20 7b 66 6c 69 70 typemethod {flip
03e0: 20 68 6f 72 69 7a 6f 6e 74 61 6c 7d 20 74 6f 20 horizontal} to
03f0: 6f 75 72 73 65 6c 76 65 73 20 61 73 20 7b 53 63 ourselves as {Sc
0400: 68 65 64 75 6c 65 20 66 6c 69 70 68 7d 0a 20 20 hedule fliph}.
0410: 20 20 64 65 6c 65 67 61 74 65 20 74 79 70 65 6d delegate typem
0420: 65 74 68 6f 64 20 7b 66 6c 69 70 20 76 65 72 74 ethod {flip vert
0430: 69 63 61 6c 7d 20 20 20 74 6f 20 6f 75 72 73 65 ical} to ourse
0440: 6c 76 65 73 20 61 73 20 7b 53 63 68 65 64 75 6c lves as {Schedul
0450: 65 20 66 6c 69 70 76 7d 0a 20 20 20 20 64 65 6c e flipv}. del
0460: 65 67 61 74 65 20 74 79 70 65 6d 65 74 68 6f 64 egate typemethod
0470: 20 74 72 61 6e 73 70 6f 73 65 20 20 20 20 20 20 transpose
0480: 20 20 20 74 6f 20 6f 75 72 73 65 6c 76 65 73 20 to ourselves
0490: 61 73 20 7b 53 63 68 65 64 75 6c 65 20 74 72 61 as {Schedule tra
04a0: 6e 73 70 6f 73 65 7d 0a 20 20 20 20 64 65 6c 65 nspose}. dele
04b0: 67 61 74 65 20 74 79 70 65 6d 65 74 68 6f 64 20 gate typemethod
04c0: 74 72 61 6e 73 76 65 72 73 65 20 20 20 20 20 20 transverse
04d0: 20 20 74 6f 20 6f 75 72 73 65 6c 76 65 73 20 61 to ourselves a
04e0: 73 20 7b 53 63 68 65 64 75 6c 65 20 74 72 61 6e s {Schedule tran
04f0: 73 76 65 72 73 65 7d 0a 20 20 20 20 74 79 70 65 sverse}. type
0500: 63 6f 6d 70 6f 6e 65 6e 74 20 6f 75 72 73 65 6c component oursel
0510: 76 65 73 20 3b 23 20 3d 3d 20 24 74 79 70 65 20 ves ;# == $type
0520: 28 73 65 65 20 74 79 70 65 63 6f 6e 73 74 72 75 (see typeconstru
0530: 63 74 6f 72 29 0a 0a 20 20 20 20 23 20 23 23 23 ctor).. # ###
0540: 20 23 23 23 20 23 23 23 20 23 23 23 23 23 23 23 ### ### #######
0550: 23 23 20 23 23 23 23 23 23 23 23 23 20 23 23 23 ## ######### ###
0560: 23 23 23 23 23 23 0a 20 20 20 20 23 23 20 49 6e ######. ## In
0570: 74 65 72 6e 61 6c 20 68 65 6c 70 65 72 73 0a 0a ternal helpers..
0580: 20 20 20 20 74 79 70 65 6d 65 74 68 6f 64 20 53 typemethod S
0590: 63 68 65 64 75 6c 65 20 7b 6f 70 65 72 61 74 69 chedule {operati
05a0: 6f 6e 20 69 6d 61 67 65 66 69 6c 65 20 66 75 74 on imagefile fut
05b0: 75 72 65 7d 20 7b 0a 09 44 65 62 75 67 2e 69 6d ure} {..Debug.im
05c0: 67 2f 6f 72 69 65 6e 74 61 74 69 6f 6e 20 7b 5b g/orientation {[
05d0: 6c 69 73 74 20 24 74 79 70 65 20 53 63 68 65 64 list $type Sched
05e0: 75 6c 65 20 24 6f 70 65 72 61 74 69 6f 6e 20 24 ule $operation $
05f0: 69 6d 61 67 65 66 69 6c 65 20 24 66 75 74 75 72 imagefile $futur
0600: 65 5d 7d 0a 0a 09 23 20 54 68 69 73 20 63 6f 64 e]}...# This cod
0610: 65 20 69 73 20 64 69 66 66 65 72 65 6e 74 20 66 e is different f
0620: 72 6f 6d 20 74 68 65 20 69 6d 67 3a 3a 67 65 6f rom the img::geo
0630: 6d 65 74 72 79 20 68 61 6e 64 6c 65 72 0a 09 23 metry handler..#
0640: 20 62 65 63 61 75 73 65 20 68 65 72 65 20 77 65 because here we
0650: 20 6d 6f 64 69 66 79 20 74 68 65 20 69 6d 61 67 modify the imag
0660: 65 2c 20 6d 61 6b 69 6e 67 20 6f 72 64 65 72 20 e, making order
0670: 69 6d 70 6f 72 74 61 6e 74 2e 20 57 65 0a 09 23 important. We..#
0680: 20 63 61 6e 6e 6f 74 20 70 75 74 20 74 68 65 20 cannot put the
0690: 63 6f 6d 70 6c 65 74 69 6f 6e 20 63 61 6c 6c 62 completion callb
06a0: 61 63 6b 73 20 66 6f 72 20 6f 6e 65 20 69 6d 61 acks for one ima
06b0: 67 65 20 74 6f 67 65 74 68 65 72 2e 0a 09 23 20 ge together...#
06c0: 42 75 74 20 77 65 20 63 61 6e 20 61 6e 64 20 64 But we can and d
06d0: 6f 20 6d 61 6e 61 67 65 20 61 20 71 75 65 75 65 o manage a queue
06e0: 20 70 65 72 20 69 6d 61 67 65 66 69 6c 65 20 77 per imagefile w
06f0: 68 69 63 68 20 69 6e 6a 65 63 74 73 0a 09 23 20 hich injects..#
0700: 74 68 65 20 72 65 71 75 65 73 74 20 69 6e 74 6f the request into
0710: 20 74 68 65 20 74 68 72 65 61 64 70 6f 6f 6c 20 the threadpool
0720: 6e 6f 74 20 6f 6e 6c 79 20 69 6e 20 74 68 65 20 not only in the
0730: 70 72 6f 70 65 72 0a 09 23 20 6f 72 64 65 72 2c proper..# order,
0740: 20 62 75 74 20 65 61 63 68 20 61 6c 73 6f 20 6f but each also o
0750: 6e 6c 79 20 61 66 74 65 72 20 74 68 65 20 70 72 nly after the pr
0760: 65 76 69 6f 75 73 20 72 65 71 75 65 73 74 0a 09 evious request..
0770: 23 20 63 6f 6d 70 6c 65 74 65 64 2e 0a 0a 09 69 # completed....i
0780: 66 20 7b 21 5b 69 6e 66 6f 20 65 78 69 73 74 73 f {![info exists
0790: 20 6f 75 72 70 65 6e 64 69 6e 67 28 24 69 6d 61 ourpending($ima
07a0: 67 65 66 69 6c 65 29 5d 7d 20 7b 0a 09 20 20 20 gefile)]} {..
07b0: 20 23 20 4e 6f 20 6a 6f 62 20 71 75 65 75 65 20 # No job queue
07c0: 66 6f 72 20 74 68 65 20 69 6d 61 67 65 20 66 69 for the image fi
07d0: 6c 65 20 79 65 74 2e 20 43 72 65 61 74 65 20 69 le yet. Create i
07e0: 74 2e 0a 09 20 20 20 20 73 65 74 20 6f 75 72 70 t... set ourp
07f0: 65 6e 64 69 6e 67 28 24 69 6d 61 67 65 66 69 6c ending($imagefil
0800: 65 29 20 5c 0a 09 09 5b 75 65 76 65 6e 74 3a 3a e) \...[uevent::
0810: 6a 6f 62 3a 3a 71 75 65 75 65 20 24 7b 74 79 70 job::queue ${typ
0820: 65 7d 3a 3a 25 41 55 54 4f 25 20 5c 0a 09 09 20 e}::%AUTO% \...
0830: 20 20 20 20 2d 67 61 74 65 2d 63 6f 6d 6d 61 6e -gate-comman
0840: 64 20 5b 6d 79 74 79 70 65 6d 65 74 68 6f 64 20 d [mytypemethod
0850: 43 61 6e 52 75 6e 4e 65 78 74 20 24 69 6d 61 67 CanRunNext $imag
0860: 65 66 69 6c 65 5d 5d 0a 09 20 20 20 20 73 65 74 efile]].. set
0870: 20 6f 75 72 6c 6f 63 6b 65 64 28 24 69 6d 61 67 ourlocked($imag
0880: 65 66 69 6c 65 29 20 30 0a 09 7d 0a 0a 09 23 20 efile) 0..}...#
0890: 50 75 74 20 74 68 65 20 6f 70 65 72 61 74 69 6f Put the operatio
08a0: 6e 20 69 6e 74 6f 20 74 68 65 20 65 78 69 73 74 n into the exist
08b0: 69 6e 67 20 71 75 65 75 65 2c 20 74 68 65 6e 20 ing queue, then
08c0: 77 61 6b 65 20 69 74 20 75 70 2c 0a 09 23 20 69 wake it up,..# i
08d0: 66 66 20 6e 6f 20 6a 6f 62 20 69 73 20 63 75 72 ff no job is cur
08e0: 72 65 6e 74 6c 79 20 72 75 6e 6e 69 6e 67 2e 0a rently running..
08f0: 09 24 6f 75 72 70 65 6e 64 69 6e 67 28 24 69 6d .$ourpending($im
0900: 61 67 65 66 69 6c 65 29 20 70 75 74 20 5c 0a 09 agefile) put \..
0910: 20 20 20 20 5b 6d 79 74 79 70 65 6d 65 74 68 6f [mytypemetho
0920: 64 20 52 75 6e 20 24 6f 70 65 72 61 74 69 6f 6e d Run $operation
0930: 20 24 69 6d 61 67 65 66 69 6c 65 20 24 66 75 74 $imagefile $fut
0940: 75 72 65 5d 0a 0a 09 69 66 20 7b 24 6f 75 72 6c ure]...if {$ourl
0950: 6f 63 6b 65 64 28 24 69 6d 61 67 65 66 69 6c 65 ocked($imagefile
0960: 29 7d 20 72 65 74 75 72 6e 0a 09 24 6f 75 72 70 )} return..$ourp
0970: 65 6e 64 69 6e 67 28 24 69 6d 61 67 65 66 69 6c ending($imagefil
0980: 65 29 20 77 61 6b 65 0a 09 72 65 74 75 72 6e 0a e) wake..return.
0990: 20 20 20 20 7d 0a 0a 20 20 20 20 74 79 70 65 6d }.. typem
09a0: 65 74 68 6f 64 20 52 75 6e 20 7b 6f 70 65 72 61 ethod Run {opera
09b0: 74 69 6f 6e 20 69 6d 61 67 65 66 69 6c 65 20 66 tion imagefile f
09c0: 75 74 75 72 65 7d 20 7b 0a 09 44 65 62 75 67 2e uture} {..Debug.
09d0: 69 6d 67 2f 6f 72 69 65 6e 74 61 74 69 6f 6e 20 img/orientation
09e0: 7b 5b 6c 69 73 74 20 24 74 79 70 65 20 52 75 6e {[list $type Run
09f0: 20 24 6f 70 65 72 61 74 69 6f 6e 20 24 69 6d 61 $operation $ima
0a00: 67 65 66 69 6c 65 20 24 66 75 74 75 72 65 5d 7d gefile $future]}
0a10: 0a 0a 09 24 6f 75 72 70 6f 6f 6c 20 70 75 74 20 ...$ourpool put
0a20: 5c 0a 09 20 20 20 20 5b 6c 69 73 74 20 24 6f 70 \.. [list $op
0a30: 65 72 61 74 69 6f 6e 20 24 69 6d 61 67 65 66 69 eration $imagefi
0a40: 6c 65 5d 20 5c 0a 09 20 20 20 20 5b 6d 79 74 79 le] \.. [myty
0a50: 70 65 6d 65 74 68 6f 64 20 44 6f 6e 65 20 24 6f pemethod Done $o
0a60: 70 65 72 61 74 69 6f 6e 20 24 69 6d 61 67 65 66 peration $imagef
0a70: 69 6c 65 20 24 66 75 74 75 72 65 5d 0a 09 73 65 ile $future]..se
0a80: 74 20 6f 75 72 6c 6f 63 6b 65 64 28 24 69 6d 61 t ourlocked($ima
0a90: 67 65 66 69 6c 65 29 20 31 0a 09 72 65 74 75 72 gefile) 1..retur
0aa0: 6e 0a 20 20 20 20 7d 0a 0a 20 20 20 20 74 79 70 n. }.. typ
0ab0: 65 6d 65 74 68 6f 64 20 43 61 6e 52 75 6e 4e 65 emethod CanRunNe
0ac0: 78 74 20 7b 69 6d 61 67 65 66 69 6c 65 7d 20 7b xt {imagefile} {
0ad0: 0a 09 72 65 74 75 72 6e 20 5b 65 78 70 72 20 7b ..return [expr {
0ae0: 21 24 6f 75 72 6c 6f 63 6b 65 64 28 24 69 6d 61 !$ourlocked($ima
0af0: 67 65 66 69 6c 65 29 7d 5d 0a 20 20 20 20 7d 0a gefile)}]. }.
0b00: 0a 20 20 20 20 74 79 70 65 6d 65 74 68 6f 64 20 . typemethod
0b10: 44 6f 6e 65 20 7b 6f 70 65 72 61 74 69 6f 6e 20 Done {operation
0b20: 69 6d 61 67 65 66 69 6c 65 20 66 75 74 75 72 65 imagefile future
0b30: 20 77 68 61 74 20 64 65 74 61 69 6c 7d 20 7b 0a what detail} {.
0b40: 09 44 65 62 75 67 2e 69 6d 67 2f 6f 72 69 65 6e .Debug.img/orien
0b50: 74 61 74 69 6f 6e 20 7b 5b 6c 69 73 74 20 24 74 tation {[list $t
0b60: 79 70 65 20 44 6f 6e 65 20 24 6f 70 65 72 61 74 ype Done $operat
0b70: 69 6f 6e 20 24 69 6d 61 67 65 66 69 6c 65 20 24 ion $imagefile $
0b80: 66 75 74 75 72 65 20 24 77 68 61 74 20 24 64 65 future $what $de
0b90: 74 61 69 6c 5d 7d 0a 0a 09 73 65 74 20 6f 75 72 tail]}...set our
0ba0: 6c 6f 63 6b 65 64 28 24 69 6d 61 67 65 66 69 6c locked($imagefil
0bb0: 65 29 20 30 0a 0a 09 23 20 52 75 6e 20 63 6f 6d e) 0...# Run com
0bc0: 70 6c 65 74 69 6f 6e 20 63 61 6c 6c 62 61 63 6b pletion callback
0bd0: 2e 0a 09 69 66 20 7b 24 77 68 61 74 20 65 71 20 ...if {$what eq
0be0: 22 6f 6b 22 7d 20 7b 0a 09 20 20 20 20 24 66 75 "ok"} {.. $fu
0bf0: 74 75 72 65 20 72 65 74 75 72 6e 20 24 64 65 74 ture return $det
0c00: 61 69 6c 0a 09 7d 20 65 6c 73 65 20 7b 0a 09 20 ail..} else {..
0c10: 20 20 20 24 66 75 74 75 72 65 20 65 72 72 6f 72 $future error
0c20: 20 24 64 65 74 61 69 6c 0a 09 7d 0a 0a 09 23 20 $detail..}...#
0c30: 57 61 6b 65 20 74 68 65 20 6e 65 78 74 20 6a 6f Wake the next jo
0c40: 62 2c 20 69 66 66 20 74 68 65 72 65 20 69 73 20 b, iff there is
0c50: 61 6e 79 2c 20 61 6e 64 20 69 66 20 6e 6f 20 65 any, and if no e
0c60: 72 72 6f 72 20 6f 63 63 75 72 65 64 2e 0a 09 69 rror occured...i
0c70: 66 20 7b 24 77 68 61 74 20 65 71 20 22 65 72 72 f {$what eq "err
0c80: 6f 72 22 7d 20 7b 0a 09 20 20 20 20 24 6f 75 72 or"} {.. $our
0c90: 70 65 6e 64 69 6e 67 28 24 69 6d 61 67 65 66 69 pending($imagefi
0ca0: 6c 65 29 20 64 72 61 69 6e 0a 09 7d 20 65 6c 73 le) drain..} els
0cb0: 65 69 66 20 7b 5b 24 6f 75 72 70 65 6e 64 69 6e eif {[$ourpendin
0cc0: 67 28 24 69 6d 61 67 65 66 69 6c 65 29 20 73 69 g($imagefile) si
0cd0: 7a 65 5d 7d 20 7b 0a 09 20 20 20 20 24 6f 75 72 ze]} {.. $our
0ce0: 70 65 6e 64 69 6e 67 28 24 69 6d 61 67 65 66 69 pending($imagefi
0cf0: 6c 65 29 20 77 61 6b 65 0a 09 7d 0a 09 72 65 74 le) wake..}..ret
0d00: 75 72 6e 0a 20 20 20 20 7d 0a 0a 20 20 20 20 23 urn. }.. #
0d10: 20 23 23 23 20 23 23 23 20 23 23 23 20 23 23 23 ### ### ### ###
0d20: 23 23 23 23 23 23 20 23 23 23 23 23 23 23 23 23 ###### #########
0d30: 20 23 23 23 23 23 23 23 23 23 0a 20 20 20 20 23 #########. #
0d40: 23 20 49 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e # Initialization
0d50: 2f 53 65 74 75 70 0a 0a 20 20 20 20 74 79 70 65 /Setup.. type
0d60: 63 6f 6e 73 74 72 75 63 74 6f 72 20 7b 0a 09 44 constructor {..D
0d70: 65 62 75 67 2e 69 6d 67 2f 6f 72 69 65 6e 74 61 ebug.img/orienta
0d80: 74 69 6f 6e 20 7b 24 74 79 70 65 20 74 79 70 65 tion {$type type
0d90: 63 6f 6e 73 74 72 75 63 74 6f 72 20 2e 2e 2e 20 constructor ...
0da0: 49 6e 69 74 69 61 6c 69 7a 65 20 69 6e 74 65 72 Initialize inter
0db0: 6e 61 6c 20 74 68 72 65 61 64 70 6f 6f 6c 7d 0a nal threadpool}.
0dc0: 0a 09 73 65 74 20 6f 75 72 73 65 6c 76 65 73 20 ..set ourselves
0dd0: 24 74 79 70 65 0a 0a 09 73 65 74 20 6f 75 72 70 $type...set ourp
0de0: 6f 6f 6c 20 5b 74 68 72 65 61 64 70 6f 6f 6c 20 ool [threadpool
0df0: 24 7b 74 79 70 65 7d 3a 3a 50 4f 4f 4c 20 5c 0a ${type}::POOL \.
0e00: 09 20 20 20 20 2d 70 6f 6c 69 63 79 20 71 75 65 . -policy que
0e10: 75 65 20 5c 0a 09 20 20 20 20 2d 73 65 74 75 70 ue \.. -setup
0e20: 20 7b 0a 09 09 70 61 63 6b 61 67 65 20 72 65 71 {...package req
0e30: 75 69 72 65 20 66 69 6c 65 75 74 69 6c 0a 0a 09 uire fileutil...
0e40: 09 70 72 6f 63 20 75 70 20 7b 69 6e 70 75 74 7d .proc up {input}
0e50: 20 7b 0a 09 09 20 20 20 20 23 20 55 73 65 20 6a {... # Use j
0e60: 68 65 61 64 27 73 20 66 61 63 69 6c 69 74 69 65 head's facilitie
0e70: 73 20 66 6f 72 20 61 75 74 6f 2d 72 6f 74 61 74 s for auto-rotat
0e80: 69 6f 6e 20 74 6f 20 73 65 74 0a 09 09 20 20 20 ion to set...
0e90: 20 23 20 74 68 65 20 69 6d 61 67 65 20 75 70 72 # the image upr
0ea0: 69 67 68 74 2e 20 54 68 69 73 20 68 61 6e 64 6c ight. This handl
0eb0: 65 73 20 61 6e 79 20 73 74 6f 72 65 64 0a 09 09 es any stored...
0ec0: 20 20 20 20 23 20 74 68 75 6d 62 6e 61 69 6c 20 # thumbnail
0ed0: 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 2e 0a 09 automatically...
0ee0: 09 20 20 20 20 65 78 65 63 20 6a 68 65 61 64 20 . exec jhead
0ef0: 2d 61 75 74 6f 72 6f 74 20 24 69 6e 70 75 74 0a -autorot $input.
0f00: 09 09 20 20 20 20 72 65 74 75 72 6e 0a 09 09 7d .. return...}
0f10: 0a 0a 09 09 70 72 6f 63 20 6c 65 66 74 20 20 20 ....proc left
0f20: 20 20 20 20 7b 69 6e 70 75 74 7d 20 7b 20 44 4f {input} { DO
0f30: 20 24 69 6e 70 75 74 20 2d 72 6f 74 61 74 65 20 $input -rotate
0f40: 32 37 30 20 7d 0a 09 09 70 72 6f 63 20 72 69 67 270 }...proc rig
0f50: 68 74 20 20 20 20 20 20 7b 69 6e 70 75 74 7d 20 ht {input}
0f60: 7b 20 44 4f 20 24 69 6e 70 75 74 20 2d 72 6f 74 { DO $input -rot
0f70: 61 74 65 20 39 30 20 20 7d 0a 09 09 70 72 6f 63 ate 90 }...proc
0f80: 20 66 6c 69 70 68 20 20 20 20 20 20 7b 69 6e 70 fliph {inp
0f90: 75 74 7d 20 7b 20 44 4f 20 24 69 6e 70 75 74 20 ut} { DO $input
0fa0: 2d 66 6c 69 70 20 68 6f 72 69 7a 6f 6e 74 61 6c -flip horizontal
0fb0: 20 7d 0a 09 09 70 72 6f 63 20 66 6c 69 70 76 20 }...proc flipv
0fc0: 20 20 20 20 20 7b 69 6e 70 75 74 7d 20 7b 20 44 {input} { D
0fd0: 4f 20 24 69 6e 70 75 74 20 2d 66 6c 69 70 20 76 O $input -flip v
0fe0: 65 72 74 69 63 61 6c 20 20 20 7d 0a 09 09 70 72 ertical }...pr
0ff0: 6f 63 20 74 72 61 6e 73 70 6f 73 65 20 20 7b 69 oc transpose {i
1000: 6e 70 75 74 7d 20 7b 20 44 4f 20 24 69 6e 70 75 nput} { DO $inpu
1010: 74 20 2d 74 72 61 6e 73 70 6f 73 65 20 20 7d 0a t -transpose }.
1020: 09 09 70 72 6f 63 20 74 72 61 6e 73 76 65 72 73 ..proc transvers
1030: 65 20 7b 69 6e 70 75 74 7d 20 7b 20 44 4f 20 24 e {input} { DO $
1040: 69 6e 70 75 74 20 2d 74 72 61 6e 73 76 65 72 73 input -transvers
1050: 65 20 7d 0a 0a 09 09 70 72 6f 63 20 44 4f 20 7b e }....proc DO {
1060: 69 6e 70 75 74 20 61 72 67 73 7d 20 7b 0a 09 09 input args} {...
1070: 20 20 20 20 23 20 55 73 69 6e 67 20 6a 68 65 61 # Using jhea
1080: 64 20 61 6e 64 20 6a 70 65 67 74 72 61 6e 20 74 d and jpegtran t
1090: 6f 20 74 72 61 6e 73 66 6f 72 6d 20 6c 6f 73 73 o transform loss
10a0: 2d 6c 65 73 73 6c 79 2c 0a 09 09 20 20 20 20 23 -lessly,... #
10b0: 20 61 6e 64 20 6b 65 65 70 69 6e 67 20 74 68 65 and keeping the
10c0: 20 74 68 75 6d 62 6e 61 69 6c 20 69 6e 20 73 79 thumbnail in sy
10d0: 6e 63 0a 0a 09 09 20 20 20 20 23 20 4e 4f 54 45 nc.... # NOTE
10e0: 3a 20 54 68 69 73 20 6b 69 6c 6c 73 20 61 20 73 : This kills a s
10f0: 74 6f 72 65 64 20 74 68 75 6d 62 6e 61 69 6c 2e tored thumbnail.
1100: 0a 09 09 20 20 20 20 69 66 20 7b 5b 63 61 74 63 ... if {[catc
1110: 68 20 7b 0a 09 09 09 73 65 74 20 72 20 20 5b 66 h {....set r [f
1120: 69 6c 65 75 74 69 6c 3a 3a 74 65 6d 70 66 69 6c ileutil::tempfil
1130: 65 20 69 6d 67 6f 72 69 65 6e 74 5f 72 65 73 5f e imgorient_res_
1140: 5d 0a 09 09 09 73 65 74 20 73 20 20 5b 66 69 6c ]....set s [fil
1150: 65 75 74 69 6c 3a 3a 74 65 6d 70 66 69 6c 65 20 eutil::tempfile
1160: 69 6d 67 6f 72 69 65 6e 74 5f 74 68 75 6d 62 72 imgorient_thumbr
1170: 65 73 5f 5d 0a 09 09 09 73 65 74 20 74 20 20 5b es_]....set t [
1180: 66 69 6c 65 75 74 69 6c 3a 3a 74 65 6d 70 66 69 fileutil::tempfi
1190: 6c 65 20 69 6d 67 6f 72 69 65 6e 74 5f 74 68 75 le imgorient_thu
11a0: 6d 62 5f 5d 0a 09 09 09 65 78 65 63 20 6a 70 65 mb_]....exec jpe
11b0: 67 74 72 61 6e 20 2d 63 6f 70 79 20 61 6c 6c 20 gtran -copy all
11c0: 7b 2a 7d 24 61 72 67 73 20 24 69 6e 70 75 74 20 {*}$args $input
11d0: 3e 20 24 72 0a 09 09 09 65 78 65 63 20 6a 68 65 > $r....exec jhe
11e0: 61 64 20 2d 73 74 20 24 74 20 24 72 0a 09 09 09 ad -st $t $r....
11f0: 65 78 65 63 20 6a 70 65 67 74 72 61 6e 20 2d 63 exec jpegtran -c
1200: 6f 70 79 20 61 6c 6c 20 7b 2a 7d 24 61 72 67 73 opy all {*}$args
1210: 20 24 74 20 3e 20 24 73 0a 09 09 09 65 78 65 63 $t > $s....exec
1220: 20 6a 68 65 61 64 20 2d 72 74 20 24 73 20 24 72 jhead -rt $s $r
1230: 0a 09 09 09 66 69 6c 65 20 72 65 6e 61 6d 65 20 ....file rename
1240: 2d 66 6f 72 63 65 20 24 72 20 24 69 6e 70 75 74 -force $r $input
1250: 0a 09 09 09 66 69 6c 65 20 64 65 6c 65 74 65 20 ....file delete
1260: 24 73 20 24 74 0a 09 09 20 20 20 20 7d 5d 7d 20 $s $t... }]}
1270: 7b 0a 09 09 09 72 65 74 75 72 6e 20 2d 63 6f 64 {....return -cod
1280: 65 20 65 72 72 6f 72 20 24 3a 3a 65 72 72 6f 72 e error $::error
1290: 49 6e 66 6f 0a 09 09 20 20 20 20 7d 0a 09 09 20 Info... }...
12a0: 20 20 20 72 65 74 75 72 6e 0a 09 09 7d 0a 09 20 return...}..
12b0: 20 20 20 7d 5d 0a 09 72 65 74 75 72 6e 0a 20 20 }]..return.
12c0: 20 20 7d 0a 0a 20 20 20 20 23 20 23 23 23 20 23 }.. # ### #
12d0: 23 23 20 23 23 23 20 23 23 23 23 23 23 23 23 23 ## ### #########
12e0: 20 23 23 23 23 23 23 23 23 23 20 23 23 23 23 23 ######### #####
12f0: 23 23 23 23 0a 20 20 20 20 23 23 20 53 74 61 74 ####. ## Stat
1300: 65 0a 0a 20 20 20 20 74 79 70 65 63 6f 6d 70 6f e.. typecompo
1310: 6e 65 6e 74 20 6f 75 72 70 6f 6f 6c 20 20 20 20 nent ourpool
1320: 20 20 20 20 20 20 20 20 20 20 3b 20 23 20 54 68 ; # Th
1330: 72 65 61 64 20 70 6f 6f 6c 20 68 61 6e 64 6c 69 read pool handli
1340: 6e 67 20 74 68 65 20 6f 72 69 65 6e 74 61 74 69 ng the orientati
1350: 6f 6e 20 6a 6f 62 73 2e 0a 20 20 20 20 74 79 70 on jobs.. typ
1360: 65 76 61 72 69 61 62 6c 65 20 20 6f 75 72 70 65 evariable ourpe
1370: 6e 64 69 6e 67 20 2d 61 72 72 61 79 20 7b 7d 20 nding -array {}
1380: 3b 20 23 20 4d 61 70 20 66 6f 72 20 74 68 65 20 ; # Map for the
1390: 70 65 72 2d 69 6d 61 67 65 20 71 75 65 75 65 73 per-image queues
13a0: 2e 0a 20 20 20 20 74 79 70 65 76 61 72 69 61 62 .. typevariab
13b0: 6c 65 20 20 6f 75 72 6c 6f 63 6b 65 64 20 20 2d le ourlocked -
13c0: 61 72 72 61 79 20 7b 7d 20 3b 20 23 20 4d 61 70 array {} ; # Map
13d0: 20 66 6f 72 20 74 68 65 20 70 65 72 2d 69 6d 61 for the per-ima
13e0: 67 65 20 71 75 65 75 65 20 6c 6f 63 6b 73 2e 0a ge queue locks..
13f0: 0a 20 20 20 20 23 20 23 23 23 20 23 23 23 20 23 . # ### ### #
1400: 23 23 20 23 23 23 23 23 23 23 23 23 20 23 23 23 ## ######### ###
1410: 23 23 23 23 23 23 20 23 23 23 23 23 23 23 23 23 ###### #########
1420: 0a 20 20 20 20 23 23 20 43 6f 6e 66 69 67 75 72 . ## Configur
1430: 61 74 69 6f 6e 0a 0a 20 20 20 20 70 72 61 67 6d ation.. pragm
1440: 61 20 2d 68 61 73 69 6e 73 74 61 6e 63 65 73 20 a -hasinstances
1450: 20 20 6e 6f 20 3b 20 23 20 73 69 6e 67 6c 65 74 no ; # singlet
1460: 6f 6e 0a 20 20 20 20 70 72 61 67 6d 61 20 2d 68 on. pragma -h
1470: 61 73 74 79 70 65 69 6e 66 6f 20 20 20 20 6e 6f astypeinfo no
1480: 0a 20 20 20 20 70 72 61 67 6d 61 20 2d 68 61 73 . pragma -has
1490: 74 79 70 65 64 65 73 74 72 6f 79 20 6e 6f 0a 20 typedestroy no.
14a0: 20 20 20 70 72 61 67 6d 61 20 2d 68 61 73 69 6e pragma -hasin
14b0: 66 6f 20 20 20 20 20 20 20 20 6e 6f 0a 20 20 20 fo no.
14c0: 20 70 72 61 67 6d 61 20 2d 73 69 6d 70 6c 65 64 pragma -simpled
14d0: 69 73 70 61 74 63 68 20 79 65 73 0a 0a 20 20 20 ispatch yes..
14e0: 20 23 23 0a 20 20 20 20 23 20 23 23 23 20 23 23 ##. # ### ##
14f0: 23 20 23 23 23 20 23 23 23 23 23 23 23 23 23 20 # ### #########
1500: 23 23 23 23 23 23 23 23 23 20 23 23 23 23 23 23 ######### ######
1510: 23 23 23 0a 7d 0a 0a 23 20 23 23 23 20 23 23 23 ###.}..# ### ###
1520: 20 23 23 23 20 23 23 23 23 23 23 23 23 23 20 23 ### ######### #
1530: 23 23 23 23 23 23 23 23 20 23 23 23 23 23 23 23 ######## #######
1540: 23 23 0a 23 23 20 52 65 61 64 79 0a 70 61 63 6b ##.## Ready.pack
1550: 61 67 65 20 70 72 6f 76 69 64 65 20 69 6d 67 3a age provide img:
1560: 3a 6f 72 69 65 6e 74 61 74 69 6f 6e 20 30 2e 31 :orientation 0.1
1570: 0a .