SQLite, C++ reading and writing blobs for graphic data

0

I am working on a game engine and have run into an issue that has been stumping me for the last day and a half. I use stb to load graphics data into an unsigned char *. I can see the data being written to the SQLite database, with binary data for the blob, but when i pull the data back i am not able to get the images, i get garbage pixel data for the objects on screen.

EDIT: I am now able to get part of the images to show on retreive, but later on in the run i get a segfault i haven't tracked down yet.

INSERT CODE

 int req_format = STBI_rgb_alpha;
 int width, height, orig_format;
 stringstream ss;
 data = stbi_load(resourcePath.c_str(), &width, &height, &orig_format, req_format);    
        sqlite3_stmt* stmt;
        ss.str("");
        ss << "insert into textures(data_name, width, height, format, req_format, data) values (" 
                << "'" << resourcePath.c_str() << "',"
                << "'" << width << "',"
                << "'" << height << "',"
                << "'" << orig_format << "',"
                << "'" << req_format << "',"
                << "?)";
        sqlite3_prepare_v2(db_connection, ss.str().c_str(), -1, &stmt, nullptr);
        sqlite3_bind_blob(stmt, 1, data, (width * height) * orig_format, SQLITE_STATIC );
        
        if (sqlite3_step(stmt) == SQLITE_DONE)
            std::cout << "Insert successful" << std::endl;
        else
            std::cout << "Insert failed" << std::endl;
                
        sqlite3_finalize(stmt);

RETRIEVE CODE

    string resourcePath = getResourceBasePath() + getPath();
    sqlite3* db_connection;
    int rc = sqlite3_open("/home/nick/Development/git/beast_hacker/resources/sqlite.db", &db_connection);    
    stringstream ss;
    vector<unsigned char> blobData;
    const unsigned char * pdata = reinterpret_cast<const unsigned char*>(sqlite3_column_blob(stmtR, 0));
        
     width = sqlite3_column_int(stmtR, 1);
     height = sqlite3_column_int(stmtR, 2);
     orig_format = sqlite3_column_int(stmtR, 3);
     req_format = sqlite3_column_int(stmtR, 4);

     blobData.resize((width * height) * orig_format);
     copy(pdata, pdata + static_cast<int>(blobData.size()), blobData.data());
     data = blobData.data();

At this point, data is no longer nullptr, but doesn't display as expected. data is an unsigned char * that stores the image data from stbi_load(). It runs well the first time, showing graphics and storing itself into the sqlite.db, but on second run, i get garbage pixel data, in the correct size and shape for the given image.

EDIT: Partial images are now viewable, however i am not sure where to go from here.

HOW I"M USING DATA

// Set up the pixel format color masks for RGB(A) byte arrays.
    // Only STBI_rgb (3) and STBI_rgb_alpha (4) are supported here!
    Uint32 rmask, gmask, bmask, amask;
    #if SDL_BYTEORDER == SDL_BIG_ENDIAN
      int shift = (req_format == STBI_rgb) ? 8 : 0;
      rmask = 0xff000000 >> shift;
      gmask = 0x00ff0000 >> shift;
      bmask = 0x0000ff00 >> shift;
      amask = 0x000000ff >> shift;
    #else // little endian, like x86
      rmask = 0x000000ff;
      gmask = 0x0000ff00;
      bmask = 0x00ff0000;
      amask = (req_format == STBI_rgb) ? 0 : 0xff000000;
    #endif

    int depth, pitch;
    if (req_format == STBI_rgb) {
      depth = 24;
      pitch = 3*width; // 3 bytes per pixel * pixels per row
    } else { // STBI_rgb_alpha (RGBA)
      depth = 32;
      pitch = 4*width;
    }

    surface = SDL_CreateRGBSurfaceFrom((void*)data, width, height, depth, pitch,
                                                 rmask, gmask, bmask, amask);

Any suggestions on what might be the problem? I can store the given binary data using streams, both read and write. I'm just stumped on how to get the blob data out of SQLite and correctly processed into an unsigned char *.

c++
sqlite
binary
blob
sdl-2
asked on Stack Overflow Feb 20, 2021 by Nick • edited Feb 21, 2021 by Nick

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0