change byte order in string data

Guiserver, GTK-server, OpenGL, PostScript,
HTML 5, MIDI, IDE
Locked
Sleeper
Posts: 24
Joined: Fri Nov 24, 2006 2:24 pm

change byte order in string data

Post by 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:

Code: Select all

;; 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

Locked