Using Boost::GIL library with Openjpeg library to convert any common format to jpeg2000

344 views Asked by At

I'm new to image processing. I was able to use the Boost Generic Image Library (Boost::GIL) to convert between common formats such as Bitmaps, JPEG, PNG, and TIFF. Now, I want to use the openjpeg library to convert any common format to jpeg2000.

Below is my image wrapper class. The boost::gil::rgb8_image_t variable contains image information such as width, height, number of channels, pixels, etc.

class image_wrapper {

private:
    boost::gil::rgb8_image_t _img;

public:
    image_wrapper() = default;
    enum class image_type : uint8_t { JPEG = 1, BMP = 2, TIFF = 3, PNG = 4, JPEG2000 = 5 };

    void read_in_image(const std::string& filename);
    void read_in_image(std::vector<uint8_t>& bytes);
    void write_out_image(const std::string& file_name, image_type img_type);
};

I want to use the unencoded image data (pixel map) from the boost::gil::rgb8_image_t variable as the intermediate format to convert any common format to jpeg2000. The pixel map is stored in a 1D uint8_t vector. I want to store that vector into an openjpeg (opj_image_t) object.

By looking at the openjpeg source code, there's a function to convert an array of bitmap data to an opj_image_t object. How could I do the same thing to convert boost::gil::rgb8_image_t to opj_image_t?

Here is the code from the openjpeg library:

static void bmp24toimage(const OPJ_UINT8* pData, OPJ_UINT32 stride, opj_image_t* image){
int index;
OPJ_UINT32 width, height;
OPJ_UINT32 x, y;
const OPJ_UINT8* pSrc = NULL;

width = image->comps[0].w;
height = image->comps[0].h;
index = 0;
pSrc = pData + (height - 1U) * stride;
for (y = 0; y < height; y++) {
    for (x = 0; x < width; x++) {
        image->comps[0].data[index] = (OPJ_INT32)pSrc[3 * x + 2]; /* R */
        image->comps[1].data[index] = (OPJ_INT32)pSrc[3 * x + 1]; /* G */
        image->comps[2].data[index] = (OPJ_INT32)pSrc[3 * x + 0]; /* B */
        index++;
    }
    pSrc -= stride;
}

Link to the openjpeg file that contains the code: https://github.com/uclouvain/openjpeg/blob/master/src/bin/jp2/convertbmp.c

0

There are 0 answers