使用Dev C ++加载图像

问题描述

我的目标相对简单,那就是使用C ++加载图像。我使用Dev C ++编译并运行这些代码。我已经在编译器选项和参数中链接了这些头文件

#include <vector>
#include <fstream>
#include <GL/gl.h>
#include <GL/glu.h>
#include <cstdint>
#include <stdexcept>
#include <cstring>


typedef union PixelInfo
{
    std::uint32_t Colour;
    struct
    {
        std::uint8_t R,G,B,A;
    };
} *PPixelInfo;

class Tga
{
private:
    std::vector<std::uint8_t> Pixels;
    bool ImageCompressed;
    std::uint32_t width,height,size,BitsPerPixel;

public:
    Tga(const char* FilePath);
    std::vector<std::uint8_t> GetPixels() {return this->Pixels;}
    std::uint32_t GetWidth() const {return this->width;}
    std::uint32_t GetHeight() const {return this->height;}
    bool HasAlphaChannel() {return BitsPerPixel == 32;}
};

Tga::Tga(const char* FilePath)
{
    std::fstream hFile(FilePath,std::ios::in | std::ios::binary);
    if (!hFile.is_open()){throw std::invalid_argument("File Not Found.");}

    std::uint8_t Header[18] = {0};
    std::vector<std::uint8_t> ImageData;
    static std::uint8_t DeCompressed[12] = {0x0,0x0,0x2,0x0};
    static std::uint8_t IsCompressed[12] = {0x0,0xA,0x0};

    hFile.read(reinterpret_cast<char*>(&Header),sizeof(Header));

    if (!std::memcmp(DeCompressed,&Header,sizeof(DeCompressed)))
    {
        BitsPerPixel = Header[16];
        width  = Header[13] * 256 + Header[12];
        height = Header[15] * 256 + Header[14];
        size  = ((width * BitsPerPixel + 31) / 32) * 4 * height;

        if ((BitsPerPixel != 24) && (BitsPerPixel != 32))
        {
            hFile.close();
            throw std::invalid_argument("Invalid File Format. required: 24 or 32 Bit Image.");
        }

        ImageData.resize(size);
        ImageCompressed = false;
        hFile.read(reinterpret_cast<char*>(ImageData.data()),size);
    }
    else if (!std::memcmp(IsCompressed,sizeof(IsCompressed)))
    {
        BitsPerPixel = Header[16];
        width  = Header[13] * 256 + Header[12];
        height = Header[15] * 256 + Header[14];
        size  = ((width * BitsPerPixel + 31) / 32) * 4 * height;

        if ((BitsPerPixel != 24) && (BitsPerPixel != 32))
        {
            hFile.close();
            throw std::invalid_argument("Invalid File Format. required: 24 or 32 Bit Image.");
        }

        PixelInfo Pixel = {0};
        int CurrentByte = 0;
        std::size_t CurrentPixel = 0;
        ImageCompressed = true;
        std::uint8_t ChunkHeader = {0};
        int BytesPerPixel = (BitsPerPixel / 8);
        ImageData.resize(width * height * sizeof(PixelInfo));

        do
        {
            hFile.read(reinterpret_cast<char*>(&ChunkHeader),sizeof(ChunkHeader));

            if(ChunkHeader < 128)
            {
                ++ChunkHeader;
                for(int I = 0; I < ChunkHeader; ++I,++CurrentPixel)
                {
                    hFile.read(reinterpret_cast<char*>(&Pixel),BytesPerPixel);

                    ImageData[CurrentByte++] = Pixel.B;
                    ImageData[CurrentByte++] = Pixel.G;
                    ImageData[CurrentByte++] = Pixel.R;
                    if (BitsPerPixel > 24) ImageData[CurrentByte++] = Pixel.A;
                }
            }
            else
            {
                ChunkHeader -= 127;
                hFile.read(reinterpret_cast<char*>(&Pixel),BytesPerPixel);

                for(int I = 0; I < ChunkHeader; ++I,++CurrentPixel)
                {
                    ImageData[CurrentByte++] = Pixel.B;
                    ImageData[CurrentByte++] = Pixel.G;
                    ImageData[CurrentByte++] = Pixel.R;
                    if (BitsPerPixel > 24) ImageData[CurrentByte++] = Pixel.A;
                }
            }
        } while(CurrentPixel < (width * height));
    }
    else
    {
        hFile.close();
        throw std::invalid_argument("Invalid File Format. required: 24 or 32 Bit TGA File.");
    }

    hFile.close();
    this->Pixels = ImageData;
}


int main()
{
    Tga info = Tga("C:/Users/Lenovo/Desktop/OpenGL/sabahpride.tga");

    gluint texture = 0;
    glGenTextures(1,&texture);
    glBindTexture(GL_TEXTURE_2D,texture);
    glTexImage2D(GL_TEXTURE_2D,info.HasAlphaChannel() ? GL_RGBA : GL_RGB,info.GetWidth(),GL_UNSIGNED_BYTE,info.GetPixels().data());

}

代码已成功编译,但是当我运行.exe文件时,它什么也没有显示。似乎根本什么都不处理。我已经在加载此图像上工作了几天,但无济于事。如果还有其他解决方案,我将不胜感激。 当我运行调试器时,它显示一个错误提示[Error] can't create precompiled header Flag of Sabah.exe: Permission denied

我还尝试使用openCV加载图像,它可以编译,但根本无法运行,并且不会创建任何.exe文件

#include "opencv2/opencv.hpp"
#include <iostream>

using namespace cv;
using namespace std;

int main(int argc,char** argv)
{
 // Read the image file
 Mat image = imread("C:/Users/Lenovo/Desktop/New folder/sabahpride.jpg");

 // Check for failure
 if (image.empty()) 
 {
  cout << "Could not open or find the image" << endl;
  cin.get(); //wait for any key press
  return -1;
 }

 String windowName = "The Flag"; //Name of the window

 namedWindow(windowName); // Create a window

 imshow(windowName,image); // Show our image inside the created window.

 waitKey(0); // Wait for any keystroke in the window

 destroyWindow(windowName); //destroy the created window

 return 0;
}

任务应该在明天到期,所以我真的需要我能提供的帮助。谢谢。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)