LibreOffice Module sd (master) 1
SlsBitmapCompressor.cxx
Go to the documentation of this file.
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
21
22#include <tools/stream.hxx>
23#include <vcl/bitmapex.hxx>
26
27namespace sd::slidesorter::cache {
28
29//===== NoBitmapCompression ===================================================
30
35 : public BitmapReplacement
36{
37public:
39
40 explicit DummyReplacement (const BitmapEx& rPreview) : maPreview(rPreview) { }
41 virtual ~DummyReplacement() {}
42 virtual sal_Int32 GetMemorySize() const override { return maPreview.GetSizeBytes(); }
43};
44
45std::shared_ptr<BitmapReplacement> NoBitmapCompression::Compress (const BitmapEx& rBitmap) const
46{
47 return std::make_shared<DummyReplacement>(rBitmap);
48}
49
51{
52 return dynamic_cast<const DummyReplacement&>(rBitmapData).maPreview;
53}
54
56{
57 return true;
58}
59
60//===== CompressionByDeletion =================================================
61
62std::shared_ptr<BitmapReplacement> CompressionByDeletion::Compress (const BitmapEx& ) const
63{
64 return std::shared_ptr<BitmapReplacement>();
65}
66
68{
69 // Return a NULL pointer. This will eventually lead to a request for
70 // the creation of a new one.
71 return BitmapEx();
72}
73
75{
76 return false;
77}
78
79//===== ResolutionReduction ===================================================
80
84{
85public:
88
90 virtual sal_Int32 GetMemorySize() const override;
91};
92
94{
95}
96
98{
99 return maPreview.GetSizeBytes();
100}
101
102std::shared_ptr<BitmapReplacement> ResolutionReduction::Compress (
103 const BitmapEx& rBitmap) const
104{
105 auto pResult = std::make_shared<ResolutionReducedReplacement>();
106 pResult->maPreview = rBitmap;
107 Size aSize (rBitmap.GetSizePixel());
108 pResult->maOriginalSize = aSize;
109 if (aSize.Width()>0 && aSize.Width()<mnWidth)
110 {
111 int nHeight = aSize.Height() * mnWidth / aSize.Width() ;
112 pResult->maPreview.Scale(Size(mnWidth,nHeight));
113 }
114
115 return pResult;
116}
117
119{
120 BitmapEx aResult;
121
123 dynamic_cast<const ResolutionReducedReplacement*>(&rBitmapData));
124
125 if ( pData && ! pData->maPreview.IsEmpty())
126 {
127 aResult = pData->maPreview;
128 if (pData->maOriginalSize.Width() > mnWidth)
129 aResult.Scale(pData->maOriginalSize);
130 }
131
132 return aResult;
133}
134
136{
137 return false;
138}
139
140//===== PNGCompression ========================================================
141
143{
144public:
145 void* mpData;
146 sal_Int32 mnDataSize;
148 : mpData(nullptr),
149 mnDataSize(0)
150 {}
152 {
153 delete [] static_cast<char*>(mpData);
154 }
155 virtual sal_Int32 GetMemorySize() const override
156 {
157 return mnDataSize;
158 }
159};
160
161std::shared_ptr<BitmapReplacement> PngCompression::Compress (const BitmapEx& rBitmap) const
162{
163 SvMemoryStream aStream (32768, 32768);
164 vcl::PngImageWriter aWriter(aStream);
165 aWriter.write(rBitmap);
166
167 auto pResult = std::make_shared<PngReplacement>();
168 pResult->mnDataSize = aStream.Tell();
169 pResult->mpData = new char[pResult->mnDataSize];
170 memcpy(pResult->mpData, aStream.GetData(), pResult->mnDataSize);
171
172 return pResult;
173}
174
176 const BitmapReplacement& rBitmapData) const
177{
178 BitmapEx aResult;
179 const PngReplacement* pData (dynamic_cast<const PngReplacement*>(&rBitmapData));
180 if (pData != nullptr)
181 {
182 SvMemoryStream aStream (pData->mpData, pData->mnDataSize, StreamMode::READ);
183 vcl::PngImageReader aReader (aStream);
184 aResult = aReader.read().GetBitmap();
185 }
186
187 return aResult;
188}
189
191{
192 return true;
193}
194
195} // end of namespace ::sd::slidesorter::cache
196
197/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
sal_Int64 GetSizeBytes() const
bool Scale(const Size &rNewSize, BmpScaleFlag nScaleFlag=BmpScaleFlag::Default)
const Size & GetSizePixel() const
constexpr tools::Long Height() const
constexpr tools::Long Width() const
const void * GetData()
sal_uInt64 Tell() const
Interface for preview bitmap replacements.
virtual BitmapEx Decompress(const BitmapReplacement &rBitmapData) const override
Decompress the given replacement data into a preview bitmap.
virtual bool IsLossless() const override
Return whether the compression and decompression is lossless.
virtual std::shared_ptr< BitmapReplacement > Compress(const BitmapEx &rBitmap) const override
Compress the given bitmap into a replacement format that is specific to the compressor class.
This dummy replacement simply stores a shared pointer to the original preview bitmap.
virtual bool IsLossless() const override
Return whether the compression and decompression is lossless.
virtual BitmapEx Decompress(const BitmapReplacement &rBitmapData) const override
Decompress the given replacement data into a preview bitmap.
virtual std::shared_ptr< BitmapReplacement > Compress(const BitmapEx &rpBitmap) const override
Compress the given bitmap into a replacement format that is specific to the compressor class.
virtual BitmapEx Decompress(const BitmapReplacement &rBitmapData) const override
Decompress the given replacement data into a preview bitmap.
virtual std::shared_ptr< BitmapReplacement > Compress(const BitmapEx &rBitmap) const override
Compress the given bitmap into a replacement format that is specific to the compressor class.
virtual bool IsLossless() const override
Return whether the compression and decompression is lossless.
Store a scaled down bitmap together with the original size.
virtual BitmapEx Decompress(const BitmapReplacement &rBitmapData) const override
Scale the replacement bitmap up to the original size.
virtual bool IsLossless() const override
Return whether the compression and decompression is lossless.
virtual std::shared_ptr< BitmapReplacement > Compress(const BitmapEx &rpBitmap) const override
Compress the given bitmap into a replacement format that is specific to the compressor class.
bool read(BitmapEx &rBitmap)
bool write(const BitmapEx &rBitmap)
std::unique_ptr< sal_Int32[]> pData