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