1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include "SlsBitmapCompressor.hxx"
22 #include <tools/stream.hxx>
23 #include <vcl/bitmapex.hxx>
24 #include <vcl/pngread.hxx>
25 #include <vcl/pngwrite.hxx>
27 namespace sd
{ namespace slidesorter
{ namespace cache
{
29 //===== NoBitmapCompression ===================================================
31 /** This dummy replacement simply stores a shared pointer to the original
34 class NoBitmapCompression::DummyReplacement
35 : public BitmapReplacement
38 BitmapEx
const maPreview
;
40 explicit DummyReplacement (const BitmapEx
& rPreview
) : maPreview(rPreview
) { }
41 virtual ~DummyReplacement() {}
42 virtual sal_Int32
GetMemorySize() const override
{ return maPreview
.GetSizeBytes(); }
45 std::shared_ptr
<BitmapReplacement
> NoBitmapCompression::Compress (const BitmapEx
& rBitmap
) const
47 return std::shared_ptr
<BitmapReplacement
>(new DummyReplacement(rBitmap
));
50 BitmapEx
NoBitmapCompression::Decompress (const BitmapReplacement
& rBitmapData
) const
52 return dynamic_cast<const DummyReplacement
&>(rBitmapData
).maPreview
;
55 bool NoBitmapCompression::IsLossless() const
60 //===== CompressionByDeletion =================================================
62 std::shared_ptr
<BitmapReplacement
> CompressionByDeletion::Compress (const BitmapEx
& ) const
64 return std::shared_ptr
<BitmapReplacement
>();
67 BitmapEx
CompressionByDeletion::Decompress (const BitmapReplacement
& ) const
69 // Return a NULL pointer. This will eventually lead to a request for
70 // the creation of a new one.
74 bool CompressionByDeletion::IsLossless() const
79 //===== ResolutionReduction ===================================================
81 /** Store a scaled down bitmap together with the original size.
83 class ResolutionReduction::ResolutionReducedReplacement
: public BitmapReplacement
89 virtual ~ResolutionReducedReplacement();
90 virtual sal_Int32
GetMemorySize() const override
;
93 ResolutionReduction::ResolutionReducedReplacement::~ResolutionReducedReplacement()
97 sal_Int32
ResolutionReduction::ResolutionReducedReplacement::GetMemorySize() const
99 return maPreview
.GetSizeBytes();
102 std::shared_ptr
<BitmapReplacement
> ResolutionReduction::Compress (
103 const BitmapEx
& rBitmap
) const
105 ResolutionReducedReplacement
* pResult
= new ResolutionReducedReplacement
;
106 pResult
->maPreview
= rBitmap
;
107 Size
aSize (rBitmap
.GetSizePixel());
108 pResult
->maOriginalSize
= aSize
;
109 if (aSize
.Width()>0 && aSize
.Width()<mnWidth
)
111 int nHeight
= aSize
.Height() * mnWidth
/ aSize
.Width() ;
112 pResult
->maPreview
.Scale(Size(mnWidth
,nHeight
));
115 return std::shared_ptr
<BitmapReplacement
>(pResult
);
118 BitmapEx
ResolutionReduction::Decompress (const BitmapReplacement
& rBitmapData
) const
122 const ResolutionReducedReplacement
* pData (
123 dynamic_cast<const ResolutionReducedReplacement
*>(&rBitmapData
));
125 if ( pData
&& ! pData
->maPreview
.IsEmpty())
127 aResult
= pData
->maPreview
;
128 if (pData
->maOriginalSize
.Width() > mnWidth
)
129 aResult
.Scale(pData
->maOriginalSize
);
135 bool ResolutionReduction::IsLossless() const
140 //===== PNGCompression ========================================================
142 class PngCompression::PngReplacement
: public BitmapReplacement
146 sal_Int32 mnDataSize
;
151 virtual ~PngReplacement()
153 delete [] static_cast<char*>(mpData
);
155 virtual sal_Int32
GetMemorySize() const override
161 std::shared_ptr
<BitmapReplacement
> PngCompression::Compress (const BitmapEx
& rBitmap
) const
163 vcl::PNGWriter
aWriter(rBitmap
);
164 SvMemoryStream
aStream (32768, 32768);
165 aWriter
.Write(aStream
);
167 PngReplacement
* pResult
= new PngReplacement();
168 pResult
->mnDataSize
= aStream
.Tell();
169 pResult
->mpData
= new char[pResult
->mnDataSize
];
170 memcpy(pResult
->mpData
, aStream
.GetData(), pResult
->mnDataSize
);
172 return std::shared_ptr
<BitmapReplacement
>(pResult
);
175 BitmapEx
PngCompression::Decompress (
176 const BitmapReplacement
& rBitmapData
) const
179 const PngReplacement
* pData (dynamic_cast<const PngReplacement
*>(&rBitmapData
));
180 if (pData
!= nullptr)
182 SvMemoryStream
aStream (pData
->mpData
, pData
->mnDataSize
, StreamMode::READ
);
183 vcl::PNGReader
aReader (aStream
);
184 aResult
= aReader
.Read().GetBitmap();
190 bool PngCompression::IsLossless() const
195 } } } // end of namespace ::sd::slidesorter::cache
197 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */