[sane-devel] [PATCH 0/2] canon_dr: DR-C225 fine calibration in software, fix gray duplex overflow

Will Garza Will at Garzas.us
Mon Oct 26 21:05:33 GMT 2026


Hello,

Two small canon_dr fixes for the DR-C225 (USB 1083:1658), tested on hardware
(Kubuntu, sane-backends 1.4.0 and current master, canon_dr BUILD 67). Both
patches are against master, apply in order, and are inline below and attached.
I do not have a GitLab account; please feel free to take them into a merge
request.

1. Gray duplex aborts with "buffer overflow detected"

copy_duplex() in the DUPLEX_INTERLACE_PER_CHANNEL branch always copies three
channels per line. In gray mode Bpl == width, so the copy runs two widths past
the end of the per-side buffers and glibc aborts during fine calibration.
Reproduce on master:

  scanimage -d canon_dr --mode Gray --source "ADF Duplex"

which aborts right after "copy_duplex: per channel". The loop now copies
bwidth/pwidth channels (3 in color, 1 in gray).

2. DR-C225 scans have vertical streaks and a color cast

For this model fcal_dest = FCAL_DEST_HW, and calibrate_fine_dest_hw()
measures the per-pixel white and dark levels and then uploads a flat
placeholder (the TODO in that function), so the measured data is thrown away.
On our unit the white reference varied about +/-8% between neighbouring
pixels and the front side read R 133 / G 145 / B 113, which shows as
vertical streaks and a green cast. The patch adds an fcal_hw_sw flag, set for
the DR-C225: after the hardware upload, calibrate_fine_src_scan() runs again
with that payload in place and its buffers are kept, so copy_simplex()
applies the per-pixel offset and gain exactly as it already does for the
DR-C120/C130 (FCAL_DEST_SW). With the patch a blank sheet is flat to within
about 1.5 levels column to column.

This may also be the cause of GitLab issue #771 ("DR C225ii Vertical Lines").

Thanks for canon_dr,
Will Garza

----- 0001-canon_dr-fix-gray-duplex-buffer-overflow.patch -----
--- a/backend/canon_dr.c
+++ b/backend/canon_dr.c
@@ -6807,7 +6807,8 @@
     DBG (10, "copy_duplex: per channel\n");
 
     for(i=0; i<len; i+=dbwidth){
-      for(j=0;j<3;j++){
+      /* 3 channels in color, 1 in gray: copying 3 overflows the gray buffers */
+      for(j=0;j<bwidth/pwidth;j++){
         memcpy(front+flen,buf+i+j*pwidth*2,pwidth);
         flen+=pwidth;
         memcpy(back+blen,buf+i+j*pwidth*2+pwidth,pwidth);

----- 0002-canon_dr-apply-DR-C225-fine-calibration-in-software.patch -----
--- a/backend/canon_dr.c
+++ b/backend/canon_dr.c
@@ -1889,6 +1889,10 @@
     s->ccal_version = 3;
     s->fcal_src = FCAL_SRC_SCAN;
     s->fcal_dest = FCAL_DEST_HW;
+    /* calibrate_fine_dest_hw() uploads a flat placeholder, which leaves
+     * per-pixel streaks and a colour cast. Re-measure under it and apply
+     * the per-pixel correction in software as well. */
+    s->fcal_hw_sw = 1;
     s->invert_tly = 1;
     s->rgb_format = 1;
     /*s->duplex_offset = 400; now set in config file*/
@@ -7412,6 +7416,15 @@
     ret = calibrate_fine_dest_hw(s);
     if (ret != SANE_STATUS_GOOD)
       goto cleanup;
+
+    /* dest_hw discards the measured data; measure again with the hw
+     * payload in place and keep the buffers so copy_simplex applies them */
+    if(s->fcal_hw_sw && s->fcal_src == FCAL_SRC_SCAN){
+      DBG (10, "calibrate_fine: re-measuring for sw correction\n");
+      ret = calibrate_fine_src_scan(s);
+      if (ret != SANE_STATUS_GOOD)
+        goto cleanup;
+    }
   }
 
   /* log current cal settings so we won't recalibrate on next scan with same params */
--- a/backend/canon_dr.h
+++ b/backend/canon_dr.h
@@ -224,6 +224,7 @@
 
   int fcal_src;      /* where fine offset/gain calibration data comes from */
   int fcal_dest;     /* where fine offset/gain calibration data is used */
+  int fcal_hw_sw;    /* after the hw upload, re-measure and also correct in sw */
 
 #define FCAL_SRC_NONE 0  /* scanner does not require fine calibration */
 #define FCAL_SRC_SCAN 1  /* make calibration scans, store gain/offset in struct */
-------------- next part --------------
A non-text attachment was scrubbed...
Name: 0001-canon_dr-fix-gray-duplex-buffer-overflow.patch
Type: text/x-diff
Size: 432 bytes
Desc: 0001-canon_dr-fix-gray-duplex-buffer-overflow.patch
URL: <http://alioth-lists.debian.net/pipermail/sane-devel/attachments/20261026/6f52a01c/attachment.patch>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: 0002-canon_dr-apply-DR-C225-fine-calibration-in-software.patch
Type: text/x-diff
Size: 1564 bytes
Desc: 0002-canon_dr-apply-DR-C225-fine-calibration-in-software.patch
URL: <http://alioth-lists.debian.net/pipermail/sane-devel/attachments/20261026/6f52a01c/attachment-0001.patch>


More information about the sane-devel mailing list