Re: [sane-devel] Editing Exposure on SANE Genesys

2017-10-16 Thread Aaron Muir Hamilton
Which part are you having trouble with? Do you know how the exposure
tables work, but don't know how to expose an option, or the other way
around?

Happy hacking.

-- 
sane-devel mailing list: sane-devel@lists.alioth.debian.org
http://lists.alioth.debian.org/cgi-bin/mailman/listinfo/sane-devel
Unsubscribe: Send mail with subject "unsubscribe your_password"
 to sane-devel-requ...@lists.alioth.debian.org


Re: [sane-devel] CanoScan LiDE 220 can't scan dpi over 1200

2017-10-07 Thread Aaron Muir Hamilton
Hmm, can confirm, it scans some portion of the image and then hangs. I
think the official software on Windows doesn't support full-page scans
at 2400dpi or greater. It does work if you limit the scan to an area.

Not sure what should be done about this, I don't think there's a
standard way to expose constraints like this to scanimage (but I'm not
sure!).

-- 
sane-devel mailing list: sane-devel@lists.alioth.debian.org
http://lists.alioth.debian.org/cgi-bin/mailman/listinfo/sane-devel
Unsubscribe: Send mail with subject "unsubscribe your_password"
 to sane-devel-requ...@lists.alioth.debian.org


Re: [sane-devel] [janitorial] Text file style convention "enforcement"

2017-05-31 Thread Aaron Muir Hamilton
Olaf Meeuwissen  writes:

> Hi all,
>
> It's been a while since my last janitorial.  Interest in contributing to
> the project comes in waves.  I've been preoccupied with the preparations
> for the 1.0.27 release.  During which, Aaron made a suggestion about how
> coding style conventions could help spot bugs[1].  I followed up with an
> "apologetic" reply[2] why I didn't want to enforce a *coding* style, but
> at the same time mentioned some, hopefully, non-controversial rules for
> all text files.
>
>  [1] https://lists.alioth.debian.org/pipermail/sane-devel/2017-May/035353.html
>  [2] https://lists.alioth.debian.org/pipermail/sane-devel/2017-May/035355.html
>
> Well, I applied those rules and pushed the result.  Here are the rules:
>
>  - all text files shall be UTF-8 encoded
>  - all lines shall end in non-whitespace
>  - all files shall end with a newline
>  - all files shall end with a non-empty line
>
> # I wondered what to do with leading empty lines but decided not to
> # touch these (for now).  They might serve some sort of "aesthetic"
> # purpose.  I also noticed a note on "follow the GNU coding standards"
> # in doc/backend-writing.txt (and shied away from that, for now?).  As
> # for the tab vs spaces mess?  Let's leave that for later.
>
> Cool.  Now what?  How does this affect you?  How do I "oblige" to this?
> How do you "enforce" this?
>
> First, the effect on people working with the SANE backend repository.
> Any of your changes may now cause conflict when you try to merge with,
> rebase on or apply patches to the `master` branch.  Most of the pain can
> be alleviated with the various `--ignore-*` flags to the command you use
> to do that.  The `git help` comman for your action of choice has details
> on how this works, as does the `patch` manual page.
>
> # If you use a GUI to do this, check its documentation.  There should be
> # an option to set those flags, on a one-time basis or as a preference.
> # If not, submit a feature request for that GUI.
>
> The "rules" are laid down in a `.editorconfig` file in the repository.
> If your editor or IDE is supported by EditorConfig, you're all set.  If
> your editor or IDE needs a plugin, do yourself a favour and install it.
> The EditorConfig[3] documentation has info on supported plugins[4].
> It's not difficult and works wonders.  Been there, done that myself and
> am now happily using the Emacs plugin and Emacs now makes sure I follow
> the "rules".
>
>  [3] http://editorconfig.org/
>  [4] http://editorconfig.org/#download
>
> If your editor or IDE is not supported by EditorConfig, do not despair.
> First submit an issue[5] asking for a "new plugin" (use the label!) ;-)
>
>  [5] https://github.com/editorconfig/editorconfig/issues/new
>
> While waiting for that plugin (or hacking up one yourself), you can use
> the `tools/style-check.sh` script to check files.  It also has a `--fix`
> option that will take care of the newline and whitespace issues.  Seeing
> the encoding cannot be guessing automagically, any compliance violations
> for the UTF-8 rule need to be handled manually.
There's also GNU indent, which defaults to GNU style these days, and
should be installed on most of our workstations (except perhaps on
OpenBSD or FreeBSD, but possibly there as well).

It might be interesting to ask people to run it on a file when they make
a considerable change and there are no known outstanding patches against
the file. Or even if there are outstanding patches which conflict with
style repairs, one can format the patch branch and it should apply.

Honestly my greatest concern is that the genesys_gl*.c per-controller
backends will drift further apart. The 800-series ones are still very
similar, but the 124[+] backend has some of the function definitions
shifted around in order, but otherwise nearly identical in function.
I'm trying to get my hands on a scanner matching each genesys controller
revision so that I can regression test anything I do in that realm.

Those files are largely identical, the functional differences are
limited to a few differing or new function definitions, and the magic
values used in given registers (though the register offsets remain
largely the same). 

The more the files drift apart, the harder it'll be to a) realize that
fixes should be ported between them and b) that they are substantially
similar, neigh on identical files.

Anyway, if all goes well I should be receiving four more genesys-based
scanners, including a couple not mentioned on the website. I'll also be
getting a whole stack of calibration targets so I can tell if something
has gone subtly wrong for a given mode on a given backend. :- )

Boy, that was a bit off-topic, do go on.

