@@ -28,10 +28,24 @@ def gentle_compare(w0, w1):
2828 return True
2929
3030
31+ def rms (a , b ):
32+ '''
33+ Returns RMS diff of raw bytes of two sequences.
34+ '''
35+ assert len (a ) == len (b )
36+ e = 0
37+ for aa , bb in zip (a , b ):
38+ e += (aa - bb ) ** 2
39+ rms = math .sqrt (e / len (a ))
40+ return rms
41+
42+
3143def pixmaps_rms (a , b , out_prefix = '' ):
3244 '''
33- Returns RMS diff of raw bytes of two pixmaps. We assert that the pixmaps
34- are the same size.
45+ Returns RMS diff of raw bytes of two pixmaps.
46+
47+ We assert that the pixmaps/sequences are the same size.
48+
3549 <a> and <b> can each be a pymupdf.Pixmap or path of a bitmap file.
3650 '''
3751 if isinstance (a , str ):
@@ -52,3 +66,34 @@ def pixmaps_rms(a, b, out_prefix=''):
5266 rms = math .sqrt (e / len (a_mv ))
5367 print (f'{ out_prefix } compare_pixmaps(): { e = } { rms = } .' )
5468 return rms
69+
70+
71+ def pixmaps_diff (a , b , out_prefix = '' ):
72+ '''
73+ Returns a pymupdf.Pixmap that represents the difference between pixmaps <a>
74+ and <b>.
75+
76+ Each byte in the returned pixmap is `128 + (b_byte - a_byte) // 2`.
77+ '''
78+ if isinstance (a , str ):
79+ print (f'{ out_prefix } pixmaps_rms(): reading pixmap from { a = } .' )
80+ a = pymupdf .Pixmap (a )
81+ if isinstance (b , str ):
82+ print (f'{ out_prefix } pixmaps_rms(): reading pixmap from { b = } .' )
83+ b = pymupdf .Pixmap (b )
84+ assert a .irect == b .irect , f'Differing rects: { a .irect = } { b .irect = } .'
85+ a_mv = a .samples_mv
86+ b_mv = b .samples_mv
87+ c = pymupdf .Pixmap (a .tobytes ())
88+ c_mv = c .samples_mv
89+ assert len (a_mv ) == len (b_mv ) == len (c_mv )
90+ if 1 :
91+ print (f'{ len (a_mv )= } ' )
92+ for i , (a_byte , b_byte , c_byte ) in enumerate (zip (a_mv , b_mv , c_mv )):
93+ assert 0 <= a_byte < 256
94+ assert 0 <= b_byte < 256
95+ assert 0 <= c_byte < 256
96+ # Set byte to 128 plus half the diff so we represent the full
97+ # -255..+255 range.
98+ c_mv [i ] = 128 + (b_byte - a_byte ) // 2
99+ return c
0 commit comments