| 1 | /** |
| 2 | * Copyright (C) 2008 by Dominik Seichter <[email protected]> |
| 3 | * Copyright (C) 2021 by Francesco Pretto <[email protected]> |
| 4 | * |
| 5 | * Licensed under GNU Library General Public 2.0 or later. |
| 6 | * Some rights reserved. See COPYING, AUTHORS. |
| 7 | */ |
| 8 | |
| 9 | #include <PdfTest.h> |
| 10 | |
| 11 | using namespace std; |
| 12 | using namespace PoDoFo; |
| 13 | |
| 14 | TEST_CASE("TestImage1") |
| 15 | { |
| 16 | PdfMemDocument doc; |
| 17 | doc.Load(TestUtils::GetTestInputFilePath("TestImage1.pdf")); |
| 18 | auto& page = doc.GetPages().GetPageAt(0); |
| 19 | auto& resources = page.MustGetResources(); |
| 20 | auto imageObj = resources.GetResource("XObject", "XOb5"); |
| 21 | unique_ptr<PdfImage> image; |
| 22 | REQUIRE(PdfXObject::TryCreateFromObject<PdfImage>(*imageObj, image)); |
| 23 | |
| 24 | charbuff buffer; |
| 25 | image->DecodeTo(buffer, PdfPixelFormat::BGRA); |
| 26 | charbuff ppmbuffer; |
| 27 | TestUtils::SaveFramePPM(ppmbuffer, buffer.data(), |
| 28 | PdfPixelFormat::BGRA, image->GetWidth(), image->GetHeight()); |
| 29 | |
| 30 | string expectedImage; |
| 31 | TestUtils::ReadTestInputFile("ReferenceImage.ppm", expectedImage); |
| 32 | |
| 33 | REQUIRE(ppmbuffer == expectedImage); |
| 34 | } |
| 35 | |
| 36 | TEST_CASE("TestImage2") |
| 37 | { |
| 38 | PdfMemDocument doc; |
| 39 | doc.Load(TestUtils::GetTestInputFilePath("Hierarchies1.pdf")); |
| 40 | // Try to extract jpeg image |
| 41 | auto imageObj = doc.GetObjects().GetObject(PdfReference(156, 0)); |
| 42 | charbuff buffer; |
| 43 | |
| 44 | // Unpacking directly the stream shall throw since it has jpeg content |
| 45 | ASSERT_THROW_WITH_ERROR_CODE(imageObj->GetStream()->CopyTo(buffer), PdfErrorCode::UnsupportedFilter); |
| 46 | |
| 47 | // Unpacking using UnpackToSafe() should succeed |
| 48 | imageObj->GetStream()->CopyToSafe(buffer); |
| 49 | |
| 50 | unique_ptr<PdfImage> image; |
| 51 | REQUIRE(PdfXObject::TryCreateFromObject<PdfImage>(*imageObj, image)); |
| 52 | |
| 53 | image->DecodeTo(buffer, PdfPixelFormat::BGRA); |
| 54 | charbuff ppmbuffer; |
| 55 | TestUtils::SaveFramePPM(ppmbuffer, buffer.data(), |
| 56 | PdfPixelFormat::BGRA, image->GetWidth(), image->GetHeight()); |
| 57 | |
| 58 | #ifdef PODOFO_PLAYGROUND |
| 59 | // NOTE: The following check may file using different, |
| 60 | // jpeg libraries such as libjpeg-turbo |
| 61 | string expectedImage; |
| 62 | TestUtils::ReadTestInputFile("ReferenceImage.ppm", expectedImage); |
| 63 | |
| 64 | REQUIRE(ppmbuffer == expectedImage); |
| 65 | #endif // PODOFO_PLAYGROUND |
| 66 | } |
| 67 | |
| 68 | static void testReferenceImage(const PdfDocument& doc) |
| 69 | { |
| 70 | auto& page = doc.GetPages().GetPageAt(0); |
| 71 | auto resources = page.MustGetResources().GetResourceIterator("XObject"); |
| 72 | for (auto& res : resources) |
| 73 | { |
| 74 | unique_ptr<const PdfImage> image; |
| 75 | REQUIRE(PdfXObject::TryCreateFromObject<PdfImage>(*res.second, image)); |
| 76 | |
| 77 | charbuff buffer; |
| 78 | image->DecodeTo(buffer, PdfPixelFormat::BGRA); |
| 79 | charbuff ppmbuffer; |
| 80 | TestUtils::SaveFramePPM(ppmbuffer, buffer.data(), |
| 81 | PdfPixelFormat::BGRA, image->GetWidth(), image->GetHeight()); |
| 82 | |
| 83 | string expectedImage; |
| 84 | TestUtils::ReadTestInputFile("ReferenceImage.ppm", expectedImage); |
| 85 | |
| 86 | REQUIRE(ppmbuffer == expectedImage); |
| 87 | |
| 88 | break; |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | TEST_CASE("TestImage3") |
| 93 | { |
| 94 | auto outputFile = TestUtils::GetTestOutputFilePath("TestImage3.pdf"); |
| 95 | { |
| 96 | PdfMemDocument doc; |
| 97 | PdfPainter painter; |
| 98 | auto& page = doc.GetPages().CreatePage(PdfPage::CreateStandardPageSize(PdfPageSize::A4)); |
| 99 | painter.SetCanvas(page); |
| 100 | auto img = doc.CreateImage(); |
| 101 | img->Load(TestUtils::GetTestInputFilePath("ReferenceImage.png")); |
| 102 | painter.DrawImage(*img, 50.0, 50.0); |
| 103 | painter.FinishDrawing(); |
| 104 | doc.Save(outputFile); |
| 105 | } |
| 106 | |
| 107 | { |
| 108 | PdfMemDocument doc; |
| 109 | doc.Load(outputFile); |
| 110 | testReferenceImage(doc); |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | TEST_CASE("TestImage4") |
| 115 | { |
| 116 | auto outputFile = TestUtils::GetTestOutputFilePath("TestImage4.pdf"); |
| 117 | { |
| 118 | PdfMemDocument doc; |
| 119 | PdfPainter painter; |
| 120 | auto& page = doc.GetPages().CreatePage(PdfPage::CreateStandardPageSize(PdfPageSize::A4)); |
| 121 | painter.SetCanvas(page); |
| 122 | auto img = doc.CreateImage(); |
| 123 | img->Load(TestUtils::GetTestInputFilePath("ReferenceImage.jpg")); |
| 124 | auto alpha = doc.CreateImage(); |
| 125 | FileStreamDevice alphaInput(TestUtils::GetTestInputFilePath("ReferenceImage.alpha")); |
| 126 | PdfImageInfo info; |
| 127 | info.Width = 128; |
| 128 | info.Height = 128; |
| 129 | info.ColorSpace = PdfColorSpace::DeviceGray; |
| 130 | info.BitsPerComponent = 8; |
| 131 | alpha->SetDataRaw(alphaInput, info); |
| 132 | img->SetSoftMask(*alpha); |
| 133 | painter.DrawImage(*img.get(), 50.0, 50.0); |
| 134 | painter.FinishDrawing(); |
| 135 | doc.Save(outputFile); |
| 136 | } |
| 137 | |
| 138 | #ifdef PODOFO_PLAYGROUND |
| 139 | // NOTE: The following check may file using different, |
| 140 | // jpeg libraries such as libjpeg-turbo |
| 141 | { |
| 142 | PdfMemDocument doc; |
| 143 | doc.Load(outputFile); |
| 144 | testReferenceImage(doc); |
| 145 | } |
| 146 | #endif // PODOFO_PLAYGROUND |
| 147 | } |
| 148 | |
| 149 | // TODO: Hash test |
| 150 | TEST_CASE("TestImage5") |
| 151 | { |
| 152 | { |
| 153 | // Image found at: |
| 154 | // https://github.com/tyranron/mozjpeg-sys-issue-23-example/blob/master/ignucius.jpg |
| 155 | PdfMemDocument doc; |
| 156 | doc.Load(TestUtils::GetTestInputFilePath("YCbCr-jpeg.pdf")); |
| 157 | auto imageObj = doc.GetObjects().GetObject(PdfReference(11, 0)); |
| 158 | unique_ptr<PdfImage> image; |
| 159 | REQUIRE(PdfXObject::TryCreateFromObject<PdfImage>(*imageObj, image)); |
| 160 | |
| 161 | charbuff buffer; |
| 162 | image->DecodeTo(buffer, PdfPixelFormat::BGRA); |
| 163 | charbuff ppmbuffer; |
| 164 | TestUtils::SaveFramePPM(ppmbuffer, buffer.data(), |
| 165 | PdfPixelFormat::BGRA, image->GetWidth(), image->GetHeight()); |
| 166 | |
| 167 | TestUtils::WriteTestOutputFile(TestUtils::GetTestOutputFilePath("YCbCr-jpeg.ppm"), ppmbuffer); |
| 168 | } |
| 169 | |
| 170 | { |
| 171 | // Image found at: |
| 172 | // https://bugzilla.redhat.com/show_bug.cgi?id=166460 |
| 173 | PdfMemDocument doc; |
| 174 | doc.Load(TestUtils::GetTestInputFilePath("YCCK-jpeg.pdf")); |
| 175 | auto imageObj = doc.GetObjects().GetObject(PdfReference(11, 0)); |
| 176 | unique_ptr<PdfImage> image; |
| 177 | REQUIRE(PdfXObject::TryCreateFromObject<PdfImage>(*imageObj, image)); |
| 178 | |
| 179 | charbuff buffer; |
| 180 | image->DecodeTo(buffer, PdfPixelFormat::BGRA); |
| 181 | charbuff ppmbuffer; |
| 182 | TestUtils::SaveFramePPM(ppmbuffer, buffer.data(), |
| 183 | PdfPixelFormat::BGRA, image->GetWidth(), image->GetHeight()); |
| 184 | |
| 185 | TestUtils::WriteTestOutputFile(TestUtils::GetTestOutputFilePath("YCCK-jpeg.ppm"), ppmbuffer); |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | TEST_CASE("TestImage7") |
| 190 | { |
| 191 | auto outputFile = TestUtils::GetTestOutputFilePath("TestImage7.pdf"); |
| 192 | { |
| 193 | PdfMemDocument doc; |
| 194 | PdfPainter painter; |
| 195 | auto& page = doc.GetPages().CreatePage(PdfPage::CreateStandardPageSize(PdfPageSize::A4)); |
| 196 | painter.SetCanvas(page); |
| 197 | |
| 198 | auto img1 = doc.CreateImage(); |
| 199 | img1->Load(TestUtils::GetTestInputFilePath("MultipleFormats.tif")); |
| 200 | painter.DrawImage(*img1, 50, 700, 0.5, 0.5); |
| 201 | |
| 202 | auto img2 = doc.CreateImage(); |
| 203 | img2->Load(TestUtils::GetTestInputFilePath("MultipleFormats.tif"), 8); |
| 204 | painter.DrawImage(*img2, 50, 600, 0.5, 0.5); |
| 205 | |
| 206 | painter.FinishDrawing(); |
| 207 | doc.Save(outputFile); |
| 208 | } |
| 209 | } |