简体   繁体   English

CImg图像无色

[英]CImg Image is Colorless

Currently I am in the process of refining a function in my basic level editor program that allows me to save the maps I create. 目前,我正在完善基本级别编辑器程序中的功能,该功能允许我保存创建的地图。 It spits out a .bmp image of the map produced. 它吐出所生成地图的.bmp图像。 It does this through a library I've just discovered called CImg, which I know next to nothing about. 它通过我刚刚发现的名为CImg的库来执行此操作,而我几乎一无所知。 Everything seems to work, but the resulting .bmp image is not colored, appearing instead in different shades of black and white. 一切似乎正常,但是生成的.bmp图像未着色,而是以不同的黑白阴影显示。 Like I said, I know basically nothing about the library, so if you know what the problem could be here, I would appreciate some help. 就像我说的那样,我基本上对库一无所知,因此,如果您知道问题可能出在这里,我将不胜感激。

Here's the save function: 这是保存功能:

void Map::Save() {
    Vertex top_left_most, top_right_most, bottom_left_most;
    int img_w = 0, img_h = 0;

    std::vector<std::pair<GLuint, GLuint>>::iterator tl = bufferIDs.begin();            //This little block gives the _most variables valid starting vals
    glBindBuffer(GL_ARRAY_BUFFER, tl->second);
    glGetBufferSubData(GL_ARRAY_BUFFER, sizeof(TextureCoord), sizeof(Vertex), &top_left_most);
    top_right_most = bottom_left_most = top_left_most;


    for (auto i = bufferIDs.begin(); i != bufferIDs.end(); ++i) {   //SEEKS TOP LEFT MOST TILE ON MAP
        Vertex current_coord;
        glBindBuffer(GL_ARRAY_BUFFER, i->second);
        glGetBufferSubData(GL_ARRAY_BUFFER, sizeof(TextureCoord), sizeof(Vertex), &current_coord);

        if ((current_coord.x < top_left_most.x && current_coord.y < top_left_most.y) ||
            (current_coord.x == top_left_most.x && current_coord.y < top_left_most.y) ||
            (current_coord.x < top_left_most.x && current_coord.y == top_left_most.y)) {

            top_left_most = current_coord;
        }
    }

    for (auto i = bufferIDs.begin(); i != bufferIDs.end(); ++i) {   //SEEKS TOP RIGHT MOST TILE ON MAP
        Vertex current_coord;
        glBindBuffer(GL_ARRAY_BUFFER, i->second);
        glGetBufferSubData(GL_ARRAY_BUFFER, sizeof(TextureCoord), sizeof(Vertex), &current_coord);

        if ((current_coord.x > top_right_most.x && current_coord.y < top_right_most.y) ||
            (current_coord.x == top_right_most.x && current_coord.y < top_right_most.y) ||
            (current_coord.x > top_right_most.x && current_coord.y == top_right_most.y)) {

            top_right_most = current_coord;
        }
    }

    for (auto i = bufferIDs.begin(); i != bufferIDs.end(); ++i) {   //SEEKS BOTTOM LEFT MOST TILE ON MAP
        Vertex current_coord;
        glBindBuffer(GL_ARRAY_BUFFER, i->second);
        glGetBufferSubData(GL_ARRAY_BUFFER, sizeof(TextureCoord), sizeof(Vertex), &current_coord);

        if ((current_coord.x < bottom_left_most.x && current_coord.y > bottom_left_most.y) ||
            (current_coord.x == bottom_left_most.x && current_coord.y > bottom_left_most.y) ||
            (current_coord.x < bottom_left_most.x && current_coord.y == bottom_left_most.y)) {

            bottom_left_most = current_coord;
        }
    }

    img_w = (top_right_most.x + 64) - top_left_most.x;      //Calculating image dimensions for the buffer
    img_h = (bottom_left_most.y + 64) - top_left_most.y;

    GLuint *image = new GLuint[img_w * img_h];      //Creating the image buffer

    int int_start_x = 0;        //start_x and y that will be used in buffer pointer positioning computations
    int int_start_y = 0;

    //these nested fors fill the buffer
    for (GLfloat start_y = top_left_most.y; start_y != bottom_left_most.y + 64; start_y += 64) {

        for (GLfloat start_x = top_left_most.x; start_x != top_right_most.x + 64; start_x += 64) {

            bool in_map = false;
            std::vector<std::pair<GLuint, GLuint>>::iterator valid_tile;
            for (auto i = bufferIDs.begin(); i != bufferIDs.end(); ++i) {           //This for checks to see if tile corresponding to start_x & y is present in map
                Vertex current_tile_pos;
                glBindBuffer(GL_ARRAY_BUFFER, i->second);
                glGetBufferSubData(GL_ARRAY_BUFFER, sizeof(TextureCoord), sizeof(Vertex), &current_tile_pos);

                if (current_tile_pos.x == start_x && current_tile_pos.y == start_y) {
                    in_map = true;
                    valid_tile = i;
                    break;
                }
            }

            GLuint *imagepos = image;       //Repositioning the pointer into the final image's buffer
            imagepos += int_start_x + (int_start_y * img_w);

            if (in_map) {       //if in map, that tile's texture is used to fill the corresponding part of the image buffer
                GLuint *texture = new GLuint[64 * 64];
                glBindTexture(GL_TEXTURE_2D, valid_tile->first);
                glGetTexImage(GL_TEXTURE_2D, 0, GL_RGBA, GL_UNSIGNED_BYTE, texture);
                GLuint *texturepos = texture;

                for (GLuint ypos = 0; ypos != 64; ++ypos) {
                    std::memcpy(imagepos, texturepos, 64 * 4);
                    texturepos += 64;
                    imagepos += img_w;
                }

                if (texture) 
                    delete[] texture;
            }
            else {              //otherwise, a default all-black array is used to fill the corresponding untiled part of the image buffer
                GLuint *black_buffer = new GLuint[64 * 64];
                GLuint *blackpos = black_buffer;
                GLuint solid_black;
                char *p = (char *)&solid_black;
                p[0] = 0;
                p[1] = 0;
                p[2] = 0;
                p[3] = 255;

                for (GLuint i = 0; i != 64 * 64; ++i) {
                    black_buffer[i] = solid_black;
                }

                for (GLuint ypos = 0; ypos != 64; ++ypos) {
                    std::memcpy(imagepos, blackpos, 64 * 4);
                    blackpos += 64;
                    imagepos += img_w;
                }

                if (black_buffer)
                    delete[] black_buffer;

            }
            int_start_x += 64;
        }
        int_start_x = 0;
        int_start_y += 64;
    }

    cimg_library::CImg<GLuint> final_image(image, img_w, img_h);   //no color!!

    final_image.save_bmp("map.bmp");

    if (image)
        delete[] image;
}