>
> Want to make this even simpler?  No sweat.  Just add the attachment to
> your `.git/hooks/pre-commit` file (or add it if you don't have one).
> Note that the file needs execute permissions.  A simple
>
>   chmod 0755 .git/hooks/pre-commit
>
> will do that trick.
>
> # For those o

Re: [sane-devel] Possible to rename network scanner?

2017-05-21 Thread Aaron Muir Hamilton

Some of these misleading indentation errors also look like genuine bugs.
Here's one gem from backends/genesys_gl847.c which turned into a
misleading indent when I ran it through clang-format:

660while (val8 & REG41_FEBUSY);
661  {
662usleep (1);
663status = sanei_genesys_get_status (dev, &val8);
664  };

Pay close attention to the semicolons.

As an aside, this is probably why the 1TBS is superiour to GNU-style.


Aaron Muir Hamilton  writes:

> Olaf Meeuwissen  writes:
>
>> Hi All(an),
>>
>> m. allan noah writes:
>>
>>> The fujitsu backend includes the serial number of the scanner in the
>>> device name. The users would have to memorize that number. Anything
>>> more would be a code change. The best place to make such a change
>>> would probably be in saned itself, so that all backends could benefit.
>>> Unfortunately, many backends use a dynamically generated device name,
>>> so saned might see different device names after the host reboots. So,
>>> the easiest place to solve this would likely be in the fujitsu backend
>>> itself. We could potentially replace the serial number with a
>>> user-specified device name. This requires more thought.
>>
>> I don't think this is something that should be addressed by a SANE
>> backend.  It should also not be addressed by a SANE frontend.  It should
>> be addressed by SANE, eh, "middleware".  Not unlike the sanei_* stuff
>> but not linked statically into whatever backend happens to use it.
>>
>> I've been doing Node.js web application/service development at the
>> office for the last year and in that field they also use the terms
>> frontend and backend.  At the same time, there's middleware all over
>> the place, to the point where there's middleware for middleware ;-), but
>> it all just gets sandwiched between the frontend and the backend.
>>
>> I realize that SANE "middleware" is not something we can deliver now or
>> anytime soon (seeing that the SANE 2 draft has been stuck so long that I
>> can't recall the year any more precisely than 200x) but it is something
>> that we may consider for discussion after 1.0.26 (or 1.0.27 ;-)
>>
>> # So many ideas, so little time ... but I might just start a SANE-R
>> # discussion after the release.  Solliciting ideas for the meaning of
>> # the R already!
>> # Then there's also the issue of Alioth -> GitLab.com that's been near
>> # the forefront of my mind for a while already and refuses to go away.
>> # Never even mind Fedora 26 and Debian 9 on the horizon which'll trigger
>> # a small "tsunami" of compiler warning fixes and autotools updates ...
>
> It's ugly work, but maybe it's time we normalize the formatting in some
> of the files (especially ones which are abandoned-ish and don't have
> open patches). A lot of the compiler warnings I see (I run Arch Linux,
> so I'm on bleeding-edge-ish GCC most of the time) are things like
> misleading indentation warnings. We don't need to invest in any
> infrastructure for formatting IMHO, we could just do a once-over with
> clang-format on some of the files.
>
> Just my $0.024298 CAD
>
>> # but that's all for after our upcoming sane-backends release.
>>
>> Just my two yen,
>> --
>> Olaf Meeuwissen, LPIC-2FSF Associate Member since 2004-01-27
>>  GnuPG key: F84A2DD9/B3C0 2F47 EA19 64F4 9F13  F43E B8A4 A88A F84A 2DD9
>>  Support Free Softwarehttps://my.fsf.org/donate
>>  Join the Free Software Foundation  https://my.fsf.org/join

-- 
sane-devel mailing list: sane-devel@lists.alioth.debian.org
http://lists.alioth.debian.org/cgi-bin/mailman/listinfo/sane-devel
Unsubscribe: Send mail with subject "unsubscribe your_password"
 to sane-devel-requ...@lists.alioth.debian.org


Re: [sane-devel] Xsane does not work any more with Lide 200 and Lide220 at Ubuntu16.04LTS

2017-05-10 Thread Aaron Muir Hamilton
> Yeah!
> But then we the solution?! Command line?
> It Xsane is not developed? 

XSane is a separate project. It's not part of sane-backends or
sane-frontends. I have no idea whether or not it's still developed.

-- 
sane-devel mailing list: sane-devel@lists.alioth.debian.org
http://lists.alioth.debian.org/cgi-bin/mailman/listinfo/sane-devel
Unsubscribe: Send mail with subject "unsubscribe your_password"
 to sane-devel-requ...@lists.alioth.debian.org


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

2017-05-01 Thread Aaron Muir Hamilton
I just tried it by hacking scanimage up a bit to threshhold and convert
the lines to monochrome right before writing the PNG, and change the PNG
header to always be 1-bit; libpng didn't complain about attaching a
grayscale profile to a 1-bit monochrome image.

-- 
sane-devel mailing list: sane-devel@lists.alioth.debian.org
http://lists.alioth.debian.org/cgi-bin/mailman/listinfo/sane-devel
Unsubscribe: Send mail with subject "unsubscribe your_password"
 to sane-devel-requ...@lists.alioth.debian.org


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

2017-05-01 Thread Aaron Muir Hamilton

> 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-2FSF Associate Member since 2004-01-27
>  GnuPG key: F84A2DD9/B3C0 2F47 EA19 64F4 9F13  F43E B8A4 A88A F84A 2DD9
>  Support Free Softwarehttps://my.fsf.org/donate
>  Join the Free Software Foundation  https://my.fsf.org/join

-- 
sane-devel mailing list: sane-devel@lists.alioth.debian.org
http://lists.alioth.debian.org/cgi-bin/mailman/listinfo/sane-devel
Unsubscribe: Send mail with subject "unsubscribe your_password"
 to sane-devel-requ...@lists.alioth.debian.org

[sane-devel] [PATCH] Write density (resolution) JFIF header information with JPEG files.

2017-05-01 Thread Aaron Muir Hamilton
---
 frontend/scanimage.c | 13 ++---
 1 file changed, 10 insertions(+), 3 deletions(-)

diff --git a/frontend/scanimage.c b/frontend/scanimage.c
index ffb9f105..e5562815 100644
--- a/frontend/scanimage.c
+++ b/frontend/scanimage.c
@@ -1213,7 +1213,7 @@ write_png_header (SANE_Frame format, int width, int 
height, int depth, int dpi,
 
 #ifdef HAVE_LIBJPEG
 static void
-write_jpeg_header (SANE_Frame format, int width, int height, FILE *ofp, struct 
jpeg_compress_struct *cinfo, struct jpeg_error_mgr *jerr)
+write_jpeg_header (SANE_Frame format, int width, int height, int dpi, FILE 
*ofp, struct jpeg_compress_struct *cinfo, struct jpeg_error_mgr *jerr)
 {
   cinfo->err = jpeg_std_error(jerr);
   jpeg_create_compress(cinfo);
@@ -1238,6 +1238,11 @@ write_jpeg_header (SANE_Frame format, int width, int 
height, FILE *ofp, struct j
 }
 
   jpeg_set_defaults(cinfo);
+  /* jpeg_set_defaults overrides density, be careful. */
+  cinfo->density_unit = 1;   /* Inches */
+  cinfo->X_density = cinfo->Y_density = dpi;
+  cinfo->write_JFIF_header = TRUE;
+
   jpeg_set_quality(cinfo, 75, TRUE);
   jpeg_start_compress(cinfo, TRUE);
 }
@@ -1393,7 +1398,8 @@ scan_it (FILE *ofp)
 #ifdef HAVE_LIBJPEG
  case OUTPUT_JPEG:
write_jpeg_header (parm.format, parm.pixels_per_line,
- parm.lines, ofp, &cinfo, &jerr);
+  parm.lines, resolution_value,
+  ofp, &cinfo, &jerr);
break;
 #endif
  }
@@ -1665,7 +1671,8 @@ scan_it (FILE *ofp)
 #ifdef HAVE_LIBJPEG
   case OUTPUT_JPEG:
write_jpeg_header (parm.format, parm.pixels_per_line,
-   parm.lines, ofp, &cinfo, &jerr);
+  parm.lines, resolution_value,
+  ofp, &cinfo, &jerr);
   break;
 #endif
   }
