GLProgramming.com

home :: about :: development guides :: irc :: forums :: search :: paste :: links :: contribute :: code dump

-> Click here to learn how to get live help <-


New Paste :: Recent Pastes:: Add Line Numbers


:( by Nokturn
bool Texture::Load (char *arg_pFilename)
{
    FILE *l_pFile = fopen (arg_pFilename, "rb");
    if (!l_pFile)
    {
        printf("Couldn't open Texture file: %s\n", arg_pFilename);
        return false;
    }
    
    Uint8 l_aFileHeader[12];
    if (fread (l_aFileHeader, 1, sizeof (l_aFileHeader), l_pFile) != sizeof (l_aFileHeader))
    {
        printf ("Error: TGA file %s lacks the first 12 bytes of the header! File probably corrupt!\n", arg_pFilename);
        fclose (l_pFile);
        return false;
    }
    
    Uint8 l_aCorrectHeader[12] = {0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0}; // The header of an uncompressed RGB/RGBA TGA file
    if (memcmp (l_aCorrectHeader, l_aFileHeader, sizeof (l_aCorrectHeader)))
    {
        printf ("Error: TGA file %s doesn't seem to be in uncompressed RGB/RGBA format!\n", arg_pFilename);
        fclose (l_pFile);
        return false;
    }
    
    Uint8 l_ImageInfo[6];    
    if (fread (l_ImageInfo, 1, sizeof (l_ImageInfo), l_pFile) != sizeof (l_ImageInfo))
    {
        printf ("Error: TGA file %s lacks the last 6 bytes of the header! File probably corrupt!\n", arg_pFilename);
        fclose (l_pFile);
        return false;
    }

    // In TGA, all integers are stored in little-endian format    
    Uint16 l_Width = l_ImageInfo[1] * 256 + l_ImageInfo[0];
    Uint16 l_Height = l_ImageInfo[3] * 256 + l_ImageInfo[2];
    if (!l_Width || !l_Height)
    {
        printf ("Error: TGA image %s has a width or height of 0!\n", arg_pFilename);
        fclose (l_pFile);
        return false;
    }
    
    Uint8 l_Bpp = l_ImageInfo[4]; // bits per pixel
    
    if (l_Bpp != 24 && l_Bpp != 32)
    {
        printf ("Error: TGA image %s is in %d bit format, but only 24 and 32 bit formats are supported.\n", arg_pFilename, l_Bpp);
        fclose (l_pFile);
        return false;
    }
    
    Uint32 l_ImageSize = l_Width * l_Height;
    
    m_TextureImage.m_pImageData = new Uint8 [l_ImageSize*(l_Bpp/8)];
    
    printf ("Allocating Pointer: %h\n", m_TextureImage.m_pImageData);    
    
    if (!m_TextureImage.m_pImageData)
    {
        printf ("Error: Could not allocate %d bytes for image data (%s). Not enough memory?\n", l_ImageSize*(l_Bpp/8), arg_pFilename);
        fclose (l_pFile);
    }
    
    /*GLubyte **l_pImageData = &m_TextureImage.m_pImageData;
    if (fread (*l_pImageData, 1, l_ImageSize*(l_Bpp/8), l_pFile) != l_ImageSize*(l_Bpp/8))
    {
        printf ("Error: Couldn't read %d bytes of image data from %s. File probably corrupt.\n", l_ImageSize*(l_Bpp/8), arg_pFilename);
        delete [] *l_pImageData;
        m_TextureImage.m_pImageData = NULL;
        fclose (l_pFile);
        return false;
    }*/
    
    fclose (l_pFile);
    
    m_TextureImage.m_Bpp = l_Bpp;
    m_TextureImage.m_Height = l_Height;
    m_TextureImage.m_Width = l_Width;
    
    // At this point we should have the complete TGA image loaded into memory. Last thing we need to do: Switch BGR(A) to RGB(A)
    Uint8 l_Temp;
    /*Uint8 *l_pPixel = *l_pImageData;
    for (Uint32 l_PixelNr = 0; l_PixelNr < l_ImageSize; l_PixelNr++)
    {
        l_Temp = *l_pPixel;
        *l_pPixel = *(l_pPixel+2);
        *(l_pPixel+2) = l_Temp;
        l_pPixel+=(l_Bpp/8);
    }*/
    return true; // Success
}