vendredi 12 juillet 2024

Red 64-bit Image

More than once while developing redCV, I regretted that Red didn't offer the possibility of creating images in 8, 16, 32 and 64 bits, with a channel number from 1 to 4 as is the case with OpenCV.  Such formats are sometimes very useful for speeding up image processing and improving precision. When we developed Matrix with Toomas Vooglaid and Qingtian Xie in 2020, we solved some of the problems. But I was left wanting more, so I did a bit of digging to find out whether Red objects could answer this question. The following code is just one illustration of such an approach.


#!/usr/local/bin/red

Red [

]


rcv: object [

create: func [

type [integer!] ;--1:byte 2:integer 3:float

bit [integer!] ;--8, 16, 32 or 64-bit

isize [pair!] ;--image size as pair!

channels [integer!] ;--1 to 4 channels

return: [vector!] ;--image data

/local

size width height [integer!]

data [vector!]

][

width: isize/x

height: isize/y

size: width * height * channels 

switch bit [

8  [data: make vector! reduce ['char! 8 (size)]]

16 [data: make vector! reduce ['integer! 16 (size)]]

32 [data: make vector! reduce ['integer! 32 (size)]]

64 [data: make vector! reduce ['float! 64 (size)]]

]

data

]

]

;********************************* tests ************************************

iSize: 256x256

random/seed now/precise

img1: rcv/create 3 64 iSize 1 ;--create a float image with 1 channel

n: length? img1

repeat i n [img1/:i: round/to random 1.0 0.01] ;--random values [0..1]

repeat i n [img1/:i: img1/:i / 1.0 * 255]         ;--random values [0..255]

bin1: copy #{} ;--binary string

repeat i n [append/dup bin1  to integer! img1/:i 3]         ;--integer values

dest1: make image! reduce [iSize bin1] ;--a grayscale Red image with 3 channels


img2: rcv/create 3 64 iSize 3 ;--create a float image with 3 channels

n: length? img2

repeat i n [img2/:i: round/to random 1.0 0.01] ;--random values [0..1]

repeat i n [img2/:i: img2/:i / 1.0 * 255]         ;--random values [0..255]

bin2: copy #{} ;--binary string

foreach [r g b] img2 [

append bin2 to-integer r ;--red channel 

append bin2 to-integer g ;--green channel

append bin2 to-integer b ;--blue channel

]

dest2: make image! reduce [iSize bin2] ;--a rgb Red image


view [

title "64-bit image test"

below

image dest1

image dest2

pad 100x0

button "Quit" [quit]




Aucun commentaire:

Enregistrer un commentaire