-- 
2.12.2


-- 
sane-devel mailing list: sane-devel@lists.alioth.debian.org
http://lists.alioth.debian.org/cgi-bin/mailman/listinfo/sane-devel
Unsubscribe: Send mail with subject "unsubscribe your_password"
 to sane-devel-requ...@lists.alioth.debian.org


Re: [sane-devel] [PATCH] Include physical resolution (pHYs section) in PNG output.

2017-04-28 Thread Aaron Muir Hamilton
Pleased to make your acquaintance, Olaf.

Thank you for reviewing the patches and taking them upstream, 
I have an equivalent patch for JPEG output on the boil.

Not to put you on the spot, but it seems like a lot of time has passed
since the last release was tagged, despite there apparently being quite
a bit of development since then.

I'm wondering who is empowered to tag a release; I think it would be a
good time to make one. 2015-10-04 was quite a while ago, and I know that
at least Archlinux has a downstream patch (authored in 2013, but not
included in 1.0.25) which seems to be integrated in master. [1]

[1]: 
https://git.archlinux.org/svntogit/packages.git/plain/trunk/segfault-avahi-fix-kodakio.patch?h=packages/sane

おつかれさま。

-- 
sane-devel mailing list: sane-devel@lists.alioth.debian.org
http://lists.alioth.debian.org/cgi-bin/mailman/listinfo/sane-devel
Unsubscribe: Send mail with subject "unsubscribe your_password"
 to sane-devel-requ...@lists.alioth.debian.org

[sane-devel] [PATCH 1/3] Separate ICC profile loading into a separate file.

2017-04-17 Thread Aaron Muir Hamilton
  This cuts out some duplicate code, and enables us to reuse this
  logic for PNG, JPEG, and any other future output format.
  sanei_load_icc_profile also allows us to know that an ICC profile
  file is not long enough before we start to write it to the output;
  this should prevent poorly-written software from overflowing into
  image data when they read the bad profile based on its length.
---
 frontend/Makefile.am |  2 +-
 frontend/Makefile.in |  5 +--
 frontend/sicc.c  | 65 ++
 frontend/sicc.h  | 19 ++
 frontend/stiff.c | 99 +---
 5 files changed, 113 insertions(+), 77 deletions(-)
 create mode 100644 frontend/sicc.c
 create mode 100644 frontend/sicc.h

diff --git a/frontend/Makefile.am b/frontend/Makefile.am
index 892b32ae..1b234dbc 100644
--- a/frontend/Makefile.am
+++ b/frontend/Makefile.am
@@ -16,7 +16,7 @@ endif
 
 AM_CPPFLAGS += -I. -I$(srcdir) -I$(top_builddir)/include 
-I$(top_srcdir)/include
 
-scanimage_SOURCES = scanimage.c stiff.c stiff.h
+scanimage_SOURCES = scanimage.c sicc.c sicc.h stiff.c stiff.h
 scanimage_LDADD = ../backend/libsane.la ../sanei/libsanei.la ../lib/liblib.la \
   $(PNG_LIBS) $(JPEG_LIBS)
 
diff --git a/frontend/Makefile.in b/frontend/Makefile.in
index e944a9cb..6571ba3a 100644
--- a/frontend/Makefile.in
+++ b/frontend/Makefile.in
@@ -114,7 +114,7 @@ AM_V_lt = $(am__v_lt_@AM_V@)
 am__v_lt_ = $(am__v_lt_@AM_DEFAULT_V@)
 am__v_lt_0 = --silent
 am__v_lt_1 = 
-am_scanimage_OBJECTS = scanimage.$(OBJEXT) stiff.$(OBJEXT)
+am_scanimage_OBJECTS = scanimage.$(OBJEXT) sicc.$(OBJEXT) stiff.$(OBJEXT)
 scanimage_OBJECTS = $(am_scanimage_OBJECTS)
 scanimage_DEPENDENCIES = ../backend/libsane.la ../sanei/libsanei.la \
../lib/liblib.la $(am__DEPENDENCIES_1) $(am__DEPENDENCIES_1)
@@ -381,7 +381,7 @@ target_alias = @target_alias@
 top_build_prefix = @top_build_prefix@
 top_builddir = @top_builddir@
 top_srcdir = @top_srcdir@
-scanimage_SOURCES = scanimage.c stiff.c stiff.h
+scanimage_SOURCES = scanimage.c sicc.c sicc.h stiff.c stiff.h
 scanimage_LDADD = ../backend/libsane.la ../sanei/libsanei.la ../lib/liblib.la \
   $(PNG_LIBS) $(JPEG_LIBS)
 
