Skip to main content
edited title
Link
user1430
user1430

(SOLVED) How tocan I read an image from a binary file?

edited title
Link

(SOLVED) How to read an image from binary file

added 126 characters in body
Source Link

I'm trying to put all the assets inside a binary file and then trying to read them from the file itself, without scattering all them around in separate folders to let them exposed. What I'm trying to achieve is something like the way that RPG Maker and Wolf Editor use to join all the images / sounds of the game into a single customized format file (e.g. Data.wolf).

It's a very simple security's method but is enough for what I'm planning.

For now, I have the following code for reading a PNG file inside the binary, that always prints an error's message when it trys to create a new surface from IMG_LoadPNG_RW, saying "Error reading the PNG file."

tString sResult{};
cFile_Manager::tRead_File oRead(sDir, std::ios::binary);

if(oRead.is_open()) {
    tString sLine{};
    while(getline(oRead, sLine)) sResult += sLine + "\n";
    oRead.close();
}else {
    throw cLog("\n\n[ERROR] Wrong dir passed.\n\nDIR: %s\n\n", sDir);
}

SDL_RWops* aRW{};
aRW = SDL_RWFromConstMem(&sResult, sizeof(sResult));

if(!aRW) // Error message

SDL_Surface* aSurface{};
aSurface = IMG_LoadPNG_RW(aRW);

if(!aSurface) // This is where SDL shows "Error reading the PNG file."

EDIT: It keeps givingOmg I made some mistakes, now this is the same errorcorrectly code. Now it gives a different error: "PNG file corrupted by ASCII conversion"

    char* sBuffer{};
    if(oRead.is_open()) {
        char* sBuffer;
        oRead >> std::noskipws;
        oRead.seekg(0, oRead.end);
        int iLength = oRead.tellg();
        sBuffer = new char[iLength];
        oRead.seekg(0, oRead.beg);
        oRead.read(sBuffer, iLength);
        oRead.close();
        delete sBuffer;
        sBuffer = nullptr;
    }else {
        throw cLog("\n\n[ERROR] Wrong dir passed.\n\nDIR: %s\n\n", sDir);
    }

    SDL_RWops* aRW{};
    aRW = SDL_RWFromConstMem(sBuffer, sizeof(sBuffer));

I'm trying to put all the assets inside a binary file and then trying to read them from the file itself, without scattering all them around in separate folders to let them exposed. What I'm trying to achieve is something like the way that RPG Maker and Wolf Editor use to join all the images / sounds of the game into a single customized format file (e.g. Data.wolf).

It's a very simple security's method but is enough for what I'm planning.

For now, I have the following code for reading a PNG file inside the binary, that always prints an error's message when it trys to create a new surface from IMG_LoadPNG_RW, saying "Error reading the PNG file."

tString sResult{};
cFile_Manager::tRead_File oRead(sDir, std::ios::binary);

if(oRead.is_open()) {
    tString sLine{};
    while(getline(oRead, sLine)) sResult += sLine + "\n";
    oRead.close();
}else {
    throw cLog("\n\n[ERROR] Wrong dir passed.\n\nDIR: %s\n\n", sDir);
}

SDL_RWops* aRW{};
aRW = SDL_RWFromConstMem(&sResult, sizeof(sResult));

if(!aRW) // Error message

SDL_Surface* aSurface{};
aSurface = IMG_LoadPNG_RW(aRW);

if(!aSurface) // This is where SDL shows "Error reading the PNG file."

EDIT: It keeps giving the same error.

    if(oRead.is_open()) {
        char* sBuffer;
        oRead >> std::noskipws;
        oRead.seekg(0, oRead.end);
        int iLength = oRead.tellg();
        sBuffer = new char[iLength];
        oRead.seekg(0, oRead.beg);
        oRead.read(sBuffer, iLength);
        oRead.close();
        delete sBuffer;
        sBuffer = nullptr;
    }else {
        throw cLog("\n\n[ERROR] Wrong dir passed.\n\nDIR: %s\n\n", sDir);
    }

I'm trying to put all the assets inside a binary file and then trying to read them from the file itself, without scattering all them around in separate folders to let them exposed. What I'm trying to achieve is something like the way that RPG Maker and Wolf Editor use to join all the images / sounds of the game into a single customized format file (e.g. Data.wolf).

It's a very simple security's method but is enough for what I'm planning.

For now, I have the following code for reading a PNG file inside the binary, that always prints an error's message when it trys to create a new surface from IMG_LoadPNG_RW, saying "Error reading the PNG file."

tString sResult{};
cFile_Manager::tRead_File oRead(sDir, std::ios::binary);

if(oRead.is_open()) {
    tString sLine{};
    while(getline(oRead, sLine)) sResult += sLine + "\n";
    oRead.close();
}else {
    throw cLog("\n\n[ERROR] Wrong dir passed.\n\nDIR: %s\n\n", sDir);
}

SDL_RWops* aRW{};
aRW = SDL_RWFromConstMem(&sResult, sizeof(sResult));

if(!aRW) // Error message

SDL_Surface* aSurface{};
aSurface = IMG_LoadPNG_RW(aRW);

if(!aSurface) // This is where SDL shows "Error reading the PNG file."

EDIT: Omg I made some mistakes, now this is the correctly code. Now it gives a different error: "PNG file corrupted by ASCII conversion"

    char* sBuffer{};
    if(oRead.is_open()) {
        oRead >> std::noskipws;
        oRead.seekg(0, oRead.end);
        int iLength = oRead.tellg();
        sBuffer = new char[iLength];
        oRead.seekg(0, oRead.beg);
        oRead.read(sBuffer, iLength);
        oRead.close();
    }else {
        throw cLog("\n\n[ERROR] Wrong dir passed.\n\nDIR: %s\n\n", sDir);
    }

    SDL_RWops* aRW{};
    aRW = SDL_RWFromConstMem(sBuffer, sizeof(sBuffer));
added 538 characters in body
Source Link
Loading
Source Link
Loading