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
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
//! Enhanced Image handling module for PDF documents with 100% quality preservation
//!
//! This module provides functionality to embed various image formats (PNG, JPEG)
//! into PDF documents with PERFECT quality preservation including:
//! - 16-bit color depth support
//! - Indexed PNG support
//! - ICC color profile preservation
//! - Gamma correction
use lopdf::content::Operation;
use lopdf::{dictionary, Dictionary, Document, Object, ObjectId, Stream};
use std::fs::File;
use std::io::{Cursor, Read};
use std::path::Path;
use jpeg_decoder;
use miniz_oxide::deflate;
use png;
/// Supported image formats
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum ImageFormat {
PNG,
JPEG,
WebP,
}
/// Image color space with enhanced support
#[derive(Debug, Clone, PartialEq)]
pub enum ColorSpace {
DeviceRGB,
DeviceGray,
DeviceCMYK,
/// Indexed color with palette
Indexed {
base: Box<ColorSpace>,
palette: Vec<u8>,
hival: u32,
},
/// ICC-based color space
ICCBased(Vec<u8>), // ICC profile data
}
impl ColorSpace {
/// Converts to PDF object
pub fn to_pdf_object(&self, doc: &mut Document) -> Object {
match self {
ColorSpace::DeviceRGB => Object::Name(b"DeviceRGB".to_vec()),
ColorSpace::DeviceGray => Object::Name(b"DeviceGray".to_vec()),
ColorSpace::DeviceCMYK => Object::Name(b"DeviceCMYK".to_vec()),
ColorSpace::Indexed {
base,
palette,
hival,
} => {
// Create indexed color space array
vec![
Object::Name(b"Indexed".to_vec()),
base.to_pdf_object(doc),
Object::Integer(*hival as i64),
Object::String(palette.clone(), lopdf::StringFormat::Literal),
]
.into()
}
ColorSpace::ICCBased(profile_data) => {
// Create ICC profile stream
let icc_dict = dictionary! {
"N" => self.components() as i32, // Number of components
"Filter" => "FlateDecode",
};
let compressed = deflate::compress_to_vec_zlib(profile_data, 9);
let icc_stream = Stream::new(icc_dict, compressed);
let icc_id = doc.add_object(icc_stream);
vec![
Object::Name(b"ICCBased".to_vec()),
Object::Reference(icc_id),
]
.into()
}
}
}
/// Gets the number of components for this color space
pub fn components(&self) -> u8 {
match self {
ColorSpace::DeviceGray => 1,
ColorSpace::DeviceRGB => 3,
ColorSpace::DeviceCMYK => 4,
ColorSpace::Indexed { .. } => 1, // Indexed uses 1 component (index values)
ColorSpace::ICCBased(profile) => {
// Parse the ICC profile header: bytes 16-19 are the colour space signature.
// ICC spec: "RGB " → 3, "GRAY" → 1, "CMYK" → 4, everything else (Lab, XYZ, …) → 3.
if profile.len() >= 20 {
match &profile[16..20] {
b"GRAY" => 1,
b"CMYK" => 4,
_ => 3, // RGB , Lab , XYZ , HSV , HLS , Luv , YCbCr …
}
} else {
3 // Malformed/empty profile; default to RGB.
}
}
}
}
}
/// Enhanced metadata about an image
#[derive(Debug, Clone)]
pub struct ImageMetadata {
/// Width in pixels
pub width: u32,
/// Height in pixels
pub height: u32,
/// Bits per component (8 or 16)
pub bits_per_component: u8,
/// Color space
pub color_space: ColorSpace,
/// Whether the image has an alpha channel
pub has_alpha: bool,
/// Image format
pub format: ImageFormat,
/// Gamma value (if present)
pub gamma: Option<f32>,
/// ICC profile data (if present)
pub icc_profile: Option<Vec<u8>>,
/// sRGB intent (if present)
pub srgb_intent: Option<u8>,
}
/// Represents an image that can be embedded in a PDF with 100% quality
#[derive(Debug, Clone)]
pub struct Image {
/// Image metadata
pub metadata: ImageMetadata,
/// Raw image data
pub data: Vec<u8>,
/// Alpha channel data (for PNG with transparency)
pub alpha_data: Option<Vec<u8>>,
/// Original file path (if loaded from file)
pub source_path: Option<String>,
}
impl Image {
/// Loads an image from a file with 100% quality preservation
pub fn from_file(path: impl AsRef<Path>) -> Result<Self, Box<dyn std::error::Error>> {
let path = path.as_ref();
let mut file = File::open(path)?;
let mut buffer = Vec::new();
file.read_to_end(&mut buffer)?;
let format = Self::detect_format(&buffer)?;
let source_path = Some(path.to_string_lossy().to_string());
match format {
ImageFormat::PNG => Self::from_png_data_enhanced(buffer, source_path),
ImageFormat::JPEG => Self::from_jpeg_data(buffer, source_path),
ImageFormat::WebP => Self::from_webp_data(buffer, source_path),
}
}
/// Creates an image from PNG data with PERFECT quality preservation
pub fn from_png_data_enhanced(
data: Vec<u8>,
source_path: Option<String>,
) -> Result<Self, Box<dyn std::error::Error>> {
let source = Cursor::new(&data);
let decoder = png::Decoder::new(source);
let mut reader = decoder.read_info()?;
// Get image info and extract all needed data before borrowing mutably
let info = reader.info();
let width = info.width;
let height = info.height;
let color_type = info.color_type;
let bit_depth = info.bit_depth as u8;
// Extract palette and transparency info ahead of time as owned data
let palette_data = info.palette.clone();
let transparency_data = info.trns.clone();
let _buffer_size = reader
.output_buffer_size()
.ok_or("Failed to get output buffer size")?;
// Extract metadata from PNG chunks
let mut gamma = None;
let mut icc_profile = None;
let mut srgb_intent = None;
// Parse PNG chunks for metadata
if let Ok(png_data) = Self::extract_png_chunks(&data) {
gamma = png_data.gamma;
icc_profile = png_data.icc_profile;
srgb_intent = png_data.srgb_intent;
}
// Handle all PNG color types including indexed
let (color_space, has_alpha, processed_data, alpha_data) = match color_type {
png::ColorType::Rgb => {
let buffer_size = reader
.output_buffer_size()
.ok_or("Failed to get output buffer size")?;
let mut img_data = vec![0u8; buffer_size];
reader.next_frame(&mut img_data)?;
// Preserve 16-bit if present
let data = if bit_depth == 16 {
img_data // Keep 16-bit data
} else {
img_data
};
let cs = if let Some(icc) = icc_profile.clone() {
ColorSpace::ICCBased(icc)
} else {
ColorSpace::DeviceRGB
};
(cs, false, data, None)
}
png::ColorType::Rgba => {
let buffer_size = reader
.output_buffer_size()
.ok_or("Failed to get output buffer size")?;
let mut img_data = vec![0u8; buffer_size];
reader.next_frame(&mut img_data)?;
// Separate RGBA channels preserving bit depth
let (rgb_data, alpha) = if bit_depth == 16 {
// Handle 16-bit RGBA
let mut rgb_data = Vec::with_capacity((img_data.len() * 3) / 4);
let mut alpha_data = Vec::with_capacity(img_data.len() / 4);
for chunk in img_data.chunks_exact(8) {
// 4 channels × 2 bytes
rgb_data.extend_from_slice(&chunk[0..2]); // R
rgb_data.extend_from_slice(&chunk[2..4]); // G
rgb_data.extend_from_slice(&chunk[4..6]); // B
alpha_data.extend_from_slice(&chunk[6..8]); // A
}
(rgb_data, Some(alpha_data))
} else {
// Handle 8-bit RGBA
let mut rgb_data = Vec::with_capacity((img_data.len() * 3) / 4);
let mut alpha_data = Vec::with_capacity(img_data.len() / 4);
for chunk in img_data.chunks_exact(4) {
rgb_data.push(chunk[0]);
rgb_data.push(chunk[1]);
rgb_data.push(chunk[2]);
alpha_data.push(chunk[3]);
}
(rgb_data, Some(alpha_data))
};
let cs = if let Some(icc) = icc_profile.clone() {
ColorSpace::ICCBased(icc)
} else {
ColorSpace::DeviceRGB
};
(cs, true, rgb_data, alpha)
}
png::ColorType::Grayscale => {
let buffer_size = reader
.output_buffer_size()
.ok_or("Failed to get output buffer size")?;
let mut img_data = vec![0u8; buffer_size];
reader.next_frame(&mut img_data)?;
(ColorSpace::DeviceGray, false, img_data, None)
}
png::ColorType::GrayscaleAlpha => {
let buffer_size = reader
.output_buffer_size()
.ok_or("Failed to get output buffer size")?;
let mut img_data = vec![0u8; buffer_size];
reader.next_frame(&mut img_data)?;
let (gray_data, alpha) = if bit_depth == 16 {
// Handle 16-bit GA
let mut gray = Vec::with_capacity(img_data.len() / 2);
let mut alpha_data = Vec::with_capacity(img_data.len() / 2);
for chunk in img_data.chunks_exact(4) {
gray.extend_from_slice(&chunk[0..2]);
alpha_data.extend_from_slice(&chunk[2..4]);
}
(gray, Some(alpha_data))
} else {
// Handle 8-bit GA
let mut gray = Vec::with_capacity(img_data.len() / 2);
let mut alpha_data = Vec::with_capacity(img_data.len() / 2);
for chunk in img_data.chunks_exact(2) {
gray.push(chunk[0]);
alpha_data.push(chunk[1]);
}
(gray, Some(alpha_data))
};
(ColorSpace::DeviceGray, true, gray_data, alpha)
}
png::ColorType::Indexed => {
// Handle indexed PNGs perfectly - use pre-extracted data
let palette = palette_data.as_ref().ok_or("Indexed PNG missing palette")?;
// Get buffer size and read data
let buffer_size = reader
.output_buffer_size()
.ok_or("Failed to get output buffer size")?;
let mut img_data = vec![0u8; buffer_size];
reader.next_frame(&mut img_data)?;
// Create indexed color space - palette is Vec<u8> with RGB values in sequence
let mut indexed_palette = Vec::with_capacity(palette.len());
indexed_palette.extend_from_slice(palette);
let indexed_cs = ColorSpace::Indexed {
base: Box::new(ColorSpace::DeviceRGB),
palette: indexed_palette,
hival: (palette.len() - 1) as u32,
};
// Check for transparency in indexed images
let alpha = if let Some(trns) = transparency_data.as_ref() {
// Convert transparency info to alpha channel
let mut alpha_data = Vec::with_capacity(img_data.len());
for &index in &img_data {
let alpha_value = trns.get(index as usize).copied().unwrap_or(255);
alpha_data.push(alpha_value);
}
Some(alpha_data)
} else {
None
};
(indexed_cs, alpha.is_some(), img_data, alpha)
}
};
let metadata = ImageMetadata {
width,
height,
bits_per_component: bit_depth,
color_space,
has_alpha,
format: ImageFormat::PNG,
gamma,
icc_profile,
srgb_intent,
};
Ok(Image {
metadata,
data: processed_data,
alpha_data,
source_path,
})
}
/// Extract PNG chunks for metadata preservation
fn extract_png_chunks(data: &[u8]) -> Result<PngChunkData, Box<dyn std::error::Error>> {
let mut gamma = None;
let mut icc_profile = None;
let mut srgb_intent = None;
// Simple PNG chunk parser
let mut pos = 8; // Skip PNG signature
while pos < data.len() - 12 {
let chunk_len =
u32::from_be_bytes([data[pos], data[pos + 1], data[pos + 2], data[pos + 3]])
as usize;
let chunk_type = &data[pos + 4..pos + 8];
match chunk_type {
b"gAMA" if chunk_len == 4 => {
let gamma_int = u32::from_be_bytes([
data[pos + 8],
data[pos + 9],
data[pos + 10],
data[pos + 11],
]);
gamma = Some(gamma_int as f32 / 100000.0);
}
b"iCCP" if chunk_len > 0 => {
// Extract ICC profile (skipping name and compression method)
let profile_start = pos + 8;
// Find null terminator for profile name
if let Some(null_pos) = data[profile_start..profile_start + chunk_len]
.iter()
.position(|&b| b == 0)
{
let compressed_start = profile_start + null_pos + 2; // +1 for null, +1 for compression
let compressed_data = &data[compressed_start..profile_start + chunk_len];
// Decompress ICC profile
if let Ok(decompressed) =
miniz_oxide::inflate::decompress_to_vec_zlib(compressed_data)
{
icc_profile = Some(decompressed);
}
}
}
b"sRGB" if chunk_len == 1 => {
srgb_intent = Some(data[pos + 8]);
}
_ => {}
}
pos += 12 + chunk_len; // 12 = length(4) + type(4) + crc(4)
}
Ok(PngChunkData {
gamma,
icc_profile,
srgb_intent,
})
}
/// Creates an image from PNG data (backwards compatibility)
pub fn from_png_data(
data: Vec<u8>,
source_path: Option<String>,
) -> Result<Self, Box<dyn std::error::Error>> {
Self::from_png_data_enhanced(data, source_path)
}
/// Creates an image from JPEG data
pub fn from_jpeg_data(
data: Vec<u8>,
source_path: Option<String>,
) -> Result<Self, Box<dyn std::error::Error>> {
// Use jpeg_decoder to get metadata
let mut decoder = jpeg_decoder::Decoder::new(&data[..]);
decoder.read_info()?;
let info = decoder.info().ok_or("Failed to read JPEG info")?;
// Extract ICC profile from JPEG if present
let icc_profile = Self::extract_jpeg_icc(&data);
let color_space = if let Some(icc) = icc_profile.clone() {
ColorSpace::ICCBased(icc)
} else {
match info.pixel_format {
jpeg_decoder::PixelFormat::RGB24 => ColorSpace::DeviceRGB,
jpeg_decoder::PixelFormat::L8 => ColorSpace::DeviceGray,
jpeg_decoder::PixelFormat::CMYK32 => ColorSpace::DeviceCMYK,
_ => return Err("Unsupported JPEG pixel format".into()),
}
};
let metadata = ImageMetadata {
width: info.width as u32,
height: info.height as u32,
bits_per_component: 8,
color_space,
has_alpha: false,
format: ImageFormat::JPEG,
gamma: None,
icc_profile,
srgb_intent: None,
};
Ok(Image {
metadata,
data, // For JPEG, we keep the original compressed data
alpha_data: None,
source_path,
})
}
/// Extract ICC profile from JPEG APP2 segments
fn extract_jpeg_icc(data: &[u8]) -> Option<Vec<u8>> {
// Simple JPEG APP2 parser for ICC profiles
let mut pos = 2; // Skip SOI marker
let mut icc_chunks = Vec::new();
while pos < data.len() - 4 {
if data[pos] == 0xFF {
let marker = data[pos + 1];
if marker == 0xE2 {
// APP2
let length = u16::from_be_bytes([data[pos + 2], data[pos + 3]]) as usize;
// Check for ICC_PROFILE
if pos + 4 + 14 <= data.len() && &data[pos + 4..pos + 16] == b"ICC_PROFILE\0" {
// Extract ICC chunk
let chunk_num = data[pos + 16];
let chunk_total = data[pos + 17];
let chunk_data = &data[pos + 18..pos + 2 + length];
icc_chunks.push((chunk_num, chunk_data.to_vec()));
if icc_chunks.len() == chunk_total as usize {
// Sort and combine chunks
icc_chunks.sort_by_key(|c| c.0);
let mut profile = Vec::new();
for (_, chunk) in icc_chunks {
profile.extend(chunk);
}
return Some(profile);
}
}
pos += 2 + length;
} else if marker == 0xD9 {
// EOI
break;
} else if marker >= 0xD0 && marker <= 0xD7 {
// RST markers
pos += 2;
} else if marker != 0x00 && marker != 0xFF {
// Other markers with length
if pos + 3 < data.len() {
let length = u16::from_be_bytes([data[pos + 2], data[pos + 3]]) as usize;
pos += 2 + length;
} else {
break;
}
} else {
pos += 1;
}
} else {
pos += 1;
}
}
None
}
/// Detects the format of image data
fn detect_format(data: &[u8]) -> Result<ImageFormat, Box<dyn std::error::Error>> {
if data.len() < 8 {
return Err("Invalid image data".into());
}
// Check PNG signature
if data[0..8] == [137, 80, 78, 71, 13, 10, 26, 10] {
return Ok(ImageFormat::PNG);
}
// Check JPEG signature
if data[0..2] == [0xFF, 0xD8] {
return Ok(ImageFormat::JPEG);
}
// Check WebP signature (RIFF....WEBP)
if data.len() >= 12 && &data[0..4] == b"RIFF" && &data[8..12] == b"WEBP" {
return Ok(ImageFormat::WebP);
}
Err("Unknown image format".into())
}
/// Gets the dimensions of the image
pub fn dimensions(&self) -> (u32, u32) {
(self.metadata.width, self.metadata.height)
}
/// Gets the aspect ratio (width / height)
pub fn aspect_ratio(&self) -> f32 {
self.metadata.width as f32 / self.metadata.height as f32
}
/// Creates an image from WebP data
pub fn from_webp_data(
data: Vec<u8>,
source_path: Option<String>,
) -> Result<Self, Box<dyn std::error::Error>> {
use image::ImageReader;
let reader = ImageReader::new(Cursor::new(&data))
.with_guessed_format()?;
let dynamic_image = reader.decode()?;
let width = dynamic_image.width();
let height = dynamic_image.height();
let (color_space, has_alpha, processed_data, alpha_data) = if dynamic_image.color().has_alpha() {
let rgba = dynamic_image.to_rgba8();
let raw = rgba.into_raw();
let pixel_count = (width * height) as usize;
let mut rgb_data = Vec::with_capacity(pixel_count * 3);
let mut alpha = Vec::with_capacity(pixel_count);
for chunk in raw.chunks(4) {
rgb_data.extend_from_slice(&chunk[..3]);
alpha.push(chunk[3]);
}
(ColorSpace::DeviceRGB, true, rgb_data, Some(alpha))
} else {
let rgb = dynamic_image.to_rgb8();
(ColorSpace::DeviceRGB, false, rgb.into_raw(), None)
};
let metadata = ImageMetadata {
width,
height,
bits_per_component: 8,
color_space,
has_alpha,
format: ImageFormat::WebP,
gamma: None,
icc_profile: None,
srgb_intent: None,
};
Ok(Image {
metadata,
data: processed_data,
alpha_data,
source_path,
})
}
/// Creates an image from bytes (for WASM compatibility)
pub fn from_bytes(
data: Vec<u8>,
source_path: Option<String>,
) -> Result<Self, Box<dyn std::error::Error>> {
let format = Self::detect_format(&data)?;
match format {
ImageFormat::PNG => Self::from_png_data_enhanced(data, source_path),
ImageFormat::JPEG => Self::from_jpeg_data(data, source_path),
ImageFormat::WebP => Self::from_webp_data(data, source_path),
}
}
/// Creates an image from PNG bytes (alias for consistency)
pub fn from_png_bytes(data: Vec<u8>) -> Result<Self, Box<dyn std::error::Error>> {
Self::from_png_data_enhanced(data, None)
}
/// Creates an image from JPEG bytes (alias for consistency)
pub fn from_jpeg_bytes(data: Vec<u8>) -> Result<Self, Box<dyn std::error::Error>> {
Self::from_jpeg_data(data, None)
}
/// Creates an image from WebP bytes (alias for consistency)
pub fn from_webp_bytes(data: Vec<u8>) -> Result<Self, Box<dyn std::error::Error>> {
Self::from_webp_data(data, None)
}
}
/// Helper struct for PNG chunk data
#[derive(Debug)]
struct PngChunkData {
gamma: Option<f32>,
icc_profile: Option<Vec<u8>>,
srgb_intent: Option<u8>,
}
/// Enhanced Manager for embedding images in PDF documents with 100% quality
pub struct ImageManager {
/// Cached images with their PDF object IDs
images: Vec<(Image, ObjectId, Option<ObjectId>)>, // (image, image_id, mask_id)
/// Counter for generating unique resource names
name_counter: usize,
}
impl Default for ImageManager {
fn default() -> Self {
Self::new()
}
}
impl ImageManager {
/// Creates a new image manager
pub fn new() -> Self {
ImageManager {
images: Vec::new(),
name_counter: 0,
}
}
/// Embeds an image in the PDF document with perfect quality
pub fn embed_image(
&mut self,
doc: &mut Document,
image: Image,
) -> Result<ObjectId, Box<dyn std::error::Error>> {
// Check if this image was already embedded
for (cached_img, obj_id, _) in &self.images {
if let (Some(path1), Some(path2)) = (&cached_img.source_path, &image.source_path) {
if path1 == path2 {
return Ok(*obj_id);
}
}
}
let (image_id, mask_id) = match image.metadata.format {
ImageFormat::JPEG => (self.embed_jpeg(doc, &image)?, None),
ImageFormat::PNG | ImageFormat::WebP => {
let (img_id, mask) = self.embed_png_enhanced(doc, &image)?;
(img_id, mask)
}
};
self.images.push((image, image_id, mask_id));
Ok(image_id)
}
/// Embeds a JPEG image
fn embed_jpeg(
&self,
doc: &mut Document,
image: &Image,
) -> Result<ObjectId, Box<dyn std::error::Error>> {
let mut dict = dictionary! {
"Type" => "XObject",
"Subtype" => "Image",
"Width" => image.metadata.width as i32,
"Height" => image.metadata.height as i32,
"BitsPerComponent" => image.metadata.bits_per_component as i32,
"Filter" => "DCTDecode",
};
// Set color space with ICC support
dict.set("ColorSpace", image.metadata.color_space.to_pdf_object(doc));
let stream = Stream::new(dict, image.data.clone());
Ok(doc.add_object(stream))
}
/// Embeds a PNG image with PERFECT quality preservation
fn embed_png_enhanced(
&self,
doc: &mut Document,
image: &Image,
) -> Result<(ObjectId, Option<ObjectId>), Box<dyn std::error::Error>> {
// Create soft mask for alpha channel if present
let mask_id = if let Some(ref alpha_data) = image.alpha_data {
let mut mask_dict = dictionary! {
"Type" => "XObject",
"Subtype" => "Image",
"Width" => image.metadata.width as i32,
"Height" => image.metadata.height as i32,
"BitsPerComponent" => image.metadata.bits_per_component as i32,
"ColorSpace" => "DeviceGray",
"Filter" => "FlateDecode",
};
// Add decode array for proper alpha interpretation
if image.metadata.bits_per_component == 16 {
mask_dict.set("Decode", vec![0.into(), 1.into()]);
} else {
mask_dict.set("Decode", vec![0.into(), 1.into()]);
}
// Compress alpha data with maximum compression
let compressed = deflate::compress_to_vec_zlib(alpha_data, 9);
let mask_stream = Stream::new(mask_dict, compressed);
Some(doc.add_object(mask_stream))
} else {
None
};
// Create main image dictionary
let mut dict = dictionary! {
"Type" => "XObject",
"Subtype" => "Image",
"Width" => image.metadata.width as i32,
"Height" => image.metadata.height as i32,
"BitsPerComponent" => image.metadata.bits_per_component as i32,
"Filter" => "FlateDecode",
};
// Set color space with full support
dict.set("ColorSpace", image.metadata.color_space.to_pdf_object(doc));
// Add gamma correction if present
if let Some(gamma) = image.metadata.gamma {
// Create CalRGB or CalGray color space with gamma
let cal_dict = match image.metadata.color_space {
ColorSpace::DeviceRGB => {
dictionary! {
"WhitePoint" => vec![0.9505.into(), 1.0.into(), 1.0890.into()],
"Gamma" => vec![gamma.into(), gamma.into(), gamma.into()],
}
}
ColorSpace::DeviceGray => {
dictionary! {
"WhitePoint" => vec![0.9505.into(), 1.0.into(), 1.0890.into()],
"Gamma" => gamma,
}
}
_ => dictionary! {},
};
if !cal_dict.is_empty() {
let cal_id = doc.add_object(cal_dict);
let cal_space = match image.metadata.color_space {
ColorSpace::DeviceRGB => {
vec![Object::Name(b"CalRGB".to_vec()), Object::Reference(cal_id)]
}
ColorSpace::DeviceGray => {
vec![Object::Name(b"CalGray".to_vec()), Object::Reference(cal_id)]
}
_ => vec![],
};
if !cal_space.is_empty() {
dict.set("ColorSpace", cal_space);
}
}
}
// Add decode array for proper color interpretation
match image.metadata.color_space {
ColorSpace::DeviceRGB | ColorSpace::ICCBased(_)
if image.metadata.color_space.components() == 3 =>
{
dict.set(
"Decode",
vec![0.into(), 1.into(), 0.into(), 1.into(), 0.into(), 1.into()],
);
}
ColorSpace::DeviceGray | ColorSpace::ICCBased(_)
if image.metadata.color_space.components() == 1 =>
{
dict.set("Decode", vec![0.into(), 1.into()]);
}
ColorSpace::Indexed { .. } => {
let max_val = if image.metadata.bits_per_component == 16 {
65535
} else {
255
};
dict.set("Decode", vec![0.into(), max_val.into()]);
}
_ => {}
}
// Add soft mask reference if we have alpha
if let Some(mask_id) = mask_id {
dict.set("SMask", Object::Reference(mask_id));
}
// Add rendering intent if sRGB
if let Some(intent) = image.metadata.srgb_intent {
let intent_name = match intent {
0 => "Perceptual",
1 => "RelativeColorimetric",
2 => "Saturation",
3 => "AbsoluteColorimetric",
_ => "Perceptual",
};
dict.set("Intent", Object::Name(intent_name.as_bytes().to_vec()));
}
// Compress image data with maximum quality
let compressed = deflate::compress_to_vec_zlib(&image.data, 9);
let stream = Stream::new(dict, compressed);
let image_id = doc.add_object(stream);
Ok((image_id, mask_id))
}
/// Adds an image to page resources and returns its resource name
pub fn add_to_resources(&mut self, resources: &mut Dictionary, image_id: ObjectId) -> String {
let name = format!("Im{}", self.name_counter);
self.name_counter += 1;
// Get or create XObject dictionary
let xobject = if let Ok(Object::Dictionary(dict)) = resources.get(b"XObject") {
dict.clone()
} else {
Dictionary::new()
};
let mut xobject = xobject;
xobject.set(name.clone(), Object::Reference(image_id));
resources.set("XObject", xobject);
name
}
/// Creates a Do operation to draw an image
pub fn create_draw_operation(resource_name: &str) -> Operation {
Operation::new("Do", vec![Object::Name(resource_name.as_bytes().to_vec())])
}
/// Creates operations to draw an image at a specific position and size
pub fn draw_image(
resource_name: &str,
x: f32,
y: f32,
width: f32,
height: f32,
) -> Vec<Operation> {
vec![
Operation::new("q", vec![]),
Operation::new(
"cm",
vec![
width.into(),
0.0.into(),
0.0.into(),
height.into(),
x.into(),
y.into(),
],
),
Self::create_draw_operation(resource_name),
Operation::new("Q", vec![]),
]
}
/// Creates operations to draw an image with rotation
pub fn draw_image_rotated(
resource_name: &str,
x: f32,
y: f32,
width: f32,
height: f32,
angle_degrees: f32,
) -> Vec<Operation> {
let angle_rad = angle_degrees * std::f32::consts::PI / 180.0;
let cos = angle_rad.cos();
let sin = angle_rad.sin();
// Calculate center point for rotation
let cx = x + width / 2.0;
let cy = y + height / 2.0;
vec![
Operation::new("q", vec![]),
// Translate to center
Operation::new(
"cm",
vec![
1.0.into(),
0.0.into(),
0.0.into(),
1.0.into(),
cx.into(),
cy.into(),
],
),
// Rotate
Operation::new(
"cm",
vec![
cos.into(),
sin.into(),
(-sin).into(),
cos.into(),
0.0.into(),
0.0.into(),
],
),
// Scale and translate back
Operation::new(
"cm",
vec![
width.into(),
0.0.into(),
0.0.into(),
height.into(),
(-width / 2.0).into(),
(-height / 2.0).into(),
],
),
Self::create_draw_operation(resource_name),
Operation::new("Q", vec![]),
]
}
/// Creates operations to draw an image maintaining aspect ratio
pub fn draw_image_fit(
resource_name: &str,
image: &Image,
x: f32,
y: f32,
max_width: f32,
max_height: f32,
) -> Vec<Operation> {
let aspect = image.aspect_ratio();
let (width, height) = if max_width / max_height > aspect {
// Height is the limiting factor
(max_height * aspect, max_height)
} else {
// Width is the limiting factor
(max_width, max_width / aspect)
};
// Center the image in the box
let offset_x = (max_width - width) / 2.0;
let offset_y = (max_height - height) / 2.0;
Self::draw_image(resource_name, x + offset_x, y + offset_y, width, height)
}
/// Gets the number of embedded images
pub fn count(&self) -> usize {
self.images.len()
}
/// Gets an embedded image by index
pub fn get(&self, index: usize) -> Option<&(Image, ObjectId, Option<ObjectId>)> {
self.images.get(index)
}
/// Clears all cached images
pub fn clear(&mut self) {
self.images.clear();
self.name_counter = 0;
}
}
/// Helper struct for building image operations
pub struct ImageBuilder {
operations: Vec<Operation>,
}
impl Default for ImageBuilder {
fn default() -> Self {
Self::new()
}
}
impl ImageBuilder {
/// Creates a new image builder
pub fn new() -> Self {
ImageBuilder {
operations: Vec::new(),
}
}
/// Adds an image at a specific position
pub fn add_image(
mut self,
resource_name: &str,
x: f32,
y: f32,
width: f32,
height: f32,
) -> Self {
self.operations
.extend(ImageManager::draw_image(resource_name, x, y, width, height));
self
}
/// Adds a rotated image
pub fn add_image_rotated(
mut self,
resource_name: &str,
x: f32,
y: f32,
width: f32,
height: f32,
angle: f32,
) -> Self {
self.operations.extend(ImageManager::draw_image_rotated(
resource_name,
x,
y,
width,
height,
angle,
));
self
}
/// Adds an image with aspect ratio preservation
pub fn add_image_fit(
mut self,
resource_name: &str,
image: &Image,
x: f32,
y: f32,
max_width: f32,
max_height: f32,
) -> Self {
self.operations.extend(ImageManager::draw_image_fit(
resource_name,
image,
x,
y,
max_width,
max_height,
));
self
}
/// Adds a custom operation
pub fn add_operation(mut self, op: Operation) -> Self {
self.operations.push(op);
self
}
/// Builds the operations
pub fn build(self) -> Vec<Operation> {
self.operations
}
}
/// Convenience functions for common image operations
pub mod utils {
use super::*;
/// Creates a thumbnail grid of images
pub fn create_thumbnail_grid(
images: &[(String, &Image)], // (resource_name, image)
x: f32,
y: f32,
cols: usize,
thumb_size: f32,
spacing: f32,
) -> Vec<Operation> {
let mut operations = Vec::new();
for (index, (resource_name, image)) in images.iter().enumerate() {
let col = index % cols;
let row = index / cols;
let img_x = x + (col as f32) * (thumb_size + spacing);
let img_y = y - (row as f32) * (thumb_size + spacing);
operations.extend(ImageManager::draw_image_fit(
resource_name,
image,
img_x,
img_y,
thumb_size,
thumb_size,
));
}
operations
}
/// Creates a watermark from an image
pub fn create_watermark(
resource_name: &str,
page_width: f32,
page_height: f32,
opacity: f32,
) -> Vec<Operation> {
let mut operations = Vec::new();
// Save graphics state
operations.push(Operation::new("q", vec![]));
// Set opacity (requires ExtGState)
if opacity < 1.0 {
// This is a placeholder - actual implementation needs ExtGState
operations.push(Operation::new(
"%",
vec![Object::string_literal(format!("Opacity: {}", opacity))],
));
}
// Draw centered watermark
let size = page_width.min(page_height) * 0.5;
let x = (page_width - size) / 2.0;
let y = (page_height - size) / 2.0;
operations.extend(ImageManager::draw_image(resource_name, x, y, size, size));
// Restore graphics state
operations.push(Operation::new("Q", vec![]));
operations
}
}