summaryrefslogtreecommitdiff
path: root/chickadee/image/jpeg.scm
blob: 98a7189abfb6e0ddd344f8ec188413b8eb05db3e (plain)
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
;;; Chickadee Game Toolkit
;;; Copyright © 2021 David Thompson <dthompson2@worcester.edu>
;;;
;;; Licensed under the Apache License, Version 2.0 (the "License");
;;; you may not use this file except in compliance with the License.
;;; You may obtain a copy of the License at
;;;
;;;    http://www.apache.org/licenses/LICENSE-2.0
;;;
;;; Unless required by applicable law or agreed to in writing, software
;;; distributed under the License is distributed on an "AS IS" BASIS,
;;; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
;;; See the License for the specific language governing permissions and
;;; limitations under the License.

;;; Commentary:
;;
;; JPEG image loading.
;;
;;; Code:

(define-module (chickadee image jpeg)
  #:use-module (chickadee config)
  #:use-module (ice-9 binary-ports)
  #:use-module (ice-9 exceptions)
  #:use-module (ice-9 format)
  #:use-module (ice-9 match)
  #:use-module (rnrs bytevectors)
  #:use-module (srfi srfi-9)
  #:use-module (srfi srfi-11)
  #:use-module (system foreign)
  #:export (load-jpeg))


;;;
;;; Enums
;;;

(define TJERR_WARNING 0)
(define TJERR_FATAL 1)

(define TJPF_RGBA 7)

(define TJFLAG_BOTTOMUP 2)


;;;
;;; Low-level bindings
;;;

(define libturbojpeg-func
  (let ((lib (dynamic-link* %libturbojpeg)))
    (lambda (return-type function-name arg-types)
      (pointer->procedure return-type
                          (dynamic-func function-name lib)
                          arg-types))))

(define-syntax-rule (define-foreign name return-type func-name arg-types)
  (define name
    (libturbojpeg-func return-type func-name arg-types)))

(define-foreign tj-get-error-code
  int "tjGetErrorCode" '(*))

(define-foreign tj-get-error-str2
  '* "tjGetErrorStr2" '(*))

(define-foreign tj-init-decompress
  '* "tjInitDecompress" '())

(define-foreign tj-destroy
  int "tjDestroy" '(*))

(define-foreign tj-decompress-header3
  int "tjDecompressHeader3" (list '* '* unsigned-long '* '* '* '*))

(define-foreign tj-decompress2
  int "tjDecompress2" (list '* '* unsigned-long '* int int int int int))


;;;
;;; High-level bindings
;;;

(define (display-tjhandle handle port)
  (display "#<tjhandle>" port))

(define-wrapped-pointer-type <tjhandle> tjhandle?
  wrap-tjhandle unwrap-tjhandle display-tjhandle)

(define (check-error handle)
  (let* ((ptr (if handle (unwrap-tjhandle handle) %null-pointer))
         (error-code (tj-get-error-code ptr))
         (error-string (pointer->string (tj-get-error-str2 ptr))))
    (if (= error-code TJERR_FATAL)
        (raise-exception (make-exception-with-message error-string))
        (format (current-error-port) "warning: ~a~%" error-string))))

(define (init-decompress)
  (wrap-tjhandle (tj-init-decompress)))

(define (destroy-handle handle)
  (tj-destroy (unwrap-tjhandle handle)))

(define (decompress-headers handle buffer)
  (let ((result (make-s32vector 4)))
    (unless (zero? (tj-decompress-header3 (unwrap-tjhandle handle)
                                          (bytevector->pointer buffer)
                                          (bytevector-length buffer)
                                          (bytevector->pointer result)
                                          (bytevector->pointer result 4)
                                          (bytevector->pointer result 8)
                                          (bytevector->pointer result 12)))
      (check-error handle))
    (values (s32vector-ref result 0)
            (s32vector-ref result 1)
            (s32vector-ref result 2)
            (s32vector-ref result 3))))

(define (decompress handle buffer width pitch height pixel-format flags)
  (let ((bv (make-bytevector (* pitch height))))
    (unless (zero? (tj-decompress2 (unwrap-tjhandle handle)
                                   (bytevector->pointer buffer)
                                   (bytevector-length buffer)
                                   (bytevector->pointer bv)
                                   width pitch height pixel-format flags))
      (check-error handle))
    bv))

(define (load-jpeg file-name)
  (if (file-exists? file-name)
      (let ((buffer (call-with-input-file file-name get-bytevector-all))
            (handle (init-decompress)))
        (dynamic-wind
          (lambda () #t)
          (lambda ()
            (let-values (((width height subsamples color-space)
                          (decompress-headers handle buffer)))
              (let ((pitch (* width 4)))
                (values (decompress handle buffer width pitch height
                                    TJPF_RGBA 0) ; TJFLAG_BOTTOMUP
                        width height))))
          (lambda ()
            (destroy-handle handle))))
      (raise-exception
       (make-exception (make-external-error)
                       (make-exception-with-message
                        (string-append "file not found: " file-name))))))