|
1 | 1 | #lang racket/base
|
2 | 2 | (require racket/class
|
| 3 | + racket/match |
3 | 4 | racket/file file/convertible
|
4 | 5 | "snip-flags.rkt"
|
5 | 6 | "load-one.rkt"
|
6 | 7 | "style.rkt"
|
7 | 8 | "private.rkt"
|
8 | 9 | racket/draw/private/syntax
|
| 10 | + (only-in racket/draw/private/bitmap specialize-unknown-kind) |
9 | 11 | racket/draw)
|
10 | 12 |
|
11 | 13 | (provide snip%
|
|
831 | 833 | (send f get dx)
|
832 | 834 | (send f get dy)
|
833 | 835 | (send f get relative))
|
834 |
| - |
835 |
| - (let-values ([(loadfile |
836 |
| - type |
837 |
| - inlined? |
838 |
| - backing-scale) |
839 |
| - (if (and (equal? filename #"") |
840 |
| - can-inline? |
841 |
| - (positive? type)) |
842 |
| - ;; read inlined image |
843 |
| - (let-boxes ([len 0]) |
844 |
| - (send f get-fixed len) |
845 |
| - (if (and (len . > . 0) |
846 |
| - (send f ok?)) |
847 |
| - (let-values ([(in out) (make-pipe)] |
848 |
| - [(backing-scale) |
849 |
| - (if (= type 4) |
850 |
| - (send f get-inexact) |
851 |
| - 1.0)]) |
852 |
| - (for ([i (in-range len)]) |
853 |
| - (display (send f get-unterminated-bytes) out)) |
854 |
| - (close-output-port out) |
855 |
| - (values in |
856 |
| - 'unknown/alpha |
857 |
| - #t |
858 |
| - backing-scale)) |
859 |
| - (values filename |
860 |
| - (int->img-type type) |
861 |
| - #f |
862 |
| - 1.0))) |
863 |
| - (values filename |
864 |
| - (int->img-type type) |
865 |
| - #f |
866 |
| - 1.0))]) |
| 836 | + (define-values (loadfile |
| 837 | + kind |
| 838 | + inlined? |
| 839 | + backing-scale) |
| 840 | + (cond |
| 841 | + [(and (equal? filename #"") |
| 842 | + can-inline? |
| 843 | + (positive? type)) |
| 844 | + ;; read inlined image |
| 845 | + (define len (send f get-fixed-exact)) |
| 846 | + (cond |
| 847 | + [(and (len . > . 0) |
| 848 | + (send f ok?)) |
| 849 | + (let-values ([(in out) (make-pipe)] |
| 850 | + [(backing-scale) |
| 851 | + (if (= type 4) |
| 852 | + (send f get-inexact) |
| 853 | + 1.0)]) |
| 854 | + (for ([i (in-range len)]) |
| 855 | + (display (send f get-unterminated-bytes) out)) |
| 856 | + (close-output-port out) |
| 857 | + (values in |
| 858 | + 'unknown/alpha |
| 859 | + #t |
| 860 | + backing-scale))] |
| 861 | + [else |
| 862 | + (values filename |
| 863 | + (int->img-type type) |
| 864 | + #f |
| 865 | + 1.0)])] |
| 866 | + [else |
| 867 | + (values filename |
| 868 | + (int->img-type type) |
| 869 | + #f |
| 870 | + 1.0)])) |
867 | 871 | ;; the call to create an image-snip% object
|
868 | 872 | ;; here should match the way that super-make-object
|
869 | 873 | ;; is called in wxme/image.rkt
|
|
873 | 877 | (if (bytes? loadfile)
|
874 | 878 | (bytes->path loadfile)
|
875 | 879 | loadfile))
|
876 |
| - type |
| 880 | + kind |
877 | 881 | (positive? relative)
|
878 | 882 | inlined?
|
879 | 883 | backing-scale)])
|
880 | 884 | (send snip resize w h)
|
881 | 885 | (send snip set-offset dx dy)
|
882 | 886 |
|
883 |
| - snip))))))) |
| 887 | + snip)))))) |
884 | 888 |
|
885 | 889 | ;; ------------------------------------------------------------
|
886 | 890 |
|
|
932 | 936 | (define viewdx 0.0)
|
933 | 937 | (define viewdy 0.0)
|
934 | 938 | (define contents-changed? #f)
|
| 939 | + (define bytes-from-png-fallback #f) |
935 | 940 |
|
936 | 941 | (super-new)
|
937 | 942 |
|
|
1049 | 1054 | ;; inline the image
|
1050 | 1055 | (let ([lenpos (send f tell)])
|
1051 | 1056 | (send f put-fixed 0)
|
1052 |
| - |
1053 | 1057 | (when (eq? write-mode 'scaled-pm)
|
1054 | 1058 | (send f put (send bm get-backing-scale)))
|
1055 |
| - |
1056 |
| - (let ([num-lines |
1057 |
| - (let-values ([(in out) (make-pipe)]) |
1058 |
| - (send bm save-file out 'png #:unscaled? #t) |
1059 |
| - (close-output-port out) |
1060 |
| - (let loop ([numlines 0]) |
1061 |
| - (let ([s (read-bytes IMG-MOVE-BUF-SIZE in)]) |
1062 |
| - (if (eof-object? s) |
1063 |
| - numlines |
1064 |
| - (begin |
1065 |
| - (send f put-unterminated s) |
1066 |
| - (loop (add1 numlines)))))))]) |
1067 |
| - |
1068 |
| - (let ([end (send f tell)]) |
1069 |
| - (send f jump-to lenpos) |
1070 |
| - (send f put-fixed num-lines) |
1071 |
| - (send f jump-to end))))))) |
| 1059 | + (define data-from-file (send bm get-data-from-file)) |
| 1060 | + (define (png-bytes-fallback) |
| 1061 | + (unless bytes-from-png-fallback |
| 1062 | + (define bp (open-output-bytes)) |
| 1063 | + (send bm save-file bp 'png #:unscaled? #t) |
| 1064 | + (close-output-port bp) |
| 1065 | + (set! bytes-from-png-fallback (get-output-bytes bp))) |
| 1066 | + (open-input-bytes bytes-from-png-fallback)) |
| 1067 | + (define in |
| 1068 | + (match data-from-file |
| 1069 | + [(vector kind bg-color bytes) |
| 1070 | + (cond |
| 1071 | + [(saved-data-compatible-with-loading-code? kind bg-color bytes) |
| 1072 | + (open-input-bytes bytes)] |
| 1073 | + [else (png-bytes-fallback)])] |
| 1074 | + [#f |
| 1075 | + (png-bytes-fallback)])) |
| 1076 | + (define num-lines |
| 1077 | + (let loop ([numlines 0]) |
| 1078 | + (let ([s (read-bytes IMG-MOVE-BUF-SIZE in)]) |
| 1079 | + (if (eof-object? s) |
| 1080 | + numlines |
| 1081 | + (begin |
| 1082 | + (send f put-unterminated s) |
| 1083 | + (loop (add1 numlines))))))) |
| 1084 | + (define end (send f tell)) |
| 1085 | + (send f jump-to lenpos) |
| 1086 | + (send f put-fixed num-lines) |
| 1087 | + (send f jump-to end))))) |
| 1088 | + |
| 1089 | + (define/private (saved-data-compatible-with-loading-code? kind bg-color bytes) |
| 1090 | + (and (not bg-color) |
| 1091 | + (case kind |
| 1092 | + [(unknown/apha) #t] |
| 1093 | + [(unknown/mask unknown) |
| 1094 | + (equal? (specialize-unknown-kind (open-input-bytes bytes) kind) |
| 1095 | + (specialize-unknown-kind (open-input-bytes bytes) 'unknown/alpha))] |
| 1096 | + [else |
| 1097 | + (equal? kind (specialize-unknown-kind (open-input-bytes bytes) 'unknown/alpha))]))) |
1072 | 1098 |
|
1073 | 1099 | (def/public (load-file [(make-or-false (make-alts path-string? input-port?)) [name #f]]
|
1074 | 1100 | [image-type? [kind 'unknown]]
|
|
1120 | 1146 | (let ([nbm (if s-admin
|
1121 | 1147 | (send s-admin call-with-busy-cursor
|
1122 | 1148 | (lambda ()
|
1123 |
| - (make-object bitmap% fullpath kind))) |
1124 |
| - (make-object bitmap% fullpath kind #f #f backing-scale))]) |
| 1149 | + (make-object bitmap% fullpath kind #f #f 1.0 #t))) |
| 1150 | + (make-object bitmap% fullpath kind #f #f backing-scale #t))]) |
1125 | 1151 | (when (send nbm ok?)
|
1126 | 1152 | (do-set-bitmap nbm #f #f))))))
|
1127 | 1153 | ;; for refresh:
|
|
0 commit comments