如何使用CImg库访问png图像的RGB内容?

时间:2018-09-21 18:28:03

标签: c++ compiler-errors game-engine cimg

因此,我一直在寻找大约一个星期的时间,以了解如何在c ++中访问指定像素的RGB内容,但是运气不好。 我确实找到了一个适合我想要的功能的名为CImg的库,但是我编写的代码不断给我带来错误,而且我不知道自己做错了什么。

#include "CImg.h"
#include <vector>
using namespace cimg_library;

std::vector<int> GetPixel(char file[50], int x, int y)
{
     CImg<int> image(file);

     int r = (int)image(x, y, 0, 0);
     int g = (int)image(x, y, 0, 1);
     int b = (int)image(x, y, 0, 2);

     std::vector<int> Pixel(2);

     Pixel[0] = r;
     Pixel[1] = g;
     Pixel[2] = b;

     return Pixel;
}

我正在使用Microsoft Visual Studio 2017,并包含CImg库的目录...我怀疑这是库问题,因为当我注释掉代码时,它会很好地构建。

编译器中的错误如下:

1>c:\program files (x86)\windows kits\10\include\10.0.17134.0\um\prsht.h(140): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\program files (x86)\windows kits\10\include\10.0.17134.0\um\prsht.h(140): error C2146: syntax error: missing ';' before identifier 'PROPSHEETPAGE_RESOURCE'
1>c:\program files (x86)\windows kits\10\include\10.0.17134.0\um\prsht.h(198): error C3646: 'pResource': unknown override specifier
1>c:\program files (x86)\windows kits\10\include\10.0.17134.0\um\prsht.h(198): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\program files (x86)\windows kits\10\include\10.0.17134.0\um\prsht.h(204): error C3646: 'pResource': unknown override specifier
1>c:\program files (x86)\windows kits\10\include\10.0.17134.0\um\prsht.h(204): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\program files (x86)\windows kits\10\include\10.0.17134.0\um\prsht.h(213): error C3646: 'pResource': unknown override specifier
1>c:\program files (x86)\windows kits\10\include\10.0.17134.0\um\prsht.h(213): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\program files (x86)\windows kits\10\include\10.0.17134.0\um\prsht.h(225): error C3646: 'pResource': unknown override specifier
1>c:\program files (x86)\windows kits\10\include\10.0.17134.0\um\prsht.h(225): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\program files (x86)\windows kits\10\include\10.0.17134.0\um\prsht.h(244): error C3646: 'pResource': unknown override specifier
1>c:\program files (x86)\windows kits\10\include\10.0.17134.0\um\prsht.h(244): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\program files (x86)\windows kits\10\include\10.0.17134.0\um\prsht.h(250): error C3646: 'pResource': unknown override specifier
1>c:\program files (x86)\windows kits\10\include\10.0.17134.0\um\prsht.h(250): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\program files (x86)\windows kits\10\include\10.0.17134.0\um\prsht.h(259): error C3646: 'pResource': unknown override specifier
1>c:\program files (x86)\windows kits\10\include\10.0.17134.0\um\prsht.h(259): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\program files (x86)\windows kits\10\include\10.0.17134.0\um\prsht.h(271): error C3646: 'pResource': unknown override specifier
1>c:\program files (x86)\windows kits\10\include\10.0.17134.0\um\prsht.h(271): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\program files (x86)\windows kits\10\include\10.0.17134.0\um\commctrl.h(7770): error C2061: syntax error: identifier 'LPSCROLLINFO'
1>c:\program files (x86)\windows kits\10\include\10.0.17134.0\um\commctrl.h(7785): error C2061: syntax error: identifier 'LPSCROLLINFO'
1>c:\users\micah notanalien\desktop\programming projects\chili directx framework\chili framework 2016\engine\drawgame.cpp(29): warning C4018: '<': signed/unsigned mismatch
1>c:\users\micah notanalien\desktop\programming projects\chili directx framework\chili framework 2016\engine\drawgame.cpp(31): warning C4018: '<': signed/unsigned mismatch
1>c:\users\micah notanalien\desktop\programming projects\chili directx framework\chili framework 2016\engine\drawgame.cpp(70): warning C4018: '<': signed/unsigned mismatch
1>c:\users\micah notanalien\desktop\programming projects\chili directx framework\chili framework 2016\engine\drawgame.cpp(72): warning C4018: '<': signed/unsigned mismatch
1>c:\users\micah notanalien\desktop\programming projects\chili directx framework\chili framework 2016\engine\drawgame.cpp(86): warning C4244: 'initializing': conversion from 'double' to 'int', possible loss of data
1>c:\users\micah notanalien\desktop\programming projects\chili directx framework\chili framework 2016\engine\drawgame.cpp(87): warning C4244: 'initializing': conversion from 'double' to 'int', possible loss of data
1>c:\users\micah notanalien\desktop\programming projects\chili directx framework\chili framework 2016\engine\drawgame.cpp(101): warning C4018: '<': signed/unsigned mismatch
1>c:\users\micah notanalien\desktop\programming projects\chili directx framework\chili framework 2016\engine\drawgame.cpp(103): warning C4018: '<': signed/unsigned mismatch

