Discussion:
[PATCH] Swap bytes in 16-bit PNGs, because PNG is big-endian.
Add Reply
Aaron Muir Hamilton
2017-04-16 00:27:54 UTC
Reply
Permalink
Raw Message
---
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-***@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-***@lists.alioth.debian.org
a***@correspondwith.me
2017-04-16 00:27:54 UTC
Reply
Permalink
Raw Message
---
frontend/scanimage.c | 13 +++++++++++++
1 file changed, 13 insertions(+)

diff --git a/frontend/scanimage.c b/frontend/scanimage.c
index 77c2288..961ca49 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-***@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-***@lists.alioth.debian.org
Aaron Muir Hamilton
2017-04-16 00:27:54 UTC
Reply
Permalink
Raw Message
---
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-***@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-***@lists.alioth.debian.org
Aaron Muir Hamilton
2017-04-16 02:01:02 UTC
Reply
Permalink
Raw Message
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-***@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-***@lists.alioth.debian.org
Olaf Meeuwissen
2017-04-27 12:57:03 UTC
Reply
Permalink
Raw Message
Hi Aaron,

Sorry for not reading your whole thread before replying ;-)
Post by Aaron Muir Hamilton
Sorry about the volume, third time is definitely the charm.
Admittedly I struggled to grasp the style rules.
Style rules? If there are any, they're at a per file basis AFAICS :-(
Which reminds me, I should exempt the SANE code from my EditorConfig[1]
default settings.

[1] http://editorconfig.org/
Post by Aaron Muir Hamilton
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.
Correct, TIFF supports any byte order so long as you tell it which one
you're using. Whether all TIFF reader support that is another issue.

Hope this helps,
--
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
--
sane-devel mailing list: sane-***@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-***@lists.alioth.debian.org
Olaf Meeuwissen
2017-04-27 12:51:31 UTC
Reply
Permalink
Raw Message
Hi Aaron,

Thanks for the patch but it is incorrect on big-endian architectures.
From 3.2.1 Image Transmission in the SANE API specification:

Conceptually, each frame is transmitted a byte at a time. Each byte
may contain 8 sample values (for an image bit depth of 1), one full
sample value (for an image bit depth of 8), or a partial sample value
(for an image bit depth of 16 or bigger). In the latter case, the
bytes of each sample value are transmitted in the machine’s native
byte order.

Your patch should only be activated on little-endian machines, not on
big-endian ones. I've fixed that up in a follow-up commit and pushed
both to Alioth.

BTW, your v2 and v3 patches were the same (for all I could see) so I
ignored those ;-)
--
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
--
sane-devel mailing list: sane-***@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-***@lists.alioth.debian.org
Loading...