@@ -550,6 +550,7 @@ distclean-compile:
 
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/saned.Po@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/scanimage.Po@am__quote@
+@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/sicc.Po@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/stiff.Po@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/test.Po@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/tstbackend.Po@am__quote@
diff --git a/frontend/sicc.c b/frontend/sicc.c
new file mode 100644
index ..21d600d1
--- /dev/null
+++ b/frontend/sicc.c
@@ -0,0 +1,65 @@
+/* Load an ICC profile for embedding in an output file
+   Copyright (C) 2017 Aaron Muir Hamilton 
+
+   This program is free software; you can redistribute it and/or
+   modify it under the terms of the GNU General Public License as
+   published by the Free Software Foundation; either version 2 of the
+   License, or (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful, but
+   WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program; if not, write to the Free Software
+   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
+
+#include 
+#include 
+#include 
+
+void *
+sanei_load_icc_profile (const char *path, size_t *size)
+{
+  FILE *fd = NULL;
+  size_t stated_size = 0;
+  void *profile = NULL;
+  struct stat s;
+
+  fd = fopen(path, "r");
+
+  if (!fd)
+  {
+fprintf(stderr, "Could not open ICC profile %s\n", path);
+  }
+  else
+  {
+fstat(fileno(fd), &s);
+stated_size = 16777216 * fgetc(fd) + 65536 * fgetc(fd) + 256 * fgetc(fd) + 
fgetc(fd);
+rewind(fd);
+
+if (stated_size > (size_t) s.st_size)
+{
+  fprintf(stderr, "Ignoring ICC profile because file %s is shorter than 
the profile\n", path);
+}
+else
+{
+  profile = malloc(stated_size);
+
+  if (fread(profile, stated_size, 1, fd) != 1)
+  {
+fprintf(stderr, "Error reading ICC profile %s\n", path);
+free(profile);
+  }
+  else
+  {
+fclose(fd);
+*size = stated_size;
+return profile;
+  }
+}
+fclose(fd);
+  }
+  return NULL;
+}
diff --git a/frontend/sicc.h b/frontend/sicc.h
new file mode 100644
index ..5c225dad
--- /dev/null
+++ b/frontend/sicc.h
@@ -0,0 +1,19 @@
+/* Load an ICC profile for embedding in an output file
+   Copyright (C) 2017 Aaron Muir Hamilton 
+
+   This program is free software; you can redistri

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

2017-04-17 Thread Aaron Muir Hamilton
  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


-- 
sane-devel mailing list: sane-devel@lists.alioth.debian.org
http://lists.alioth.debian.org/cgi-bin/mailman/listinfo/sane-devel
Unsubscribe: Send mail with subject "unsubscribe your_password"
 to sane-devel-requ...@lists.alioth.debian.org

[sane-devel] [PATCH 2/3] Add ICC profile embedding for PNG output.

2017-04-17 Thread Aaron Muir Hamilton
---
 frontend/scanimage.c | 21 ++---
 1 file changed, 18 insertions(+), 3 deletions(-)

diff --git a/frontend/scanimage.c b/frontend/scanimage.c
index 77c22883..0f71422c 100644
--- a/frontend/scanimage.c
+++ b/frontend/scanimage.c
@@ -56,6 +56,7 @@
 #include "../include/sane/sanei.h"
 #include "../include/sane/saneopts.h"
 
+#include "sicc.h"
 #include "stiff.h"
 
 #include "../include/md5.h"
@@ -1165,9 +1166,11 @@ write_pnm_header (SANE_Frame format, int width, int 
height, int depth, FILE *ofp
 
 #ifdef HAVE_LIBPNG
 static void
-write_png_header (SANE_Frame format, int width, int height, int depth, FILE 
*ofp, png_structp* png_ptr, png_infop* info_ptr)
+write_png_header (SANE_Frame format, int width, int height, int depth, const 
char * icc_profile, FILE *ofp, png_structp* png_ptr, png_infop* info_ptr)
 {
   int color_type;
+  size_t icc_size = 0;
+  void *icc_buffer;
 
   *png_ptr = png_create_write_struct
(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
@@ -1200,6 +1203,16 @@ write_png_header (SANE_Frame format, int width, int 
height, int depth, FILE *ofp
 depth, color_type, PNG_INTERLACE_NONE,
 PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
 
+  if (icc_profile)
+{
+  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);
+ free(icc_buffer);
+   }
+}
+
   png_write_info(*png_ptr, *info_ptr);
 }
 #endif
@@ -1379,7 +1392,8 @@ scan_it (FILE *ofp)
 #ifdef HAVE_LIBPNG
  case OUTPUT_PNG:
write_png_header (parm.format, parm.pixels_per_line,
- parm.lines, parm.depth, ofp, &png_ptr, 
&info_ptr);
+ parm.lines, parm.depth, icc_profile,
+ ofp, &png_ptr, &info_ptr);
break;
 #endif
 #ifdef HAVE_LIBJPEG
@@ -1635,7 +1649,8 @@ scan_it (FILE *ofp)
 #ifdef HAVE_LIBPNG
   case OUTPUT_PNG:
write_png_header (parm.format, parm.pixels_per_line,
-  image.height, parm.depth, ofp, &png_ptr, &info_ptr);
+ image.height, parm.depth, icc_profile,
+ ofp, &png_ptr, &info_ptr);
   break;
 #endif
 #ifdef HAVE_LIBJPEG
-- 
2.12.2


-- 
sane-devel mailing list: sane-devel@lists.alioth.debian.org
http://lists.alioth.debian.org/cgi-bin/mailman/listinfo/sane-devel
Unsubscribe: Send mail with subject "unsubscribe your_password"
 to sane-devel-requ...@lists.alioth.debian.org


Re: [sane-devel] Please: CanoScan Lide 220

2017-04-16 Thread Aaron Muir Hamilton
If you could provide the exact versions of sane-backends and
sane-frontends you are using, that would be more useful.

I have a Canon CanoScan LiDE 220 sitting right next to me and it is
functioning quite well (I'm using scanimage to drive it).

Try running 
 $ scanimage -d genesys --format=tiff --resolution 75 > test.tiff
and see if that works as expected.

I often find that the thing that's making the scanner not work is the
application, not the SANE driver.

Cheers, and happy easter to you!

-- 
sane-devel mailing list: sane-devel@lists.alioth.debian.org
http://lists.alioth.debian.org/cgi-bin/mailman/listinfo/sane-devel
Unsubscribe: Send mail with subject "unsubscribe your_password"
 to sane-devel-requ...@lists.alioth.debian.org


[sane-devel] [PATCH 1/2] Separate ICC profile loading into a separate file.

2017-04-16 Thread Aaron Muir Hamilton
  This cuts out some duplicate code, and enables us to reuse this
  logic for PNG, JPEG, and any other future output format.
  sanei_load_icc_profile also allows us to know that an ICC profile
  file is not long enough before we start to write it to the output;
  this should prevent poorly-written software from overflowing into
  image data when they read the bad profile based on its length.
---
 frontend/Makefile.am |  2 +-
 frontend/Makefile.in |  5 +--
 frontend/sicc.c  | 65 ++
 frontend/sicc.h  | 19 ++
 frontend/stiff.c | 99 +---
 5 files changed, 113 insertions(+), 77 deletions(-)
 create mode 100644 frontend/sicc.c
 create mode 100644 frontend/sicc.h

diff --git a/frontend/Makefile.am b/frontend/Makefile.am
index 892b32a..1b234db 100644
--- a/frontend/Makefile.am
+++ b/frontend/Makefile.am
@@ -16,7 +16,7 @@ endif
 
 AM_CPPFLAGS += -I. -I$(srcdir) -I$(top_builddir)/include 
-I$(top_srcdir)/include
 
-scanimage_SOURCES = scanimage.c stiff.c stiff.h
+scanimage_SOURCES = scanimage.c sicc.c sicc.h stiff.c stiff.h
 scanimage_LDADD = ../backend/libsane.la ../sanei/libsanei.la ../lib/liblib.la \
   $(PNG_LIBS) $(JPEG_LIBS)
 
diff --git a/frontend/Makefile.in b/frontend/Makefile.in
index e944a9c..6571ba3 100644
--- a/frontend/Makefile.in
+++ b/frontend/Makefile.in
@@ -114,7 +114,7 @@ AM_V_lt = $(am__v_lt_@AM_V@)
 am__v_lt_ = $(am__v_lt_@AM_DEFAULT_V@)
 am__v_lt_0 = --silent
 am__v_lt_1 = 
-am_scanimage_OBJECTS = scanimage.$(OBJEXT) stiff.$(OBJEXT)
+am_scanimage_OBJECTS = scanimage.$(OBJEXT) sicc.$(OBJEXT) stiff.$(OBJEXT)
 scanimage_OBJECTS = $(am_scanimage_OBJECTS)
 scanimage_DEPENDENCIES = ../backend/libsane.la ../sanei/libsanei.la \
../lib/liblib.la $(am__DEPENDENCIES_1) $(am__DEPENDENCIES_1)
@@ -381,7 +381,7 @@ target_alias = @target_alias@
 top_build_prefix = @top_build_prefix@
 top_builddir = @top_builddir@
 top_srcdir = @top_srcdir@
-scanimage_SOURCES = scanimage.c stiff.c stiff.h
+scanimage_SOURCES = scanimage.c sicc.c sicc.h stiff.c stiff.h
 scanimage_LDADD = ../backend/libsane.la ../sanei/libsanei.la ../lib/liblib.la \
   $(PNG_LIBS) $(JPEG_LIBS)
 
@@ -550,6 +550,7 @@ distclean-compile:
 
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/saned.Po@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/scanimage.Po@am__quote@
+@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/sicc.Po@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/stiff.Po@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/test.Po@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/tstbackend.Po@am__quote@
diff --git a/frontend/sicc.c b/frontend/sicc.c
new file mode 100644
index 000..21d600d
--- /dev/null
+++ b/frontend/sicc.c
@@ -0,0 +1,65 @@
+/* Load an ICC profile for embedding in an output file
+   Copyright (C) 2017 Aaron Muir Hamilton 
+
+   This program is free software; you can redistribute it and/or
+   modify it under the terms of the GNU General Public License as
+   published by the Free Software Foundation; either version 2 of the
+   License, or (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful, but
+   WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program; if not, write to the Free Software
+   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
+
+#include 
+#include 
+#include 
+
+void *
+sanei_load_icc_profile (const char *path, size_t *size)
+{
+  FILE *fd = NULL;
+  size_t stated_size = 0;
+  void *profile = NULL;
+  struct stat s;
+
+  fd = fopen(path, "r");
+
+  if (!fd)
+  {
+fprintf(stderr, "Could not open ICC profile %s\n", path);
+  }
+  else
+  {
+fstat(fileno(fd), &s);
+stated_size = 16777216 * fgetc(fd) + 65536 * fgetc(fd) + 256 * fgetc(fd) + 
fgetc(fd);
+rewind(fd);
+
+if (stated_size > (size_t) s.st_size)
+{
+  fprintf(stderr, "Ignoring ICC profile because file %s is shorter than 
the profile\n", path);
+}
+else
+{
+  profile = malloc(stated_size);
+
+  if (fread(profile, stated_size, 1, fd) != 1)
+  {
+fprintf(stderr, "Error reading ICC profile %s\n", path);
+free(profile);
+  }
+  else
+  {
+fclose(fd);
+*size = stated_size;
+return profile;
+  }
+}
+fclose(fd);
+  }
+  return NULL;
+}
diff --git a/frontend/sicc.h b/frontend/sicc.h
new file mode 100644
index 000..5c225da
--- /dev/null
+++ b/frontend/sicc.h
@@ -0,0 +1,19 @@
+/* Load an ICC profile for embedding in an output file
+   Copyright (C) 2017 Aaron Muir Hamilton 
+
+   This program is free software; you can redistribute it 

[sane-devel] [PATCH 2/2] Add ICC profile embedding for PNG output.

2017-04-16 Thread Aaron Muir Hamilton
---
 frontend/scanimage.c | 21 ++---
 1 file changed, 18 insertions(+), 3 deletions(-)

diff --git a/frontend/scanimage.c b/frontend/scanimage.c
index 77c2288..0f71422 100644
--- a/frontend/scanimage.c
+++ b/frontend/scanimage.c
@@ -56,6 +56,7 @@
 #include "../include/sane/sanei.h"
 #include "../include/sane/saneopts.h"
 
+#include "sicc.h"
 #include "stiff.h"
 
 #include "../include/md5.h"
@@ -1165,9 +1166,11 @@ write_pnm_header (SANE_Frame format, int width, int 
height, int depth, FILE *ofp
 
 #ifdef HAVE_LIBPNG
 static void
-write_png_header (SANE_Frame format, int width, int height, int depth, FILE 
*ofp, png_structp* png_ptr, png_infop* info_ptr)
+write_png_header (SANE_Frame format, int width, int height, int depth, const 
char * icc_profile, FILE *ofp, png_structp* png_ptr, png_infop* info_ptr)
 {
   int color_type;
+  size_t icc_size = 0;
+  void *icc_buffer;
 
   *png_ptr = png_create_write_struct
(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
@@ -1200,6 +1203,16 @@ write_png_header (SANE_Frame format, int width, int 
height, int depth, FILE *ofp
 depth, color_type, PNG_INTERLACE_NONE,
 PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
 
+  if (icc_profile)
+{
+  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);
+ free(icc_buffer);
+   }
+}
+
   png_write_info(*png_ptr, *info_ptr);
 }
 #endif
@@ -1379,7 +1392,8 @@ scan_it (FILE *ofp)
 #ifdef HAVE_LIBPNG
  case OUTPUT_PNG:
write_png_header (parm.format, parm.pixels_per_line,
- parm.lines, parm.depth, ofp, &png_ptr, 
&info_ptr);
+ parm.lines, parm.depth, icc_profile,
+ ofp, &png_ptr, &info_ptr);
break;
 #endif
 #ifdef HAVE_LIBJPEG
@@ -1635,7 +1649,8 @@ scan_it (FILE *ofp)
 #ifdef HAVE_LIBPNG
   case OUTPUT_PNG:
write_png_header (parm.format, parm.pixels_per_line,
-  image.height, parm.depth, ofp, &png_ptr, &info_ptr);
+ image.height, parm.depth, icc_profile,
+ ofp, &png_ptr, &info_ptr);
   break;
 #endif
 #ifdef HAVE_LIBJPEG
-- 
1.8.3.1


-- 
sane-devel mailing list: sane-devel@lists.alioth.debian.org
http://lists.alioth.debian.org/cgi-bin/mailman/listinfo/sane-devel
Unsubscribe: Send mail with subject "unsubscribe your_password"
 to sane-devel-requ...@lists.alioth.debian.org


[sane-devel] [PATCH 2/2] Add ICC profile embedding for PNG output.

2017-04-16 Thread Aaron Muir Hamilton
---
 frontend/scanimage.c | 21 ++---
 1 file changed, 18 insertions(+), 3 deletions(-)

diff --git a/frontend/scanimage.c b/frontend/scanimage.c
index 77c2288..0f71422 100644
--- a/frontend/scanimage.c
+++ b/frontend/scanimage.c
@@ -56,6 +56,7 @@
 #include "../include/sane/sanei.h"
 #include "../include/sane/saneopts.h"
 
+#include "sicc.h"
 #include "stiff.h"
 
 #include "../include/md5.h"
@@ -1165,9 +1166,11 @@ write_pnm_header (SANE_Frame format, int width, int 
height, int depth, FILE *ofp
 
 #ifdef HAVE_LIBPNG
 static void
-write_png_header (SANE_Frame format, int width, int height, int depth, FILE 
*ofp, png_structp* png_ptr, png_infop* info_ptr)
+write_png_header (SANE_Frame format, int width, int height, int depth, const 
char * icc_profile, FILE *ofp, png_structp* png_ptr, png_infop* info_ptr)
 {
   int color_type;
+  size_t icc_size = 0;
+  void *icc_buffer;
 
   *png_ptr = png_create_write_struct
(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
@@ -1200,6 +1203,16 @@ write_png_header (SANE_Frame format, int width, int 
height, int depth, FILE *ofp
 depth, color_type, PNG_INTERLACE_NONE,
 PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
 
+  if (icc_profile)
+{
+  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);
+ free(icc_buffer);
+   }
+}
+
   png_write_info(*png_ptr, *info_ptr);
 }
 #endif
@@ -1379,7 +1392,8 @@ scan_it (FILE *ofp)
 #ifdef HAVE_LIBPNG
  case OUTPUT_PNG:
write_png_header (parm.format, parm.pixels_per_line,
- parm.lines, parm.depth, ofp, &png_ptr, 
&info_ptr);
+ parm.lines, parm.depth, icc_profile,
+ ofp, &png_ptr, &info_ptr);
break;
 #endif
 #ifdef HAVE_LIBJPEG
@@ -1635,7 +1649,8 @@ scan_it (FILE *ofp)
 #ifdef HAVE_LIBPNG
   case OUTPUT_PNG:
write_png_header (parm.format, parm.pixels_per_line,
-  image.height, parm.depth, ofp, &png_ptr, &info_ptr);
+ image.height, parm.depth, icc_profile,
+ ofp, &png_ptr, &info_ptr);
   break;
 #endif
 #ifdef HAVE_LIBJPEG
-- 
1.8.3.1


-- 
sane-devel mailing list: sane-devel@lists.alioth.debian.org
http://lists.alioth.debian.org/cgi-bin/mailman/listinfo/sane-devel
Unsubscribe: Send mail with subject "unsubscribe your_password"
 to sane-devel-requ...@lists.alioth.debian.org


[sane-devel] [PATCH 1/2] Separate ICC profile loading into a separate file.

2017-04-16 Thread Aaron Muir Hamilton
  This cuts out some duplicate code, and enables us to reuse this
  logic for PNG, JPEG, and any other future output format.
  sanei_load_icc_profile also allows us to know that an ICC profile
  file is not long enough before we start to write it to the output;
  this should prevent poorly-written software from overflowing into
  image data when they read the bad profile based on its length.
---
 frontend/Makefile.am |  2 +-
 frontend/Makefile.in |  5 +--
 frontend/sicc.c  | 65 ++
 frontend/sicc.h  | 19 ++
 frontend/stiff.c | 99 +---
 5 files changed, 113 insertions(+), 77 deletions(-)
 create mode 100644 frontend/sicc.c
 create mode 100644 frontend/sicc.h

diff --git a/frontend/Makefile.am b/frontend/Makefile.am
index 892b32a..1b234db 100644
--- a/frontend/Makefile.am
+++ b/frontend/Makefile.am
@@ -16,7 +16,7 @@ endif
 
 AM_CPPFLAGS += -I. -I$(srcdir) -I$(top_builddir)/include 
-I$(top_srcdir)/include
 
-scanimage_SOURCES = scanimage.c stiff.c stiff.h
+scanimage_SOURCES = scanimage.c sicc.c sicc.h stiff.c stiff.h
 scanimage_LDADD = ../backend/libsane.la ../sanei/libsanei.la ../lib/liblib.la \
   $(PNG_LIBS) $(JPEG_LIBS)
 
diff --git a/frontend/Makefile.in b/frontend/Makefile.in
index e944a9c..6571ba3 100644
--- a/frontend/Makefile.in
+++ b/frontend/Makefile.in
@@ -114,7 +114,7 @@ AM_V_lt = $(am__v_lt_@AM_V@)
 am__v_lt_ = $(am__v_lt_@AM_DEFAULT_V@)
 am__v_lt_0 = --silent
 am__v_lt_1 = 
-am_scanimage_OBJECTS = scanimage.$(OBJEXT) stiff.$(OBJEXT)
+am_scanimage_OBJECTS = scanimage.$(OBJEXT) sicc.$(OBJEXT) stiff.$(OBJEXT)
 scanimage_OBJECTS = $(am_scanimage_OBJECTS)
 scanimage_DEPENDENCIES = ../backend/libsane.la ../sanei/libsanei.la \
../lib/liblib.la $(am__DEPENDENCIES_1) $(am__DEPENDENCIES_1)
@@ -381,7 +381,7 @@ target_alias = @target_alias@
 top_build_prefix = @top_build_prefix@
 top_builddir = @top_builddir@
 top_srcdir = @top_srcdir@
-scanimage_SOURCES = scanimage.c stiff.c stiff.h
+scanimage_SOURCES = scanimage.c sicc.c sicc.h stiff.c stiff.h
 scanimage_LDADD = ../backend/libsane.la ../sanei/libsanei.la ../lib/liblib.la \
   $(PNG_LIBS) $(JPEG_LIBS)
 
@@ -550,6 +550,7 @@ distclean-compile:
 
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/saned.Po@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/scanimage.Po@am__quote@
+@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/sicc.Po@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/stiff.Po@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/test.Po@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/tstbackend.Po@am__quote@
diff --git a/frontend/sicc.c b/frontend/sicc.c
new file mode 100644
index 000..21d600d
--- /dev/null
+++ b/frontend/sicc.c
@@ -0,0 +1,65 @@
+/* Load an ICC profile for embedding in an output file
+   Copyright (C) 2017 Aaron Muir Hamilton 
+
+   This program is free software; you can redistribute it and/or
+   modify it under the terms of the GNU General Public License as
+   published by the Free Software Foundation; either version 2 of the
+   License, or (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful, but
+   WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program; if not, write to the Free Software
+   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
+
+#include 
+#include 
+#include 
+
+void *
+sanei_load_icc_profile (const char *path, size_t *size)
+{
+  FILE *fd = NULL;
+  size_t stated_size = 0;
+  void *profile = NULL;
+  struct stat s;
+
+  fd = fopen(path, "r");
+
+  if (!fd)
+  {
+fprintf(stderr, "Could not open ICC profile %s\n", path);
+  }
+  else
+  {
+fstat(fileno(fd), &s);
+stated_size = 16777216 * fgetc(fd) + 65536 * fgetc(fd) + 256 * fgetc(fd) + 
fgetc(fd);
+rewind(fd);
+
+if (stated_size > (size_t) s.st_size)
+{
+  fprintf(stderr, "Ignoring ICC profile because file %s is shorter than 
the profile\n", path);
+}
+else
+{
+  profile = malloc(stated_size);
+
+  if (fread(profile, stated_size, 1, fd) != 1)
+  {
+fprintf(stderr, "Error reading ICC profile %s\n", path);
+free(profile);
+  }
+  else
+  {
+fclose(fd);
+*size = stated_size;
+return profile;
+  }
+}
+fclose(fd);
+  }
+  return NULL;
+}
diff --git a/frontend/sicc.h b/frontend/sicc.h
new file mode 100644
index 000..5c225da
--- /dev/null
+++ b/frontend/sicc.h
@@ -0,0 +1,19 @@
+/* Load an ICC profile for embedding in an output file
+   Copyright (C) 2017 Aaron Muir Hamilton 
+
+   This program is free software; you can redistribute it 

[sane-devel] [PATCH] Include physical resolution (pHYs section) in PNG output.

2017-04-16 Thread Aaron Muir Hamilton

---
 frontend/scanimage.c | 15 ---
 1 file changed, 12 insertions(+), 3 deletions(-)

diff --git a/frontend/scanimage.c b/frontend/scanimage.c
index 77c2288..432c2ea 100644
--- a/frontend/scanimage.c
+++ b/frontend/scanimage.c
@@ -1165,9 +1165,12 @@ write_pnm_header (SANE_Frame format, int width, int 
height, int depth, FILE *ofp
 
 #ifdef HAVE_LIBPNG
 static void
-write_png_header (SANE_Frame format, int width, int height, int depth, FILE 
*ofp, png_structp* png_ptr, png_infop* info_ptr)
+write_png_header (SANE_Frame format, int width, int height, int depth, int 
dpi, FILE *ofp, png_structp* png_ptr, png_infop* info_ptr)
 {
   int color_type;
+  /* PNG does not have imperial reference units, so we must convert to metric. 
*/
+  /* There are nominally 39.3700787401575 inches in a meter. */
+  const double pixels_per_meter = dpi * 39.3700787401575;
 
   *png_ptr = png_create_write_struct
