An earlier version of the following article and the listing of 2PCX.C were published in the August 19, 1996 issue of ELECTRONIC DESIGN, pp. 137..138 in the "Software Ideas for Design" department. Demo program PTREE.C and the related text have not been included in the ED publication.
Output of Demo Program PTREE.C

Pythagoras Tree.
Command line PTREE PTREE.PCX creates this screen output.
It is then saved to file ptree.pcx as proposed in the article.

Import Graphics From Own Programs To Windows Applications

Bernd Kohler / binär industrie-informatik

Many engineers use software programs they have written themselves to perform some calculations or analyze aquired data. Most existing programs of this kind are written to run under the DOS operating system, and they often produce some sort of graphics output on the PC monitor screen.

Including file 2PCX.C will allow any C program to copy the actual EGA/VGA screen to a file in the ZSoft PCX graphics format. This format can be read by "Paintbrush", which is included with the Windows operating system. After loading the file into "Paintbrush", you then can freely edit it, change colors, add some explanatory text, combine it with other images, and so on.

Via the Windows clipboard you may then insert your graphics into a Microsoft Word document or use it in the context of other Windows applications. This gives you more possibilities to create prints of your own graphics than a simple hardcopy routine directly adressing a printer.

Sample program PTREE.C demonstrates the usage of function shootpcx() contained in 2PCX.C. PTREE creates a nice fractal drawing based on the law of Pythagoras (Pythagoras' tree). Running the program with a file name for argument will save the image to that file. You can start the demo from the Windows program manager or file manager by typing the command line "PTREE PTREE.PCX" and then load PTREE.PCX into Paintbrush.

The version of the routine shown here is restricted to a 16-color palette because more colors (up to 256) use different palette encoding in the PCX format. Using a simple run length encoding scheme, PCX produces much shorter files than the usual Windows BMP (bitmap) format. Even smaller files would be possible if only a black-and-white image had to be saved. But to keep things simple and readable, the necessary changes have not been included. Appropriate versions can be provided by the author. Different EGA/VGA graphic resolutions should cause no problems.

The INVERT switch may be used to change from the black drawing background in a DOS graphics program (where black pixels are sometimes difficult to draw) to a white background, which is usually found in the Windows environment. All other colors are preserved by the actual implementation of this feature, which relies on the assumption that black and white form the first and the last entries respectively in your color palette.

The listed sources are written for the Borland Turbo C compiler, but porting them to other compilers should pose no problems, especially not the shootpcx() function itself, which uses the graphics library only to read the actual color palette.

© 1997 by binär industrie-informatik Bernd Kohler


Please let me know wether this article is useful to you. Please also send any criticism or suggestions.

Code Download

The code mentioned in the above article is published here according to the following principles:
You may freely use it and distribute it as long as You have to agree to these rules, if you download the code. Should you want any other conditions, please contact binär industrie-informatik.

Nice persons will not forget to send me a picture postcard (by snail mail) after download.

Very time-pressed persons may anyhow do me the favor and drop a line of e-mail with their name, institution and place of living. Placing the line Send 2PCX<CR>" into the body of their message will even save them the time needed to

  • Download ZIP file, 3.2 kB Also visit binär's home page.


    © 1997 by binär industrie-informatik