gnudatalanguage / gdl

GDL - GNU Data Language
GNU General Public License v2.0
274 stars 61 forks source link

WRITE_PNG not accepting rgb 3d array image automatically #1854

Closed ChunkyPanda03 closed 1 month ago

ChunkyPanda03 commented 2 months ago

In my codebase we have a procedure that outputs plots to png files when testing I can not seem to output to plots to png properly. When displaying it to using tv,image it displays properly however doing write_png does not work it will only output a black image I have not done much digging to diagnose why this is happening I just know that it does not work. Version is Version v1.0.6-13-g857bfa25-dirty Code to reproduce:

pro gdlplottingtest
    ;plotridlv0p5, ydata = lindgen(10), filetype = 'png', output = "C:\Users\beh9172\Documents", filename = 'plot', colorsarr = [255]
    thisDevice = !D.Name
    Set_Plot, 'Z'
    if !version.release eq '6.3' then Device, Set_Resolution=[500,500] else Device, Set_Resolution=[500,500], decomposed = 0, SET_PIXEL_DEPTH = 8

;    ; Load drawing colors.
    LoadCT, 0
    TVLCT, (transpose([0,0,255])), 1
    TVLCT, (transpose([255,0,0])), 2
    TVLCT, (transpose([255,255,255])), !P.Background
    TVLCT, (transpose([0,0,0])), !P.Color

    ; Draw graphics.
    Plot, Indgen(10), Color=1
    OPlot, Indgen(10)+1, Color=2

    ; Add a legend.
    ;

    ; Save the image for output to a PNG file.
    image = TVRD()
    TVLCT, r, g, b, /Get
    Write_PNG, 'test.png', image, r, g, b
    Set_Plot, 'win'
    device, decomposed = 0
    tv, image
    Set_Plot, thisDevice

end

I run this on idl 6.3 and it does work test

ChunkyPanda03 commented 2 months ago

I think that the problem is that the write_png GDL procedure is is not implicitly applying the rgb values when presented a 3d array this is stated in the examples part of the documentation in idl 6.3 however I don't know if it is still valid in later versions I will change the title to better explain the problem.

pro gdlplottingtest
    ;plotridlv0p5, ydata = lindgen(10), filetype = 'png', output = "C:\Users\beh9172\Documents", filename = 'plot', colorsarr = [255]
    thisDevice = !D.Name
    Set_Plot, 'Z'
    if !version.release eq '6.3' then Device, Set_Resolution=[500,500] else Device, Set_Resolution=[500,500], decomposed = 0, SET_PIXEL_DEPTH = 8

;    ; Load drawing colors.
    LoadCT, 0
    TVLCT, (transpose([0,0,255])), 1
    TVLCT, (transpose([255,0,0])), 2
    TVLCT, (transpose([255,255,255])), !P.Background
    TVLCT, (transpose([0,0,0])), !P.Color

    ; Draw graphics.
    Plot, Indgen(10), Color=1
    OPlot, Indgen(10)+1, Color=2

    ; Add a legend.
    ;

    ; Save the image for output to a PNG file.
    image = TVRD()
    TVLCT, r, g, b, /Get
    image24 = BytArr(3, 500, 500)
    image24[0,*,*] = r[image]
    image24[1,*,*] = g[image]
    image24[2,*,*] = b[image]
    Write_PNG, 'test.png', image24,r,g,b
    write_jpeg, 'test.jpg',image24, quality=100, true = 1
    ;write_jpg, 'test.jpg', image
    Set_Plot, 'win'
    device, decomposed = 0
    tv, image
    Set_Plot, thisDevice

end

image https://www.nv5geospatialsoftware.com/docs/write_png.html

GillesDuvert commented 2 months ago

Will need to make some test locally, this is easy to reproduce.

GillesDuvert commented 1 month ago

@ChunkyPanda03 you do not present a 3D image in your example

    image = TVRD()
    TVLCT, r, g, b, /Get
    Write_PNG, 'test.png', image, r, g, b

a 3D image would have been with image = TVRD(/True) and the WRITE_PNG would have worked.

But indeed, there was a problem in all palette-indexed image writers (write_jpeg, write_bmp...) , especially with PNG where imagemagick does not easily accept indexed images as imput, only 3D images.

1864 solved this I believe.