(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
@@ -1200,6 +1203,10 @@ write_png_header (SANE_Frame format, int width, int 
height, int depth, FILE *ofp
 depth, color_type, PNG_INTERLACE_NONE,
 PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
 
+  png_set_pHYs(*png_ptr, *info_ptr,
+pixels_per_meter, pixels_per_meter,
+PNG_RESOLUTION_METER);
+
   png_write_info(*png_ptr, *info_ptr);
 }
 #endif
@@ -1379,7 +1386,8 @@ scan_it (FILE *ofp)
 #ifdef HAVE_LIBPNG
  case OUTPUT_PNG:
write_png_header (parm.format, parm.pixels_per_line,
- parm.lines, parm.depth, ofp, &png_ptr, 
&info_ptr);
+ parm.lines, parm.depth, resolution_value,
+ ofp, &png_ptr, &info_ptr);
break;
 #endif
 #ifdef HAVE_LIBJPEG
@@ -1635,7 +1643,8 @@ scan_it (FILE *ofp)
 #ifdef HAVE_LIBPNG
   case OUTPUT_PNG:
write_png_header (parm.format, parm.pixels_per_line,
-  image.height, parm.depth, ofp, &png_ptr, &info_ptr);
+ image.height, parm.depth, resolution_value,
+ ofp, &png_ptr, &info_ptr);
   break;
 #endif
 #ifdef HAVE_LIBJPEG
