Branch data Line data Source code
1 : : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : : /*************************************************************************
3 : : *
4 : : * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 : : *
6 : : * Copyright 2000, 2010 Oracle and/or its affiliates.
7 : : *
8 : : * OpenOffice.org - a multi-platform office productivity suite
9 : : *
10 : : * This file is part of OpenOffice.org.
11 : : *
12 : : * OpenOffice.org is free software: you can redistribute it and/or modify
13 : : * it under the terms of the GNU Lesser General Public License version 3
14 : : * only, as published by the Free Software Foundation.
15 : : *
16 : : * OpenOffice.org is distributed in the hope that it will be useful,
17 : : * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 : : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 : : * GNU Lesser General Public License version 3 for more details
20 : : * (a copy is included in the LICENSE file that accompanied this code).
21 : : *
22 : : * You should have received a copy of the GNU Lesser General Public License
23 : : * version 3 along with OpenOffice.org. If not, see
24 : : * <http://www.openoffice.org/license.html>
25 : : * for a copy of the LGPLv3 License.
26 : : *
27 : : ************************************************************************/
28 : :
29 : :
30 : : #include <vcl/svapp.hxx>
31 : : #include <vcl/graph.hxx>
32 : : #include <vcl/bmpacc.hxx>
33 : : #include <vcl/msgbox.hxx>
34 : : #include <svl/solar.hrc>
35 : : #include <svtools/fltcall.hxx>
36 : : #include <svtools/FilterConfigItem.hxx>
37 : :
38 : : //============================ PGMWriter ==================================
39 : :
40 : : class PGMWriter {
41 : :
42 : : private:
43 : :
44 : : SvStream& m_rOStm; // Die auszugebende PGM-Datei
45 : : sal_uInt16 mpOStmOldModus;
46 : :
47 : : sal_Bool mbStatus;
48 : : sal_uInt32 mnMode;
49 : : BitmapReadAccess* mpAcc;
50 : : sal_uLong mnWidth, mnHeight; // Bildausmass in Pixeln
51 : :
52 : : sal_Bool ImplWriteHeader();
53 : : void ImplWriteBody();
54 : : void ImplWriteNumber( sal_Int32 );
55 : :
56 : : com::sun::star::uno::Reference< com::sun::star::task::XStatusIndicator > xStatusIndicator;
57 : :
58 : : public:
59 : : PGMWriter(SvStream &rStream);
60 : : ~PGMWriter();
61 : :
62 : : sal_Bool WritePGM( const Graphic& rGraphic, FilterConfigItem* pFilterConfigItem );
63 : : };
64 : :
65 : : //=================== Methoden von PGMWriter ==============================
66 : :
67 : 0 : PGMWriter::PGMWriter(SvStream &rStream)
68 : : : m_rOStm(rStream)
69 : : , mbStatus(sal_True)
70 : 0 : , mpAcc(NULL)
71 : : {
72 : 0 : }
73 : :
74 : : // ------------------------------------------------------------------------
75 : :
76 : 0 : PGMWriter::~PGMWriter()
77 : : {
78 : 0 : }
79 : :
80 : : // ------------------------------------------------------------------------
81 : :
82 : 0 : sal_Bool PGMWriter::WritePGM( const Graphic& rGraphic, FilterConfigItem* pFilterConfigItem )
83 : : {
84 : 0 : if ( pFilterConfigItem )
85 : : {
86 : 0 : mnMode = pFilterConfigItem->ReadInt32( String( RTL_CONSTASCII_USTRINGPARAM( "FileFormat" ) ), 0 );
87 : :
88 : 0 : xStatusIndicator = pFilterConfigItem->GetStatusIndicator();
89 : 0 : if ( xStatusIndicator.is() )
90 : : {
91 : 0 : rtl::OUString aMsg;
92 : 0 : xStatusIndicator->start( aMsg, 100 );
93 : : }
94 : : }
95 : :
96 : 0 : BitmapEx aBmpEx( rGraphic.GetBitmapEx() );
97 : 0 : Bitmap aBmp = aBmpEx.GetBitmap();
98 : 0 : aBmp.Convert( BMP_CONVERSION_8BIT_GREYS );
99 : :
100 : 0 : mpOStmOldModus = m_rOStm.GetNumberFormatInt();
101 : 0 : m_rOStm.SetNumberFormatInt( NUMBERFORMAT_INT_BIGENDIAN );
102 : :
103 : 0 : mpAcc = aBmp.AcquireReadAccess();
104 : 0 : if( mpAcc )
105 : : {
106 : 0 : if ( ImplWriteHeader() )
107 : : {
108 : 0 : ImplWriteBody();
109 : : }
110 : 0 : aBmp.ReleaseAccess( mpAcc );
111 : : }
112 : : else
113 : 0 : mbStatus = sal_False;
114 : :
115 : 0 : m_rOStm.SetNumberFormatInt( mpOStmOldModus );
116 : :
117 : 0 : if ( xStatusIndicator.is() )
118 : 0 : xStatusIndicator->end();
119 : :
120 : 0 : return mbStatus;
121 : : }
122 : :
123 : : // ------------------------------------------------------------------------
124 : :
125 : 0 : sal_Bool PGMWriter::ImplWriteHeader()
126 : : {
127 : 0 : mnWidth = mpAcc->Width();
128 : 0 : mnHeight = mpAcc->Height();
129 : 0 : if ( mnWidth && mnHeight )
130 : : {
131 : 0 : if ( mnMode == 0 )
132 : 0 : m_rOStm << "P5\x0a";
133 : : else
134 : 0 : m_rOStm << "P2\x0a";
135 : :
136 : 0 : ImplWriteNumber( mnWidth );
137 : 0 : m_rOStm << (sal_uInt8)32;
138 : 0 : ImplWriteNumber( mnHeight );
139 : 0 : m_rOStm << (sal_uInt8)32;
140 : 0 : ImplWriteNumber( 255 ); // max. gray value
141 : 0 : m_rOStm << (sal_uInt8)10;
142 : : }
143 : : else
144 : 0 : mbStatus = sal_False;
145 : :
146 : 0 : return mbStatus;
147 : : }
148 : :
149 : : // ------------------------------------------------------------------------
150 : :
151 : 0 : void PGMWriter::ImplWriteBody()
152 : : {
153 : 0 : if ( mnMode == 0 )
154 : : {
155 : 0 : for ( sal_uLong y = 0; y < mnHeight; y++ )
156 : : {
157 : 0 : for ( sal_uLong x = 0; x < mnWidth; x++ )
158 : : {
159 : 0 : m_rOStm << (sal_uInt8)( mpAcc->GetPixel( y, x ) );
160 : : }
161 : : }
162 : : }
163 : : else
164 : : {
165 : 0 : for ( sal_uLong y = 0; y < mnHeight; y++ )
166 : : {
167 : 0 : int nCount = 70;
168 : 0 : for ( sal_uLong x = 0; x < mnWidth; x++ )
169 : : {
170 : : sal_uInt8 nDat, nNumb;
171 : 0 : if ( nCount < 0 )
172 : : {
173 : 0 : nCount = 69;
174 : 0 : m_rOStm << (sal_uInt8)10;
175 : : }
176 : 0 : nDat = (sal_uInt8)mpAcc->GetPixel( y, x );
177 : 0 : nNumb = nDat / 100;
178 : 0 : if ( nNumb )
179 : : {
180 : 0 : m_rOStm << (sal_uInt8)( nNumb + '0' );
181 : 0 : nDat -= ( nNumb * 100 );
182 : 0 : nNumb = nDat / 10;
183 : 0 : m_rOStm << (sal_uInt8)( nNumb + '0' );
184 : 0 : nDat -= ( nNumb * 10 );
185 : 0 : m_rOStm << (sal_uInt8)( nDat + '0' );
186 : 0 : nCount -= 4;
187 : : }
188 : : else
189 : : {
190 : 0 : nNumb = nDat / 10;
191 : 0 : if ( nNumb )
192 : : {
193 : 0 : m_rOStm << (sal_uInt8)( nNumb + '0' );
194 : 0 : nDat -= ( nNumb * 10 );
195 : 0 : m_rOStm << (sal_uInt8)( nDat + '0' );
196 : 0 : nCount -= 3;
197 : : }
198 : : else
199 : : {
200 : 0 : m_rOStm << (sal_uInt8)( nDat + '0' );
201 : 0 : nCount -= 2;
202 : : }
203 : : }
204 : 0 : m_rOStm << (sal_uInt8)' ';
205 : : }
206 : 0 : m_rOStm << (sal_uInt8)10;
207 : : }
208 : : }
209 : 0 : }
210 : :
211 : : // ------------------------------------------------------------------------
212 : : // eine Dezimalzahl im ASCII format wird in den Stream geschrieben
213 : :
214 : 0 : void PGMWriter::ImplWriteNumber(sal_Int32 nNumber)
215 : : {
216 : 0 : const rtl::OString aNum(rtl::OString::valueOf(nNumber));
217 : 0 : m_rOStm << aNum.getStr();
218 : 0 : }
219 : :
220 : : // ------------------------------------------------------------------------
221 : :
222 : : // ---------------------
223 : : // - exported function -
224 : : // ---------------------
225 : :
226 : : extern "C" SAL_DLLPUBLIC_EXPORT sal_Bool __LOADONCALLAPI
227 : 0 : GraphicExport(SvStream& rStream, Graphic& rGraphic, FilterConfigItem* pFilterConfigItem, sal_Bool)
228 : : {
229 : 0 : PGMWriter aPGMWriter(rStream);
230 : :
231 : 0 : return aPGMWriter.WritePGM( rGraphic, pFilterConfigItem );
232 : : }
233 : :
234 : : // ------------------------------------------------------------------------
235 : :
236 : :
237 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|