UNPKG

1.92 kBtext/x-cView Raw
1// Copyright (c) 2010 LearnBoost <tj@learnboost.com>
2
3#pragma once
4
5#include "Canvas.h"
6#include <jpeglib.h>
7#include <nan.h>
8#include <png.h>
9#include <stdint.h> // node < 7 uses libstdc++ on macOS which lacks complete c++11
10#include <vector>
11
12#ifndef PAGE_SIZE
13 #define PAGE_SIZE 4096
14#endif
15
16/*
17 * Image encoding closures.
18 */
19
20struct Closure {
21 std::vector<uint8_t> vec;
22 Nan::Callback cb;
23 Canvas* canvas = nullptr;
24 cairo_status_t status = CAIRO_STATUS_SUCCESS;
25
26 static cairo_status_t writeVec(void *c, const uint8_t *odata, unsigned len) {
27 Closure* closure = static_cast<Closure*>(c);
28 try {
29 closure->vec.insert(closure->vec.end(), odata, odata + len);
30 } catch (const std::bad_alloc &) {
31 return CAIRO_STATUS_NO_MEMORY;
32 }
33 return CAIRO_STATUS_SUCCESS;
34 }
35
36 Closure(Canvas* canvas) : canvas(canvas) {};
37};
38
39struct PdfSvgClosure : Closure {
40 PdfSvgClosure(Canvas* canvas) : Closure(canvas) {};
41};
42
43struct PngClosure : Closure {
44 uint32_t compressionLevel = 6;
45 uint32_t filters = PNG_ALL_FILTERS;
46 uint32_t resolution = 0; // 0 = unspecified
47 // Indexed PNGs:
48 uint32_t nPaletteColors = 0;
49 uint8_t* palette = nullptr;
50 uint8_t backgroundIndex = 0;
51
52 PngClosure(Canvas* canvas) : Closure(canvas) {};
53};
54
55struct JpegClosure : Closure {
56 uint32_t quality = 75;
57 uint32_t chromaSubsampling = 2;
58 bool progressive = false;
59 jpeg_destination_mgr* jpeg_dest_mgr = nullptr;
60
61 static void init_destination(j_compress_ptr cinfo);
62 static boolean empty_output_buffer(j_compress_ptr cinfo);
63 static void term_destination(j_compress_ptr cinfo);
64
65 JpegClosure(Canvas* canvas) : Closure(canvas) {
66 jpeg_dest_mgr = new jpeg_destination_mgr;
67 jpeg_dest_mgr->init_destination = init_destination;
68 jpeg_dest_mgr->empty_output_buffer = empty_output_buffer;
69 jpeg_dest_mgr->term_destination = term_destination;
70 };
71
72 ~JpegClosure() {
73 delete jpeg_dest_mgr;
74 }
75};