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 : //============================ PPMWriter ==================================
30 :
31 : class PPMWriter {
32 :
33 : private:
34 :
35 : SvStream& m_rOStm; // Die auszugebende PPM-Datei
36 : sal_uInt16 mpOStmOldModus;
37 :
38 : sal_Bool mbStatus;
39 : sal_Int32 mnMode;
40 : BitmapReadAccess* mpAcc;
41 : sal_uLong mnWidth, mnHeight; // Bildausmass 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 : PPMWriter(SvStream &rStrm);
51 : ~PPMWriter();
52 :
53 : sal_Bool WritePPM( const Graphic& rGraphic, FilterConfigItem* pFilterConfigItem );
54 : };
55 :
56 : //=================== Methods of PPMWriter ==============================
57 0 : PPMWriter::PPMWriter(SvStream &rStrm)
58 : : m_rOStm(rStrm)
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 : PPMWriter::~PPMWriter()
69 : {
70 0 : }
71 :
72 :
73 :
74 0 : sal_Bool PPMWriter::WritePPM( const Graphic& rGraphic, FilterConfigItem* pFilterConfigItem )
75 : {
76 0 : if ( pFilterConfigItem )
77 : {
78 0 : mnMode = pFilterConfigItem->ReadInt32( "FileFormat", 0 );
79 :
80 0 : xStatusIndicator = pFilterConfigItem->GetStatusIndicator();
81 0 : if ( xStatusIndicator.is() )
82 : {
83 0 : OUString aMsg;
84 0 : xStatusIndicator->start( aMsg, 100 );
85 : }
86 : }
87 :
88 0 : BitmapEx aBmpEx( rGraphic.GetBitmapEx() );
89 0 : Bitmap aBmp = aBmpEx.GetBitmap();
90 0 : aBmp.Convert( BMP_CONVERSION_24BIT );
91 :
92 0 : mpOStmOldModus = m_rOStm.GetNumberFormatInt();
93 0 : m_rOStm.SetNumberFormatInt( NUMBERFORMAT_INT_BIGENDIAN );
94 :
95 0 : mpAcc = aBmp.AcquireReadAccess();
96 0 : if( mpAcc )
97 : {
98 0 : if ( ImplWriteHeader() )
99 : {
100 0 : ImplWriteBody();
101 : }
102 0 : aBmp.ReleaseAccess( mpAcc );
103 : }
104 : else
105 0 : mbStatus = sal_False;
106 :
107 0 : m_rOStm.SetNumberFormatInt( mpOStmOldModus );
108 :
109 0 : if ( xStatusIndicator.is() )
110 0 : xStatusIndicator->end();
111 :
112 0 : return mbStatus;
113 : }
114 :
115 :
116 :
117 0 : sal_Bool PPMWriter::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( "P6\x0a" );
125 : else
126 0 : m_rOStm.WriteCharPtr( "P3\x0a" );
127 :
128 0 : ImplWriteNumber( mnWidth );
129 0 : m_rOStm.WriteUChar( (sal_uInt8)32 );
130 0 : ImplWriteNumber( mnHeight );
131 0 : m_rOStm.WriteUChar( (sal_uInt8)32 );
132 0 : ImplWriteNumber( 255 ); // max. col.
133 0 : m_rOStm.WriteUChar( (sal_uInt8)10 );
134 : }
135 : else
136 0 : mbStatus = sal_False;
137 :
138 0 : return mbStatus;
139 : }
140 :
141 :
142 :
143 0 : void PPMWriter::ImplWriteBody()
144 : {
145 0 : if ( mnMode == 0 )
146 : {
147 0 : for ( sal_uLong y = 0; y < mnHeight; y++ )
148 : {
149 0 : for ( sal_uLong x = 0; x < mnWidth; x++ )
150 : {
151 0 : const BitmapColor& rColor = mpAcc->GetPixel( y, x );
152 0 : m_rOStm.WriteUChar( rColor.GetRed() );
153 0 : m_rOStm.WriteUChar( rColor.GetGreen() );
154 0 : m_rOStm.WriteUChar( rColor.GetBlue() );
155 0 : }
156 : }
157 : }
158 : else
159 : {
160 0 : for ( sal_uLong y = 0; y < mnHeight; y++ )
161 : {
162 0 : int nCount = 70;
163 0 : for ( sal_uLong x = 0; x < mnWidth; x++ )
164 : {
165 : sal_uInt8 i, nDat[3], nNumb;
166 0 : if ( nCount < 0 )
167 : {
168 0 : nCount = 69;
169 0 : m_rOStm.WriteUChar( (sal_uInt8)10 );
170 : }
171 0 : nDat[0] = mpAcc->GetPixel( y, x ).GetRed();
172 0 : nDat[1] = mpAcc->GetPixel( y, x ).GetGreen();
173 0 : nDat[2] = mpAcc->GetPixel( y, x ).GetBlue();
174 0 : for ( i = 0; i < 3; i++ )
175 : {
176 0 : nNumb = nDat[ i ] / 100;
177 0 : if ( nNumb )
178 : {
179 0 : m_rOStm.WriteUChar( (sal_uInt8)( nNumb + '0' ) );
180 0 : nDat[ i ] -= ( nNumb * 100 );
181 0 : nNumb = nDat[ i ] / 10;
182 0 : m_rOStm.WriteUChar( (sal_uInt8)( nNumb + '0' ) );
183 0 : nDat[ i ] -= ( nNumb * 10 );
184 0 : m_rOStm.WriteUChar( (sal_uInt8)( nDat[ i ] + '0' ) );
185 0 : nCount -= 4;
186 : }
187 : else
188 : {
189 0 : nNumb = nDat[ i ] / 10;
190 0 : if ( nNumb )
191 : {
192 0 : m_rOStm.WriteUChar( (sal_uInt8)( nNumb + '0' ) );
193 0 : nDat[ i ] -= ( nNumb * 10 );
194 0 : m_rOStm.WriteUChar( (sal_uInt8)( nDat[ i ] + '0' ) );
195 0 : nCount -= 3;
196 : }
197 : else
198 : {
199 0 : m_rOStm.WriteUChar( (sal_uInt8)( nDat[ i ] + '0' ) );
200 0 : nCount -= 2;
201 : }
202 : }
203 0 : m_rOStm.WriteUChar( (sal_uInt8)' ' );
204 : }
205 : }
206 0 : m_rOStm.WriteUChar( (sal_uInt8)10 );
207 : }
208 : }
209 0 : }
210 :
211 :
212 : // a decimal number in ASCII format is being written into the stream
213 :
214 0 : void PPMWriter::ImplWriteNumber(sal_Int32 nNumber)
215 : {
216 0 : const OString aNum(OString::number(nNumber));
217 0 : m_rOStm.WriteCharPtr( aNum.getStr() );
218 0 : }
219 :
220 :
221 :
222 :
223 : // - exported function -
224 :
225 :
226 : // this needs to be kept in sync with
227 : // ImpFilterLibCacheEntry::GetImportFunction() from
228 : // vcl/source/filter/graphicfilter.cxx
229 : #if defined(DISABLE_DYNLOADING)
230 : #define GraphicExport eppGraphicExport
231 : #endif
232 :
233 : extern "C" SAL_DLLPUBLIC_EXPORT bool SAL_CALL
234 0 : GraphicExport( SvStream& rStream, Graphic& rGraphic, FilterConfigItem* pFilterConfigItem )
235 : {
236 0 : PPMWriter aPPMWriter(rStream);
237 0 : return aPPMWriter.WritePPM( rGraphic, pFilterConfigItem );
238 : }
239 :
240 :
241 :
242 :
243 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|