简体   繁体   中英

How do you retrieve the colour space with CImg of an image loaded with CImg?

I am loading an image with CIMG, like so:

CImg<unsigned char> image("lena.png");

How do I know the eg four channels of that image represent RGBA colours and not CMYK? I know CImg<>::spectrum() gives me the amount of channels, but even the documentation says "The meaning of the pixel values is not linked up to the number of channels (eg a 4-channel image may indifferently stands for a RGBA or CMYK color image)" .

I have searched the documentation and tutorials, but there seems to be no function to tell me the meaning of the channel data.

So, how do I know kind of data I'm working with? Can I just assume all newly loaded image are in either RGB or RGBA?

If your image is in PNG format, it is necessarily RGB or RGBA because PNG doesn't support CMYK.

If it is JPEG, I would assume it is in sRGB colourspace, but you could check with ImageMagick first, using:

identify -format "%[colorspace]" YourImage.xxx

I generated a CMYK image using Imagemagick and loaded it using CImg . It appears to perform no transformation whatsoever as a result of the colorspace and merely returns the first byte of each pixel as red (though it is cyan in reality), the second as green (though it is magenta) and so on...

I also compiled CImg with debugging info, with cimg_use_jpeg defined as 1 (so it uses libjpeg ) and stepped through it. It could see in the jpeg_info_struct in function _load_jpeg that the image is CMYK, but it ignores this info - I guess you could patch CImg and modify a global variable quite readily to store the colourspace - if you like hacking about! Or you could request David Tschumperlé to do so via the CImg GitHub page which he does respond to...

在此处输入图片说明


Here is a possible work-around. If you don't build with cimg_use_jpeg , or cimg_use_png , or cimg_use_tiff , CImg will delegate reading files to ImageMagick 's command-line tool called convert . It will then convert your image to PNM format (in a pipe, not on disk) and CImg will read the data from the pipe. Crucially, ImageMagick always converts images being written to PNM format into sRGB colourspace, so if you have ImageMagick convert your images (by not using CImg 's built-in libraries), your images will always be read in sRGB colourspace. Essentially, CImg does this to read files when delegating to ImageMagick :

convert input.jpg pnm:-

and reads the output.

You will note that your 4-channel (previously CMYK) image suddenly becomes a 3-channel (now sRGB) image when CImg has delegated to ImageMagick because the output from ImageMagick is a, necessarily 3-channel, PNM file.


If you want to go even further...

If you set an environment variable, you can control where CImg looks for convert , so you could point it to your own "wrapper" around the real ImageMagick convert .

So, in concrete terms, if you do:

export cimg_convert_path="/Users/YourUser/bin/convertWrapper"

before running anything with CImg in it, or at the start of your program, with:

setenv("cimg_convert_path","/Users/YourUser/bin/convertWrapper",1);

CImg will run the following command to load your files:

/Users/YourUser/bin/convertWrapper input.jpg pnm:-

So, I am suggesting that in that wrapper file, you could modify the parameters passed to convert so it actually does something else:

convert input.jpg -auto-level pnm:-

So, convertWrapper would contain:

#!/bin/bash

# Pick up arguments and put in array
args=( "$@" )

# Remove last argument ("pnm:-")
unset 'args[${#args[@]}-1]'

# Add our "-auto-level" parameter in before last item
args+=("-auto-level")

# Re-append "pnm:-"
args+=("pnm:-")

# Debug output to /tmp/a
echo "BEFORE: $@"         >> /tmp/a
echo "AFTER:  ${args[@]}" >> /tmp/a

# Call real "convert" with modified parameters
/usr/local/bin/magick "${args[@]}"

This approach means you don't need to modify CImg .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM