home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2011 June / maximum-cd-2011-06.iso / DiscContents / gimp-2.6.11-i686-setup-1.exe / {app} / share / gimp / 2.0 / scripts / script-fu-compat.init < prev    next >
Encoding:
Text File  |  2010-10-04  |  7.1 KB  |  365 lines

  1. ;The Scheme code in this file provides some compatibility with scripts that
  2. ;were originally written for use with the older SIOD based Script-Fu plug-in
  3. ;of GIMP.
  4. ;
  5. ;All items defined in this file except for the random number routines are
  6. ;deprecated. Existing scripts should be updated to avoid the use of the
  7. ;compability functions and define statements which follow the random number 
  8. ;generator routines.
  9. ;
  10. ;The items marked as deprecated at the end of this file may be removed
  11. ;at some later date.
  12.  
  13.  
  14. ;The random number generator routines below have been slightly reformatted.
  15. ;A couple of define blocks which are not needed have been commented out.
  16. ;The original file was called rand2.scm and can be found in:
  17. ;http://www-2.cs.cmu.edu/afs/cs/project/ai-repository/ai/lang/scheme/code/math/random/
  18.  
  19. ; Minimal Standard Random Number Generator
  20. ; Park & Miller, CACM 31(10), Oct 1988, 32 bit integer version.
  21. ; better constants, as proposed by Park.
  22. ; By Ozan Yigit
  23.  
  24. ;(define *seed* 1)
  25.  
  26. (define (srand seed)
  27.   (set! *seed* seed)
  28.   *seed*
  29. )
  30.  
  31. (define (msrg-rand)
  32.   (let (
  33.        (A 48271)
  34.        (M 2147483647)
  35.        (Q 44488)
  36.        (R 3399)
  37.        )
  38.     (let* (
  39.           (hi (quotient *seed* Q))
  40.           (lo (modulo *seed* Q))
  41.           (test (- (* A lo) (* R hi)))
  42.           )
  43.       (if (> test 0)
  44.         (set! *seed* test)
  45.         (set! *seed* (+ test M))
  46.       )
  47.     )
  48.   )
  49.   *seed*
  50. )
  51.  
  52. ; poker test
  53. ; seed 1
  54. ; cards 0-9 inclusive (random 10)
  55. ; five cards per hand
  56. ; 10000 hands
  57. ;
  58. ; Poker Hand     Example    Probability  Calculated
  59. ; 5 of a kind    (aaaaa)      0.0001      0
  60. ; 4 of a kind    (aaaab)      0.0045      0.0053
  61. ; Full house     (aaabb)      0.009       0.0093
  62. ; 3 of a kind    (aaabc)      0.072       0.0682
  63. ; two pairs      (aabbc)      0.108       0.1104
  64. ; Pair           (aabcd)      0.504       0.501
  65. ; Bust           (abcde)      0.3024      0.3058
  66.  
  67. (define (random n)
  68.   (let* (
  69.         (n (inexact->exact (truncate n)))
  70.         (M 2147483647)
  71.         (slop (modulo M n))
  72.         )
  73.     (let loop ((r (msrg-rand)))
  74.       (if (> r slop)
  75.         (modulo r n)
  76.         (loop (msrg-rand))
  77.       )
  78.     )
  79.   )
  80. )
  81.  
  82. ;(define (rngtest)
  83. ;  (display "implementation ")
  84. ;  (srand 1)
  85. ;  (do
  86. ;    ( (n 0 (+ n 1)) )
  87. ;    ( (>= n 10000) )
  88. ;    (msrg-rand)
  89. ;  )
  90. ;  (if (= *seed* 399268537)
  91. ;      (display "looks correct.")
  92. ;      (begin
  93. ;        (display "failed.")
  94. ;        (newline)
  95. ;        (display "   current seed ") (display *seed*)
  96. ;        (newline)
  97. ;        (display "   correct seed 399268537")
  98. ;      )
  99. ;  )
  100. ;  (newline)
  101. ;)
  102.  
  103.  
  104. ;This macro defines a while loop which is needed by some older scripts.
  105. ;This is here since it is not defined in R5RS and could be handy to have.
  106.  
  107. ;This while macro was found at:
  108. ;http://www.aracnet.com/~briand/scheme_eval.html
  109. (define-macro (while test . body)
  110.   `(let loop ()
  111.      (cond
  112.        (,test
  113.          ,@body
  114.          (loop)
  115.        )
  116.      )
  117.    )
  118. )
  119.  
  120.  
  121. ;The following define block(s) require the tsx extension to be loaded
  122.  
  123. (define (realtime)
  124.   (car (gettimeofday))
  125. )
  126.  
  127.  
  128. ;Items below this line are for compatability with Script-Fu but
  129. ;may be useful enough to keep around
  130.  
  131. (define (delq item lis)
  132.   (let ((l '()))
  133.     (unless (null? lis)
  134.       (while (pair? lis)
  135.         (if (<> item (car lis))
  136.           (set! l (append l (list (car lis))))
  137.         )
  138.         (set! lis (cdr lis))
  139.       )
  140.     )
  141.  
  142.     l
  143.   )
  144. )
  145.  
  146. (define (make-list count fill)
  147.   (vector->list (make-vector count fill))
  148. )
  149.  
  150. (define (strbreakup str sep)
  151.   (let* (
  152.         (seplen (string-length sep))
  153.         (start 0)
  154.         (end (string-length str))
  155.         (i start)
  156.         (l '())
  157.         )
  158.  
  159.     (if (= seplen 0)
  160.       (set! l (list str))
  161.       (begin
  162.         (while (<= i (- end seplen))
  163.           (if (substring-equal? sep str i (+ i seplen))
  164.             (begin
  165.                (if (= start 0)
  166.                  (set! l (list (substring str start i)))
  167.                  (set! l (append l (list (substring str start i))))
  168.                )
  169.                (set! start (+ i seplen))
  170.                (set! i (+ i seplen -1))
  171.             )
  172.           )
  173.  
  174.           (set! i (+ i 1))
  175.         )
  176.  
  177.         (set! l (append l (list (substring str start end))))
  178.       )
  179.     )
  180.  
  181.     l
  182.   )
  183. )
  184.  
  185. (define (substring-equal? str str2 start end)
  186.   (string=? str (substring str2 start end))
  187. )
  188.  
  189. (define (string-trim str)
  190.   (string-trim-right (string-trim-left str))
  191. )
  192.  
  193. (define (string-trim-left str)
  194.   (let (
  195.        (strlen (string-length str))
  196.        (i 0)
  197.        )
  198.  
  199.     (while (and (< i strlen)
  200.                 (char-whitespace? (string-ref str i))
  201.            )
  202.       (set! i (+ i 1))
  203.     )
  204.  
  205.     (substring str i (string-length str))
  206.   )
  207. )
  208.  
  209. (define (string-trim-right str)
  210.   (let ((i (- (string-length str) 1)))
  211.  
  212.     (while (and (>= i 0)
  213.                 (char-whitespace? (string-ref str i))
  214.            )
  215.       (set! i (- i 1))
  216.     )
  217.  
  218.     (substring str 0 (+ i 1))
  219.   )
  220. )
  221.  
  222. (define (unbreakupstr stringlist sep)
  223.   (let ((str (car stringlist)))
  224.  
  225.     (set! stringlist (cdr stringlist))
  226.     (while (not (null? stringlist))
  227.       (set! str (string-append str sep (car stringlist)))
  228.       (set! stringlist (cdr stringlist))
  229.     )
  230.  
  231.     str
  232.   )
  233. )
  234.  
  235.  
  236. ;Items below this line are deprecated and should not be used in new scripts.
  237.  
  238. (define aset vector-set!)
  239. (define aref vector-ref)
  240. (define fopen open-input-file)
  241. (define mapcar map)
  242. (define nil '())
  243. (define nreverse reverse)
  244. (define pow expt)
  245. (define prin1 write)
  246.  
  247. (define (print obj . port)
  248.   (apply write obj port)
  249.   (newline)
  250. )
  251.  
  252. (define strcat string-append)
  253. (define string-lessp string<?)
  254. (define symbol-bound? defined?)
  255. (define the-environment current-environment)
  256.  
  257. (define *pi*
  258.   (* 4 (atan 1.0))
  259. )
  260.  
  261. (define (butlast x)
  262.   (if (= (length x) 1)
  263.     '()
  264.     (reverse (cdr (reverse x)))
  265.   )
  266. )
  267.  
  268. (define (cons-array count type)
  269.   (case type
  270.     ((long)   (make-vector count 0))
  271.     ((short)  (make-vector count 0))
  272.     ((byte)   (make-vector count 0))
  273.     ((double) (make-vector count 0.0))
  274.     ((string) (vector->list (make-vector count "")))
  275.     (else type)
  276.   )
  277. )
  278.  
  279. (define (fmod a b)
  280.   (- a (* (truncate (/ a b)) b))
  281. )
  282.  
  283. (define (fread arg1 file)
  284.  
  285.   (define (fread-get-chars count file)
  286.     (let (
  287.          (str "")
  288.          (c 0)
  289.          )
  290.  
  291.       (while (> count 0)
  292.         (set! count (- count 1))
  293.         (set! c (read-char file))
  294.         (if (eof-object? c)
  295.             (set! count 0)
  296.             (set! str (string-append str (make-string 1 c)))
  297.         )
  298.       )
  299.  
  300.       (if (eof-object? c)
  301.           ()
  302.           str
  303.       )
  304.     )
  305.   )
  306.  
  307.   (if (number? arg1)
  308.       (begin
  309.         (set! arg1 (inexact->exact (truncate arg1)))
  310.         (fread-get-chars arg1 file)
  311.       )
  312.       (begin
  313.         (set! arg1 (fread-get-chars (string-length arg1) file))
  314.         (string-length arg1)
  315.       )
  316.   )
  317. )
  318.  
  319. (define (last x)
  320.   (cons (car (reverse x)) '())
  321. )
  322.  
  323. (define (nth k list)
  324.   (list-ref list k)
  325. )
  326.  
  327. (define (prog1 form1 . form2)
  328.   (let ((a form1))
  329.     (if (not (null? form2))
  330.       form2
  331.     )
  332.     a
  333.   )
  334. )
  335.  
  336. (define (rand . modulus)
  337.   (if (null? modulus)
  338.     (msrg-rand)
  339.     (apply random modulus)
  340.   )
  341. )
  342.  
  343. (define (strcmp str1 str2)
  344.   (if (string<? str1 str2)
  345.       -1
  346.       (if (string>? str1 str2)
  347.           1
  348.           0
  349.       )
  350.   )
  351. )
  352.  
  353. (define (trunc n)
  354.   (inexact->exact (truncate n))
  355. )
  356.  
  357. (define verbose
  358.   (lambda n
  359.     (if (or (null? n) (not (number? (car n))))
  360.       0
  361.       (car n)
  362.     )
  363.   )
  364. )
  365.