"GRAY"-images are not always interpreted as RAW

Post any defects you find in the released or beta versions of the ImageMagick software here. Include the ImageMagick version, OS, and any command-line required to reproduce the problem. Got a patch for a bug? Post it here.
Post Reply
leitgeb

"GRAY"-images are not always interpreted as RAW

Post by leitgeb »

We have a problem with loading RAW images into the ImageMagick++ engine.
A RAW image in our application is an array of 8 bit values (interpreted as grey scale intensity),
to which we have to supply image width, image height and BPP.

Here's our code:

int Width = JPEGImage.getWidth();
int Height = JPEGImage.getHeight();
int Components = JPEGImage.getComponents();
int Length = JPEGImage.getWidth() * JPEGImage.getHeight() * JPEGImage.getComponents();
Magick::Blob MyBlob(JPEGImage.releaseRAW(),Length);
Magick::Image MyImage(MyBlob,Magick::Geometry(Width,Height),8,"GRAY");

JPEGImage is a well tested internal class in our project, the method releaseRAW provides a char * pointer to RAW image data.
For most images this works fine, however, if the first two bytes of image data are "BA", the Magick::Image constructor throws an exception:
terminate called after throwing an instance of 'Magick::ErrorCorruptImage'
what(): ImageMagick: Improper image header `' @ bmp.c/ReadBMPImage/593
Aborted

This is very frustrating because the code explicitly tells ImageMagick++ to handle this data as GRAY, not as BMP data :(
User avatar
magick
Site Admin
Posts: 11064
Joined: 2003-05-31T11:32:55-07:00

Re: "GRAY"-images are not always interpreted as RAW

Post by magick »

Add
  • fileName( magick_ + ':');
to the two read(..., onst std::string &magick_) methods in Image.cpp to tell ImageMagick that the "GRAY" format is explicit rather than implicit. We made the change to the ImageMagick Subversion trunk already-- available sometime tomorrow.
leitgeb

Re: "GRAY"-images are not always interpreted as RAW

Post by leitgeb »

I just tried this - same result :(

Here are my updated methods in Magick::Image::Image. I also tried putting the fileName call to the beginning of the read methods - same result. Did I do something wrong?


// Read image of specified size, depth, and format from in-memory BLOB
void Magick::Image::read ( const Blob &blob_,
const Geometry &size_,
const unsigned int depth_,
const std::string &magick_ )
{
// Set image size
size( size_ );
// Set image depth
depth( depth_ );
// Set image magick
magick( magick_ );
// do what the guy said online :-)
fileName( magick_ + ':');
// Read from Blob
read( blob_ );
}

// Read image of specified size, and format from in-memory BLOB
void Magick::Image::read ( const Blob &blob_,
const Geometry &size_,
const std::string &magick_ )
{
// Set image size
size( size_ );
// Set image magick
magick( magick_ );
// do what the guy said online :-)
fileName( magick_ + ':');
// Read from Blob
read( blob_ );
}
User avatar
magick
Site Admin
Posts: 11064
Joined: 2003-05-31T11:32:55-07:00

Re: "GRAY"-images are not always interpreted as RAW

Post by magick »

Does this code work for you? Without the patch we posted, it throws an exception. We're using ImageMagick 6.4.7-8.

Code: Select all

#include <Magick++.h>
#include <string>
#include <iostream>
#include<stdio.h>
using namespace std;
using namespace Magick;

int main(){
Image m_pImage;
for (int i=0; i < 1; i++)
{
  try {
    int Width = 10;
    int Height = 10;
    unsigned char blob[100];
    blob[0]='B';
    blob[1]='A';
    Magick::Blob MyBlob(blob,Width*Height);
    Magick::Image MyImage(MyBlob,Magick::Geometry(Width,Height),8,"GRAY");
  }
  catch( Exception &error_ )
    {
      cout << "Caught exception: " << error_.what() << endl;
    }
  catch( exception &error_ )
    {
      cout << "Caught exception: " << error_.what() << endl;
    }
}
return EXIT_SUCCESS;
}
leitgeb

Re: "GRAY"-images are not always interpreted as RAW

Post by leitgeb »

A quick and important update: Yesterday I proclaimed that the patch did no good: THIS WAS WRONG!
I redid the whole thing and now it works. Must have miscompiled or poorly installed ImageMagick :oops: :oops: :oops:

In other words: putting in this fileName( magick_ + ':') works great!

Sorry for the confusion, thanks for your help!
Post Reply