1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: imagecontainer.cxx,v $
13 * This file is part of OpenOffice.org.
15 * OpenOffice.org is free software: you can redistribute it and/or modify
16 * it under the terms of the GNU Lesser General Public License version 3
17 * only, as published by the Free Software Foundation.
19 * OpenOffice.org is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU Lesser General Public License version 3 for more details
23 * (a copy is included in the LICENSE file that accompanied this code).
25 * You should have received a copy of the GNU Lesser General Public License
26 * version 3 along with OpenOffice.org. If not, see
27 * <http://www.openoffice.org/license.html>
28 * for a copy of the LGPLv3 License.
30 ************************************************************************/
32 // MARKER(update_precomp.py): autogen include statement, do not remove
33 #include "precompiled_sdext.hxx"
35 #include "imagecontainer.hxx"
36 #include "genericelements.hxx"
37 #include "xmlemitter.hxx"
39 #include <rtl/ustrbuf.hxx>
43 #include <com/sun/star/graphic/XGraphicProvider.hpp>
44 #include <com/sun/star/beans/PropertyValue.hpp>
46 #include <cppuhelper/implbase1.hxx>
47 #include <comphelper/stl_types.hxx>
49 #include <boost/bind.hpp>
51 using namespace com::sun::star
;
59 static const sal_Char aBase64EncodeTable
[] =
60 { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
61 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
62 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
63 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
64 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/' };
66 rtl::OUString
encodeBase64( const sal_Int8
* i_pBuffer
, const sal_uInt32 i_nBufferLength
)
68 rtl::OUStringBuffer
aBuf( (i_nBufferLength
+1) * 4 / 3 );
69 const sal_Int32
nRemain(i_nBufferLength
%3);
70 const sal_Int32
nFullTripleLength( i_nBufferLength
- (i_nBufferLength
%3));
71 sal_Int32
nBufPos( 0 );
72 for( sal_Int32 i
= 0; i
< nFullTripleLength
; i
+= 3, nBufPos
+= 4 )
74 const sal_Int32 nBinary
= (((sal_uInt8
)i_pBuffer
[i
+ 0]) << 16) +
75 (((sal_uInt8
)i_pBuffer
[i
+ 1]) << 8) +
76 ((sal_uInt8
)i_pBuffer
[i
+ 2]);
78 aBuf
.appendAscii("====");
80 sal_uInt8
nIndex (static_cast<sal_uInt8
>((nBinary
& 0xFC0000) >> 18));
81 aBuf
.setCharAt(nBufPos
, aBase64EncodeTable
[nIndex
]);
83 nIndex
= static_cast<sal_uInt8
>((nBinary
& 0x3F000) >> 12);
84 aBuf
.setCharAt(nBufPos
+1, aBase64EncodeTable
[nIndex
]);
86 nIndex
= static_cast<sal_uInt8
>((nBinary
& 0xFC0) >> 6);
87 aBuf
.setCharAt(nBufPos
+2, aBase64EncodeTable
[nIndex
]);
89 nIndex
= static_cast<sal_uInt8
>((nBinary
& 0x3F));
90 aBuf
.setCharAt(nBufPos
+3, aBase64EncodeTable
[nIndex
]);
94 aBuf
.appendAscii("====");
95 sal_Int32
nBinary( 0 );
96 const sal_Int32
nStart(i_nBufferLength
-nRemain
);
99 case 1: nBinary
= ((sal_uInt8
)i_pBuffer
[nStart
+ 0]) << 16;
101 case 2: nBinary
= (((sal_uInt8
)i_pBuffer
[nStart
+ 0]) << 16) +
102 (((sal_uInt8
)i_pBuffer
[nStart
+ 1]) << 8);
105 sal_uInt8
nIndex (static_cast<sal_uInt8
>((nBinary
& 0xFC0000) >> 18));
106 aBuf
.setCharAt(nBufPos
, aBase64EncodeTable
[nIndex
]);
108 nIndex
= static_cast<sal_uInt8
>((nBinary
& 0x3F000) >> 12);
109 aBuf
.setCharAt(nBufPos
+1, aBase64EncodeTable
[nIndex
]);
113 nIndex
= static_cast<sal_uInt8
>((nBinary
& 0xFC0) >> 6);
114 aBuf
.setCharAt(nBufPos
+2, aBase64EncodeTable
[nIndex
]);
118 return aBuf
.makeStringAndClear();
123 ImageContainer::ImageContainer() :
127 ImageId
ImageContainer::addImage( const uno::Sequence
<beans::PropertyValue
>& xBitmap
)
129 m_aImages
.push_back( xBitmap
);
130 return m_aImages
.size()-1;
133 void ImageContainer::writeBase64EncodedStream( ImageId nId
, EmitContext
& rContext
)
135 OSL_ASSERT( nId
>= 0 && nId
< ImageId( m_aImages
.size()) );
137 const uno::Sequence
<beans::PropertyValue
>& rEntry( m_aImages
[nId
] );
139 // find "InputSequence" property
140 const beans::PropertyValue
* pAry(rEntry
.getConstArray());
141 const sal_Int32
nLen(rEntry
.getLength());
142 const beans::PropertyValue
* pValue(
143 std::find_if(pAry
,pAry
+nLen
,
144 boost::bind(comphelper::TPropertyValueEqualFunctor(),
146 rtl::OUString::createFromAscii("InputSequence"))));
147 OSL_ENSURE( pValue
!= pAry
+nLen
,
148 "InputSequence not found" );
150 uno::Sequence
<sal_Int8
> aData
;
151 if( !(pValue
->Value
>>= aData
) )
152 OSL_ENSURE(false,"Wrong data type");
154 rContext
.rEmitter
.write( encodeBase64( aData
.getConstArray(), aData
.getLength() ));