首页>
知识库>
详情

地形教程 - TGA库源代码

2020-07-02 来源:CloudBest 阅读量: 0
关键词:

    You may use this library for whatever you want. This library is provide as is, meaning that I won't take any responsability for any damages that you may incur from its usage.
    你可以随便怎么使用这个库,但我不承担这个库的bug给你带了的损失。
    #include
    #include
    #include
    #include "tga.h"
    // this variable is used for image series
    static int savedImages=0;
    // load the image header fields. We only keep those that matter!
    void tgaLoadHeader(FILE *file, tgaInfo *info) {
    unsigned char cGarbage;
    short int iGarbage;
    fread(&cGarbage, sizeof(unsigned char), 1, file);
    fread(&cGarbage, sizeof(unsigned char), 1, file);
    // type must be 2 or 3
    fread(&info->type, sizeof(unsigned char), 1, file);
    fread(&iGarbage, sizeof(short int), 1, file);
    fread(&iGarbage, sizeof(short int), 1, file);
    fread(&cGarbage, sizeof(unsigned char), 1, file);
    fread(&iGarbage, sizeof(short int), 1, file);
    fread(&iGarbage, sizeof(short int), 1, file);
    fread(&info->width, sizeof(short int), 1, file);
    fread(&info->height, sizeof(short int), 1, file);
    fread(&info->pixelDepth, sizeof(unsigned char), 1, file);
    fread(&cGarbage, sizeof(unsigned char), 1, file);
    }
    // loads the image pixels. You shouldn't call this function
    // directly
    void tgaLoadImageData(FILE *file, tgaInfo *info) {
    int mode,total,i;
    unsigned char aux;
    // mode equal the number of components for each pixel
    mode = info->pixelDepth / 8;
    // total is the number of bytes we'll have to read
    total = info->height * info->width * mode;
    fread(info->imageData,sizeof(unsigned char),total,file);
    // mode=3 or 4 implies that the image is RGB(A)。 However TGA
    // stores it as BGR(A) so we'll have to swap R and B.
    if (mode >= 3)
    for (i=0; i < total; i+= mode) {
    aux = info->imageData[i];
    info->imageData[i] = info->imageData[i+2];
    info->imageData[i+2] = aux;
    }
    }
    // this is the function to call when we want to load
    // an image
    tgaInfo * tgaLoad(char *filename) {
    FILE *file;
    tgaInfo *info;
    int mode,total;
    // allocate memory for the info struct and check!
    info = (tgaInfo *)malloc(sizeof(tgaInfo));
    if (info == NULL)
    return(NULL);
    // open the file for reading (binary mode)
    file = fopen(filename, "rb");
    if (file == NULL) {
    info->status = TGA_ERROR_FILE_OPEN;
    return(info);
    }
    // load the header
    tgaLoadHeader(file,info);
    // check for errors when loading the header
    if (ferror(file)) {
    info->status = TGA_ERROR_READING_FILE;
    fclose(file);
    return(info);
    }
    // check if the image is color indexed
    if (info->type == 1) {
    info->status = TGA_ERROR_INDEXED_COLOR;
    fclose(file);
    return(info);
    }
    // check for other types (compressed images)
    if ((info->type != 2) && (info->type !=3)) {
    info->status = TGA_ERROR_COMPRESSED_FILE;
    fclose(file);
    return(info);
    }
    // mode equals the number of image components
    mode = info->pixelDepth / 8;
    // total is the number of bytes to read
    total = info->height * info->width * mode;
    // al