visual-cryptography 0.1.0

A Rust implementation of main visual cryptography algorithms supporting multiple schemes and block sizes
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
//! Visual cryptography algorithms implementation

use crate::{
    error::{Result, VCError},
    matrix::{generate_basic_matrices, generate_xor_matrices},
    share::{stack_shares, Share},
    utils::convert_to_binary,
    VCConfig,
};
use image::{DynamicImage, ImageBuffer, Luma, Rgb};
use rand::{seq::SliceRandom, Rng};

/// Available visual cryptography algorithms
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Algorithm {
    /// Basic (k,n) threshold scheme
    BasicThreshold,
    /// Naor-Shamir original scheme (1994)
    NaorShamir,
    /// Taghaddos-Latif for grayscale (2014)
    TaghaddosLatif,
    /// Dhiman-Kasana for color images (2018)
    DhimanKasana,
    /// XOR-based scheme for better contrast
    XorBased,
    /// Yamaguchi-Nakajima Extended Visual Cryptography for Natural Images
    YamaguchiNakajima,
}

/// Trait for visual cryptography schemes
pub trait VCScheme {
    /// Encrypt an image into shares
    fn encrypt(
        &self,
        image: &DynamicImage,
        config: &VCConfig,
        cover_images: Option<Vec<DynamicImage>>,
    ) -> Result<Vec<Share>>;

    /// Decrypt shares back into an image
    fn decrypt(&self, shares: &[Share], config: &VCConfig) -> Result<DynamicImage>;
}

/// Main encryption function that dispatches to the appropriate algorithm
pub fn encrypt(
    image: &DynamicImage,
    config: &VCConfig,
    cover_images: Option<Vec<DynamicImage>>,
) -> Result<Vec<Share>> {
    match config.algorithm {
        Algorithm::BasicThreshold => basic_threshold_encrypt(image, config),
        Algorithm::NaorShamir => naor_shamir_encrypt(image, config),
        Algorithm::TaghaddosLatif => taghaddos_latif_encrypt(image, config),
        Algorithm::DhimanKasana => dhiman_kasana_encrypt(image, config, cover_images),
        Algorithm::XorBased => xor_based_encrypt(image, config),
        Algorithm::YamaguchiNakajima => yamaguchi_nakajima_encrypt(image, config, cover_images),
    }
}

/// Main decryption function that dispatches to the appropriate algorithm
pub fn decrypt(shares: &[Share], config: &VCConfig) -> Result<DynamicImage> {
    match config.algorithm {
        Algorithm::BasicThreshold => basic_threshold_decrypt(shares, config),
        Algorithm::NaorShamir => naor_shamir_decrypt(shares, config),
        Algorithm::TaghaddosLatif => taghaddos_latif_decrypt(shares, config),
        Algorithm::DhimanKasana => dhiman_kasana_decrypt(shares, config),
        Algorithm::XorBased => xor_based_decrypt(shares, config),
        Algorithm::YamaguchiNakajima => yamaguchi_nakajima_decrypt(shares, config),
    }
}

/// XOR-based encryption for better contrast
fn xor_based_encrypt(image: &DynamicImage, config: &VCConfig) -> Result<Vec<Share>> {
    let binary = convert_to_binary(image);
    let (width, height) = (binary.width(), binary.height());

    // Generate XOR matrices
    let xor_matrices = generate_xor_matrices(config.num_shares)?;

    let mut shares = vec![ImageBuffer::new(width, height); config.num_shares];

    let mut rng = rand::rng();

    // Process each pixel
    for y in 0..height {
        for x in 0..width {
            let pixel = binary.get_pixel(x, y)[0];
            let is_black = pixel == 0;

            // Select appropriate matrix
            let matrix = if is_black {
                &xor_matrices.black_pixel
            } else {
                &xor_matrices.white_pixel
            };

            // Select random column
            let col = rng.random_range(0..matrix.ncols());

            // Distribute values to shares
            for share_idx in 0..config.num_shares {
                let value = matrix[(share_idx, col)];
                let pixel_value = if value == 1 { 0u8 } else { 255u8 };
                shares[share_idx].put_pixel(x, y, Luma([pixel_value]));
            }
        }
    }

    // Convert to Share objects
    let result: Vec<Share> = shares
        .into_iter()
        .enumerate()
        .map(|(i, img)| {
            Share::new(
                DynamicImage::ImageLuma8(img),
                i + 1,
                config.num_shares,
                width,
                height,
                1,
                false,
            )
        })
        .collect();

    Ok(result)
}

