-------------------------------------------------------------------------------
Written by Lazebnik Denis.                             08/05/97

E-Mail address:                      Postal address:
---------------                      --------------
denisl@actcom.co.il                  Lazebnik Denis
                                     Yavniely 7/24
                                     Kiryat-Ata 28093
                                     ISRAEL
-------------------------------------------------------------------------------
DISCLAIMER:

I assume no responsibility whatsoever for any damage that this program or
the information contained in the document may cause you, your computer or
anything else you can think of.
-------------------------------------------------------------------------------
You may do anything you want with the code, change it, use it in your own
programs, just give me credit for it.
-------------------------------------------------------------------------------
INTRODUCTION:

The purpose of this program is to show the basics of decoding two widely
used file formats: PCX and TGA. Note: this program can only handle 8-bit
uncompressed TGA files. Another purpose is to show how a basic C++ program
is build.

The program was written for and compiled using the DJGPP compiler, a FREE
32-bit protected mode C/C++ compiler. For more details about DJGPP go to
http://www.delorie.com/djgpp/. Because programs compiled with DJGPP are
32-bit programs, they need a DPMI server to access the protected mode
features. If you use Windows, you may delete the file called cwsdpmi.exe. 
This program is the DPMI server for programs compiled with DJGPP. As soon 
as you run the main program (ldpcxtga.exe) it will check if a DPMI server 
is available (for example if you run Windows), and if not, it will call 
cwsdpmi.exe. For more details look into cwsdpmi.doc

CONTENTS OF THIS PACKAGE:

ldmain.cpp   -> source code for the main program.
ldpcx.cpp    -> source code for the PCX routines.
ldtga.cpp    -> source code for the TGA routines.
cwsdpmi.doc  -> documentation for the DPMI server.
cwsdpmi.exe  -> the DPMI server itself.
ldpcxtga.exe -> the main program.
ldpcxtga.h   -> the include file for the source code.
mysha.pcx    -> a PCX file to test the program.
shapes.tga   -> a TGA file to test the program.
readme.txt   -> the file you are reading right now.

FILE FORMATS:

Now, when we are done with that, let's actually get into the structure of
those file formats.

PCX FILE FORMAT:
First 128 bytes is the header, then the actual data of the Image and after
that, follows the 768 bytes long pallete data. All you need to get from the
header is Image's Width (a word size located in the 8th byte) and it's Height
(also a word size locate in the 10th byte of the header). After that you go to
the end of the file and count back 768 bytes, that's where the start of the
pallete is located. You read those 768 bytes into an array, divide each value
by 4, and then set the pallete (look into the sources to see how I did that).
After the pallete is set, go back to the first byte after the header, and
start decoding the data. The encoding method used in the PCX files is very
simple. You read in a byte, if it's top two bits aren't set, you just plot it
onto the screen (without any decoding). If the top two bits are set (to check if
the top two bits are set do this:  if((Temp & 192) == 192) ) that means that
the low six bits represent the number of times to plot the next byte. It
sounds very complex, but if you look into the source code, you'll see that
it's actually very simple. What you do is this: NumberOfTimes = Temp & 63
(the low six bits), then you read the next byte, and you plot that byte
to the screen NumberOfTimes. That's all you need to know for writing you own
PCX viewer.

TGA FILE FORMAT:
The format of a simple, 8-bit uncompressed TGA file is even much simpler than
the format of a PCX file. The first 18 bytes is the header, Width is a word
size located at the 8th byte, Height is also a word size located at the 10th
byte of the header. After the 18 byte header, starts the 768 bytes long pallete
data. You read it into an array and divide all the values by 4, just like in
a PCX file. But there is one thing that is different in the pallete of a TGA
file, it's reversed. It's not in the usual format of Red, Green, Blue but
in the format of Blue, Green, Red. So, for having the pallete set in the right
way you must reverse it back (See in the source code how I did that). After the
pallete data, goes the actual data of the Image without any encoding or
compression (there are compressed TGA files, which are not handled by this
program). You just read in a byte and plot it onto the screen and do that
until you get to to the end of the file. THAT'S ALL!

IN CONCLUSION:
I hope you'll find this useful and enjoy it as much as I enjoyed writing this.
Should you have any questions/comments/suggestions, please E-Mail me without
any hesitations. Even if you don't have anything to comment or to ask, just
send me a short E-Mail message so I'll know that somebody actually looked
into it. I would REALLY appreciate this.

Lazebnik Denis
denisl@actcom.co.il