我们将不胜感激,并且如果有人拥有另一个可以正常运行的库,那就很好了。

谢谢,芬恩。

---编辑---

这是使用GetPixel()以及其他两个GetWidth()和GetHeight()函数的函数...

int GetWidth(char file[50]) {
    CImg<int> image(file);
    return image.width();
}

int GetHeight(char file[50]) {
    CImg<int> image(file);
    return image.height();
}

(现在使用这些功能的功能)

void DrawGame::DrawSprite(char file[50], int x, int y)
{
    int width = GetWidth(file);
    int height = GetHeight(file);

    for (int i = 0; i < width; i++) {
        for (int j = 0; j < height; j++)
        {
            std::vector<int> Pixel;
            Pixel.resize(2);
            Pixel = GetPixel(file, i, j);

            SetVSPixel(x + i, y + j, Pixel[0], Pixel[1], Pixel[2]);
        }
    }
}

(最后是主要功能)

#include "MainWindow.h"
#include "Game.h"
#include "DrawGame.h"
#include "ChiliException.h"

int WINAPI wWinMain( HINSTANCE hInst,HINSTANCE,LPWSTR pArgs,INT )
{
    try
    {
        MainWindow wnd( hInst,pArgs );
        try
        {
            DrawGame DrawTheGame(wnd);
            DrawTheGame.CreateMatrix();
            DrawTheGame.SetVSPixel(3, 5, 255, 255, 255);

            Game Player(wnd, DrawTheGame);

            while( wnd.ProcessMessage() )
            {
                DrawTheGame.Go();
                DrawTheGame.DrawSprite("person.png", 8, 8);
                Player.Go();
            }
        }
        catch( const ChiliException& e )
        {
            const std::wstring eMsg = e.GetFullMessage() + 
                L"\n\nException caught at Windows message loop.";
            wnd.ShowMessageBox( e.GetExceptionType(),eMsg );
        }
        catch( const std::exception& e )
        {
            // need to convert std::exception what() string from narrow to wide string
            const std::string whatStr( e.what() );
            const std::wstring eMsg = std::wstring( whatStr.begin(),whatStr.end() ) + 
                L"\n\nException caught at Windows message loop.";
            wnd.ShowMessageBox( L"Unhandled STL Exception",eMsg );
        }
        catch( ... )
        {
            wnd.ShowMessageBox( L"Unhandled Non-STL Exception",
                L"\n\nException caught at Windows message loop." );
        }
    }
    catch( const ChiliException& e )
    {
        const std::wstring eMsg = e.GetFullMessage() +
            L"\n\nException caught at main window creation.";
        MessageBox( nullptr,eMsg.c_str(),e.GetExceptionType().c_str(),MB_OK );
    }
    catch( const std::exception& e )
    {
        // need to convert std::exception what() string from narrow to wide string
        const std::string whatStr( e.what() );
        const std::wstring eMsg = std::wstring( whatStr.begin(),whatStr.end() ) +
            L"\n\nException caught at main window creation.";
        MessageBox( nullptr,eMsg.c_str(),L"Unhandled STL Exception",MB_OK );
    }
    catch( ... )
    {
        MessageBox( nullptr,L"\n\nException caught at main window creation.",
            L"Unhandled Non-STL Exception",MB_OK );
    }

    return 0;
}

主要功能是我正在观看的YouTube频道从一个入门项目中使用的,以习惯于c ++。

0 个答案:

没有答案