/// XOR-based decryption
fn xor_based_decrypt(shares: &[Share], _config: &VCConfig) -> Result<DynamicImage> {
    if shares.is_empty() {
        return Err(VCError::InsufficientShares {
            required: 1,
            provided: 0,
        });
    }

    let (width, height) = shares[0].dimensions();
    let mut result = ImageBuffer::new(width, height);

    for y in 0..height {
        for x in 0..width {
            let mut xor_value = 0u8;

            for share in shares {
                if let DynamicImage::ImageLuma8(img) = &share.image {
                    let pixel = img.get_pixel(x, y)[0];
                    let bit = if pixel == 0 { 1 } else { 0 };
                    xor_value ^= bit;
                }
            }

            // XOR result: 1 = black, 0 = white
            let pixel_value = if xor_value == 1 { 0u8 } else { 255u8 };
            result.put_pixel(x, y, Luma([pixel_value]));
        }
    }

    Ok(DynamicImage::ImageLuma8(result))
}

/// Basic (k,n) threshold scheme encryption
fn basic_threshold_encrypt(image: &DynamicImage, config: &VCConfig) -> Result<Vec<Share>> {
    // Convert to binary image
    let binary = convert_to_binary(image);
    let (width, height) = (binary.width(), binary.height());

    // Generate sharing matrices
    let matrices = generate_basic_matrices(config.threshold, config.num_shares, config.block_size)?;

    // Create shares with expanded dimensions
    let share_width = width * config.block_size as u32;
    let share_height = height * config.block_size as u32;

    let mut shares = vec![ImageBuffer::new(share_width, share_height); config.num_shares];

    let mut rng = rand::rng();

    // Process each pixel
    for y in 0..height {
        for x in 0..width {
            let pixel = binary.get_pixel(x, y)[0];
            let matrix_idx = if pixel == 0 { 1 } else { 0 }; // black = 0, white = 255
            let matrix = &matrices[matrix_idx];

            // Select a random column from the matrix
            let col = rng.random_range(0..matrix.ncols());

            // Distribute the column values to shares
            for share_idx in 0..config.num_shares {
                let value = matrix[(share_idx, col)];
                let block_value = if value == 1 { 0u8 } else { 255u8 };

                // Expand pixel to block
                let base_x = x * config.block_size as u32;
                let base_y = y * config.block_size as u32;

                for dy in 0..config.block_size as u32 {
                    for dx in 0..config.block_size as u32 {
                        shares[share_idx].put_pixel(base_x + dx, base_y + dy, Luma([block_value]));
                    }
                }
            }
        }
    }

    // Convert to Share objects
    let result: Vec<Share> = shares
        .into_iter()
        .enumerate()
        .map(|(i, img)| {
            Share::new(
                DynamicImage::ImageLuma8(img),
                i + 1,
                config.num_shares,
                width,
                height,
                config.block_size,
                false,
            )
        })
        .collect();

    Ok(result)
}

/// Basic threshold scheme decryption
fn basic_threshold_decrypt(shares: &[Share], _config: &VCConfig) -> Result<DynamicImage> {
    if let Some(stacked) = stack_shares(shares) {
        Ok(DynamicImage::ImageLuma8(stacked))
    } else {
        Err(VCError::DecryptionError(
            "Failed to stack shares".to_string(),
        ))
    }
}

/// Naor-Shamir original scheme encryption
fn naor_shamir_encrypt(image: &DynamicImage, config: &VCConfig) -> Result<Vec<Share>> {
    // The original Naor-Shamir is a (2,2) scheme with 2x2 pixel expansion
    if config.num_shares != 2 || config.threshold != 2 {
        return Err(VCError::InvalidConfiguration(
            "Original Naor-Shamir scheme requires exactly 2 shares with threshold 2".to_string(),
        ));
    }

    let binary = convert_to_binary(image);
    let (width, height) = (binary.width(), binary.height());

    // Fixed matrices for Naor-Shamir as per the original paper
    // For white pixels: both shares get identical patterns
    let white_matrix = vec![vec![1, 1, 0, 0], vec![1, 1, 0, 0]];
    // For black pixels: shares get complementary patterns
    let black_matrix = vec![vec![1, 1, 0, 0], vec![0, 0, 1, 1]];

    let share_width = width * 2;
    let share_height = height * 2;

    let mut share1 = ImageBuffer::new(share_width, share_height);
    let mut share2 = ImageBuffer::new(share_width, share_height);

    let mut rng = rand::rng();

    for y in 0..height {
        for x in 0..width {
            let pixel = binary.get_pixel(x, y)[0];

            // Select appropriate matrix based on pixel value
            let matrix = if pixel == 0 {
                // black pixel
                &black_matrix
            } else {
                // white pixel
                &white_matrix
            };

            // Randomly permute columns (this is the key step in Naor-Shamir)
            let mut columns: Vec<usize> = (0..4).collect();
            columns.as_mut_slice().shuffle(&mut rng);

            // Create permuted patterns for both shares
            let share1_pattern = [
                matrix[0][columns[0]],
                matrix[0][columns[1]],
                matrix[0][columns[2]],
                matrix[0][columns[3]],
            ];
            let share2_pattern = [
                matrix[1][columns[0]],
                matrix[1][columns[1]],
                matrix[1][columns[2]],
                matrix[1][columns[3]],
            ];

            // Apply 2x2 patterns to shares
            // Share 1
            share1.put_pixel(
                x * 2,
                y * 2,
                Luma([if share1_pattern[0] == 1 { 0 } else { 255 }]),
            );
            share1.put_pixel(
                x * 2 + 1,
                y * 2,
                Luma([if share1_pattern[1] == 1 { 0 } else { 255 }]),
            );
            share1.put_pixel(
                x * 2,
                y * 2 + 1,
                Luma([if share1_pattern[2] == 1 { 0 } else { 255 }]),
            );
            share1.put_pixel(
                x * 2 + 1,
                y * 2 + 1,
                Luma([if share1_pattern[3] == 1 { 0 } else { 255 }]),
            );

            // Share 2
            share2.put_pixel(
                x * 2,
                y * 2,
                Luma([if share2_pattern[0] == 1 { 0 } else { 255 }]),
            );
            share2.put_pixel(
                x * 2 + 1,
                y * 2,
                Luma([if share2_pattern[1] == 1 { 0 } else { 255 }]),
            );
            share2.put_pixel(
                x * 2,
                y * 2 + 1,
                Luma([if share2_pattern[2] == 1 { 0 } else { 255 }]),
            );
            share2.put_pixel(
                x * 2 + 1,
                y * 2 + 1,
                Luma([if share2_pattern[3] == 1 { 0 } else { 255 }]),
            );
        }
    }

    Ok(vec![
        Share::new(
            DynamicImage::ImageLuma8(share1),
            1,
            2,
            width,
            height,
            2,
            false,
        ),
        Share::new(
            DynamicImage::ImageLuma8(share2),
            2,
            2,
            width,
            height,
            2,
            false,
        ),
    ])
}

/// Naor-Shamir decryption
fn naor_shamir_decrypt(shares: &[Share], config: &VCConfig) -> Result<DynamicImage> {
    basic_threshold_decrypt(shares, config)
}

/// Taghaddos-Latif grayscale scheme following the original paper exactly
fn taghaddos_latif_encrypt(image: &DynamicImage, config: &VCConfig) -> Result<Vec<Share>> {
    if config.num_shares != 2 {
        return Err(VCError::InvalidConfiguration(
            "Taghaddos-Latif scheme requires exactly 2 shares".to_string(),
        ));
    }

    let gray = image.to_luma8();
    let (width, height) = (gray.width(), gray.height());

    // The 6 specific patterns from the original paper
    let patterns = [
        [1u8, 1u8, 0u8, 0u8],
        [1u8, 0u8, 1u8, 0u8],
        [1u8, 0u8, 0u8, 1u8],
        [0u8, 1u8, 1u8, 0u8],
        [0u8, 1u8, 0u8, 1u8],
        [0u8, 0u8, 1u8, 1u8],
    ];

    // Pixel expansion by 2 (2x2 blocks)
    let share_width = width * 2;
    let share_height = height * 2;

    let mut share_a = ImageBuffer::new(share_width, share_height);
    let mut share_b = ImageBuffer::new(share_width, share_height);

    let mut rng = rand::rng();

    // Process each pixel
    for y in 0..height {
        for x in 0..width {
            let pixel_value = gray.get_pixel(x, y)[0];
            let mut share_a_colors = [0u8; 4];
            let mut share_b_colors = [0u8; 4];

            // Process each bit plane (0-7)
            for bit_pos in 0..8 {
                let bit = (pixel_value >> bit_pos) & 1;

                // Randomly select one of the 6 patterns
                let pattern = patterns[rng.random_range(0..6)];

                if bit == 1 {
                    // White pixel (bit = 1): both shares get identical patterns
                    for i in 0..4 {
                        share_a_colors[i] |= pattern[i] << bit_pos;
                    }
                    share_b_colors.copy_from_slice(&share_a_colors);
                } else {
                    // Black pixel (bit = 0): share B gets complement of share A
                    for i in 0..4 {
                        share_a_colors[i] |= pattern[i] << bit_pos;
                        share_b_colors[i] |= (1 - pattern[i]) << bit_pos;
                    }
                }
            }

            // Draw 2x2 blocks for each share
            let base_x = x * 2;
            let base_y = y * 2;

            // Share A 2x2 block
            share_a.put_pixel(base_x, base_y, Luma([share_a_colors[0]]));
            share_a.put_pixel(base_x + 1, base_y, Luma([share_a_colors[1]]));
            share_a.put_pixel(base_x, base_y + 1, Luma([share_a_colors[2]]));
            share_a.put_pixel(base_x + 1, base_y + 1, Luma([share_a_colors[3]]));

            // Share B 2x2 block
            share_b.put_pixel(base_x, base_y, Luma([share_b_colors[0]]));
            share_b.put_pixel(base_x + 1, base_y, Luma([share_b_colors[1]]));
            share_b.put_pixel(base_x, base_y + 1, Luma([share_b_colors[2]]));
            share_b.put_pixel(base_x + 1, base_y + 1, Luma([share_b_colors[3]]));
        }
    }

    Ok(vec![
        Share::new(
            DynamicImage::ImageLuma8(share_a),
            1,
            2,
            width,
            height,
            2, // pixel expansion = 2
            false,
        ),
        Share::new(
            DynamicImage::ImageLuma8(share_b),
            2,
            2,
            width,
            height,
            2, // pixel expansion = 2
            false,
        ),
    ])
}

