Game Development Reference
BYTE * CTGA::GetPixelAddress(int x, int y, TGAHEADER &hdr, BYTE ↵
// Make sure it's in range and if it isn't return zero.
if ((x >= hdr.ImageWidth)|| (y >= hdr.ImageHeight)||(x<0)||(y<0))
// Calculate the scan line storage width.
swidth = hdr.ImageWidth * 3;
swidth = (swidth + 3) & ~3; //DWORD align
return (BYTE*)(bits + (hdr.ImageHeight-y-1) * swidth + x*3);
Both Windows bitmaps and TGA files are simple to understand and
Loading a Jpeg file
A Jpeg file is much more complicated and to load the file you must use a
library that provides the necessary code. Even then the techniques are
rather complex, but they are worth using when file size is an issue. In the
final chapter of the topic we look at distributing real-time 3D character
animation on the Internet; if this is your aim then Jpeg files are the best
option for the texture maps that will be used in the animation. In the
sample code for loading Jpeg files I make use of some code that was
originally written by Chris Losinger and makes the loading and saving of
Jpeg files much simpler. As usual, the full source code is included as part
of the Toon3D source code. To load a Jpeg file we use many calls to the
Jpeglib library. This is provided in the Toon3D source code folder. It must
be added to your project along with the header file 'jpeglib.h' if you intend
to use this sample code. In the code Chris wrote, he breaks the job into
1 First, we must allocate and initialize a decompression object and the
error routines that the library is going to use.
2 We supply the source file to the decompression object.
3 Next we read the Jpeg file header.
4 If necessary, we could set various decompression options.
5 Now it is safe to start the decompression.
6 With the decompression object initialized, we can read the actual pixel
data into a scan line buffer and store the results of the decompression.