Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

raising error on warnings #21

Open
bhenderson opened this Issue · 9 comments

3 participants

@bhenderson

https://f.cloud.github.com/assets/26167/962446/027b0198-04f0-11e3-9c07-3bae19ce7608.jpg

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

https://github.com/seattlerb/image_science/blob/master/lib/image_science.rb#L176

it works.

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

Source/LibJPEG/jdmarker.c:946
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.

@zenspider
Owner

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

@zenspider
Owner

I've looked at the code and it is doing exactly what you're saying... I've filed http://sourceforge.net/p/freeimage/support-requests/31/ as a result. Not sure at this point if I have any control on how FreeImage wires up to LibJPEG.

@bhenderson
@zenspider
Owner
@zenspider zenspider was assigned
@bhenderson

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

@zenspider
Owner

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) {
        rb_raise(rb_eRuntimeError,
                 "FreeImage exception for type %s: %s",
                  (fif == FIF_UNKNOWN) ? "???" : FreeImage_GetFormatFromFIF(fif),
                  message);
      }

      FreeImage_SetOutputMessage(FreeImageErrorHandler);

      VALUE with_image(char * input) {
        FREE_IMAGE_FORMAT fif = FIF_UNKNOWN;
        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.

@nocode

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

@zenspider
Owner

#23 applied.

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

@zenspider
Owner

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.