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 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
NetPBMWriter(Format formatA)40 NetPBMWriter::NetPBMWriter(Format formatA) : format(formatA)
41 {
42 }
43
init(FILE * f,int widthA,int heightA,int hDPI,int vDPI)44 bool NetPBMWriter::init(FILE *f, int widthA, int heightA, int hDPI, int vDPI)
45 {
46 file = f;
47 width = widthA;
48 if (format == MONOCHROME) {
49 fprintf(file, "P4\n");
50 fprintf(file, "%d %d\n", widthA, heightA);
51 } else {
52 fprintf(file, "P6\n");
53 fprintf(file, "%d %d\n", widthA, heightA);
54 fprintf(file, "255\n");
55 }
56 return true;
57 }
58
writePointers(unsigned char ** rowPointers,int rowCount)59 bool NetPBMWriter::writePointers(unsigned char **rowPointers, int rowCount)
60 {
61 for (int i = 0; i < rowCount; i++)
62 writeRow(&rowPointers[i]);
63 return true;
64 }
65
writeRow(unsigned char ** row)66 bool NetPBMWriter::writeRow(unsigned char **row)
67 {
68 if (format == MONOCHROME) {
69 // PBM uses 0 = white, 1 = black so we need to invert the colors
70 int size = (width + 7)/8;
71 for (int i = 0; i < size; i++)
72 fputc((*row)[i] ^ 0xff, file);
73 } else {
74 fwrite(*row, 1, width*3, file);
75 }
76 return true;
77 }
78
79
close()80 bool NetPBMWriter::close()
81 {
82 return true;
83 }
84
85