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 : //============================ PGMWriter ==================================
30 :
31 : class PGMWriter {
32 :
33 : private:
34 :
35 : SvStream& m_rOStm; // the output PGM file
36 :
37 : bool mbStatus;
38 : sal_uInt32 mnMode;
39 : BitmapReadAccess* mpAcc;
40 : sal_uLong mnWidth, mnHeight; // image size in pixeln
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 : PGMWriter(SvStream &rStream);
50 : ~PGMWriter();
51 :
52 : bool WritePGM( const Graphic& rGraphic, FilterConfigItem* pFilterConfigItem );
53 : };
54 :
55 : //=================== Methoden von PGMWriter ==============================
56 0 : PGMWriter::PGMWriter(SvStream &rStream)
57 : : m_rOStm(rStream)
58 : , mbStatus(true)
59 : , mnMode(0)
60 : , mpAcc(NULL)
61 : , mnWidth(0)
62 0 : , mnHeight(0)
63 : {
64 0 : }
65 :
66 0 : PGMWriter::~PGMWriter()
67 : {
68 0 : }
69 :
70 0 : bool PGMWriter::WritePGM( const Graphic& rGraphic, FilterConfigItem* pFilterConfigItem )
71 : {
72 0 : if ( pFilterConfigItem )
73 : {
74 0 : mnMode = pFilterConfigItem->ReadInt32( "FileFormat", 0 );
75 :
76 0 : xStatusIndicator = pFilterConfigItem->GetStatusIndicator();
77 0 : if ( xStatusIndicator.is() )
78 : {
79 0 : OUString aMsg;
80 0 : xStatusIndicator->start( aMsg, 100 );
81 : }
82 : }
83 :
84 0 : BitmapEx aBmpEx( rGraphic.GetBitmapEx() );
85 0 : Bitmap aBmp = aBmpEx.GetBitmap();
86 0 : aBmp.Convert( BMP_CONVERSION_8BIT_GREYS );
87 :
88 0 : SvStreamEndian aOStmOldModus = m_rOStm.GetEndian();
89 0 : m_rOStm.SetEndian( SvStreamEndian::BIG );
90 :
91 0 : mpAcc = aBmp.AcquireReadAccess();
92 0 : if( mpAcc )
93 : {
94 0 : if ( ImplWriteHeader() )
95 : {
96 0 : ImplWriteBody();
97 : }
98 0 : Bitmap::ReleaseAccess( mpAcc );
99 : }
100 : else
101 0 : mbStatus = false;
102 :
103 0 : m_rOStm.SetEndian( aOStmOldModus );
104 :
105 0 : if ( xStatusIndicator.is() )
106 0 : xStatusIndicator->end();
107 :
108 0 : return mbStatus;
109 : }
110 :
111 :
112 :
113 0 : bool PGMWriter::ImplWriteHeader()
114 : {
115 0 : mnWidth = mpAcc->Width();
116 0 : mnHeight = mpAcc->Height();
117 0 : if ( mnWidth && mnHeight )
118 : {
119 0 : if ( mnMode == 0 )
120 0 : m_rOStm.WriteCharPtr( "P5\x0a" );
121 : else
122 0 : m_rOStm.WriteCharPtr( "P2\x0a" );
123 :
124 0 : ImplWriteNumber( mnWidth );
125 0 : m_rOStm.WriteUChar( 32 );
126 0 : ImplWriteNumber( mnHeight );
127 0 : m_rOStm.WriteUChar( 32 );
128 0 : ImplWriteNumber( 255 ); // max. gray value
129 0 : m_rOStm.WriteUChar( 10 );
130 : }
131 : else
132 0 : mbStatus = false;
133 :
134 0 : return mbStatus;
135 : }
136 :
137 :
138 :
139 0 : void PGMWriter::ImplWriteBody()
140 : {
141 0 : if ( mnMode == 0 )
142 : {
143 0 : for ( sal_uLong y = 0; y < mnHeight; y++ )
144 : {
145 0 : for ( sal_uLong x = 0; x < mnWidth; x++ )
146 : {
147 0 : m_rOStm.WriteUChar( mpAcc->GetPixelIndex( y, x ) );
148 : }
149 : }
150 : }
151 : else
152 : {
153 0 : for ( sal_uLong y = 0; y < mnHeight; y++ )
154 : {
155 0 : int nCount = 70;
156 0 : for ( sal_uLong x = 0; x < mnWidth; x++ )
157 : {
158 : sal_uInt8 nDat, nNumb;
159 0 : if ( nCount < 0 )
160 : {
161 0 : nCount = 69;
162 0 : m_rOStm.WriteUChar( 10 );
163 : }
164 0 : nDat = mpAcc->GetPixelIndex( y, x );
165 0 : nNumb = nDat / 100;
166 0 : if ( nNumb )
167 : {
168 0 : m_rOStm.WriteUChar( nNumb + '0' );
169 0 : nDat -= ( nNumb * 100 );
170 0 : nNumb = nDat / 10;
171 0 : m_rOStm.WriteUChar( nNumb + '0' );
172 0 : nDat -= ( nNumb * 10 );
173 0 : m_rOStm.WriteUChar( nDat + '0' );
174 0 : nCount -= 4;
175 : }
176 : else
177 : {
178 0 : nNumb = nDat / 10;
179 0 : if ( nNumb )
180 : {
181 0 : m_rOStm.WriteUChar( nNumb + '0' );
182 0 : nDat -= ( nNumb * 10 );
183 0 : m_rOStm.WriteUChar( nDat + '0' );
184 0 : nCount -= 3;
185 : }
186 : else
187 : {
188 0 : m_rOStm.WriteUChar( nDat + '0' );
189 0 : nCount -= 2;
190 : }
191 : }
192 0 : m_rOStm.WriteUChar( ' ' );
193 : }
194 0 : m_rOStm.WriteUChar( 10 );
195 : }
196 : }
197 0 : }
198 :
199 :
200 : // write a decimal number in ascii format into the stream
201 0 : void PGMWriter::ImplWriteNumber(sal_Int32 nNumber)
202 : {
203 0 : const OString aNum(OString::number(nNumber));
204 0 : m_rOStm.WriteCharPtr( aNum.getStr() );
205 0 : }
206 :
207 :
208 :
209 :
210 : // - exported function -
211 :
212 :
213 : // this needs to be kept in sync with
214 : // ImpFilterLibCacheEntry::GetImportFunction() from
215 : // vcl/source/filter/graphicfilter.cxx
216 : #if defined(DISABLE_DYNLOADING)
217 : #define GraphicExport epgGraphicExport
218 : #endif
219 :
220 : extern "C" SAL_DLLPUBLIC_EXPORT bool SAL_CALL
221 0 : GraphicExport( SvStream& rStream, Graphic& rGraphic, FilterConfigItem* pFilterConfigItem )
222 : {
223 0 : PGMWriter aPGMWriter(rStream);
224 :
225 0 : return aPGMWriter.WritePGM( rGraphic, pFilterConfigItem );
226 0 : }
227 :
228 :
229 :
230 :
231 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|