<commit_before>#lang racket/base

(require (for-syntax racket/base
                     racket/class racket/draw racket/math)
         racket/class racket/draw)

(provide compiled-bitmap compiled-bitmap-list)

(begin-for-syntax
  (define (save-png bm)
    (define p (open-output-bytes))
    (send bm save-file p 'png #:unscaled? #t)
    (define bs (get-output-bytes p))
    ;(printf "Wrote PNG: ~v bytes~n" (bytes-length bs))
    bs)
  
  (define (save-jpeg bm quality)
    (define s (send bm get-backing-scale))
    (define (scale v) (inexact->exact (ceiling (* s v))))
    (define w (scale (send bm get-width)))
    (define h (scale (send bm get-height)))
    (define bs (make-bytes (* 4 w h)))
    
    (send bm get-argb-pixels 0 0 w h bs #t #:unscaled? #t)
    (for ([i  (in-range 0 (* 4 w h) 4)])
      (define a (bytes-ref bs i))
      (bytes-set! bs i 255)
      (bytes-set! bs (+ i 1) a)
      (bytes-set! bs (+ i 2) a)
      (bytes-set! bs (+ i 3) a))
    
    (define alpha-bm (make-bitmap w h #f))
    (send alpha-bm set-argb-pixels 0 0 w h bs)
    (define alpha-p (open-output-bytes))
    (send alpha-bm save-file alpha-p 'jpeg quality)
    
    (send bm get-argb-pixels 0 0 w h bs #f)
    (define rgb-bm (make-bitmap w h #f))
    (send rgb-bm set-argb-pixels 0 0 w h bs #f)
    (define rgb-p (open-output-bytes))
    (send rgb-bm save-file rgb-p 'jpeg quality)
    <commit_msg>Fix `compiled-bitmap` and `compiled-bitmap-list` on 2x bitmaps, lossy compression<commit_after>#lang racket/base

(require (for-syntax racket/base
                     racket/class racket/draw racket/math)
         racket/class racket/draw)

(provide compiled-bitmap compiled-bitmap-list)

(begin-for-syntax
  (define (save-png bm)
    (define p (open-output-bytes))
    (send bm save-file p 'png #:unscaled? #t)
    (define bs (get-output-bytes p))
    ;(printf "Wrote PNG: ~v bytes~n" (bytes-length bs))
    bs)
  
  (define (save-jpeg bm quality)
    (define s (send bm get-backing-scale))
    (define (scale v) (inexact->exact (ceiling (* s v))))
    (define w (scale (send bm get-width)))
    (define h (scale (send bm get-height)))
    (define bs (make-bytes (* 4 w h)))
    
    (send bm get-argb-pixels 0 0 w h bs #t #:unscaled? #t)
    (for ([i  (in-range 0 (* 4 w h) 4)])
      (define a (bytes-ref bs i))
      (bytes-set! bs i 255)
      (bytes-set! bs (+ i 1) a)
      (bytes-set! bs (+ i 2) a)
      (bytes-set! bs (+ i 3) a))
    
    (define alpha-bm (make-bitmap w h #f))
    (send alpha-bm set-argb-pixels 0 0 w h bs)
    (define alpha-p (open-output-bytes))
    (send alpha-bm save-file alpha-p 'jpeg quality)
    
    (send bm get-argb-pixels 0 0 w h bs #f #:unscaled? #t)
    (define rgb-bm (make-bitmap w h #f))
    (send rgb-bm set-argb-pixels 0 0 w h bs #f)
    (define rgb-p (open-output-bytes))
    (send rgb-bm save-file rgb-p 'jpeg quality)
    
{'fraction_non_alphanumeric': 0.1522878099895215, 'fraction_numerical': 0.012923506811037374, 'mean_word_length': 2.425837320574163, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7584182', 'n_tokens_mistral': 1186, 'n_tokens_neox': 1090, 'n_words': 354}