/// Taghaddos-Latif decryption using AND operation as per original paper
fn taghaddos_latif_decrypt(shares: &[Share], _config: &VCConfig) -> Result<DynamicImage> {
    if shares.len() < 2 {
        return Err(VCError::InsufficientShares {
            required: 2,
            provided: shares.len(),
        });
    }

    let (expanded_width, expanded_height) = shares[0].dimensions();

    // Original dimensions (accounting for pixel expansion)
    let width = expanded_width / 2;
    let height = expanded_height / 2;

    let mut result = ImageBuffer::new(width, height);

    // Extract share images
    let share_a = if let DynamicImage::ImageLuma8(img) = &shares[0].image {
        img
    } else {
        return Err(VCError::DecryptionError(
            "Share A is not grayscale".to_string(),
        ));
    };

    let share_b = if let DynamicImage::ImageLuma8(img) = &shares[1].image {
        img
    } else {
        return Err(VCError::DecryptionError(
            "Share B is not grayscale".to_string(),
        ));
    };

    // Process each original pixel (reconstructing from 2x2 blocks)
    for y in 0..height {
        for x in 0..width {
            let base_x = x * 2;
            let base_y = y * 2;

            // Get the 2x2 block values from both shares
            let share_a_block = [
                share_a.get_pixel(base_x, base_y)[0],
                share_a.get_pixel(base_x + 1, base_y)[0],
                share_a.get_pixel(base_x, base_y + 1)[0],
                share_a.get_pixel(base_x + 1, base_y + 1)[0],
            ];

            let share_b_block = [
                share_b.get_pixel(base_x, base_y)[0],
                share_b.get_pixel(base_x + 1, base_y)[0],
                share_b.get_pixel(base_x, base_y + 1)[0],
                share_b.get_pixel(base_x + 1, base_y + 1)[0],
            ];

            // Reconstruct pixel value using AND operation
            let mut reconstructed_value = 0u8;

            for bit_pos in 0..8 {
                let mut reconstructed_bits = [0u8; 4];

                // Apply AND operation for each sub-pixel in the 2x2 block
                for i in 0..4 {
                    let bit_a = (share_a_block[i] >> bit_pos) & 1;
                    let bit_b = (share_b_block[i] >> bit_pos) & 1;
                    reconstructed_bits[i] = bit_a & bit_b;
                }

                // Average the 4 sub-pixels to get the final bit
                // (this follows the HVS perception model from the paper)
                let sum = reconstructed_bits.iter().map(|&x| x as u32).sum::<u32>();
                let average_bit = if sum >= 2 { 1 } else { 0 }; // majority voting for sub-pixels

                reconstructed_value |= (average_bit as u8) << bit_pos;
            }

            result.put_pixel(x, y, Luma([reconstructed_value]));
        }
    }

    Ok(DynamicImage::ImageLuma8(result))
}

