## 1 Introduction  
The use of metal coatings on surfaces is a very common practice in different technological sectors with the aim of either protecting the surface or to achieve specific material properties, such as in electronics, photonics, and biomaterials [1]. The determination of gold leaf thickness for heritage purposes is a practice that has been performed for decades [2]. The information yielded by such investigations can give further insight into the technology, manufacturing techniques, and binding mechanisms between the gold leaf and the artwork it was applied to [3; 4]--be it panel paintings, canvases, gilded metals, or frescoes [2; 5]. When establishing restoration protocols, this information is of great value, so the artwork can be restored to as close as possible to its original state. Moreover, technological traits of the manufacturing techniques of gold leaves can serve as anchors for the establishment of chronologies and different workshops.  
Customarily, the determination of gold leaf thickness is performed by microsampling and cross-section analysis, a microdestructive approach that is not always feasible (or desired) when dealing with objects of cultural heritage [6; 7]. An alternative to microsampling is the use of the differential attenuation of fluorescence radiation [8], basing on the concatenation effects of (a) the production of photons in the sample's substrate and (b) their subsequent attenuation by the gilding's uppermost layer. This approach has been previously applied to a series of objects of cultural heritage [9; 10; 11; 12]. Nevertheless, archaeological materials and works of art are known to be considerably heterogeneous, i.e., the substrate underneath the gold foil can differ throughout the sample, so the study of only one or few points on the surface may yield biased information [8; 13].  
Furthermore, different mordants used to adhere the gold foils or leaves to the substrate surface and protective layers further applied to the finished gilded surface can enhance the attenuation effects. [PERSON] et al. [9], when investigating gold foils protected with mica, demonstrated that the addition of a protective layer can introduce considerable errors to the estimated mean thickness by the different attenuation method.  
Macro-X-ray fluorescence scanning (MA-XRF) is a nondestructive and widespread technique in the field of heritage science, which provides a large set of spectra and can generate elemental distribution maps [14; 15; 16; 17]. Using its dataset for applying the differential attenuation method is an obvious approach to improve sampling space and verify the existence of substrate inhomogeneities, avoiding biases, and obtaining a more accurate mean gilding thickness value. Unfortunately, the evaluation and manual inspection of each spectrum for a precise determination of the net peak areas is a time consuming operation; therefore, the development of an automated algorithm for processing the large data provided by MA-XRF scans, giving each spectrum net peak ratios, is required [18].  
In this study, to determine the influence of a binding interface and apply the differential attenuation method to MA-XRF datasets representing gilded artworks (paintings), an algorithm was developed to automatically extract the net peak areas, generate the elemental distribution maps, and to automatically calculate the overlapping layer thickness at each sampled point. The dataset used was obtained by scanning one gilded sample with known parameters crafted to test the capabilities and limitations of the proposed methodology and algorithm. Monte Carlo simulations were performed to generate known spectra and test the outputs of the algorithm as well as to assess the influence of an intermediate resin layer between the gold leaf and the substrate and the continuum contribution.