change byte order in string data

Started by Sleeper, February 01, 2007, 03:19:40 AM

Previous topic - Next topic

Sleeper

Recently i wrote a few  opengl demos in newlisp just for fun, in which i used simple TGA texture loading function. The problem is that in tga image data colors go in BGR or BGRA order and i use GL_BGR_EXT and GL_BGRA_EXT extensions. What i want is to change color order to standard RGB and RGBA in a fastest way and use GL_RGB and GL_RGBA. I wrote two functions for this purpose:



;; bgra -> rgba
(define (swaprb32 data)
    (join (map (lambda (x) (select x 2 1 0 3)) (explode data 4)) ""))

;; bgr -> rgb
(define (swaprb24 data)
    (join (map reverse (explode data 3)) ""))

;; testing
(print "test-rgba: ")
(set 'testdata (dup "bgra" (* 512 512)))  ; data from file
(set 'testres  (dup "rgba" (* 512 512)))  ; what i want to get
(print (time (set 'res (swaprb32 testdata))) " ")
(println (= res testres))

(print "test-rgb: ")
(set 'testdata (dup "bgr" (* 512 512)))
(set 'testres  (dup "rgb" (* 512 512)))
(print (time (set 'res (swaprb24 testdata))) " ")
(println (= res testres))


but maybe there is a better way to do this thing?

any suggestions?

results i get for 512x512 image:

 test-rgba: 781 true

 test-rgb: 609 true