/// Dhiman-Kasana EVCT(3,3) color scheme
fn dhiman_kasana_encrypt(
    image: &DynamicImage,
    config: &VCConfig,
    cover_images: Option<Vec<DynamicImage>>,
) -> Result<Vec<Share>> {
    if config.num_shares != 3 {
        return Err(VCError::InvalidConfiguration(
            "Dhiman-Kasana EVCT(3,3) scheme requires exactly 3 shares".to_string(),
        ));
    }

    let rgb = image.to_rgb8();
    let (width, height) = (rgb.width(), rgb.height());

    // Specific bit position coordinates for each RGB channel
    let components = [
        // R channel positions
        [
            (4, 4),
            (4, 2),
            (3, 1),
            (2, 3),
            (2, 0),
            (1, 4),
            (1, 2),
            (0, 1),
        ],
        // G channel positions
        [
            (4, 3),
            (3, 4),
            (3, 2),
            (2, 1),
            (1, 3),
            (1, 0),
            (0, 4),
            (0, 2),
        ],
        // B channel positions
        [
            (4, 1),
            (3, 3),
            (3, 0),
            (2, 4),
            (2, 2),
            (1, 1),
            (0, 3),
            (0, 0),
        ],
    ];

    // 5x5 pixel expansion
    let share_width = width * 5;
    let share_height = height * 5;

    let mut shares = Vec::new();
    for _ in 0..3 {
        shares.push(ImageBuffer::new(share_width, share_height));
    }

    // Check if cover images are provided before moving them
    let has_cover_images = cover_images.is_some();

    // Use cover images if provided, otherwise use default cover colors
    let covers = if let Some(covers) = cover_images {
        if covers.len() != 3 {
            return Err(VCError::CoverImageError(
                "Dhiman-Kasana requires exactly 3 cover images".to_string(),
            ));
        }
        covers.into_iter().map(|img| img.to_rgb8()).collect()
    } else {
        // Default cover colors (white background)
        vec![
            ImageBuffer::from_pixel(width, height, Rgb([255, 255, 255])),
            ImageBuffer::from_pixel(width, height, Rgb([255, 255, 255])),
            ImageBuffer::from_pixel(width, height, Rgb([255, 255, 255])),
        ]
    };

    // Process each pixel
    for y in 0..height {
        for x in 0..width {
            let secret_pixel = rgb.get_pixel(x, y);
            let [r, g, b] = secret_pixel.0;

            // Process each share
            for share_idx in 0..3 {
                let cover_pixel = covers[share_idx].get_pixel(x, y);

                // Create 5x5 block filled with cover pixel color
                let mut block = ImageBuffer::from_pixel(5, 5, *cover_pixel);

                // Process each color channel
                for (channel_idx, &channel_value) in [r, g, b].iter().enumerate() {
                    let bit_positions = &components[channel_idx];

                    // Process each bit of the channel (8 bits)
                    for (bit_idx, &(bit_y, bit_x)) in bit_positions.iter().enumerate() {
                        let bit = (channel_value >> bit_idx) & 1;

                        // Encode bit: 1 = black (0,0,0), 0 = dark grey (30,30,30)
                        let pixel_color = if bit == 1 {
                            Rgb([0, 0, 0]) // Black for bit 1
                        } else {
                            Rgb([30, 30, 30]) // Dark grey for bit 0
                        };

                        block.put_pixel(bit_x, bit_y, pixel_color);
                    }
                }

                // Paste the 5x5 block into the share
                let base_x = x * 5;
                let base_y = y * 5;
                for block_y in 0..5 {
                    for block_x in 0..5 {
                        let pixel = block.get_pixel(block_x, block_y);
                        shares[share_idx].put_pixel(base_x + block_x, base_y + block_y, *pixel);
                    }
                }
            }
        }
    }

    // Convert to Share objects
    let result: Vec<Share> = shares
        .into_iter()
        .enumerate()
        .map(|(i, img)| {
            Share::new(
                DynamicImage::ImageRgb8(img),
                i + 1,
                3,
                width,
                height,
                5, // 5x5 pixel expansion
                has_cover_images,
            )
        })
        .collect();

    Ok(result)
}