In case some explanation would be helpful, Vertex is a simple struct of two GLfloat s (as is TextureCoord ), bufferIDs is an std::vector of std::pair s of GLuint s, the first representing a texture ID, and the second representing a VBO ID. 如果有一些解释会有所帮助,则Vertex是两个GLfloat的简单structTextureCoord ), bufferIDsGLuintstd::pairstd::vector ,第一个表示纹理ID,第二个表示纹理ID代表VBO ID。

Here are the requested sample images: 以下是所需的示例图像:

what the image should look like (this is in monochrome) 图像应该是什么样(单色)

Same exact image as above, but created using the reinterpret_cast method 与上述图像完全相同,但使用reinterpret_cast方法创建

Your line 你的线

cimg_library::CImg<GLuint> final_image(image, img_w, img_h);

is wrong if you are expecting a colour image because that creates a single channel image. 如果您期望彩色图像,那是错误的,因为它会创建一个单通道图像。 You need a 3 at the end to make 3 channels - one for Red, one for Green and one for Blue. 您最后需要3个才能制作3个通道-一个用于红色,一个用于绿色,一个用于蓝色。

Also, your data is stored in GLuint which means that a 4x2 pixel image will be stored like this, ie band-interleaved-by-pixel : 同样,您的数据存储在GLuint ,这意味着将像这样存储4x2像素的图像,即按像素逐行交错

RGBA RGBA RGBA RGBA
RGBA RGBA RGBA RGBA

whereas CImg wants to store that in a band-interleaved-by-plane fashion: CImg希望以带交错的平面方式存储它:

RRRRRRRR
GGGGGGGG
BBBBBBBB
AAAAAAAA

This link explains the layout of CImg memory buffers. 链接说明CImg内存缓冲区的布局。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM