Hey guys, I just recently started using GapiDraw, and was trying to get some drop shadow effects. I tried the Photoshop script, but it wasn't giving decent results due to the fact that it blends against a white background. I thought I would post some code I write which would read in a 32-bit TGA file into a CGapiSurface which is 2x the height of the original image. It reads in the RGB values to the top half and the alpha values to the bottom half much like the script, but it correctly splits the two as it uses the information straight from the TGA file. Let me know if you find this useful.
- Code:
// Loads in a TGA file (32bit)
static bool LoadTGA(CGapiSurface& surface,LPCTSTR filename)
{
unsigned char image_id[256], rgb[4];
unsigned char id_length, palette_type, image_type, palette_entry_size;
unsigned char bpp, descriptor_bits;
short unsigned int first_color, palette_colors;
short unsigned int left, top, image_width, image_height;
unsigned int x, y, yc;
DWORD bytesRead;
// Open the file
HANDLE hFile = CreateFile(filename,GENERIC_READ ,FILE_SHARE_READ,NULL,OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,0);
if(!hFile)
return(false);
// Read in header info
ReadFile(hFile,&id;_length,1,&bytesRead;,NULL);
ReadFile(hFile,&palette;_type,1,&bytesRead;,NULL);
ReadFile(hFile,ℑ_type,1,&bytesRead;,NULL);
ReadFile(hFile,&first;_color,2,&bytesRead;,NULL);
ReadFile(hFile,&palette;_colors,2,&bytesRead;,NULL);
ReadFile(hFile,&palette;_entry_size,1,&bytesRead;,NULL);
ReadFile(hFile,&left;,2,&bytesRead;,NULL);
ReadFile(hFile,⊤,2,&bytesRead;,NULL);
ReadFile(hFile,ℑ_width,2,&bytesRead;,NULL);
ReadFile(hFile,ℑ_height,2,&bytesRead;,NULL);
ReadFile(hFile,&bpp;,1,&bytesRead;,NULL);
ReadFile(hFile,&descriptor;_bits,1,&bytesRead;,NULL);
// Make sure we're 32bpp
if(bpp!=32)
return(false);
if(id_length>0)
ReadFile(hFile,ℑ_id,id_length,&bytesRead;,NULL);
// We are ready to read the file, create the surface
if(surface.CreateSurface(0,image_width,image_height*2)!=GD_OK)
return(false);
// Start reading the data into the image
for (y=image_height; y; y--) {
for (x=0; x<image_width; x++)
{
ReadFile(hFile,rgb,4,&bytesRead;,NULL);
surface.SetPixel(x,y-1,RGB(rgb[2],rgb[1],rgb[0]));
surface.SetPixel(x,y-1+image_height,RGB(rgb[3],rgb[3],rgb[3]));
}
}
// Close the file up
CloseHandle(hFile);
return(true);
}