1

I just started using CImg libraries and I would like to test it with raw data, but I am experiencing big problems to figure out why I cannot obtain correct results.

My RGB24 raw file format is RGB interleaved (RGBRGBRGB ...) while CImg stores data as explained here.

The (last) test code I wrote is this, where I collect all the possible 4! = 24 combination for permute_axes method

#ifndef _USE_MATH_DEFINES
#define _USE_MATH_DEFINES
#endif

#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif

#ifdef cimg_display
#undef cimg_display
#define cimg_display 0
#endif

#include "CImg.h"

#include <iostream>
#include <iomanip>
#include <sstream>
#include <cstdint>
#include <cstdlib>
#include <thread>
#include <vector>
#include <array>
#include <cmath>

#include <unistd.h>
#include <sys/file.h>
#include <fcntl.h>

using namespace std;
using namespace cimg_library;

#define MODE_700 (mode_t) S_IRWXU // 00700
#define MODE_400 (mode_t) S_IRUSR // 00400
#define MODE_200 (mode_t) S_IWUSR // 00200
#define MODE_100 (mode_t) S_IXUSR // 00100
#define MODE_070 (mode_t) S_IRWXG // 00070
#define MODE_040 (mode_t) S_IRGRP // 00040
#define MODE_020 (mode_t) S_IWGRP // 00020
#define MODE_010 (mode_t) S_IXGRP // 00010
#define MODE_007 (mode_t) S_IRWXO // 00007
#define MODE_004 (mode_t) S_IROTH // 00004
#define MODE_002 (mode_t) S_IWOTH // 00002
#define MODE_001 (mode_t) S_IXOTH // 00001

#define MODE_777 (mode_t) MODE_700 | MODE_070 | MODE_007

int main(int argc, const char *argv[]) 
{      
    //uint8_t buffer[960 * 1280 * 3];
    CImg <uint8_t> image(960, 1280, 1, 3);
    // CImg <float> gaussk(7,7,3); 
    uint8_t buffer[960 * 1280 * 3] = {0};
    float var = 1.5*1.5;
    // int32_t xtmp;
    // int32_t ytmp;
    int32_t fd = -1;

    stringstream sstr;  

    cout << "image size is: " << unsigned(image.size()) << endl;
    printf("opening %s\n", argv[1]);
    fd = open(argv[1], O_RDONLY);
    if(fd == -1)
    {
        perror("open: ");
        return -1;
    }

    cout << "Reading..." << endl;
    // read(fd, image.data(), image.size());
    read(fd, buffer, sizeof(buffer));
    close(fd);
    fd = -1;

    for(uint8_t i = 0; i < 24; i++)
    {
        sstr << "blurred_" << unsigned(i) << ".raw";
        image.assign(buffer, 960, 1280, 1, 3);

        switch(i)
        {
            case 0:
                image.permute_axes("xyzc");
                break;
            case 1:
                image.permute_axes("xycz");
                break;
            case 2:
                image.permute_axes("xzyc");
                break;
            case 3:
                image.permute_axes("xzcy");
                break;
            case 4:
                image.permute_axes("xcyz");
                break;
            case 5:
                image.permute_axes("xczy");
                break;
            case 6:
                image.permute_axes("yxzc");
                break;
            case 7:
                image.permute_axes("yxcz");
                break;
            case 8:
                image.permute_axes("yzxc");
                break;
            case 9:
                image.permute_axes("yzcx");
                break;
            case 10:
                image.permute_axes("ycxz");
                break;
            case 11:
                image.permute_axes("yczx");
                break;
            case 12:
                image.permute_axes("zxyc");
                break;
            case 13:
                image.permute_axes("zxcy");
                break;
            case 14:
                image.permute_axes("zyxc");
                break;
            case 15:
                image.permute_axes("zycx");
                break;
            case 16:
                image.permute_axes("zcxy");
                break;
            case 17:
                image.permute_axes("zcyx");
                break;
            case 18:
                image.permute_axes("cxyz");
                break;
            case 19:
                image.permute_axes("cxzy");
                break;
            case 20:
                image.permute_axes("cyxz");
                break;
            case 21:
                image.permute_axes("cyzx");
                break;
            case 22:
                image.permute_axes("czxy");
                break;
            case 23:
                image.permute_axes("czyx");
                break;
        }
        image.blur(2.5);
        // image.save("blurred.bmp");
        cout << "Writing " << sstr.str() << endl;
        fd = open(sstr.str().c_str(), O_RDWR | O_CREAT, MODE_777);
        write(fd, image.data(), image.size());
        close(fd);
        sstr.str("");
    }

    return 0;
}

But the output blurred frame is wrong, most of the times in grayscale.

According to the header file CImg.h this conversion should be possible, and it's what I understand also after reading this blog post.

Before I start to write my own function to try to fix this problem, what is the correct way to deal with raw file and to make such a conversion with CImg ?

1 Answers1

3

So you have interleaved data and want it non-interleaved to be able to let CImg functions work with it.

So why didn't you just do exactly what the blog post you referenced advised under IMPORTANT

CImg result(dataPointer, spectrum, width, height, depth, false);
result.permute_axes("yzcx");

for your code that would be

image.assign(buffer, 3, 960, 1280, 1); // note the 3 being first
image.permute_axes("yzcx");
image.blur(2.5);
// image.save("blurred.bmp");
//now convert back to interleaved
image.permute_axes("cxyz");
fd = open(sstr.str().c_str(), O_RDWR | O_CREAT, MODE_777);
write(fd, image.data(), image.size());
PeterT
  • 7,981
  • 1
  • 26
  • 34
  • You are right. I was reading back the link and I noticed that I was completely wrong in the assigment. It's time to relax a little... Thanks. –  Jan 12 '18 at 17:50