/src/poppler/goo/NetPBMWriter.cc
Line | Count | Source |
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, 2025 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 "NetPBMWriter.h" |
32 | | |
33 | | // Writer for the NetPBM formats (PBM and PPM) |
34 | | // This format is documented at: |
35 | | // http://netpbm.sourceforge.net/doc/pbm.html |
36 | | // http://netpbm.sourceforge.net/doc/ppm.html |
37 | | |
38 | 0 | NetPBMWriter::NetPBMWriter(Format formatA) : format(formatA) { } |
39 | | |
40 | | bool NetPBMWriter::init(FILE *f, int widthA, int heightA, double /*hDPI*/, double /*vDPI*/) |
41 | 0 | { |
42 | 0 | file = f; |
43 | 0 | width = widthA; |
44 | 0 | if (format == MONOCHROME) { |
45 | 0 | fprintf(file, "P4\n"); |
46 | 0 | fprintf(file, "%d %d\n", widthA, heightA); |
47 | 0 | } else { |
48 | 0 | fprintf(file, "P6\n"); |
49 | 0 | fprintf(file, "%d %d\n", widthA, heightA); |
50 | 0 | fprintf(file, "255\n"); |
51 | 0 | } |
52 | 0 | return true; |
53 | 0 | } |
54 | | |
55 | | bool NetPBMWriter::writePointers(unsigned char **rowPointers, int rowCount) |
56 | 0 | { |
57 | 0 | for (int i = 0; i < rowCount; i++) { |
58 | 0 | writeRow(&rowPointers[i]); |
59 | 0 | } |
60 | 0 | return true; |
61 | 0 | } |
62 | | |
63 | | bool NetPBMWriter::writeRow(unsigned char **row) |
64 | 0 | { |
65 | 0 | if (format == MONOCHROME) { |
66 | | // PBM uses 0 = white, 1 = black so we need to invert the colors |
67 | 0 | int size = (width + 7) / 8; |
68 | 0 | for (int i = 0; i < size; i++) { |
69 | 0 | fputc((*row)[i] ^ 0xff, file); |
70 | 0 | } |
71 | 0 | } else { |
72 | 0 | fwrite(*row, 1, width * 3, file); |
73 | 0 | } |
74 | 0 | return true; |
75 | 0 | } |
76 | | |
77 | | bool NetPBMWriter::close() |
78 | 0 | { |
79 | 0 | return true; |
80 | 0 | } |