[sane-devel] [PATCH 3/3] Avoid attaching grayscale ICC profiles to rgb images, or vice-versa.

Aaron Muir Hamilton aaron at correspondwith.me
Mon May 1 22:29:20 UTC 2017


> Olaf Meeuwissen writes:
> Hi Aaron,
>
> I'm working on your ICC profile patches but this one made me wonder what
> happens with monochrome scans, i.e. grayscale with a bit depth of one.
Unfortunately, 1-bit images are not supported by genesys, and I don't
think I have any other scanners set up to try this with.

Maybe somebody else could test it with another scanner.
I don't personally work with Grayscale profiles at all, but if you need
a profile to test with, there are downloads on this page:
https://www.idealliance.org/gracol/
Any one of these profiles should be fine to determine if 1-bit images
with Grayscale profiles work in libpng.


I would say that it's probably safe though, because libpng only
complains if the type is mismatched. 1-bit (as well as 2-, 4-, 8-, and
16-bit) are considered to be PNG_COLOR_TYPE_GRAY.

I think that since it works correctly with 8- and 16-bit modes
correctly, it should also work with the other libpng grayscale modes.
> If that works fine, I'll push your patches with two fixes after merging
> against master.
>
> Aaron Muir Hamilton writes:
>
> >   If the ICC profile added does not match the colour format of the
> >   image, libpng will abort. This can leave the scanner motors in an
> >   incorrect state and possibly cause permanent damage.
> > ---
> >  frontend/scanimage.c | 22 +++++++++++++++++++++-
> >  1 file changed, 21 insertions(+), 1 deletion(-)
> >
> > diff --git a/frontend/scanimage.c b/frontend/scanimage.c
> > index 0f71422c..74546763 100644
> > --- a/frontend/scanimage.c
> > +++ b/frontend/scanimage.c
> > @@ -1208,7 +1208,27 @@ write_png_header (SANE_Frame format, int width, int height, int depth, const cha
> >        icc_buffer = sanei_load_icc_profile(icc_profile, &icc_size);
> >        if (icc_size > 0)
> >          {
> > -	  png_set_iCCP(*png_ptr, *info_ptr, basename(icc_profile), PNG_COMPRESSION_TYPE_BASE, icc_buffer, icc_size);
> > +	  /* libpng will abort if the profile and image colour spaces do not match*/
> > +	  /* The data colour space field is at bytes 16 to 20 in an ICC profile */
> > +	  /* see: ICC.1:2010 § 7.2.6 */
> > +	  int is_gray_profile = strncmp(icc_buffer + 16, "GRAY", 4) == 0;
> > +	  int is_rgb_profile = strncmp(icc_buffer + 16, "RGB ", 4) == 0;
> > +	  if ((is_gray_profile && color_type == PNG_COLOR_TYPE_GRAY) ||
> > +	      (is_rgb_profile && color_type == PNG_COLOR_TYPE_RGB))
> > +	    {
> > +	      png_set_iCCP(*png_ptr, *info_ptr, basename(icc_profile), PNG_COMPRESSION_TYPE_BASE, icc_buffer, icc_size);
> > +	    }
> > +	  else
> > +	    {
> > +	      if (is_gray_profile)
> > +	        {
> > +		  fprintf(stderr, "Ignoring 'GRAY' space ICC profile because the image is RGB.\n");
> > +	        }
> > +	      if (is_rgb_profile)
> > +	        {
> > +		  fprintf(stderr, "Ignoring 'RGB ' space ICC profile because the image is Grayscale.\n");
> > +		}
> > +	    }
> >  	  free(icc_buffer);
> >  	}
> >      }
> > --
> > 2.12.2
>
> --
> Olaf Meeuwissen, LPIC-2            FSF Associate Member since 2004-01-27
>  GnuPG key: F84A2DD9/B3C0 2F47 EA19 64F4 9F13  F43E B8A4 A88A F84A 2DD9
>  Support Free Software                        https://my.fsf.org/donate
>  Join the Free Software Foundation              https://my.fsf.org/join



More information about the sane-devel mailing list