@@ -516,6 +516,23 @@ public class EFQRCodeStyleResampleImage: EFQRCodeStyleBase {
516
516
return pointList
517
517
}
518
518
519
+ /**
520
+ * Generates SVG markup for image-resampled QR code modules.
521
+ *
522
+ * This method creates the resample image effect by sampling colors from the source image
523
+ * and applying them to the QR code modules. It processes the image pixels and maps them
524
+ * to QR code module positions with proper contrast adjustments.
525
+ *
526
+ * - Parameters:
527
+ * - params: The resample image styling parameters.
528
+ * - qrcode: The QR code model containing module structure.
529
+ * - image: The image parameters for resampling, or nil if no image is available.
530
+ * - newWidth: The target width for the resampled image in pixels.
531
+ * - newHeight: The target height for the resampled image in pixels.
532
+ * - color: The fallback hex color string for modules when image sampling fails.
533
+ * - Returns: An SVG string containing the image-resampled QR code modules.
534
+ * - Throws: `EFQRCodeError` if image processing or color conversion fails.
535
+ */
519
536
func writeResImage( params: EFStyleResampleImageParams , qrcode: QRCode , image: EFStyleResampleImageParamsImage ? , newWidth: Int , newHeight: Int , color: String ) throws -> String {
520
537
guard let image = image else { return " " }
521
538
@@ -578,6 +595,17 @@ public class EFQRCodeStyleResampleImage: EFQRCodeStyleBase {
578
595
+ part2
579
596
}
580
597
598
+ /**
599
+ * Generates custom SVG markup for non-data modules in the resample image QR code.
600
+ *
601
+ * This method creates SVG elements for alignment patterns, timing patterns, and
602
+ * position detection patterns that are not part of the main data resampling.
603
+ * These elements use traditional QR code styling rather than image sampling.
604
+ *
605
+ * - Parameter qrcode: The QR code model containing module structure and type information.
606
+ * - Returns: An SVG string containing the custom-styled QR code control patterns.
607
+ * - Throws: `EFQRCodeError` if color conversion fails.
608
+ */
581
609
func customSVG( qrcode: QRCode ) throws -> String {
582
610
let alignType : EFStyleResampleImageParamAlignStyle = params. align. style
583
611
let alignColor : String = try params. align. color. hexString ( )
@@ -663,17 +691,51 @@ public class EFQRCodeStyleResampleImage: EFQRCodeStyleBase {
663
691
}
664
692
}
665
693
694
+ /**
695
+ * Extensions for CGImage to support resample image QR code generation.
696
+ *
697
+ * This extension provides utility methods for processing images used in
698
+ * resample image QR code styles, including color analysis and pixel sampling.
699
+ */
666
700
extension CGImage {
667
701
668
- // red, green, blue: [0, 255]
669
- // alpha: [0, 1]
670
- // return: [0, 255]
702
+ /**
703
+ * Calculates the grayscale value for RGBA color components with gamma correction.
704
+ *
705
+ * This method converts RGB color values to grayscale using standard luminance
706
+ * coefficients and applies alpha blending for proper color representation.
707
+ *
708
+ * - Parameters:
709
+ * - red: The red component value (0-255).
710
+ * - green: The green component value (0-255).
711
+ * - blue: The blue component value (0-255).
712
+ * - alpha: The alpha component value (0-1).
713
+ * - Returns: The calculated grayscale value (0-255).
714
+ */
671
715
func gamma( _ red: CGFloat , _ green: CGFloat , _ blue: CGFloat , _ alpha: CGFloat ) -> CGFloat {
672
716
let gray : CGFloat = 0.2126 * red + 0.7152 * green + 0.0722 * blue
673
717
let weightedGray : CGFloat = gray * alpha + ( 1 - alpha) * 255.0
674
718
return weightedGray
675
719
}
676
720
721
+ /**
722
+ * Generates QR code modules by sampling grayscale values from the image.
723
+ *
724
+ * This method processes the image to create QR code modules based on pixel brightness,
725
+ * applying contrast and exposure adjustments. It handles different QR code module types
726
+ * (data, alignment, timing) and creates appropriate SVG elements for each.
727
+ *
728
+ * - Parameters:
729
+ * - params: The resample image styling parameters.
730
+ * - qrcode: The QR code model containing module structure and type information.
731
+ * - newWidth: The target width for image resampling.
732
+ * - newHeight: The target height for image resampling.
733
+ * - contrast: The contrast adjustment value. Defaults to 0.
734
+ * - exposure: The exposure adjustment value. Defaults to 0.
735
+ * - color: The fallback color string for modules when image sampling fails.
736
+ * - Returns: An array of SVG strings representing the QR code modules.
737
+ * - Throws: `EFQRCodeError` if color conversion or image processing fails.
738
+ */
677
739
func getGrayPointList( params: EFStyleResampleImageParams , qrcode: QRCode , newWidth: Int , newHeight: Int , contrast: CGFloat = 0 , exposure: CGFloat = 0 , color: String ) throws -> [ String ] {
678
740
// filter trans area
679
741
let nCount : Int = qrcode. model. moduleCount
0 commit comments