convert an image to a matrix

70 views
Skip to first unread message

Azzeddine Remmal

unread,
Oct 10, 2022, 9:51:33 PM10/10/22
to The Ring Programming Language
Hello ilir
How do I convert an image to a matrix RGB in ring

Ilir

unread,
Oct 11, 2022, 4:42:34 AM10/11/22
to The Ring Programming Language
Hello,

best way is to use some library, look at RingAllegro which have nice support for that because there are so many RGB formats like


Anyway, I made quick sample based on STB library only to give you look and feel, this is not production usable because it doesn't care about big/little endianess, if there is alpha channel in it (4 channels), etc.

# Load the library
    load "stbimage.ring"
# Image Information
    width=0    height=0 channels=0
# Ring will Free cData automatically in the end of the program
    cData = stbi_load("ring.jpg",:width,:height,:channels,STBI_rgb)
# Display the output
    ? "Size (bytes): " + len(cData)
    ? "Width : " + width
    ? "Height: " + height
    ? "Channels: " + channels
    pos1 = 1
    for h = 1 to height
        pos2 = h * width * channels
        for w = pos1 to pos2 step channels
            see " R = " + ascii(cData[w])
            see " G = " + ascii(cData[w+1])
            see " B = " + ascii(cData[w+2])
            see nl
        next
        pos1 = pos2
    next
# End of program
    ? :done

Ilir

unread,
Oct 11, 2022, 5:18:41 AM10/11/22
to The Ring Programming Language
Hello,

and here is the usable code (see attachment) using RingAllegro (get_pixel) in ARGB format

Greetings,
Ilir

get_pixel.zip

Azzeddine Remmal

unread,
Oct 11, 2022, 6:51:09 AM10/11/22
to The Ring Programming Language
Hello ilir

Sketch1.png 


Thanks for the help. In fact, I wanted an integer array so that I could make calculations on it. I have an example that I made using the Qt
library
But it is very slow and can not handle large images
The array size becomes larger
This is an example of what I want to get


Sketch2.png

func ImageToPixelArray(cFileName)

   pixmap= new qpixMap(cFileName)

   image    = pixmap.toImage()

   height    = image.height()

   width = image.width()

   Rlist     = list(width ,height)

   Glist        = list(width ,height)

   Blist        = list(width ,height)

    Alist        = list(width ,height)

   RGBAlist = []

    ? "pixelRGBAlist : "

   for i = 1 to width

       for j=1 to height

           color = image.pixel(i, j)

           oColor = new QColor()

           oColor.setRGBA(color)

           Rlist[i][j]     = oColor.red()

           Glist[i][j]     = oColor.green()

           Blist[i][j]     = oColor.blue()

            Alist[i][j]     = oColor.alpha()

               /*?"{ R :" + oColor.red() + ", " +

               "G :" + oColor.green() + ", " +

               "B :" + oColor.blue() + ", " +

               "A :" + oColor.alpha() + "} " */

       next

   next

   RGBAlist + Rlist

   RGBAlist + Glist

   RGBAlist + Blist

    RGBAlist + Alist

   ? :DONE

   return [

           :Height = height,

           :Width = width,

           :RGBA = RGBAlist

           /*:Red = Rlist,

           :Green = Glist,

           :Blue = Blist ,

           :Alpha = Alist*/

           ]

Thanks
drawImageFromArray.ring

Ilir

unread,
Oct 11, 2022, 7:01:19 AM10/11/22
to The Ring Programming Language
Hello,

do you have some sample image? I'm getting this error

Line 50 Error (R2) : Array Access (Index out of range) !
In function draw() in file drawImageFromArray.ring
called from line 11598  In qapp_exec() In method exec() in file ring\libraries\guilib\classes/ring_qt.ring
called from line 38  in file drawImageFromArray.ring

Azzeddine Remmal

unread,
Oct 11, 2022, 7:20:29 AM10/11/22
to The Ring Programming Language
hello ilir
Sketch4.png

The array is stored in a text file
ToPixelArray.txt

Ilir

unread,
Oct 11, 2022, 7:53:16 AM10/11/22
to The Ring Programming Language
Hello,

well, that took 6 seconds to render in Ring2B+ (10 sec official Ring), but I'm getting error if I try ring.jpg as sample.

I think render times are acceptable.

Greetings,
Ilir

Reply all
Reply to author
Forward
0 new messages