Search code examples
c++imageimagemagickppm

How to make image magic convert output ppm to stdout


I am writing some image processing routines. My image class supports reading and writing images in the ppm (P6) format. To use other types of images I wanted to convert them to a ppm through image magic convert. To do this I need convert to write the contents of the ppm to stdout since I am reading them with a pipe (note that creating temporary files in not acceptable).

Right now I have this code.

void Image::read(const char* file)
{
    std::string cmd = std::string("convert \"") + file + std::string("\" -depth 16 -format ppm /dev/stdout");

    std::cout << cmd << std::endl;

    FILE* f = popen(cmd.c_str(), "r");
    img_assert(f, "Could not start conversion process.");

    try
    {
        read_ppm(f);
    }
    catch(...)
    {
        pclose(f);
        img_assert(false, "Conversion failed.");
    }

    pclose(f);
}

The command the popen is using is as follows. convert "/home/chase/Desktop/m27.jpg" -depth 16 -format ppm /dev/stdout

When I run this in the terminal I am not getting proper PPM output since the first line of the files does not start with P6. What would be the proper command line to do this?


Solution

  • You probably want:

    convert input.jpg -depth 16 ppm:
    

    which will give you P6 binary data, or

    convert input.jpg -depth 16 -compress none ppm:
    

    which will give you P3 ASCII data