/// Dhiman-Kasana decryption using XOR operation
fn dhiman_kasana_decrypt(shares: &[Share], _config: &VCConfig) -> Result<DynamicImage> {
    if shares.len() < 3 {
        return Err(VCError::InsufficientShares {
            required: 3,
            provided: shares.len(),
        });
    }

    // Get dimensions from the first share (expanded)
    let (expanded_width, expanded_height) = shares[0].dimensions();

    // Original dimensions (accounting for 5x5 pixel expansion)
    let width = expanded_width / 5;
    let height = expanded_height / 5;

    let mut result = ImageBuffer::new(width, height);

    // Bit position coordinates for each RGB channel
    let components = [
        // R channel positions
        [
            (4, 4),
            (4, 2),
            (3, 1),
            (2, 3),
            (2, 0),
            (1, 4),
            (1, 2),
            (0, 1),
        ],
        // G channel positions
        [
            (4, 3),
            (3, 4),
            (3, 2),
            (2, 1),
            (1, 3),
            (1, 0),
            (0, 4),
            (0, 2),
        ],
        // B channel positions
        [
            (4, 1),
            (3, 3),
            (3, 0),
            (2, 4),
            (2, 2),
            (1, 1),
            (0, 3),
            (0, 0),
        ],
    ];

    // Extract RGB images from shares
    let share_images: Vec<&ImageBuffer<Rgb<u8>, Vec<u8>>> = shares
        .iter()
        .map(|share| {
            if let DynamicImage::ImageRgb8(img) = &share.image {
                img
            } else {
                panic!("Share is not RGB format");
            }
        })
        .collect();

    // Process each original pixel
    for y in 0..height {
        for x in 0..width {
            let mut reconstructed_pixel = [0u8; 3];

            // Process each color channel
            for channel_idx in 0..3 {
                let bit_positions = &components[channel_idx];
                let mut channel_value = 0u8;

                // Extract bits from each position
                for (bit_idx, &(bit_y, bit_x)) in bit_positions.iter().enumerate() {
                    let base_x = x * 5;
                    let base_y = y * 5;

                    // Get the pixel from the corresponding share at the bit position
                    let pixel = share_images[channel_idx].get_pixel(base_x + bit_x, base_y + bit_y);

                    // Decode bit: (0,0,0) = 1, anything else = 0
                    let bit = if pixel.0 == [0, 0, 0] { 1 } else { 0 };

                    channel_value |= bit << bit_idx;
                }

                reconstructed_pixel[channel_idx] = channel_value;
            }

            result.put_pixel(x, y, Rgb(reconstructed_pixel));
        }
    }

    Ok(DynamicImage::ImageRgb8(result))
}

/// Yamaguchi-Nakajima Extended Visual Cryptography for Natural Images
/// Based on Nakajima & Yamaguchi (2002)
fn yamaguchi_nakajima_encrypt(
    image: &DynamicImage,
    config: &VCConfig,
    cover_images: Option<Vec<DynamicImage>>,
) -> Result<Vec<Share>> {
    if config.num_shares != 2 {
        return Err(VCError::InvalidConfiguration(
            "Yamaguchi-Nakajima scheme requires exactly 2 shares".to_string(),
        ));
    }

    let cover_images = cover_images.ok_or_else(|| {
        VCError::CoverImageError(
            "Yamaguchi-Nakajima scheme requires 2 cover images (sheet images)".to_string(),
        )
    })?;

    if cover_images.len() != 2 {
        return Err(VCError::CoverImageError(
            "Yamaguchi-Nakajima scheme requires exactly 2 cover images".to_string(),
        ));
    }

    let target = image.to_luma8();
    let sheet1 = cover_images[0].to_luma8();
    let sheet2 = cover_images[1].to_luma8();
    let (width, height) = (target.width(), target.height());

    // Resize all images to same size
    let sheet1 = image::imageops::resize(
        &sheet1,
        width,
        height,
        image::imageops::FilterType::Lanczos3,
    );
    let sheet2 = image::imageops::resize(
        &sheet2,
        width,
        height,
        image::imageops::FilterType::Lanczos3,
    );

    // Number of subpixels per pixel (default 16 for 4x4 subpixel structure)
    let m = config.block_size.max(4);
    let sub_size = (m as f64).sqrt() as usize;

    // Contrast adjustment
    let contrast = 0.6;
    let l = (1.0 - contrast) / 2.0; // Lower bound

    // Process images with contrast adjustment and halftoning
    let sheet1_processed = apply_contrast_and_halftone(&sheet1, contrast, l);
    let sheet2_processed = apply_contrast_and_halftone(&sheet2, contrast, l);
    let target_processed = apply_halftone_target(&target, contrast);

    // Create output sheets with subpixel expansion
    let out_width = width * sub_size as u32;
    let out_height = height * sub_size as u32;
    let mut out_sheet1 = ImageBuffer::new(out_width, out_height);
    let mut out_sheet2 = ImageBuffer::new(out_width, out_height);

    let mut rng = rand::rng();

    // Process each pixel
    for y in 0..height {
        for x in 0..width {
            // Get transparency values (0.0 to 1.0)
            let t1 = sheet1_processed.get_pixel(x, y)[0] as f64 / 255.0;
            let t2 = sheet2_processed.get_pixel(x, y)[0] as f64 / 255.0;
            let tt = target_processed.get_pixel(x, y)[0] as f64 / 255.0;

            // Adjust triplet if constraint violated
            let (t1, t2, tt) = adjust_triplet(t1, t2, tt);

            // Convert to subpixel counts
            let s1 = (t1 * m as f64).round() as usize;
            let s2 = (t2 * m as f64).round() as usize;
            let st = (tt * m as f64).round() as usize;

            // Generate valid Boolean matrices
            let matrices = generate_boolean_matrices(s1, s2, st, m);

            if !matrices.is_empty() {
                // Randomly select a matrix
                let matrix = &matrices[rng.random_range(0..matrices.len())];

                // Fill subpixels
                for i in 0..sub_size {
                    for j in 0..sub_size {
                        let idx = i * sub_size + j;
                        if idx < m {
                            let pixel_val1 = if matrix[0][idx] == 1 { 255 } else { 0 };
                            let pixel_val2 = if matrix[1][idx] == 1 { 255 } else { 0 };

                            out_sheet1.put_pixel(
                                x * sub_size as u32 + j as u32,
                                y * sub_size as u32 + i as u32,
                                Luma([pixel_val1]),
                            );
                            out_sheet2.put_pixel(
                                x * sub_size as u32 + j as u32,
                                y * sub_size as u32 + i as u32,
                                Luma([pixel_val2]),
                            );
                        }
                    }
                }
            } else {
                // Fallback: fill with random pattern
                for i in 0..sub_size {
                    for j in 0..sub_size {
                        let val1 = if rng.random_bool(0.5) { 255 } else { 0 };
                        let val2 = if rng.random_bool(0.5) { 255 } else { 0 };

                        out_sheet1.put_pixel(
                            x * sub_size as u32 + j as u32,
                            y * sub_size as u32 + i as u32,
                            Luma([val1]),
                        );
                        out_sheet2.put_pixel(
                            x * sub_size as u32 + j as u32,
                            y * sub_size as u32 + i as u32,
                            Luma([val2]),
                        );
                    }
                }
            }
        }
    }

    Ok(vec![
        Share::new(
            DynamicImage::ImageLuma8(out_sheet1),
            1,
            2,
            width,
            height,
            sub_size,
            true,
        ),
        Share::new(
            DynamicImage::ImageLuma8(out_sheet2),
            2,
            2,
            width,
            height,
            sub_size,
            true,
        ),
    ])
}

