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