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: SlsBitmapCompressor.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_sd.hxx"
34 #include "SlsBitmapCompressor.hxx"
36 #include <tools/stream.hxx>
37 #include <vcl/bitmapex.hxx>
38 #include <vcl/pngread.hxx>
39 #include <vcl/pngwrite.hxx>
41 namespace sd
{ namespace slidesorter
{ namespace cache
{
44 //===== NoBitmapCompression ===================================================
46 /** This dummy replacement simply stores a shared pointer to the original
49 class NoBitmapCompression::DummyReplacement
50 : public BitmapReplacement
53 ::boost::shared_ptr
<BitmapEx
> mpPreview
;
56 DummyReplacement (const ::boost::shared_ptr
<BitmapEx
>& rpPreview
) : mpPreview(rpPreview
)
60 virtual ~DummyReplacement();
62 virtual sal_Int32
GetMemorySize (void) const;
65 NoBitmapCompression::DummyReplacement::~DummyReplacement()
69 sal_Int32
NoBitmapCompression::DummyReplacement::GetMemorySize (void) const
71 return mpPreview
->GetSizeBytes();
74 ::boost::shared_ptr
<BitmapReplacement
> NoBitmapCompression::Compress (
75 const ::boost::shared_ptr
<BitmapEx
>& rpBitmap
) const
77 return ::boost::shared_ptr
<BitmapReplacement
>(new DummyReplacement(rpBitmap
));
80 ::boost::shared_ptr
<BitmapEx
> NoBitmapCompression::Decompress (
81 const BitmapReplacement
& rBitmapData
) const
83 return dynamic_cast<const DummyReplacement
&>(rBitmapData
).mpPreview
;
89 bool NoBitmapCompression::IsLossless (void) const
97 //===== CompressionByDeletion =================================================
99 ::boost::shared_ptr
<BitmapReplacement
> CompressionByDeletion::Compress (
100 const ::boost::shared_ptr
<BitmapEx
>& ) const
102 return ::boost::shared_ptr
<BitmapReplacement
>();
108 ::boost::shared_ptr
<BitmapEx
> CompressionByDeletion::Decompress (
109 const BitmapReplacement
& ) const
111 // Return a NULL pointer. This will eventually lead to a request for
112 // the creation of a new one.
113 return ::boost::shared_ptr
<BitmapEx
>();
119 bool CompressionByDeletion::IsLossless (void) const
127 //===== ResolutionReduction ===================================================
129 /** Store a scaled down bitmap together with the original size.
131 class ResolutionReduction::ResolutionReducedReplacement
: public BitmapReplacement
134 ::boost::shared_ptr
<BitmapEx
> mpPreview
;
137 virtual ~ResolutionReducedReplacement();
139 virtual sal_Int32
GetMemorySize (void) const;
142 ResolutionReduction::ResolutionReducedReplacement::~ResolutionReducedReplacement()
146 sal_Int32
ResolutionReduction::ResolutionReducedReplacement::GetMemorySize (void) const
148 if (mpPreview
.get() != NULL
)
149 return mpPreview
->GetSizeBytes();
154 ::boost::shared_ptr
<BitmapReplacement
> ResolutionReduction::Compress (
155 const ::boost::shared_ptr
<BitmapEx
>& rpBitmap
) const
157 ResolutionReducedReplacement
* pResult
= new ResolutionReducedReplacement();
158 pResult
->mpPreview
.reset(new BitmapEx(*rpBitmap
));
159 Size
aSize (rpBitmap
->GetSizePixel());
160 pResult
->maOriginalSize
= aSize
;
161 if (aSize
.Width()>0 && aSize
.Width()<mnWidth
)
163 int nHeight
= aSize
.Height() * mnWidth
/ aSize
.Width() ;
164 pResult
->mpPreview
->Scale(Size(mnWidth
,nHeight
));
167 return ::boost::shared_ptr
<BitmapReplacement
>(pResult
);
173 ::boost::shared_ptr
<BitmapEx
> ResolutionReduction::Decompress (
174 const BitmapReplacement
& rBitmapData
) const
176 ::boost::shared_ptr
<BitmapEx
> pResult
;
178 const ResolutionReducedReplacement
* pData (
179 dynamic_cast<const ResolutionReducedReplacement
*>(&rBitmapData
));
181 if (pData
->mpPreview
.get() != NULL
)
183 pResult
.reset(new BitmapEx(*pData
->mpPreview
));
184 if (pData
->maOriginalSize
.Width() > mnWidth
)
185 pResult
->Scale(pData
->maOriginalSize
);
194 bool ResolutionReduction::IsLossless (void) const
202 //===== PNGCompression ========================================================
205 class PngCompression::PngReplacement
: public BitmapReplacement
209 sal_Int32 mnDataSize
;
211 PngReplacement (void)
216 virtual ~PngReplacement (void)
218 delete [] (char*)mpData
;
220 virtual sal_Int32
GetMemorySize (void) const
229 ::boost::shared_ptr
<BitmapReplacement
> PngCompression::Compress (
230 const ::boost::shared_ptr
<BitmapEx
>& rpBitmap
) const
232 ::vcl::PNGWriter
aWriter (*rpBitmap
);
233 SvMemoryStream
aStream (32768, 32768);
234 aWriter
.Write(aStream
);
236 PngReplacement
* pResult
= new PngReplacement();
237 pResult
->maImageSize
= rpBitmap
->GetSizePixel();
238 pResult
->mnDataSize
= aStream
.Tell();
239 pResult
->mpData
= new char[pResult
->mnDataSize
];
240 memcpy(pResult
->mpData
, aStream
.GetData(), pResult
->mnDataSize
);
242 return ::boost::shared_ptr
<BitmapReplacement
>(pResult
);
248 ::boost::shared_ptr
<BitmapEx
> PngCompression::Decompress (
249 const BitmapReplacement
& rBitmapData
) const
251 BitmapEx
* pResult
= NULL
;
252 const PngReplacement
* pData (dynamic_cast<const PngReplacement
*>(&rBitmapData
));
255 SvMemoryStream
aStream (pData
->mpData
, pData
->mnDataSize
, STREAM_READ
);
256 ::vcl::PNGReader
aReader (aStream
);
257 pResult
= new BitmapEx(aReader
.Read());
260 // sal_Int32 nRatio ((100L * (ULONG)pResult->GetSizeBytes()) / (ULONG)pData->mnDataSize);
262 return ::boost::shared_ptr
<BitmapEx
>(pResult
);
268 bool PngCompression::IsLossless (void) const
276 } } } // end of namespace ::sd::slidesorter::cache