/// Yamaguchi-Nakajima decryption using AND operation
fn yamaguchi_nakajima_decrypt(shares: &[Share], _config: &VCConfig) -> Result<DynamicImage> {
    if shares.len() < 2 {
        return Err(VCError::InsufficientShares {
            required: 2,
            provided: shares.len(),
        });
    }

    let (expanded_width, expanded_height) = shares[0].dimensions();

    // Extract share images
    let sheet1 = if let DynamicImage::ImageLuma8(img) = &shares[0].image {
        img
    } else {
        return Err(VCError::DecryptionError(
            "Share 1 is not grayscale".to_string(),
        ));
    };

    let sheet2 = if let DynamicImage::ImageLuma8(img) = &shares[1].image {
        img
    } else {
        return Err(VCError::DecryptionError(
            "Share 2 is not grayscale".to_string(),
        ));
    };

    // Create result image with expanded dimensions (no downsampling)
    let mut result = ImageBuffer::new(expanded_width, expanded_height);

    // Reveal target using Boolean AND operation
    for y in 0..expanded_height {
        for x in 0..expanded_width {
            let pixel1 = sheet1.get_pixel(x, y)[0];
            let pixel2 = sheet2.get_pixel(x, y)[0];

            // Boolean AND: both must be white (255) for result to be white
            let result_pixel = if pixel1 == 255 && pixel2 == 255 {
                255
            } else {
                0
            };
            result.put_pixel(x, y, Luma([result_pixel]));
        }
    }

    Ok(DynamicImage::ImageLuma8(result))
}

/// Apply contrast adjustment and Floyd-Steinberg error diffusion halftoning
fn apply_contrast_and_halftone(
    image: &ImageBuffer<Luma<u8>, Vec<u8>>,
    contrast: f64,
    l: f64,
) -> ImageBuffer<Luma<u8>, Vec<u8>> {
    let (width, height) = (image.width(), image.height());
    let mut working_image = ImageBuffer::new(width, height);

    // Apply contrast adjustment
    for y in 0..height {
        for x in 0..width {
            let pixel = image.get_pixel(x, y)[0] as f64 / 255.0;
            let adjusted = l + pixel * contrast;
            working_image.put_pixel(x, y, Luma([(adjusted * 255.0) as u8]));
        }
    }

    // Apply Floyd-Steinberg error diffusion
    floyd_steinberg_dithering(&working_image)
}