-- 
1.8.3.1


-- 
sane-devel mailing list: sane-devel@lists.alioth.debian.org
http://lists.alioth.debian.org/cgi-bin/mailman/listinfo/sane-devel
Unsubscribe: Send mail with subject "unsubscribe your_password"
 to sane-devel-requ...@lists.alioth.debian.org


Re: [sane-devel] [PATCH v3] Swap bytes in 16-bit PNGs, because PNG is big-endian.

2017-04-15 Thread Aaron Muir Hamilton
Sorry about the volume, third time is definitely the charm.
Admittedly I struggled to grasp the style rules.

I noticed the PNG byte order issue fixed by this patch when using
genesys with a CanoScan LiDE 220. I do not have any other supported
scanners close at hand, so I hope somebody can confirm if this is an
issue specific to genesys. The TIFF 16-bit output already worked
correctly since TIFF is happy to be little-endian.

Cheers!

-- 
sane-devel mailing list: sane-devel@lists.alioth.debian.org
http://lists.alioth.debian.org/cgi-bin/mailman/listinfo/sane-devel
Unsubscribe: Send mail with subject "unsubscribe your_password"
 to sane-devel-requ...@lists.alioth.debian.org


[sane-devel] [PATCH v3] Swap bytes in 16-bit PNGs, because PNG is big-endian.

