Как получить доступ к содержимому RGB изображения png с помощью библиотеки CImg? - PullRequest
0 голосов
/ 21 сентября 2018

Итак, я около недели искал, как получить доступ к RGB-содержимому указанного пикселя в c ++, и мне не повезло ... Я случайно нашел библиотеку под названием 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

Любая помощь приветствуется, и если у кого-то есть другая библиотека, которая будет работать, это будет прекрасно.

Спасибо, Финн.

--- EDIT ---

Эта функция использует 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 ++.

...