Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 :
20 :
21 : #include <vcl/svapp.hxx>
22 : #include <vcl/graph.hxx>
23 : #include <vcl/bmpacc.hxx>
24 : #include <vcl/msgbox.hxx>
25 : #include <svl/solar.hrc>
26 : #include <vcl/fltcall.hxx>
27 : #include <vcl/FilterConfigItem.hxx>
28 :
29 : //============================ PBMWriter ==================================
30 :
31 : class PBMWriter {
32 :
33 : private:
34 :
35 : SvStream& m_rOStm; // the output PBM file
36 :
37 : bool mbStatus;
38 : sal_Int32 mnMode; // 0 -> raw, 1-> ascii
39 : BitmapReadAccess* mpAcc;
40 : sal_uLong mnWidth, mnHeight; // size in pixel
41 :
42 : bool ImplWriteHeader();
43 : void ImplWriteBody();
44 : void ImplWriteNumber( sal_Int32 );
45 :
46 : com::sun::star::uno::Reference< com::sun::star::task::XStatusIndicator > xStatusIndicator;
47 :
48 : public:
49 : PBMWriter(SvStream &rPBM);
50 : ~PBMWriter();
51 :
52 : bool WritePBM( const Graphic& rGraphic, FilterConfigItem* pFilterConfigItem );
53 : };
54 :
55 : //=================== Methods of PBMWriter ==============================
56 :
57 0 : PBMWriter::PBMWriter(SvStream &rPBM)
58 : : m_rOStm(rPBM)
59 : , mbStatus(true)
60 : , mnMode(0)
61 : , mpAcc(NULL)
62 : , mnWidth(0)
63 0 : , mnHeight(0)
64 : {
65 0 : }
66 :
67 :
68 :
69 0 : PBMWriter::~PBMWriter()
70 : {
71 0 : }
72 :
73 :
74 :
75 0 : bool PBMWriter::WritePBM( const Graphic& rGraphic, FilterConfigItem* pFilterConfigItem )
76 : {
77 0 : if ( pFilterConfigItem )
78 : {
79 0 : mnMode = pFilterConfigItem->ReadInt32( "FileFormat", 0 );
80 :
81 0 : xStatusIndicator = pFilterConfigItem->GetStatusIndicator();
82 0 : if ( xStatusIndicator.is() )
83 : {
84 0 : OUString aMsg;
85 0 : xStatusIndicator->start( aMsg, 100 );
86 : }
87 : }
88 :
89 0 : BitmapEx aBmpEx( rGraphic.GetBitmapEx() );
90 0 : Bitmap aBmp = aBmpEx.GetBitmap();
91 0 : aBmp.Convert( BMP_CONVERSION_1BIT_THRESHOLD );
92 :
93 0 : SvStreamEndian aOStmOldModus = m_rOStm.GetEndian();
94 0 : m_rOStm.SetEndian( SvStreamEndian::BIG );
95 :
96 0 : mpAcc = aBmp.AcquireReadAccess();
97 0 : if( mpAcc )
98 : {
99 0 : if ( ImplWriteHeader() )
100 0 : ImplWriteBody();
101 :
102 0 : Bitmap::ReleaseAccess( mpAcc );
103 : }
104 : else
105 0 : mbStatus = false;
106 :
107 0 : m_rOStm.SetEndian( aOStmOldModus );
108 :
109 0 : if ( xStatusIndicator.is() )
110 0 : xStatusIndicator->end();
111 :
112 0 : return mbStatus;
113 : }
114 :
115 :
116 :
117 0 : bool PBMWriter::ImplWriteHeader()
118 : {
119 0 : mnWidth = mpAcc->Width();
120 0 : mnHeight = mpAcc->Height();
121 0 : if ( mnWidth && mnHeight )
122 : {
123 0 : if ( mnMode == 0 )
124 0 : m_rOStm.WriteCharPtr( "P4\x0a" );
125 : else
126 0 : m_rOStm.WriteCharPtr( "P1\x0a" );
127 :
128 0 : ImplWriteNumber( mnWidth );
129 0 : m_rOStm.WriteUChar( 32 );
130 0 : ImplWriteNumber( mnHeight );
131 0 : m_rOStm.WriteUChar( 10 );
132 : }
133 0 : else mbStatus = false;
134 0 : return mbStatus;
135 : }
136 :
137 :
138 :
139 0 : void PBMWriter::ImplWriteBody()
140 : {
141 0 : if ( mnMode == 0 )
142 : {
143 0 : sal_uInt8 nBYTE = 0;
144 0 : for ( sal_uLong y = 0; y < mnHeight; y++ )
145 : {
146 : sal_uLong x;
147 0 : for ( x = 0; x < mnWidth; x++ )
148 : {
149 0 : nBYTE <<= 1;
150 0 : if (!(mpAcc->GetPixelIndex( y, x ) & 1 ) )
151 0 : nBYTE++;
152 0 : if ( ( x & 7 ) == 7 )
153 0 : m_rOStm.WriteUChar( nBYTE );
154 : }
155 0 : if ( ( x & 7 ) != 0 )
156 0 : m_rOStm.WriteUChar( nBYTE << ( ( x ^ 7 ) + 1 ) );
157 : }
158 : }
159 : else
160 : {
161 0 : for ( sal_uLong y = 0; y < mnHeight; y++ )
162 : {
163 0 : int nxCount = 70;
164 0 : for ( sal_uLong x = 0; x < mnWidth; x++ )
165 : {
166 0 : if (!( --nxCount ) )
167 : {
168 0 : nxCount = 69;
169 0 : m_rOStm.WriteUChar( 10 );
170 : }
171 0 : m_rOStm.WriteUChar( ( mpAcc->GetPixelIndex( y, x ) ^ 1 ) + '0' ) ;
172 : }
173 0 : m_rOStm.WriteUChar( 10 );
174 : }
175 : }
176 0 : }
177 :
178 :
179 : // A decimal number in ascii format is written in the stream.
180 :
181 0 : void PBMWriter::ImplWriteNumber(sal_Int32 nNumber)
182 : {
183 0 : const OString aNum(OString::number(nNumber));
184 0 : m_rOStm.WriteCharPtr( aNum.getStr() );
185 0 : }
186 :
187 :
188 :
189 :
190 : // - exported function -
191 :
192 :
193 : // this needs to be kept in sync with
194 : // ImpFilterLibCacheEntry::GetImportFunction() from
195 : // vcl/source/filter/graphicfilter.cxx
196 : #if defined(DISABLE_DYNLOADING)
197 : #define GraphicExport epbGraphicExport
198 : #endif
199 :
200 : extern "C" SAL_DLLPUBLIC_EXPORT bool SAL_CALL
201 0 : GraphicExport( SvStream& rStream, Graphic& rGraphic, FilterConfigItem* pFilterConfigItem )
202 : {
203 0 : PBMWriter aPBMWriter(rStream);
204 :
205 0 : return aPBMWriter.WritePBM( rGraphic, pFilterConfigItem );
206 0 : }
207 :
208 :
209 :
210 :
211 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|