2017-04-15 Thread Aaron Muir Hamilton
---
 frontend/scanimage.c | 13 +
 1 file changed, 13 insertions(+)

diff --git a/frontend/scanimage.c b/frontend/scanimage.c
index 77c2288..0925039 100644
--- a/frontend/scanimage.c
+++ b/frontend/scanimage.c
@@ -1529,6 +1529,19 @@ scan_it (FILE *ofp)
  for(j = 0; j < parm.bytes_per_line; j++)
pngbuf[j] = ~pngbuf[j];
}
+ /* PNG is big-endian */
+ /* see: 
https://www.w3.org/TR/2003/REC-PNG-20031110/#7Integers-and-byte-order */
+ if (parm.depth == 16)
+   {
+ int j;
+ for (j = 0; j < parm.bytes_per_line; j += 2)
+   {
+ SANE_Byte LSB;
+ LSB = pngbuf[j];
+ pngbuf[j] = pngbuf[j + 1];
+ pngbuf[j + 1] = LSB;
+   }
+   }
  png_write_row(png_ptr, pngbuf);
  i += parm.bytes_per_line - pngrow;
  left -= parm.bytes_per_line - pngrow;
-- 
1.8.3.1


-- 
sane-devel mailing list: sane-devel@lists.alioth.debian.org
http://lists.alioth.debian.org/cgi-bin/mailman/listinfo/sane-devel
Unsubscribe: Send mail with subject "unsubscribe your_password"
 to sane-devel-requ...@lists.alioth.debian.org


