I am still looking for a way to save graphs, in any image format. Graph unit allows saving image as a bitmap onto heap. Where do I go from there? I have a pointer, pointing at bitmap on a heap. I am still foggy on pointers actually. Everything I learned from file handling did not help. I can't write into untyped file using write() method. Is there a bitmap type of file? When I create file type of pointer, write(filename, p) saves 4 bytes with one pointer symbol, not the actual image. Can't believe there is no simple way of saving graphical image generated by graph unit to a file. Please help, I kept terrorizing Google for all day yesterday, and part of today with no results. I see similar questions being asked, with no answer.
Here is a reverse-engineering article about the structure of the BGI-saved images:
http://www.verycomputer.com/12_8dc949e052a2dc9e_1.htm. It's relatively easy: a 6-byte header with 2 bytes for width, 2 bytes for height and 2 bytes reserved, then the image data depending on the graphic mode (bits per pixel). In my own experiments, I see that this is not quite correct: A bgi image saved in standard VGA mode has a 12-byte header because the numbers are 4 bytes each nowadays. In vga mode the following pixel data require 2 bytes each, with values 0..15 (that's very probably only part of the story, but something to get started...).
Based on this the FPC image routines can be used to convert the pixeldata to a matrix of TFPColor values, and FCL has all the machinery to write this to a file... A nice exercise in FCLImage!
The attached demo is a working quick-and-dirty solution which certainly can be improved. But it works: it saves the test graphic as bmp, png and jpeg files You can have more, but you must add the image writer unit to the uses clause. Find all writers in the directory packages/fclimage/src of your fpc directory; they are named like fpwrite* where * represents the graphic format.
I did not take too much care about matching colors - simply adapt the color-transformation function BgiColorToFPColor. Using a color picker I see, for example, that the BGI blue has r=0, g=0, b=168 (and you must be aware that FPColor has 16-bit color channels). Replacing the BGI blue by the result of FPColor(0, 0, 168 shl 8 ) gives a better impression than replacing it by the straight-forward colDkBlue constant.
You will also have to adapt the code if you work in a different graphic mode.