Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
Macros | Functions | Variables
coreGraphicsPdf2png.cpp File Reference
#include <cstdio>
#include <memory>
#include <ApplicationServices/ApplicationServices.h>

Go to the source code of this file.

Macros

#define ASSERT(x)
 

Functions

int main (int argc, char **argv)
 

Variables

const int PAGE = 1
 

Macro Definition Documentation

◆ ASSERT

#define ASSERT (   x)
Value:
do { \
if (!(x)) { \
fprintf(stderr, "ERROR: " __FILE__ \
":%d (%s)\n", __LINE__, #x); \
return 1; \
} \
} while (false) \
double x

Definition at line 15 of file coreGraphicsPdf2png.cpp.

16 { \
17 if (!(x)) { \
18 fprintf(stderr, "ERROR: " __FILE__ \
19 ":%d (%s)\n", __LINE__, #x); \
20 return 1; \
21 } \
22 } while (false) \
23

Function Documentation

◆ main()

int main ( int  argc,
char **  argv 
)

Definition at line 26 of file coreGraphicsPdf2png.cpp.

26 {
27 if (argc <= 1 || !*(argv[1]) || 0 == strcmp(argv[1], "-")) {
28 fprintf(stderr, "usage:\n\t%s INPUT_PDF_FILE_PATH [OUTPUT_PNG_PATH]\n", argv[0]);
29 return 1;
30 }
31 const char* output = argc > 2 ? argv[2] : nullptr;
32 CGDataProviderRef data = CGDataProviderCreateWithFilename(argv[1]);
33 ASSERT(data);
34 CGPDFDocumentRef pdf = CGPDFDocumentCreateWithProvider(data);
35 CGDataProviderRelease(data);
36 ASSERT(pdf);
37 CGPDFPageRef page = CGPDFDocumentGetPage(pdf, PAGE);
38 ASSERT(page);
39 CGRect bounds = CGPDFPageGetBoxRect(page, kCGPDFMediaBox);
40 int w = (int)CGRectGetWidth(bounds);
41 int h = (int)CGRectGetHeight(bounds);
42 CGBitmapInfo info = kCGBitmapByteOrder32Big | kCGImageAlphaPremultipliedLast;
43 CGColorSpaceRef cs = CGColorSpaceCreateDeviceRGB();
44 ASSERT(cs);
45 std::unique_ptr<uint32_t[]> bitmap(new uint32_t[w * h]);
46 memset(bitmap.get(), 0xFF, 4 * w * h);
47 CGContextRef ctx = CGBitmapContextCreate(bitmap.get(), w, h, 8, w * 4, cs, info);
48 ASSERT(ctx);
49 CGContextDrawPDFPage(ctx, page);
50 CGPDFDocumentRelease(pdf);
51 CGImageRef image = CGBitmapContextCreateImage(ctx);
53 CGDataConsumerCallbacks procs;
54 procs.putBytes = [](void* f, const void* buf, size_t s) {
55 return fwrite(buf, 1, s, (FILE*)f);
56 };
57 procs.releaseConsumer = [](void* info) { fclose((FILE*)info); };
58 FILE* ofile = (!output || !output[0] || 0 == strcmp(output, "-"))
59 ? stdout : fopen(output, "wb");
60 ASSERT(ofile);
61 CGDataConsumerRef consumer = CGDataConsumerCreate(ofile, &procs);
62 ASSERT(consumer);
63 CGImageDestinationRef dst =
64 CGImageDestinationCreateWithDataConsumer(consumer, kUTTypePNG, 1, nullptr);
65 CFRelease(consumer);
66 ASSERT(dst);
67 CGImageDestinationAddImage(dst, image, nullptr);
68 ASSERT(CGImageDestinationFinalize(dst));
69 CFRelease(dst);
70 CGImageRelease(image);
71 CGColorSpaceRelease(cs);
72 CGContextRelease(ctx);
73 return 0;
74}
static void info(const char *fmt,...) SK_PRINTF_LIKE(1
Definition DM.cpp:213
Type::kYUV Type::kRGBA() int(0.7 *637)
const int PAGE
#define ASSERT(x)
sk_sp< SkImage > image
Definition examples.cpp:29
struct MyStruct s
char ** argv
Definition library.h:9
Optional< SkRect > bounds
Definition SkRecords.h:189
DEF_SWITCHES_START aot vmservice shared library Name of the *so containing AOT compiled Dart assets for launching the service isolate vm snapshot data
Definition switches.h:41
dst
Definition cp.py:12
-comparison
SkScalar w
SkScalar h

Variable Documentation

◆ PAGE

const int PAGE = 1

Definition at line 24 of file coreGraphicsPdf2png.cpp.