Reducing images with bilinear filtering

  • Hi,

    I'm using DevIL to load images from memory and generate Jpeg thumbnails to memory. I'm quite happy because it's easy to use.

    But DevIL won't apply antialiasing filtering whatever filter I use: nearest, linear, bilinear.

    I've found this thread but it was two years ago:

    An excerpt from my program:

        iluImageParameter(ILU_FILTER, ILU_BILINEAR);
        if (iluScale(width_thumbnail, height_thumbnail, 1) == IL_FALSE) {


    • Denton Woods
      Denton Woods


      I'm glad that you like DevIL, and I'm trying to improve it constantly.  I took a look at the bilinear code, and it was broken.  It was years ago when it was written, and it was uncommented.  So I just decided to rewrite it from scratch, and it looks *lots* better now.  The fix will be in Subversion tomorrow when I finish it.  The linear interpolation code may have to be rewritten, and I will take a look at the more complex forms later.

    • I've installed DevIL 1.7.8 but I don't see progress in bilinear filtering.

      For the record, I'm trying to scale down an image to make thumbnails. I'm still using the excerpt shown above.

      Here is my test image:

      The result from DevIL:

      And the result from ImageMagick:

      This is the result I am expecting. The diagonals are smooth and the file is 20% smaller.

      But ImageMagick's API and documentation are horrible. Please do something similar with DevIL, thanks.

    • Maybe I'm not using DevIL correctly? Here is my source code:

      • Denton Woods
        Denton Woods

        I just tried the equivalent to your code but did not save it as a .jpg.  When I did, the image was similar to the ImageMagick output.  I noticed that you are setting the quality to 80.  What if you set it to 100?  The file will of course be larger, but maybe we can rule out iluScale as a source of the error.

    • I've changed the quality to 100 but it was visually identical.

      Just to check it took the change in count, I've opened the thumbnail and the one generated in gimp. Here is a summary of the differences as seen in the "Save As" dialog:

      DevIL thumbnail:
      - Quality 99%
      - Subsampling 2x2, 1x1, 1x1 (smallest file)

      ImageMagick thumbnail:
      - Quality 80%
      - Subsampling 1x1, 1x1, 1x1 (best quality)

      I couldn't find the DCT method used.

      Quite puzzling that IM is using the best quality method but produces the smallest file.

      Then I've saved the thumbnail as a PNG image and, while the result is appealing, it doesn't have the bilinear filtering I was expecting. I've uploaded the result:

      I've also tested with a PNG from end to end: (it's a gallery). You can see that the text of the thumbnail is aliased. ImageMagick renders a smooth text (though the image is a bit too blurry) .

      And for completing the experiment:

      $ pkg-config --modversion IL

      So I guess the bilinear filtering is broken or something. Or maybe ImageMagick uses black magick. ;-)

      Thanks for taking a look.

    • nitro

      I ran into the same issue today (Devil 1.7.8). In certain situations I have to generate mip maps manually and I do this using iluScale.
      However, no matter if use this code

      iluImageParameter(ILU_FILTER, ILU_BILINEAR);
      iluScale(newSize.x, newSize.y, 1);           

      or use IL_LINEAR or IL_FILTER_LANCZOS3 or any other filter, the resulting image looks always the same. It looks almost as if it was always nearest-filtered or something.
      The resulting mip maps are of such a bad quality, that they are completely unusable (e.g. a grey checkern pattern turns black at a distance!)

    • So as mentioned in  none of the advanced filters will work when you are down sampling an image.

      See iluScale around line 57.
      They all fall through to bilinear. Unfortunately bilinear filtering is behaving in a degenerate manor when you hand it a value <= .5)  iluScale2DBilinear_ calculates the fractional value as fracX = 0, fracY = 0 effectively only using one pixel, what it should be is averaging all four together. Adding a hack to override the frac values to be .5 if you are scaling down by 1/2 improves the quality of mipmaps a ton.

      Really DevIL should have a specialized pipeline for mipmaps that probably provides the option to use the NV texture tools, (much like the DDS save pipeline) they have lots of specialized mipmapping filters that would probably do a better job then the fixed bilinear code above.


  • Anonymous


    this exact problem is driving me crazy. i _really_ need to be able to generate high quality thumbnails. I don't entirely understand your hack gedalia, could you explain in more detail how i might pull this off? also does it only work when  shrinking to 50% ? i need something that works for arbitrary sizes (only needs to work when _reducing_ images, i dont need to enlarge an image)

    Also, another oddity, if im not mistaken the scaling algorithms appear to  generate thumbnails fine in linux using the debian devil package. That is the lanczos3 filter and bspline filters etc all appear to produce high quality thumbnails in linux. Only in linux though, in macosx (using macports) and windows all the thumbnails appear to be very low quality using the IL_NEAREST filter.

    can anyone help me with this? i really need to generate high quality thumbnails using DevIL, i do not want to go back to ImageMagick :/