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