# How to show Matrix{RGB} on Cairo surface?

**URL:** <https://discourse.julialang.org/t/how-to-show-matrix-rgb-on-cairo-surface/79166>\
**Category:** Visualization\
**Created:** [April 7, 2022, 1:38pm UTC](https://discourse.julialang.org/t/how-to-show-matrix-rgb-on-cairo-surface/79166 "2022-04-07T13:38:48Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rratic](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rratic/32/24917_2.png) [@Rratic](https://discourse.julialang.org/u/Rratic)\
**Post date:** [April 7, 2022, 1:38pm UTC](https://discourse.julialang.org/t/how-to-show-matrix-rgb-on-cairo-surface/79166/1 "2022-04-07T13:38:49Z")

</div>

Cairo provides `read_from_png`, but returns value of a special type `CairoSurfaceBase`, which is hard to operate, so I choose `Matrix{RGB}`(the thing ImageIO returns).  
Cairo does provide something called `set_matrix`, but the second argument must be of type`CairoMatrix`. There aren’t any documents on this, so I can’t figure out a way to convert between `CairoMatrix` and `Matrix{RGB}`. Or, more importantly, I want to show the `Matrix{RGB}`.

[sync:stackoverflow](https://stackoverflow.com/questions/71783436/how-to-show-matrixrgb-on-cairo-surface)

---

<div class="post-metadata">

**Author:** ![lobingera](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lobingera/32/211_2.png) [@lobingera](https://discourse.julialang.org/u/lobingera)\
**Post date:** [April 7, 2022, 2:33pm UTC](https://discourse.julialang.org/t/how-to-show-matrix-rgb-on-cairo-surface/79166/2 "2022-04-07T14:33:47Z")

</div>

CairoSurfaceImage surface can be created from a Matrix{T} with T from Uint32, RGB24 and ARGB32, you need to convert from Matrix{RGB}.

---

<div class="post-metadata">

**Author:** ![Rratic](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rratic/32/24917_2.png) [@Rratic](https://discourse.julialang.org/u/Rratic)\
**Post date:** [April 24, 2022, 2:28pm UTC](https://discourse.julialang.org/t/how-to-show-matrix-rgb-on-cairo-surface/79166/4 "2022-04-24T14:28:32Z")

</div>

Thanks, but when I tried

```julia
julia> mat=FileIO.load("flag.png");nothing

julia> m2=Matrix{RGB24}(undef,32,32);nothing

julia> for i in 1:32
               for j in 1:32
                   m2[i,j]=RGB24(mat[i,j])
               end
           end

ctx=CairoImageSurface(m2)
Cairo.CairoSurfaceImage{RGB24}(Ptr{Nothing} @0x00...

julia> write_to_png(ctx,"D:/foo.png")

```

I found that the image has been reflected  
original:  
 ![flag](https://global.discourse-cdn.com/julialang/original/3X/1/7/17bc673b59ac983b4ba98be854d0d18e90ca73d3.png)  
foo.png:  
 ![foo](https://global.discourse-cdn.com/julialang/original/3X/e/7/e786e05e74a86199f0e8ec6e81fa5f00b12f358c.png)

A simple solution is to use `=mat[j,i]`  
but is this normal? Is it that developers are thinking differntly?

---

<div class="post-metadata">

**Author:** ![lobingera](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lobingera/32/211_2.png) [@lobingera](https://discourse.julialang.org/u/lobingera)\
**Post date:** [April 24, 2022, 5:03pm UTC](https://discourse.julialang.org/t/how-to-show-matrix-rgb-on-cairo-surface/79166/5 "2022-04-24T17:03:30Z")

</div>

Most bitmaps in graphics are row-first i.e. consecutive pixels in x-axis.