/// Apply halftoning to target image
fn apply_halftone_target(
    image: &ImageBuffer<Luma<u8>, Vec<u8>>,
    contrast: f64,
) -> ImageBuffer<Luma<u8>, Vec<u8>> {
    let (width, height) = (image.width(), image.height());
    let mut working_image = ImageBuffer::new(width, height);

    // Apply contrast adjustment (different for target)
    for y in 0..height {
        for x in 0..width {
            let pixel = image.get_pixel(x, y)[0] as f64 / 255.0;
            let adjusted = pixel * contrast;
            working_image.put_pixel(x, y, Luma([(adjusted * 255.0) as u8]));
        }
    }

    // Apply Floyd-Steinberg error diffusion
    floyd_steinberg_dithering(&working_image)
}

/// Floyd-Steinberg error diffusion algorithm
fn floyd_steinberg_dithering(
    image: &ImageBuffer<Luma<u8>, Vec<u8>>,
) -> ImageBuffer<Luma<u8>, Vec<u8>> {
    let (width, height) = (image.width(), image.height());
    let mut working = vec![vec![0.0; width as usize]; height as usize];
    let mut result = ImageBuffer::new(width, height);

    // Copy to working array
    for y in 0..height {
        for x in 0..width {
            working[y as usize][x as usize] = image.get_pixel(x, y)[0] as f64 / 255.0;
        }
    }

    // Floyd-Steinberg error diffusion
    for y in 0..height {
        for x in 0..width {
            let old_pixel = working[y as usize][x as usize];
            let new_pixel = if old_pixel > 0.5 { 1.0 } else { 0.0 };
            result.put_pixel(x, y, Luma([(new_pixel * 255.0) as u8]));

            let error = old_pixel - new_pixel;

            // Distribute error to neighboring pixels
            if x + 1 < width {
                working[y as usize][(x + 1) as usize] += error * 7.0 / 16.0;
            }
            if y + 1 < height {
                if x > 0 {
                    working[(y + 1) as usize][(x - 1) as usize] += error * 3.0 / 16.0;
                }
                working[(y + 1) as usize][x as usize] += error * 5.0 / 16.0;
                if x + 1 < width {
                    working[(y + 1) as usize][(x + 1) as usize] += error * 1.0 / 16.0;
                }
            }
        }
    }

    result
}

/// Adjust triplet values if they violate the constraint
fn adjust_triplet(t1: f64, t2: f64, tt: f64) -> (f64, f64, f64) {
    let min_tt = (0.0_f64).max(t1 + t2 - 1.0);
    let max_tt = t1.min(t2);

    let tt_adj = if tt < min_tt {
        min_tt
    } else if tt > max_tt {
        max_tt
    } else {
        tt
    };

    (t1, t2, tt_adj)
}

/// Generate all valid Boolean matrices for given transparency values
fn generate_boolean_matrices(s1: usize, s2: usize, st: usize, m: usize) -> Vec<Vec<Vec<u8>>> {
    // Calculate subpixel pair counts
    let p11 = st; // Both transparent
    let p10 = s1.saturating_sub(st); // Sheet1 transparent, sheet2 opaque
    let p01 = s2.saturating_sub(st); // Sheet1 opaque, sheet2 transparent
    let p00 = m.saturating_sub(p11 + p10 + p01); // Both opaque

    // Check validity
    if p11 + p10 + p01 + p00 != m {
        return vec![];
    }

    // Create base matrix patterns
    let mut base_patterns = Vec::new();

    // Add transparent-transparent pairs
    for _ in 0..p11 {
        base_patterns.push([1, 1]);
    }
    // Add transparent-opaque pairs
    for _ in 0..p10 {
        base_patterns.push([1, 0]);
    }
    // Add opaque-transparent pairs
    for _ in 0..p01 {
        base_patterns.push([0, 1]);
    }
    // Add opaque-opaque pairs
    for _ in 0..p00 {
        base_patterns.push([0, 0]);
    }

    // Generate a single valid matrix (could be extended to generate all permutations)
    let mut rng = rand::rng();
    base_patterns.shuffle(&mut rng);

    let matrix = vec![
        base_patterns
            .iter()
            .map(|pair| pair[0])
            .collect::<Vec<u8>>(),
        base_patterns
            .iter()
            .map(|pair| pair[1])
            .collect::<Vec<u8>>(),
    ];

    vec![matrix]
}