summaryrefslogtreecommitdiff
path: root/2d/wrappers/freeimage.scm
blob: 544bf515edf2ca786ad3d05c6a959cabe0507a0d (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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
;;; guile-2d
;;; Copyright (C) 2013, 2014 David Thompson <dthompson2@worcester.edu>
;;;
;;; This program is free software: you can redistribute it and/or
;;; modify it under the terms of the GNU General Public License as
;;; published by the Free Software Foundation, either version 3 of the
;;; License, or (at your option) any later version.
;;;
;;; This program is distributed in the hope that it will be useful,
;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
;;; General Public License for more details.
;;;
;;; You should have received a copy of the GNU General Public License
;;; along with this program.  If not, see
;;; <http://www.gnu.org/licenses/>.

;;; Commentary:
;;
;; Quick and dirty wrapper for some freeimage functions.
;;
;;; Code:

(define-module (2d wrappers freeimage)
  #:use-module (system foreign)
  #:use-module (2d wrappers util)
  #:use-module (ice-9 format))

(define libfreeimage (dynamic-link "libfreeimage"))

(define-syntax-rule (define-foreign name ret string-name args)
  (define name
    (pointer->procedure ret (dynamic-func string-name libfreeimage) args)))

(define (number->boolean n)
  (not (zero? n)))

;;;
;;; FreeImage file formats
;;;

(define-enumeration freeimage-format
  (unknown -1)
  (bmp      0)
  (ico      1)
  (jpeg     2)
  (jng      3)
  (koala    4)
  (lbm      5)
  (iff      5)
  (mng      6)
  (pbm      7)
  (pbmraw   8)
  (pcd      9)
  (pcx      10)
  (pgm      11)
  (pgmraw   12)
  (png      13)
  (ppm      14)
  (ppmraw   15)
  (ras      16)
  (targa    17)
  (tiff     18)
  (wbmp     19)
  (psd      20)
  (cut      21)
  (xbm      22)
  (xpm      23)
  (dds      24)
  (gif      25)
  (hdr      26)
  (faxg3    27)
  (sgi      28)
  (exr      29)
  (j2k      30)
  (jp2      31)
  (pfm      32)
  (pict     33)
  (raw      34))

(export freeimage-format)

;;;
;;; General functions
;;;

(define-foreign %freeimage-get-version
  '* "FreeImage_GetVersion" '())
(define-foreign %freeimage-set-output-message
  void "FreeImage_SetOutputMessage" '(*))

(define (freeimage-get-version)
  (pointer->string (%freeimage-get-version)))

(define (freeimage-set-output-message callback)
  (%freeimage-set-output-message
   (procedure->pointer void
                       (lambda (image-format message)
                         (callback image-format (pointer->string message)))
                       (list unsigned-int '*))))

;; Set a default output message callback to writes to stdout.
(freeimage-set-output-message
 (lambda (image-format message)
   (display "freeimage error: ")
   (display message)
   (newline)))

(export freeimage-get-version
        freeimage-set-output-message)


;;;
;;; Bitmap management functions
;;;

(define-wrapped-pointer-type <freeimage-bitmap>
  freeimage-bitmap?
  wrap-freeimage-bitmap unwrap-freeimage-bitmap
  (lambda (r port)
    (let ((bitmap (unwrap-freeimage-bitmap r)))
      (format port
              "<freeimage-bitmap ~x width: ~d height: ~d bpp: ~d>"
              (pointer-address bitmap)
              (%freeimage-get-width bitmap)
              (%freeimage-get-height bitmap)
              (%freeimage-get-bpp bitmap)))))

(define-foreign %freeimage-load
  '* "FreeImage_Load" (list unsigned-int '* unsigned-int))
(define-foreign %freeimage-unload
  void "FreeImage_Unload" '(*))

(define (freeimage-load image-format filename)
  (wrap-freeimage-bitmap
   (%freeimage-load image-format (string->pointer filename) 0)))

(define (freeimage-unload bitmap)
  (%freeimage-unload (unwrap-freeimage-bitmap bitmap)))

(export <freeimage-bitmap>
        freeimage-bitmap?
        freeimage-load
        freeimage-unload)

;;;
;;; Bitmap information functions
;;;

(define-foreign %freeimage-get-image-type
  unsigned-int "FreeImage_GetImageType" '(*))
(define-foreign %freeimage-get-bpp
  unsigned-int "FreeImage_GetBPP" '(*))
(define-foreign %freeimage-get-width
  unsigned-int "FreeImage_GetWidth" '(*))
(define-foreign %freeimage-get-height
  unsigned-int "FreeImage_GetHeight" '(*))
(define-foreign %freeimage-get-pitch
  unsigned-int "FreeImage_GetPitch" '(*))
(define-foreign %freeimage-get-red-mask
  unsigned-int "FreeImage_GetRedMask" '(*))
(define-foreign %freeimage-get-green-mask
  unsigned-int "FreeImage_GetGreenMask" '(*))
(define-foreign %freeimage-get-blue-mask
  unsigned-int "FreeImage_GetBlueMask" '(*))
(define-foreign %freeimage-has-pixels
  unsigned-int "FreeImage_HasPixels" '(*))

(define (freeimage-get-image-type bitmap)
  (%freeimage-get-image-type (unwrap-freeimage-bitmap bitmap)))

(define (freeimage-get-bpp bitmap)
  (%freeimage-get-bpp (unwrap-freeimage-bitmap bitmap)))

(define (freeimage-get-width bitmap)
  (%freeimage-get-width (unwrap-freeimage-bitmap bitmap)))

(define (freeimage-get-height bitmap)
  (%freeimage-get-height (unwrap-freeimage-bitmap bitmap)))

(define (freeimage-get-pitch bitmap)
  (%freeimage-get-pitch (unwrap-freeimage-bitmap bitmap)))

(define (freeimage-get-red-mask bitmap)
  (%freeimage-get-red-mask (unwrap-freeimage-bitmap bitmap)))

(define (freeimage-get-green-mask bitmap)
  (%freeimage-get-green-mask (unwrap-freeimage-bitmap bitmap)))

(define (freeimage-get-blue-mask bitmap)
  (%freeimage-get-blue-mask (unwrap-freeimage-bitmap bitmap)))

(define (freeimage-has-pixels? bitmap)
  (number->boolean
   (%freeimage-has-pixels (unwrap-freeimage-bitmap bitmap))))

(export freeimage-get-image-type
        freeimage-get-bpp
        freeimage-get-width
        freeimage-get-height
        freeimage-get-red-mask
        freeimage-get-green-mask
        freeimage-get-blue-mask
        freeimage-has-pixels?)

;;;
;;; Filetype functions
;;;

(define-foreign %freeimage-get-file-type
  unsigned-int "FreeImage_GetFileType" (list '* int))

(define (freeimage-get-file-type filename)
  (%freeimage-get-file-type (string->pointer filename) 0))

(export freeimage-get-file-type)

;;;
;;; Pixel access functions
;;;

(define-foreign %freeimage-get-bits '* "FreeImage_GetBits" '(*))

(define (freeimage-get-bits bitmap)
  (pointer->bytevector
     (%freeimage-get-bits (unwrap-freeimage-bitmap bitmap))
     (* (freeimage-get-height bitmap)
        (freeimage-get-pitch bitmap))))

(export freeimage-get-bits)

;;;
;;; Conversion functions
;;;

(define-foreign %freeimage-convert-to-24-bits
  '* "FreeImage_ConvertTo24Bits" '(*))
(define-foreign %freeimage-convert-to-32-bits
  '* "FreeImage_ConvertTo32Bits" '(*))

(define (freeimage-convert-to-24-bits bitmap)
  (wrap-freeimage-bitmap
   (%freeimage-convert-to-24-bits (unwrap-freeimage-bitmap bitmap))))

(define (freeimage-convert-to-32-bits bitmap)
  (wrap-freeimage-bitmap
   (%freeimage-convert-to-32-bits (unwrap-freeimage-bitmap bitmap))))

(export freeimage-convert-to-24-bits
        freeimage-convert-to-32-bits)

;;;
;;; Rotation and flipping
;;;

(define-foreign %freeimage-flip-vertical
  uint8 "FreeImage_FlipVertical" '(*))

(define (freeimage-flip-vertical bitmap)
  (number->boolean
   (%freeimage-flip-vertical (unwrap-freeimage-bitmap bitmap))))

(export freeimage-flip-vertical)