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

Aaron Muir Hamilton aaron at correspondwith.me
Sun Apr 16 00:27:54 UTC 2017


---
 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



More information about the sane-devel mailing list