cid#1607171 Data race condition
[LibreOffice.git] / sd / source / ui / slidesorter / cache / SlsBitmapCompressor.cxx
blob2d63bd47493ec4f622bea8823114cb11782b8875
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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/filter/PngImageReader.hxx>
25 #include <vcl/filter/PngImageWriter.hxx>
27 namespace sd::slidesorter::cache {
29 //===== NoBitmapCompression ===================================================
31 /** This dummy replacement simply stores a shared pointer to the original
32 preview bitmap.
34 class NoBitmapCompression::DummyReplacement
35 : public BitmapReplacement
37 public:
38 BitmapEx 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::make_shared<DummyReplacement>(rBitmap);
50 BitmapEx NoBitmapCompression::Decompress (const BitmapReplacement& rBitmapData) const
52 return dynamic_cast<const DummyReplacement&>(rBitmapData).maPreview;
55 bool NoBitmapCompression::IsLossless() const
57 return true;
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.
71 return BitmapEx();
74 bool CompressionByDeletion::IsLossless() const
76 return false;
79 //===== ResolutionReduction ===================================================
81 /** Store a scaled down bitmap together with the original size.
83 class ResolutionReduction::ResolutionReducedReplacement : public BitmapReplacement
85 public:
86 BitmapEx maPreview;
87 Size maOriginalSize;
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 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)
111 int nHeight = aSize.Height() * mnWidth / aSize.Width() ;
112 pResult->maPreview.Scale(Size(mnWidth,nHeight));
115 return pResult;
118 BitmapEx ResolutionReduction::Decompress (const BitmapReplacement& rBitmapData) const
120 BitmapEx aResult;
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);
132 return aResult;
135 bool ResolutionReduction::IsLossless() const
137 return false;
140 //===== PNGCompression ========================================================
142 class PngCompression::PngReplacement : public BitmapReplacement
144 public:
145 void* mpData;
146 sal_Int32 mnDataSize;
147 PngReplacement()
148 : mpData(nullptr),
149 mnDataSize(0)
151 virtual ~PngReplacement()
153 delete [] static_cast<char*>(mpData);
155 virtual sal_Int32 GetMemorySize() const override
157 return mnDataSize;
161 std::shared_ptr<BitmapReplacement> PngCompression::Compress (const BitmapEx& rBitmap) const
163 SvMemoryStream aStream (32768, 32768);
164 vcl::PngImageWriter aWriter(aStream);
165 aWriter.write(rBitmap);
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);
172 return pResult;
175 BitmapEx PngCompression::Decompress (
176 const BitmapReplacement& rBitmapData) const
178 BitmapEx aResult;
179 const PngReplacement* pData (dynamic_cast<const PngReplacement*>(&rBitmapData));
180 if (pData != nullptr)
182 SvMemoryStream aStream (pData->mpData, pData->mnDataSize, StreamMode::READ);
183 vcl::PngImageReader aReader (aStream);
184 aResult = aReader.read().GetBitmap();
187 return aResult;
190 bool PngCompression::IsLossless() const
192 return true;
195 } // end of namespace ::sd::slidesorter::cache
197 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */