Changeset 12156 in project for release/4/imlib2/trunk/imlib2.scm
- Timestamp:
- 10/14/08 23:09:27 (13 years ago)
- Location:
- release/4/imlib2
- Files:
-
- 1 edited
- 1 copied
Legend:
- Unmodified
- Added
- Removed
-
release/4/imlib2/trunk/imlib2.scm
r10055 r12156 7 7 ;;; 8 8 ; 9 ; Version 0. 49 ; Version 0.7 10 10 ; 11 ; Copyright (c) 2005 , 2006Peter Bex (Peter.Bex@xs4all.nl)11 ; Copyright (c) 2005-2008 Peter Bex (Peter.Bex@xs4all.nl) 12 12 ; All rights reserved. 13 13 ; … … 38 38 ;; XXX The bools in the C interface are really all chars 39 39 40 (declare 41 (export imlib:create imlib:destroy imlib:clone imlib:load imlib:save 42 imlib:image? imlib:format-set! imlib:format imlib:width imlib:height 43 imlib:filename imlib:alpha? imlib:alpha-set! 44 imlib:track-changes-on-disk 45 imlib:flip-horizontal imlib:flip-horizontal! imlib:flip-vertical 46 imlib:flip-vertical! imlib:flip-diagonal imlib:flip-diagonal! 47 imlib:orientate imlib:orientate! imlib:sharpen imlib:sharpen! 48 imlib:blur imlib:blur! imlib:tile imlib:tile! imlib:tile-horizontal 49 imlib:tile-horizontal! imlib:tile-vertical imlib:tile-vertical! 50 imlib:crop imlib:scale 51 imlib:crop&scale imlib:pixel/rgba imlib:pixel/hsva imlib:pixel/hlsa 52 imlib:pixel/cmya imlib:color/rgba imlib:color/hsva imlib:color/hlsa 53 imlib:color/cmya imlib:color? 54 imlib:draw-pixel imlib:draw-line imlib:draw-rectangle 55 imlib:fill-rectangle)) 40 (module imlib2 41 (image-create image-destroy image-clone image-load image-save 42 image? image-format-set! image-format image-width image-height 43 image-filename image-alpha? image-alpha-set! 44 image-track-changes-on-disk 45 image-flip-horizontal image-flip-horizontal! image-flip-vertical 46 image-flip-vertical! image-flip-diagonal image-flip-diagonal! 47 image-orientate image-orientate! image-sharpen image-sharpen! 48 image-blur image-blur! image-tile image-tile! image-tile-horizontal 49 image-tile-horizontal! image-tile-vertical image-tile-vertical! 50 image-crop image-scale 51 image-crop&scale image-pixel/rgba image-pixel/hsva image-pixel/hlsa 52 image-pixel/cmya color/rgba color/hsva color/hlsa color/cmya color? 53 image-draw-pixel image-draw-line image-draw-rectangle 54 image-fill-rectangle) 55 56 (import chicken scheme foreign) 56 57 57 58 ; Not needed with the latest Imlib2 (1.2.2) … … 62 63 (foreign-declare "#include <Imlib2.h>") 63 64 64 (use syntax-case) 65 66 (define-record imlib:image ptr) 67 (define-record imlib:color setter one two three alpha) 65 (define-record image ptr) 66 (define-record color setter one two three alpha) 68 67 69 68 (define-foreign-type image c-pointer) … … 119 118 ;; Internal convenience macros 120 119 (define (assert-image img loc . args) 121 (when (not (im lib:image-ptr img))120 (when (not (image-ptr img)) 122 121 (imlib-err loc "Invalid image parameter" args))) 123 122 … … 128 127 (define (func img arg ...) 129 128 (assert-image img (quote func) arg ...) 130 (imlib-context-set-image (im lib:image-ptr img))129 (imlib-context-set-image (image-ptr img)) 131 130 body ...)))) 132 131 133 (define (im lib:load filename)132 (define (image-load filename) 134 133 (let-location ([err int]) ; int should really be imlib-load-error 135 134 (let* ((load-image (foreign-lambda image … … 139 138 (image (load-image filename (location err)))) 140 139 (if (= err IMLIB_LOAD_ERROR_NONE) 141 (set-finalizer! (make-im lib:image image) imlib:collect)140 (set-finalizer! (make-image image) gc-collect) 142 141 (abort 143 142 (make-composite-condition 144 143 (make-property-condition 'exn 'i/o 'file 145 'location 'im lib:load144 'location 'image-load 146 145 'message (load-error->message err) 147 146 'arguments (list filename)) 148 147 (make-property-condition 'imlib))))))) 149 148 150 (define/img (im lib:save img filename)149 (define/img (image-save img filename) 151 150 (let-location ([err int]) ; int should really be imlib-load-error 152 151 ((foreign-lambda void … … 160 159 (make-composite-condition 161 160 (make-property-condition 'exn 'i/o 'file 162 'location 'im lib:save161 'location 'image-save 163 162 'message (load-error->message err) 164 163 'arguments (list filename)) … … 168 167 ;; Create a new image, completely transparent. 169 168 ;; 170 (define (im lib:create width height)169 (define (image-create width height) 171 170 (if (or (< width 0) (< height 0)) 172 (imlib-err 'im lib:create "Width and height must be positive" width height)173 (let ((img (make-im lib:image ((foreign-lambda image imlib_create_image int int) width height))))171 (imlib-err 'image-create "Width and height must be positive" width height) 172 (let ((img (make-image ((foreign-lambda image imlib_create_image int int) width height)))) 174 173 (if (not img) 175 (imlib-err 'im lib:create "Could not create new image" (list width height))174 (imlib-err 'image-create "Could not create new image" (list width height)) 176 175 (begin 177 (im lib:alpha-set! img #t)178 (im lib:fill-rectangle img (imlib:color/rgba 0 0 0 0) 0 0 width height)179 (set-finalizer! img imlib:collect))))))180 181 (define ( imlib:collect img)182 (when (im lib:image-ptr img)176 (image-alpha-set! img #t) 177 (image-fill-rectangle img (color/rgba 0 0 0 0) 0 0 width height) 178 (set-finalizer! img gc-collect)))))) 179 180 (define (gc-collect img) 181 (when (image-ptr img) 183 182 (let ((old (imlib-context-get-image))) 184 (imlib-context-set-image (im lib:image-ptr img))183 (imlib-context-set-image (image-ptr img)) 185 184 ((foreign-lambda void imlib_free_image)) 186 185 (imlib-context-set-image old)))) 187 186 188 (define/img (im lib:destroy img)187 (define/img (image-destroy img) 189 188 ((foreign-lambda void imlib_free_image)) 190 (im lib:image-ptr-set! img #f))191 192 (define/img (im lib:format-set! img format)189 (image-ptr-set! img #f)) 190 191 (define/img (image-format-set! img format) 193 192 ((foreign-lambda void imlib_image_set_format c-string) format)) 194 193 195 (define/img (im lib:format img)194 (define/img (image-format img) 196 195 ((foreign-lambda c-string imlib_image_format))) 197 196 198 (define/img (im lib:width img)197 (define/img (image-width img) 199 198 ((foreign-lambda int imlib_image_get_width))) 200 199 201 (define/img (im lib:height img)200 (define/img (image-height img) 202 201 ((foreign-lambda int imlib_image_get_height))) 203 202 204 (define/img (im lib:filename img)203 (define/img (image-filename img) 205 204 ((foreign-lambda c-string imlib_image_get_filename))) 206 205 … … 211 210 ((define/clone ?name ?name!) 212 211 (define (?name img . args) 213 (let ((new-img (im lib:clone img)))212 (let ((new-img (image-clone img))) 214 213 (apply ?name! new-img args) 215 214 new-img))))) 216 215 217 (define/img (im lib:flip-horizontal! img)216 (define/img (image-flip-horizontal! img) 218 217 ((foreign-lambda void imlib_image_flip_horizontal))) 219 (define/clone im lib:flip-horizontal imlib:flip-horizontal!)220 221 (define/img (im lib:flip-vertical! img)218 (define/clone image-flip-horizontal image-flip-horizontal!) 219 220 (define/img (image-flip-vertical! img) 222 221 ((foreign-lambda void imlib_image_flip_vertical))) 223 (define/clone im lib:flip-vertical imlib:flip-horizontal!)224 225 (define/img (im lib:flip-diagonal! img)222 (define/clone image-flip-vertical image-flip-horizontal!) 223 224 (define/img (image-flip-diagonal! img) 226 225 ((foreign-lambda void imlib_image_flip_diagonal))) 227 (define/clone im lib:flip-diagonal imlib:flip-diagonal!)228 229 (define/img (im lib:orientate! img orientation)226 (define/clone image-flip-diagonal image-flip-diagonal!) 227 228 (define/img (image-orientate! img orientation) 230 229 (if (or (< orientation 0) (> orientation 7)) 231 (imlib-err 'im lib:orientate230 (imlib-err 'image-orientate 232 231 "Orientation must be between 0 and 7 inclusive" 233 232 (list orientation)) 234 233 ((foreign-lambda void imlib_image_orientate int) orientation))) 235 (define/clone im lib:orientate imlib:orientate!)236 237 (define/img (im lib:sharpen! img radius) ;; XXX: What does a radius < 0 mean?!234 (define/clone image-orientate image-orientate!) 235 236 (define/img (image-sharpen! img radius) ;; XXX: What does a radius < 0 mean?! 238 237 ((foreign-lambda void imlib_image_sharpen int) radius)) 239 (define/clone im lib:sharpen imlib:sharpen!)240 241 (define/img (im lib:blur! img radius)238 (define/clone image-sharpen image-sharpen!) 239 240 (define/img (image-blur! img radius) 242 241 (if (< radius 0) 243 (imlib-err 'im lib:blur "Radius cannot be less than 0" (list radius))242 (imlib-err 'image-blur "Radius cannot be less than 0" (list radius)) 244 243 ((foreign-lambda void imlib_image_blur int) radius))) 245 (define/clone im lib:blur imlib:blur!)246 247 (define/img (im lib:tile-horizontal! img)244 (define/clone image-blur image-blur!) 245 246 (define/img (image-tile-horizontal! img) 248 247 ((foreign-lambda void imlib_image_tile_horizontal))) 249 (define/clone im lib:tile-horizontal imlib:tile-horizontal!)250 251 (define/img (im lib:tile-vertical! img)248 (define/clone image-tile-horizontal image-tile-horizontal!) 249 250 (define/img (image-tile-vertical! img) 252 251 ((foreign-lambda void imlib_image_tile_vertical))) 253 (define/clone im lib:tile-vertical imlib:tile-vertical!)254 255 (define/img (im lib:tile! img)252 (define/clone image-tile-vertical image-tile-vertical!) 253 254 (define/img (image-tile! img) 256 255 ((foreign-lambda void imlib_image_tile))) 257 (define/clone im lib:tile imlib:tile!)256 (define/clone image-tile image-tile!) 258 257 259 258 ;; imlib_image_get_data … … 261 260 ;; imlib_image_put_back_data 262 261 263 (define/img (im lib:alpha? img)262 (define/img (image-alpha? img) 264 263 ((foreign-lambda bool imlib_image_has_alpha))) 265 264 266 (define/img (im lib:alpha-set! img val)265 (define/img (image-alpha-set! img val) 267 266 ((foreign-lambda void imlib_image_set_has_alpha bool) val)) 268 267 269 (define/img (im lib:track-changes-on-disk img)268 (define/img (image-track-changes-on-disk img) 270 269 ((foreign-lambda void imlib_image_set_changes_on_disk))) 271 270 272 271 273 272 ;; XXX What does cropping/scaling mean when x/y are out of bounds? It is allowed 274 (define/img (im lib:crop img x y width height)273 (define/img (image-crop img x y width height) 275 274 (set-finalizer! 276 (make-im lib:image275 (make-image 277 276 ((foreign-lambda image imlib_create_cropped_image int int int int) 278 277 x y width height)) 279 imlib:collect))280 281 (define/img (im lib:crop&scale img src-x src-y src-width src-height dest-width dest-height)278 gc-collect)) 279 280 (define/img (image-crop&scale img src-x src-y src-width src-height dest-width dest-height) 282 281 (set-finalizer! 283 (make-im lib:image282 (make-image 284 283 ((foreign-lambda image imlib_create_cropped_scaled_image int int int 285 284 int int int) 286 285 src-x src-y src-width src-height dest-width dest-height)) 287 imlib:collect))288 289 (define/img (im lib:scale img width height)290 (im lib:crop&scale img 0 0 (imlib:width img) (imlib:height img) width height))291 292 (define (im lib:clone img)293 (set-finalizer! (make-im lib:image (imlib-clone-image img)) imlib:collect))286 gc-collect)) 287 288 (define/img (image-scale img width height) 289 (image-crop&scale img 0 0 (image-width img) (image-height img) width height)) 290 291 (define (image-clone img) 292 (set-finalizer! (make-image (imlib-clone-image img)) gc-collect)) 294 293 295 294 (define/img (imlib-clone-image img) … … 298 297 299 298 (define (check-coords loc img x y) 300 (let ((width (im lib:width img))301 (height (im lib:height img))299 (let ((width (image-width img)) 300 (height (image-height img)) 302 301 (fail (lambda (msg) 303 302 (imlib-err loc msg (list x y))))) … … 306 305 ((or (>= y height) (< y 0)) (fail "Y coordinate out of range"))))) 307 306 308 (define (im lib:pixel/rgba img x y)309 (check-coords 'im lib:pixel/rgba img x y)307 (define (image-pixel/rgba img x y) 308 (check-coords 'image-pixel/rgba img x y) 310 309 (let ((query-func (foreign-lambda* void ([int x] 311 310 [int y] … … 324 323 (values r g b a)))) 325 324 326 (define/img (im lib:pixel/hsva img x y)327 (check-coords 'im lib:pixel/hsva img x y)325 (define/img (image-pixel/hsva img x y) 326 (check-coords 'image-pixel/hsva img x y) 328 327 (let-location ([h float] [s float] [v float] [a int]) 329 328 ((foreign-lambda void imlib_image_query_pixel_hsva int int (c-pointer float) (c-pointer float) (c-pointer float) (c-pointer int)) x y (location h) (location s) (location v) (location a)) 330 329 (values h s v a))) 331 330 332 (define/img (im lib:pixel/hlsa img x y)333 (check-coords 'im lib:pixel/hlsa img x y)331 (define/img (image-pixel/hlsa img x y) 332 (check-coords 'image-pixel/hlsa img x y) 334 333 (let-location ([h float] [l float] [s float] [a int]) 335 334 ((foreign-lambda void imlib_image_query_pixel_hlsa int int (c-pointer float) (c-pointer float) (c-pointer float) (c-pointer int)) x y (location h) (location l) (location s) (location a)) 336 335 (values h l s a))) 337 336 338 (define/img (im lib:pixel/cmya img x y)339 (check-coords 'im lib:pixel/cmya img x y)337 (define/img (image-pixel/cmya img x y) 338 (check-coords 'image-pixel/cmya img x y) 340 339 (let-location ([c int] [m int] [y int] [a int]) 341 340 ((foreign-lambda void imlib_image_query_pixel_cmya int int (c-pointer int) (c-pointer int) (c-pointer int) (c-pointer int)) x y (location c) (location m) (location y) (location a)) … … 373 372 (imlib-err loc "Cyan, Magenta, Yellow and Alpha values must be between 0 and 255" c m y a))) 374 373 375 (define ( imlib:color/rgba r g b a)376 (check-colorvalues/rgba ' imlib:color/rgba r g b a)377 (make- imlib:color imlib-context-set-color/rgba r g b a))378 379 (define ( imlib:color/cmya c m y a)380 (check-colorvalues/cmya ' imlib:color/cmya c m y a)381 (make- imlib:color imlib-context-set-color/cmya c m y a))382 383 (define ( imlib:color/hlsa h l s a)384 (check-colorvalues/hlsa ' imlib:color/hlsa h l s a)385 (make- imlib:color imlib-context-set-color/hlsa h l s a))386 387 (define ( imlib:color/hsva h s v a)388 (check-colorvalues/hsva ' imlib:color/hsva h s v a)389 (make- imlib:color imlib-context-set-color/hsva h s v a))374 (define (color/rgba r g b a) 375 (check-colorvalues/rgba 'color/rgba r g b a) 376 (make-color imlib-context-set-color/rgba r g b a)) 377 378 (define (color/cmya c m y a) 379 (check-colorvalues/cmya 'color/cmya c m y a) 380 (make-color imlib-context-set-color/cmya c m y a)) 381 382 (define (color/hlsa h l s a) 383 (check-colorvalues/hlsa 'color/hlsa h l s a) 384 (make-color imlib-context-set-color/hlsa h l s a)) 385 386 (define (color/hsva h s v a) 387 (check-colorvalues/hsva 'color/hsva h s v a) 388 (make-color imlib-context-set-color/hsva h s v a)) 390 389 391 390 (define (context-set-color color) 392 (( imlib:color-setter color) (imlib:color-one color)393 (imlib:color-two color)394 (imlib:color-three color)395 (imlib:color-alpha color)))391 ((color-setter color) (color-one color) 392 (color-two color) 393 (color-three color) 394 (color-alpha color))) 396 395 397 396 ;;; … … 404 403 ;; without having an exception thrown. 405 404 ;; 406 (define/img (im lib:draw-pixel img color x y)405 (define/img (image-draw-pixel img color x y) 407 406 (context-set-color color) 408 (check-coords 'im lib:draw-pixel img x y)407 (check-coords 'image-draw-pixel img x y) 409 408 ((foreign-lambda updates imlib_image_draw_pixel int int bool) x y #f)) 410 409 411 (define/img (im lib:draw-line img color x1 y1 x2 y2)410 (define/img (image-draw-line img color x1 y1 x2 y2) 412 411 (context-set-color color) 413 412 ((foreign-lambda updates imlib_image_draw_line int int int int bool) x1 y1 x2 y2 #f)) 414 413 415 (define/img (im lib:draw-rectangle img color x y width height)414 (define/img (image-draw-rectangle img color x y width height) 416 415 (context-set-color color) 417 416 ((foreign-lambda void imlib_image_draw_rectangle int int int int) x y width height)) 418 417 419 (define/img (im lib:fill-rectangle img color x y width height)418 (define/img (image-fill-rectangle img color x y width height) 420 419 (context-set-color color) 421 420 ((foreign-lambda void imlib_image_fill_rectangle int int int int) x y width height)) … … 503 502 (define (imlib-context-set-color/cmya c m y a) 504 503 ((foreign-lambda void imlib_context_set_color_cmya int int int int) c m y a)) 504 )
Note: See TracChangeset
for help on using the changeset viewer.