Skip to content


Subversion checkout URL

You can clone with
Download ZIP


raising error on warnings #21

bhenderson opened this Issue · 9 comments

3 participants


When trying to process this image, we get an error raised:

RuntimeError: FreeImage exception for type JPEG: Corrupt JPEG data: 49 extraneous bytes before marker 0xd9

if I comment out this line

it works.

I don't know how to link to the FreeImage source, but it looks like that message is coming from here:

WARNMS2(cinfo, JWRN_EXTRANEOUS_DATA, cinfo->marker->discarded_bytes, c);

which looks like it should be a warning and not an error.
do you have any thoughts? I'm still trying to find documentation for FreeImage_SetOutputMessage.


I cannot believe you sent me a picture full of dicks. How unprofessional.


I've looked at the code and it is doing exactly what you're saying... I've filed as a result. Not sure at this point if I have any control on how FreeImage wires up to LibJPEG.

@zenspider zenspider was assigned

there was a response on sourceforge. I'm not sure how to fix it though.


I've added my response to their rejection and it is awaiting moderation since I'm now anonymous (I no longer have an account because it was generating spam years ago).

Here is my response:

That design doesn't make a lick of sense and it breaks the contract described in the documentation.

Here's the code I have:

      void FreeImageErrorHandler(FREE_IMAGE_FORMAT fif, const char *message) {
                 "FreeImage exception for type %s: %s",
                  (fif == FIF_UNKNOWN) ? "???" : FreeImage_GetFormatFromFIF(fif),


      VALUE with_image(char * input) {
        int flags;

        fif = FreeImage_GetFileType(input, 0);
        if (fif == FIF_UNKNOWN) fif = FreeImage_GetFIFFromFilename(input);
        if ((fif != FIF_UNKNOWN) && FreeImage_FIFSupportsReading(fif)) {
          FIBITMAP *bitmap;
          VALUE result = Qnil;
          flags = fif == FIF_JPEG ? JPEG_ACCURATE : 0;
          if ((bitmap = FreeImage_Load(fif, input, flags))) {
            bitmap = ReOrient(bitmap);
            result = wrap_and_yield(bitmap, self, fif);
          return result;
        rb_raise(rb_eTypeError, "Unknown file format");
        return Qnil;

As you describe, FreeImage_SetOutputMessage is used for BOTH warnings and errors (despite what the documentation says) but I'm supposed to check whether or not I get a valid pointer from FreeImage_Load, but that pointer isn't provided or available at the time the callback is triggered.

So, the problem might be with my code... but it is also with the documentation and the design of FreeImage. Either only errors should go to the registered error handler, or the documentation needs to be clear AND there needs to be more information passed to the error handler so it can do its job properly.

Just to save some time on the back and forth... Having the error handler set some global state is NOT an acceptable workaround. This code can be called from a web service and multiple calls into with_image can be occuring at any given time.


Note: raising here is dangerous as it can leak memory.
Proposed patch in #23


#23 applied.

I still don't have a good solution for the OP other than "rescue and hope for the best".


Unless someone has a suggestion for an alternative library that isn't imagemagick. :D

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.