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 : sal_Bool mbStatus;
39 : sal_Int32 mnMode; // 0 -> raw, 1-> ascii
40 : BitmapReadAccess* mpAcc;
41 : sal_uLong mnWidth, mnHeight; // size in pixel
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 : PBMWriter(SvStream &rPBM);
51 : ~PBMWriter();
52 :
53 : sal_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 : , mbStatus(sal_True)
61 0 : , mpAcc(NULL)
62 : {
63 0 : }
64 :
65 : // ------------------------------------------------------------------------
66 :
67 0 : PBMWriter::~PBMWriter()
68 : {
69 0 : }
70 :
71 : // ------------------------------------------------------------------------
72 :
73 0 : sal_Bool PBMWriter::WritePBM( 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_1BIT_THRESHOLD );
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 0 : ImplWriteBody();
99 :
100 0 : aBmp.ReleaseAccess( mpAcc );
101 : }
102 : else
103 0 : mbStatus = sal_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 : sal_Bool PBMWriter::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 << "P4\x0a";
123 : else
124 0 : m_rOStm << "P1\x0a";
125 :
126 0 : ImplWriteNumber( mnWidth );
127 0 : m_rOStm << (sal_uInt8)32;
128 0 : ImplWriteNumber( mnHeight );
129 0 : m_rOStm << (sal_uInt8)10;
130 : }
131 0 : else mbStatus = sal_False;
132 0 : return mbStatus;
133 : }
134 :
135 : // ------------------------------------------------------------------------
136 :
137 0 : void PBMWriter::ImplWriteBody()
138 : {
139 0 : if ( mnMode == 0 )
140 : {
141 0 : sal_uInt8 nBYTE = 0;
142 0 : for ( sal_uLong y = 0; y < mnHeight; y++ )
143 : {
144 : sal_uLong x;
145 0 : for ( x = 0; x < mnWidth; x++ )
146 : {
147 0 : nBYTE <<= 1;
148 0 : if (!(mpAcc->GetPixelIndex( y, x ) & 1 ) )
149 0 : nBYTE++;
150 0 : if ( ( x & 7 ) == 7 )
151 0 : m_rOStm << nBYTE;
152 : }
153 0 : if ( ( x & 7 ) != 0 )
154 0 : m_rOStm << (sal_uInt8)( nBYTE << ( ( x ^ 7 ) + 1 ) );
155 : }
156 : }
157 : else
158 : {
159 : int nxCount;
160 0 : for ( sal_uLong y = 0; y < mnHeight; y++ )
161 : {
162 0 : nxCount = 70;
163 0 : for ( sal_uLong x = 0; x < mnWidth; x++ )
164 : {
165 0 : if (!( --nxCount ) )
166 : {
167 0 : nxCount = 69;
168 0 : m_rOStm << (sal_uInt8)10;
169 : }
170 0 : m_rOStm << (sal_uInt8)( ( mpAcc->GetPixelIndex( y, x ) ^ 1 ) + '0' ) ;
171 : }
172 0 : m_rOStm << (sal_uInt8)10;
173 : }
174 : }
175 0 : }
176 :
177 : // ------------------------------------------------------------------------
178 : // A decimal number in ascii format is written in the stream.
179 :
180 0 : void PBMWriter::ImplWriteNumber(sal_Int32 nNumber)
181 : {
182 0 : const OString aNum(OString::valueOf(nNumber));
183 0 : m_rOStm << aNum.getStr();
184 0 : }
185 :
186 : // ------------------------------------------------------------------------
187 :
188 : // ---------------------
189 : // - exported function -
190 : // ---------------------
191 :
192 : #if defined(DISABLE_DYNLOADING) || defined(LIBO_MERGELIBS)
193 : #define GraphicExport epbGraphicExport
194 : #endif
195 :
196 : extern "C" SAL_DLLPUBLIC_EXPORT sal_Bool SAL_CALL
197 0 : GraphicExport(SvStream& rStream, Graphic& rGraphic, FilterConfigItem* pFilterConfigItem, sal_Bool)
198 : {
199 0 : PBMWriter aPBMWriter(rStream);
200 :
201 0 : return aPBMWriter.WritePBM( rGraphic, pFilterConfigItem );
202 0 : }
203 :
204 : // ------------------------------------------------------------------------
205 :
206 :
207 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|