raising error on warnings #21

Closed
bhenderson opened this Issue Aug 14, 2013 · 10 comments

Projects

None yet

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
Member

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

@zenspider
Member

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

Thanks!

On Wednesday, August 14, 2013, Ryan Davis wrote:

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.


Reply to this email directly or view it on GitHubhttps://github.com/seattlerb/image_science/issues/21#issuecomment-22670160
.

@zenspider
Member

Since I'm just raising an exception, feel free to rescue it, look at the message and retailer if it isn't this one.

On Aug 14, 2013, at 15:40, Brian Henderson notifications@github.com wrote:

Thanks!

On Wednesday, August 14, 2013, Ryan Davis wrote:

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.

�\
Reply to this email directly or view it on GitHubhttps://github.com/seattlerb/image_science/issues/21#issuecomment-22670160
.


Reply to this email directly or view it on GitHub.

@zenspider zenspider was assigned Aug 19, 2013
@bhenderson

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

@zenspider
Member

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
nocode commented Nov 27, 2013

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

@zenspider
Member

#23 applied.

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

@zenspider
Member

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

@zenspider
Member

This should be fixed. Thanks!

@zenspider zenspider closed this Mar 30, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment