Repository Public License
1// ORIX Convert — Packages/EnginePDF/Tests/EnginePDFTests/CompressAndImagesTests.swift2// Description: Compression (measured size drop, text and structure kept, never a bigger file) and image extraction.3// Author: Simon-Pierre Boucher — contact@spboucher.ai4import Foundation5import CoreGraphics6import PDFKit7import Testing8import ConvertCore9@testable import EnginePDF1011extension PDFEngineTests {12 @Suite struct Compression {13 typealias Op = PDFEngine.Operation1415 static func imageWidths(_ url: URL) -> [[String]] {16 guard let document = CGPDFDocument(url as CFURL) else { return [] }17 return (1...document.numberOfPages).map { number in18 let collector = ImageCollector()19 if let page = document.page(at: number) { collector.collect(page: page, number: number) }20 return collector.images.map { "\(CGPDFRead.int($0.dictionary, "Width") ?? 0)x\(CGPDFRead.int($0.dictionary, "Height") ?? 0) \(CGPDFRead.filters($0.dictionary).joined(separator: "+"))" }21 }22 }2324 @Test func imageHeavyDocumentShrinks() async throws {25 let (pdf, _) = Fx.imageHeavyPDF()26 let before = Fx.size(pdf)27 #expect(Compression.imageWidths(pdf) == [["3000x2000 DCTDecode"], ["1600x1200 FlateDecode"]])28 var previous = Int.max29 for (dpi, quality) in [(300, 0.8), (150, 0.6), (72, 0.3)] {30 let result = try await Fx.run(Fx.request(pdf, to: .pdf, op: Op.compress, ["compress.dpi": .int(dpi), "quality": .double(quality)]))31 let after = Fx.size(result.file)32 print("MEASURE compress dpi=\(dpi) quality=\(quality) before=\(before) after=\(after) ratio=\(String(format: "%.1f", Double(before) / Double(after)))x saved=\(result.output.facts["saved.percent"] ?? "")% images=\(Compression.imageWidths(result.file))")33 #expect(result.output.facts["result"] == "compressed" && result.output.facts["bytes.before"] == String(before) && result.output.facts["bytes.after"] == String(after))34 #expect(after < previous)35 previous = after36 #expect(Fx.structure(result.file).pages == 2)37 #expect(Fx.pdfText(result.file, page: 0).contains("Compressible photo page SELECTABLE") && Fx.pdfText(result.file, page: 1).contains("Lossless image page SELECTABLE"))38 // Images are downsampled to the requested resolution at their drawn size (6 in wide), not rasterised pages.39 let images = Compression.imageWidths(result.file)40 #expect(images.count == 2 && images[0].count == 1 && images[1].count == 1)41 let width = Int(images[0][0].split(separator: "x")[0]) ?? 042 #expect(abs(width - min(3000, 6 * dpi)) <= 2)43 #expect(images[0][0].hasSuffix("DCTDecode") && images[1][0].hasSuffix("DCTDecode"))44 }45 let standard = try await Fx.run(Fx.request(pdf, to: .pdf, op: Op.compress))46 #expect(Double(Fx.size(standard.file)) < 0.2 * Double(before)) // default settings: at least 5× smaller on this fixture47 }4849 @Test func neverReturnsABiggerFile() async throws {50 let pdf = Fx.simplePDF("text-only.pdf", pages: 3)51 let result = try await Fx.run(Fx.request(pdf, to: .pdf, op: Op.compress))52 #expect(result.output.facts["result"] == "already optimised" && result.hasWarning("already-optimised"))53 #expect(try Data(contentsOf: result.file) == Data(contentsOf: pdf))54 print("MEASURE compress text-only before=\(Fx.size(pdf)) attempt=\(result.output.facts["bytes.attempt"] ?? "") returned=\(Fx.size(result.file))")55 // Already-small images: 72 dpi JPEG drawn at 72 dpi.56 let small = Fx.write([Fx.noise(300, 200)], "small.jpg", type: .jpeg)57 let made = try await Fx.run(Fx.request(small, to: .pdf, from: .jpeg))58 let again = try await Fx.run(Fx.request(made.file, to: .pdf, op: Op.compress, ["quality": .double(1.0), "compress.dpi": .int(300)]))59 #expect(Fx.size(again.file) <= Fx.size(made.file))60 // With a password the source bytes cannot be returned: the copy is reported as not compressed.61 let protected = try await Fx.run(Fx.request(pdf, to: .pdf, op: Op.compress, ["output.password.user": .string("pw")]))62 #expect(protected.output.facts["result"] == "already optimised" && protected.hasWarning("already-optimised"))63 #expect(PDFDocument(url: protected.file)?.isLocked == true)64 }6566 @Test func structureSurvivesCompression() async throws {67 let (images, _) = Fx.imageHeavyPDF("struct-images.pdf")68 let merged = try await Fx.run(Fx.request(Fx.richPDF("struct-rich.pdf"), to: .pdf, op: Op.merge, more: [images]))69 let result = try await Fx.run(Fx.request(merged.file, to: .pdf, op: Op.compress))70 #expect(result.output.facts["result"] == "compressed")71 #expect(Fx.structure(result.file) == Fx.structure(merged.file))72 #expect(PDFInspector.infoDictionary(result.file, password: nil)["Title"] == "Rich Fixture")73 // The vector line of the photo page is still vector: rendering large shows a crisp edge, and the page has one image only.74 #expect(Compression.imageWidths(result.file)[4].count == 1)75 }76 }7778 @Suite struct ImageExtraction {79 typealias Op = PDFEngine.Operation8081 @Test func jpegBytesAndLosslessPixels() async throws {82 let (pdf, jpeg) = Fx.imageHeavyPDF("extract.pdf")83 let result = try await Fx.run(Fx.request(pdf, to: .folder, op: Op.extractImages))84 #expect(result.files.map(\.lastPathComponent) == ["extract-001-page1.jpg", "extract-002-page2.png"])85 #expect(try Data(contentsOf: result.files[0]) == jpeg) // byte-exact DCT stream86 let png = try #require(Fx.load(result.files[1])), original = Fx.noise(1600, 1200, seed: 7)87 #expect(png.width == 1600 && png.height == 1200)88 for (x, y) in [(0, 0), (800, 600), (1599, 1199), (37, 911)] {89 let a = Fx.pixel(png, x, y), b = Fx.pixel(original, x, y)90 #expect(a.r == b.r && a.g == b.g && a.b == b.b)91 }92 #expect(result.output.facts["images.jpeg"] == "1" && result.output.facts["images.png"] == "1" && result.output.facts["images.skipped"] == "0")93 let onlyBig = try await Fx.run(Fx.request(pdf, to: .folder, op: Op.extractImages, ["images.min-size": .int(1500), "pages": .string("1-")]))94 #expect(onlyBig.files.count == 1 && onlyBig.output.facts["images.below-min-size"] == "1")95 // The same image on several pages is written once.96 let twice = try await Fx.run(Fx.request(pdf, to: .pdf, op: Op.merge, more: [pdf]))97 let fromMerged = try await Fx.run(Fx.request(twice.file, to: .folder, op: Op.extractImages, ["pages": .string("1")]))98 #expect(fromMerged.files.count == 1)99 #expect(try Data(contentsOf: fromMerged.files[0]) == jpeg) // merging copies the stream, no re-encoding100 // Rotation and the OCR rewrite leave the JPEG stream untouched as well.101 let rotated = try await Fx.run(Fx.request(pdf, to: .pdf, op: Op.rotate))102 #expect(try Data(contentsOf: try await Fx.run(Fx.request(rotated.file, to: .folder, op: Op.extractImages, ["pages": .string("1")])).files[0]) == jpeg)103 try await withVision {104 let searchable = try await Fx.run(Fx.request(pdf, to: .pdf, op: Op.ocr, ["ocr.pages": .string("all"), "ocr.level": .string("fast")]))105 #expect(searchable.output.facts["result"] == "text layer added")106 #expect(try Data(contentsOf: try await Fx.run(Fx.request(searchable.file, to: .folder, op: Op.extractImages, ["pages": .string("1")])).files[0]) == jpeg)107 }108 }109110 @Test func handWrittenColourSpaces() async throws {111 func s(_ t: String) -> Data { Data(t.utf8) }112 let objects: [(Int, Data)] = [113 (1, s("<< /Type /Catalog /Pages 2 0 R >>")),114 (2, s("<< /Type /Pages /Kids [3 0 R] /Count 1 >>")),115 (3, s("<< /Type /Page /Parent 2 0 R /MediaBox [0 0 200 200] /Contents 4 0 R /Resources << /XObject << /A 5 0 R /B 6 0 R /C 7 0 R /D 8 0 R /F 10 0 R >> >> >>")),116 (4, Fx.stream("", s("q 50 0 0 50 0 0 cm /A Do Q q 50 0 0 50 60 0 cm /B Do Q q 50 0 0 50 120 0 cm /C Do Q q 50 0 0 50 0 60 cm /D Do Q q 1 0 0 1 0 120 cm /F Do Q"))),117 // Indexed RGB, 2 × 2: red, green / blue, white.118 (5, Fx.stream("/Type /XObject /Subtype /Image /Width 2 /Height 2 /ColorSpace [/Indexed /DeviceRGB 3 <FF0000 00FF00 0000FF FFFFFF>] /BitsPerComponent 8", Data([0, 1, 2, 3]))),119 // CMYK 1 × 1: magenta + yellow = red.120 (6, Fx.stream("/Type /XObject /Subtype /Image /Width 1 /Height 1 /ColorSpace /DeviceCMYK /BitsPerComponent 8", Data([0, 255, 255, 0]))),121 // 4-bit grey, 2 × 1: black, white.122 (7, Fx.stream("/Type /XObject /Subtype /Image /Width 2 /Height 1 /ColorSpace /DeviceGray /BitsPerComponent 4", Data([0x0F]))),123 // RGB with a soft mask: opaque left pixel, transparent right pixel.124 (8, Fx.stream("/Type /XObject /Subtype /Image /Width 2 /Height 1 /ColorSpace /DeviceRGB /BitsPerComponent 8 /SMask 9 0 R", Data([0, 255, 0, 0, 255, 0]))),125 (9, Fx.stream("/Type /XObject /Subtype /Image /Width 2 /Height 1 /ColorSpace /DeviceGray /BitsPerComponent 8", Data([255, 0]))),126 // A form XObject holding a Separation image (unsupported) and a truncated RGB image (malformed).127 (10, Fx.stream("/Type /XObject /Subtype /Form /BBox [0 0 50 50] /Resources << /XObject << /S 11 0 R /T 12 0 R >> >>", s("q 20 0 0 20 0 0 cm /S Do Q q 20 0 0 20 25 0 cm /T Do Q"))),128 (11, Fx.stream("/Type /XObject /Subtype /Image /Width 1 /Height 1 /ColorSpace [/Separation /Spot /DeviceGray << /FunctionType 2 /Domain [0 1] /C0 [1] /C1 [0] /N 1 >>] /BitsPerComponent 8", Data([128]))),129 (12, Fx.stream("/Type /XObject /Subtype /Image /Width 100 /Height 100 /ColorSpace /DeviceRGB /BitsPerComponent 8", Data([1, 2, 3]))),130 ]131 let pdf = Fx.dir("fixture").appendingPathComponent("spaces.pdf")132 try Fx.rawPDF(objects, root: 1).write(to: pdf)133 let result = try await Fx.run(Fx.request(pdf, to: .folder, op: Op.extractImages))134 #expect(result.files.count == 4 && result.output.facts["images.found"] == "6" && result.output.facts["images.skipped"] == "2")135 #expect(result.warnings.filter { $0.code == "images-skipped" }.count == 2)136 var matched = 0137 for url in result.files {138 let image = try #require(Fx.load(url))139 switch (image.width, image.height) {140 case (2, 2):141 let a = Fx.pixel(image, 0, 0), b = Fx.pixel(image, 1, 0), c = Fx.pixel(image, 0, 1), d = Fx.pixel(image, 1, 1)142 #expect(a.r > 200 && a.g < 60 && b.g > 200 && b.r < 60 && c.b > 200 && c.r < 60 && d.r > 240 && d.g > 240 && d.b > 240)143 matched += 1144 case (1, 1):145 let p = Fx.pixel(image, 0, 0)146 #expect(p.r > 180 && p.g < 90 && p.b < 90)147 matched += 1148 default:149 let left = Fx.pixel(image, 0, 0), right = Fx.pixel(image, 1, 0)150 if image.alphaInfo == .none || image.alphaInfo == .noneSkipLast || image.alphaInfo == .noneSkipFirst {151 #expect(left.r < 30 && right.r > 225) // 4-bit grey152 } else {153 #expect(left.a == 255 && left.g > 200 && right.a == 0) // soft mask → alpha154 }155 matched += 1156 }157 }158 #expect(matched == 4)159 #expect(result.hasWarning("images-altered")) // CMYK → sRGB is reported160 }161162 // Regression (2026-11-21): a PDF without any extractable image used to give an empty folder, which the core163 // then refused with a generic "contains no file". The engine now says why, with the counts.164 @Test func unsupportedFiltersAndInlineImagesAreCounted() async throws {165 let pdf = Fx.featurePDF("extract-features.pdf")166 let request = Fx.request(pdf, to: .folder, op: Op.extractImages)167 do {168 _ = try await Fx.run(request)169 Issue.record("extracting from a PDF with no extractable image must fail")170 } catch let ConversionError.unreadableInput(message) {171 #expect(message.contains("no embedded image that can be extracted"))172 #expect(message.contains("1 skipped") && message.contains("CCITTFaxDecode"))173 #expect(message.contains("1 inline image"))174 }175 }176177 @Test func pdfWithoutImagesIsRefusedClearlyAndAnnouncedByPreflight() async throws {178 let pdf = Fx.simplePDF("extract-none.pdf", pages: 3)179 let request = Fx.request(pdf, to: .folder, op: Op.extractImages)180 let pre = Fx.engine.preflight(request, inspection: nil)181 #expect(pre.contains { $0.code == "no-images" && $0.message.contains("no embedded image that can be extracted") })182 do {183 _ = try await Fx.run(request)184 Issue.record("extracting from a text-only PDF must fail")185 } catch let ConversionError.unreadableInput(message) {186 #expect(message.hasPrefix("This PDF contains no embedded image that can be extracted"))187 #expect(message.contains("PNG or JPEG"))188 }189 // A PDF that has images gets no such warning, and a minimum size that excludes them all is named.190 let heavy = Fx.imageHeavyPDF("extract-some.pdf").url191 #expect(!Fx.engine.preflight(Fx.request(heavy, to: .folder, op: Op.extractImages), inspection: nil).contains { $0.code == "no-images" })192 do {193 _ = try await Fx.run(Fx.request(heavy, to: .folder, op: Op.extractImages, ["images.min-size": .int(100_000)]))194 Issue.record("a minimum size above every image must fail")195 } catch let ConversionError.unreadableInput(message) {196 #expect(message.contains("smaller than the minimum size of 100000 px"))197 }198 }199 }200}201