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