1 | //======================================================================== |
2 | // |
3 | // NetPBMWriter.h |
4 | // |
5 | // Copyright 1998-2003 Glyph & Cog, LLC |
6 | // |
7 | //======================================================================== |
8 | // |
9 | //======================================================================== |
10 | // |
11 | // Modified under the Poppler project - http://poppler.freedesktop.org |
12 | // |
13 | // All changes made under the Poppler project to this file are licensed |
14 | // under GPL version 2 or later |
15 | // |
16 | // Copyright (C) 2005, 2007, 2011, 2022 Albert Astals Cid <aacid@kde.org> |
17 | // Copyright (C) 2006 Rainer Keller <class321@gmx.de> |
18 | // Copyright (C) 2008 Timothy Lee <timothy.lee@siriushk.com> |
19 | // Copyright (C) 2008 Vasile Gaburici <gaburici@cs.umd.edu> |
20 | // Copyright (C) 2009 Carlos Garcia Campos <carlosgc@gnome.org> |
21 | // Copyright (C) 2009 William Bader <williambader@hotmail.com> |
22 | // Copyright (C) 2010 Jakob Voss <jakob.voss@gbv.de> |
23 | // Copyright (C) 2012, 2013 Adrian Johnson <ajohnson@redneon.com> |
24 | // Copyright (C) 2013 Thomas Fischer <fischer@unix-ag.uni-kl.de> |
25 | // |
26 | // To see a description of the changes please see the Changelog file that |
27 | // came with your tarball or type make ChangeLog if you are building from git |
28 | // |
29 | //======================================================================== |
30 | |
31 | #include "poppler-config.h" |
32 | |
33 | #include "NetPBMWriter.h" |
34 | |
35 | // Writer for the NetPBM formats (PBM and PPM) |
36 | // This format is documented at: |
37 | // http://netpbm.sourceforge.net/doc/pbm.html |
38 | // http://netpbm.sourceforge.net/doc/ppm.html |
39 | |
40 | NetPBMWriter::NetPBMWriter(Format formatA) : format(formatA) { } |
41 | |
42 | bool NetPBMWriter::init(FILE *f, int widthA, int heightA, double /*hDPI*/, double /*vDPI*/) |
43 | { |
44 | file = f; |
45 | width = widthA; |
46 | if (format == MONOCHROME) { |
47 | fprintf(stream: file, format: "P4\n" ); |
48 | fprintf(stream: file, format: "%d %d\n" , widthA, heightA); |
49 | } else { |
50 | fprintf(stream: file, format: "P6\n" ); |
51 | fprintf(stream: file, format: "%d %d\n" , widthA, heightA); |
52 | fprintf(stream: file, format: "255\n" ); |
53 | } |
54 | return true; |
55 | } |
56 | |
57 | bool NetPBMWriter::writePointers(unsigned char **rowPointers, int rowCount) |
58 | { |
59 | for (int i = 0; i < rowCount; i++) { |
60 | writeRow(row: &rowPointers[i]); |
61 | } |
62 | return true; |
63 | } |
64 | |
65 | bool NetPBMWriter::writeRow(unsigned char **row) |
66 | { |
67 | if (format == MONOCHROME) { |
68 | // PBM uses 0 = white, 1 = black so we need to invert the colors |
69 | int size = (width + 7) / 8; |
70 | for (int i = 0; i < size; i++) { |
71 | fputc(c: (*row)[i] ^ 0xff, stream: file); |
72 | } |
73 | } else { |
74 | fwrite(ptr: *row, size: 1, n: width * 3, s: file); |
75 | } |
76 | return true; |
77 | } |
78 | |
79 | bool NetPBMWriter::close() |
80 | { |
81 | return true; |
82 | } |
83 | |