0

I can't seem to find an example of the proper syntax/method for initializing an ImageMagick Magick++ object from an iostream stream_buffer in C++.

I'm trying to use the result from an aws sdk getObject which seems to return a stream buffer to push into ImageMagick to create thumbnails via lambda on demand.

example of the relevant code from the aws-sdk-cpp I'm using to retrieve the object:

auto get_object_outcome = s3_client.GetObject(object_request);
if (get_object_outcome.IsSuccess())
{
    // Get an Aws::IOStream reference to the retrieved file
    auto &retrieved_file = get_object_outcome.GetResultWithOwnership().GetBody();

    // read the object's contents and write to a file
    std::ofstream output_file(file_name, std::ios::binary);
    output_file << retrieved_file.rdbuf();
    return true;
}
else
{
    auto error = get_object_outcome.GetError();
    std::cout << "ERROR: " << error.GetExceptionName() << ": "
              << error.GetMessage() << std::endl;
    return false;
}

Any help is appreciated - new to c++ so I'm not yet versed in converting more advanced data formats such as streams/blobs/buffers.

Scott
  • 7,983
  • 2
  • 26
  • 41

1 Answers1

0

I try taking your retrieved_file, copy it into a std::vector, create an magick blob, create an image from the blob:

// create an empty buffer        
std::vector<char> buffer; 

// file your buffer with the retrieved file
std::copy(istream_iterator(retrieved_file), istream_iterator(), std::back_inserter(buffer));

// create a Magick++ blob with your data
Blob my_blob(buffer.data(), buffer.size());

// create a Magick++ image from your blob
Image image_from_blob(my_blob);
Markus Schumann
  • 7,636
  • 1
  • 21
  • 27
  • I tried a bunch of ways to try to create a blob but wasn't familiar enough with working in c++ with buffers to know how. Thanks, I'll give this a try shortly. Fighting with a Magick++ dependency at the moment. – Scott Sep 06 '19 at 17:05
  • that syntax is complaining on the std::copy with: "Use of class template 'istream_iterator' requires template arguments" – Scott Sep 06 '19 at 17:11
  • I saw examples inserting the after each istream_iterator and it got rid of the warning, but the data seems to be corrupted somehow. I set buffer.size() to a size_t variable to make sure it had content and it does, but it complains about corrupt jpeg, "Bogus DQT index 5". I confirmed the data is good by using the save-to-file method first. – Scott Sep 06 '19 at 17:33