首页>
知识库>
详情

地形教程 - 一个TGA库

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

    This library is introduced here in order to make this tutorial self-contained. The library is pretty simple, handling only uncompressed images, RGBA or greyscale. No support for color palettes is provided. If you're interested in a full-blown TGA library I would suggest a look at Paul Groves' TGA loader. Check out his home page.
    http://home.clara.net/paulyg
    因为这个教程使用到了这个库,所以我们引入它近来。这个库相当的简练,只能处理非压缩的图像,RGBA或者灰度图。不支持调色板。如果你对功能全面的TGA感兴趣,我建议你看下 Paul Groves' TGA loader,看看下面这个地址。
    The functions in this library include loading and saving TGA images, colour reduction from RGB to greyscale, and a screen grabber.
    这个库的函数包含读取和存储TGA图像,灰度转换,一个屏幕抢夺(?)功能。
    TGA file structure
    TGA文件结构
    A TGA file has a header that consists of 12 fields. These are:
    一个TGA的文件头包含12个域,它们是:
    id (unsigned char)
    colour map type (unsigned char) 色彩图类型
    image type (unsigned char) 图像类型
    colour map first entry (short int) 颜色图第一个入口
    colour map length (short int) 颜色图长度
    map entry size (short int) 图入口尺寸
    horizontal origin (short int) 水平起始
    vertical origin (short int) 垂直起始
    width (short int) 宽度
    height (short int) 高度
    pixel depth (unsigned char)像素深度
    image descriptor (unsigned char) 图像描述
    From all these fields we only really care about the image type, in order to find out if the image is uncompressed and it is not color indexed, the width and height of the image, the pixel depth, and finally the image descriptor that contains the image pixels.
    全部的这些域里面,我们唯一关心的只是图像类型(image type),为了找出图是非压缩的并且颜色不是索引的,和图像的高度和宽度,颜色深度,最后图像表述和图像的像素内容。
    Some possible values for the image type are:
    一些可能的图像类型如下:
    1 - colour map image
    2 - RGB(A) uncompressed
    3 - greyscale uncompressed
    9 - greyscale RLE (compressed)
    10 - RGB(A) RLE (compressed)
    The only types that we're dealing with here are 2 and 3. As for the pixel depth, it represents the number of bits per pixel used, i.e. a greyscale image has 8 for pixel depth, where as a RGBA has 32.
    我们只能处理2,3两种图像类型,对于像素深度,它表达了每个像素使用的字节数,例如,灰度图每个像素使用8位,而RGBA每个像素使用32位。
    One note of interest is that a TGA stores the pixels in BGR mode, i.e. the red and blue components are swapped, relative to RGB. This implies that we'll have to swap them when we load or save the image.
    这里要提醒的是TGA储存像素的方式是BGR模式,相对于RGB模式,红色和蓝色分量互换了位置。这个也预示我们在读取和储存的时候,我们需要交换它们的位置。
    Now for some library details. The following status codes where defined:
    下面定义了一些状态吗:
    TGA_ERROR_FILE_OPEN
    TGA_ERROR_READING_FILE
    TGA_ERROR_INDEXED_COLOR - when we're presented with a color indexed file
    TGA_ERROR_MEMORY
    TGA_ERROR_COMPRESSED_FILE - when we're presented with a compressed file
    TGA_OK - This is what we want! 这才是我们所需要的
    The following structure provides the necessary fields to hold the image information and pixels:
    下面的结构提供了必要的域来保存图像信息和像素:
    typedef struct {
    int status;
    unsigned char type, pixelDepth;
    short int width, height;
    unsigned char *imageData;
    }tgaInfo;
    The following functions are available in the library:
    下面这些函数是库中可用的:
    tgaInfo* tgaLoad(char *filename);
    Parameters:
    filename - the name of the image file
    文件名 - 图像的名称
    This functions returns a structure with all the image info and pixels. Pixels are stored in imageData, which is an one-dimensional array with values from 0 to 255. In order to read this array correctly, we'll need to check the values of pixelDepth, wi