| 1 | /** |
| 2 | * SPDX-FileCopyrightText: (C) 2005 Dominik Seichter <[email protected]> |
| 3 | * SPDX-License-Identifier: GPL-2.0-or-later |
| 4 | */ |
| 5 | |
| 6 | #include <podofo/private/PdfDeclarationsPrivate.h> |
| 7 | #include "ImageExtractor.h" |
| 8 | |
| 9 | #include <sys/stat.h> |
| 10 | #include <cstdlib> |
| 11 | #include <cstdio> |
| 12 | |
| 13 | #ifdef _MSC_VER |
| 14 | #define snprintf _snprintf |
| 15 | #endif |
| 16 | |
| 17 | using namespace std; |
| 18 | using namespace PoDoFo; |
| 19 | |
| 20 | ImageExtractor::ImageExtractor() |
| 21 | : m_ImageCount(0), m_fileCounter(0), m_buffer{} |
| 22 | { |
| 23 | } |
| 24 | |
| 25 | void ImageExtractor::Init(const string_view& input, const string_view& output) |
| 26 | { |
| 27 | PdfMemDocument document; |
| 28 | document.Load(input); |
| 29 | |
| 30 | m_outputDirectory = output; |
| 31 | |
| 32 | for (auto obj : document.GetObjects()) |
| 33 | { |
| 34 | if (obj->IsDictionary()) |
| 35 | { |
| 36 | PdfObject* typeObj = obj->GetDictionary().GetKey(PdfName::KeyType); |
| 37 | PdfObject* subtypeObj = obj->GetDictionary().GetKey(PdfName::KeySubtype); |
| 38 | if ((typeObj && typeObj->IsName() && (typeObj->GetName() == "XObject")) || |
| 39 | (subtypeObj && subtypeObj->IsName() && (subtypeObj->GetName() == "Image"))) |
| 40 | { |
| 41 | auto filter = obj->GetDictionary().GetKey(PdfName::KeyFilter); |
| 42 | if (filter != nullptr && filter->IsArray() && filter->GetArray().GetSize() == 1 && |
| 43 | filter->GetArray()[0].IsName() && (filter->GetArray()[0].GetName() == "DCTDecode")) |
| 44 | filter = &filter->GetArray()[0]; |
| 45 | |
| 46 | if (filter && filter->IsName() && (filter->GetName() == "DCTDecode")) |
| 47 | { |
| 48 | // The only filter is JPEG -> create a JPEG file |
| 49 | ExtractImage(*obj, true); |
| 50 | } |
| 51 | else |
| 52 | { |
| 53 | ExtractImage(*obj, false); |
| 54 | } |
| 55 | } |
| 56 | } |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | void ImageExtractor::ExtractImage(const PdfObject& obj, bool jpeg) |
| 61 | { |
| 62 | FILE* file = nullptr; |
| 63 | const char* extension = jpeg ? "jpg" : "ppm"; |
| 64 | |
| 65 | // Do not overwrite existing files: |
| 66 | do |
| 67 | { |
| 68 | snprintf(m_buffer, MAX_PATH, "%s/pdfimage_%04i.%s", m_outputDirectory.data(), m_fileCounter++, extension); |
| 69 | } |
| 70 | while (FileExists(m_buffer)); |
| 71 | |
| 72 | file = fopen(m_buffer, "wb"); |
| 73 | if (file == nullptr) |
| 74 | { |
| 75 | PODOFO_RAISE_ERROR(PdfErrorCode::InvalidHandle); |
| 76 | } |
| 77 | |
| 78 | printf("-> Writing image object %s to the file: %s\n", obj.GetIndirectReference().ToString().data(), m_buffer); |
| 79 | |
| 80 | if (jpeg) |
| 81 | { |
| 82 | auto& memprovider = dynamic_cast<const PdfMemoryObjectStream&>(obj.GetStream()->GetProvider()); |
| 83 | auto& buffer = memprovider.GetBuffer(); |
| 84 | fwrite(buffer.data(), buffer.size(), sizeof(char), file); |
| 85 | } |
| 86 | else |
| 87 | { |
| 88 | //long lBitsPerComponent = pObject->GetDictionary().GetKey( PdfName("BitsPerComponent" ) )->GetNumber(); |
| 89 | // TODO: Handle colorspaces |
| 90 | |
| 91 | // Create a ppm image |
| 92 | const char* ppmHeader = "P6\n# Image extracted by PoDoFo\n%u %u\n%li\n"; |
| 93 | |
| 94 | fprintf(file, ppmHeader, |
| 95 | (unsigned)obj.GetDictionary().GetKey("Width")->GetNumber(), |
| 96 | (unsigned)obj.GetDictionary().GetKey("Height")->GetNumber(), |
| 97 | 255); |
| 98 | |
| 99 | auto buffer = obj.GetStream()->GetCopy(); |
| 100 | fwrite(buffer.data(), buffer.size(), sizeof(char), file); |
| 101 | } |
| 102 | |
| 103 | fclose(file); |
| 104 | m_ImageCount++; |
| 105 | } |
| 106 | |
| 107 | bool ImageExtractor::FileExists(const string_view& filepath) |
| 108 | { |
| 109 | bool result = true; |
| 110 | |
| 111 | // if there is an error, it's probably because the file doesn't yet exist |
| 112 | struct stat stBuf; |
| 113 | if (stat(filepath.data(), &stBuf) == -1) |
| 114 | result = false; |
| 115 | |
| 116 | return result; |
| 117 | } |