[sane-devel] [PATCH] Swap bytes in 16-bit PNGs, because PNG is big-endian.

2017-04-15 Thread Aaron Muir Hamilton

---
 frontend/scanimage.c | 13 +
 1 file changed, 13 insertions(+)

diff --git a/frontend/scanimage.c b/frontend/scanimage.c
index 77c2288..d719c4b 100644
--- a/frontend/scanimage.c
+++ b/frontend/scanimage.c
@@ -1529,6 +1529,19 @@ scan_it (FILE *ofp)
  for(j = 0; j < parm.bytes_per_line; j++)
pngbuf[j] = ~pngbuf[j];
}
+  /* PNG is big-endian, */
+  /* see: 
https://www.w3.org/TR/2003/REC-PNG-20031110/#7Integers-and-byte-order */
+  if (parm.depth == 16)
+{
+  int j;
+  for (j = 0; j < parm.bytes_per_line; j += 2)
+{
+  SANE_Byte LSB;
+  LSB = pngbuf[j];
+  pngbuf[j] = pngbuf[j + 1];
+  pngbuf[j + 1] = LSB;
+}
+}
  png_write_row(png_ptr, pngbuf);
  i += parm.bytes_per_line - pngrow;
  left -= parm.bytes_per_line - pngrow;
-- 
1.8.3.1

-- 
sane-devel mailing list: sane-devel@lists.alioth.debian.org
http://lists.alioth.debian.org/cgi-bin/mailman/listinfo/sane-devel
Unsubscribe: Send mail with subject "unsubscribe your_password"
 to sane-devel-requ...@lists.alioth.debian.org