It's perhaps easier than you might imagine. You'll need to have a 3 dimension array. One dimension of size 3 for the R, G, and B values, and addressed in (X,Y) with the other two dimensions. These X and Y correspond to the X and Y of your image.
The put_dib call allows you to specify the array size that you are using AND the array portion that you have filled. You can get BMP or JPG as a result.
In my code, I use the following declaration:
character dib_array(3,0:max_across_horizontal,0:max_down_horizontal)
where the horizontal indicates that the direction of the 'Y' axis.
So, the X direction is down and the Y direction is across.
The first dimension of size 3 indicates that index=1 for R, 2=G, 3=B.
Get each pixel at the row and column address and store the RGB values as indicated.
Then output using something like:
call put_dib_block@(trim(graphicfilename),dib_array,
$ max_across_horizontal+1,max_down_horizontal+1,
$ dib_adx,dib_ady,max_plot_y,max_plot_x,0L,0L,err)
dib_adx and dib_ady are set to zero as initial values. The 0L are used because my code is compiled with 16 bit integers as standard (I2 not i4).
If the filename ends in BMP, then it's BMP, if .JPG, the JPG.
About as easy as it can get.