Source

infoh503_starterkit / infoh503_starterkit / TGAFile.hpp

Full commit
#pragma once

#include <fstream>
   
// Simple TGA image file reader. Only supports uncompressed RGB images, 8 bits per channel.
class TGAFile
{
public:
    typedef struct RGBPixel_
    {
        unsigned char r, g, b;
    } RGBPixel;
    TGAFile();
    ~TGAFile();

    void readFromFile(const std::string&_filename);
    unsigned short getWidth() const {return mWidth;};
    unsigned short getHeigth() const {return mHeight;};
    int getBufferLength() const { return mWidth * mHeight * (mBPP/8);};
    char *getPixelData() const {return mPixelData;};
    RGBPixel getPixel(unsigned short i, unsigned short j) const;
    RGBPixel getPixel(unsigned int index) const;

protected:
    void _readHeaderDataFromStream(std::ifstream&_inputstream);
    void _readPixelDataFromStream(std::ifstream&_inputstream);
    void _checkHeaderData();

protected:
    struct tga_header {
        char  id_length;
        char  color_map_type;
        char  data_type_code;
        unsigned char  color_map_origin[2];
        unsigned char  color_map_length[2];
        char  color_map_depth;
        unsigned char  x_origin[2];
        unsigned char  y_origin[2];
        unsigned char  width[2];
        unsigned char  height[2];
        char  bits_per_pixel;
        char  image_descriptor;
    } mHeader;

    unsigned short mWidth, mHeight;
    unsigned short mBPP;
    char *mPixelData;
};