Branch data Line data Source code
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 : :
20 : :
21 : : #include "SlsBitmapCompressor.hxx"
22 : :
23 : : #include <tools/stream.hxx>
24 : : #include <vcl/bitmapex.hxx>
25 : : #include <vcl/pngread.hxx>
26 : : #include <vcl/pngwrite.hxx>
27 : :
28 : : namespace sd { namespace slidesorter { namespace cache {
29 : :
30 : :
31 : : //===== NoBitmapCompression ===================================================
32 : :
33 : : /** This dummy replacement simply stores a shared pointer to the original
34 : : preview bitmap.
35 : : */
36 : : class NoBitmapCompression::DummyReplacement
37 : : : public BitmapReplacement
38 : : {
39 : : public:
40 : : Bitmap maPreview;
41 : : Size maOriginalSize;
42 : :
43 [ # # ]: 0 : DummyReplacement (const Bitmap& rPreview) : maPreview(rPreview) { }
44 [ # # ][ # # ]: 0 : virtual ~DummyReplacement(void) {}
45 : 0 : virtual sal_Int32 GetMemorySize (void) const { return maPreview.GetSizeBytes(); }
46 : : };
47 : :
48 : :
49 : :
50 : 0 : ::boost::shared_ptr<BitmapReplacement> NoBitmapCompression::Compress (const Bitmap& rBitmap) const
51 : : {
52 [ # # ]: 0 : return ::boost::shared_ptr<BitmapReplacement>(new DummyReplacement(rBitmap));
53 : : }
54 : :
55 : 0 : Bitmap NoBitmapCompression::Decompress (const BitmapReplacement& rBitmapData) const
56 : : {
57 [ # # ]: 0 : return dynamic_cast<const DummyReplacement&>(rBitmapData).maPreview;
58 : : }
59 : :
60 : :
61 : :
62 : :
63 : 0 : bool NoBitmapCompression::IsLossless (void) const
64 : : {
65 : 0 : return true;
66 : : }
67 : :
68 : :
69 : :
70 : :
71 : : //===== CompressionByDeletion =================================================
72 : :
73 : 0 : ::boost::shared_ptr<BitmapReplacement> CompressionByDeletion::Compress (const Bitmap& ) const
74 : : {
75 : 0 : return ::boost::shared_ptr<BitmapReplacement>();
76 : : }
77 : :
78 : :
79 : :
80 : :
81 : 0 : Bitmap CompressionByDeletion::Decompress (const BitmapReplacement& ) const
82 : : {
83 : : // Return a NULL pointer. This will eventually lead to a request for
84 : : // the creation of a new one.
85 : 0 : return Bitmap();
86 : : }
87 : :
88 : :
89 : :
90 : :
91 : 0 : bool CompressionByDeletion::IsLossless (void) const
92 : : {
93 : 0 : return false;
94 : : }
95 : :
96 : :
97 : :
98 : :
99 : : //===== ResolutionReduction ===================================================
100 : :
101 : : /** Store a scaled down bitmap together with the original size.
102 : : */
103 [ # # ]: 0 : class ResolutionReduction::ResolutionReducedReplacement : public BitmapReplacement
104 : : {
105 : : public:
106 : : Bitmap maPreview;
107 : : Size maOriginalSize;
108 : :
109 : : virtual ~ResolutionReducedReplacement();
110 : : virtual sal_Int32 GetMemorySize (void) const;
111 : : };
112 : :
113 [ # # ]: 0 : ResolutionReduction::ResolutionReducedReplacement::~ResolutionReducedReplacement()
114 : : {
115 [ # # ]: 0 : }
116 : :
117 : 0 : sal_Int32 ResolutionReduction::ResolutionReducedReplacement::GetMemorySize (void) const
118 : : {
119 : 0 : return maPreview.GetSizeBytes();
120 : : }
121 : :
122 : 0 : ::boost::shared_ptr<BitmapReplacement> ResolutionReduction::Compress (
123 : : const Bitmap& rBitmap) const
124 : : {
125 [ # # ][ # # ]: 0 : ResolutionReducedReplacement* pResult = new ResolutionReducedReplacement();
126 [ # # ]: 0 : pResult->maPreview = rBitmap;
127 [ # # ]: 0 : Size aSize (rBitmap.GetSizePixel());
128 : 0 : pResult->maOriginalSize = aSize;
129 [ # # ][ # # ]: 0 : if (aSize.Width()>0 && aSize.Width()<mnWidth)
[ # # ]
130 : : {
131 : 0 : int nHeight = aSize.Height() * mnWidth / aSize.Width() ;
132 [ # # ]: 0 : pResult->maPreview.Scale(Size(mnWidth,nHeight));
133 : : }
134 : :
135 [ # # ]: 0 : return ::boost::shared_ptr<BitmapReplacement>(pResult);
136 : : }
137 : :
138 : :
139 : :
140 : :
141 : 0 : Bitmap ResolutionReduction::Decompress (const BitmapReplacement& rBitmapData) const
142 : : {
143 : 0 : Bitmap aResult;
144 : :
145 : : const ResolutionReducedReplacement* pData (
146 [ # # ]: 0 : dynamic_cast<const ResolutionReducedReplacement*>(&rBitmapData));
147 : :
148 [ # # ]: 0 : if ( ! pData->maPreview.IsEmpty())
149 : : {
150 [ # # ]: 0 : aResult = pData->maPreview;
151 [ # # ]: 0 : if (pData->maOriginalSize.Width() > mnWidth)
152 [ # # ]: 0 : aResult.Scale(pData->maOriginalSize);
153 : : }
154 : :
155 : 0 : return aResult;
156 : : }
157 : :
158 : :
159 : :
160 : :
161 : 0 : bool ResolutionReduction::IsLossless (void) const
162 : : {
163 : 0 : return false;
164 : : }
165 : :
166 : :
167 : :
168 : :
169 : : //===== PNGCompression ========================================================
170 : :
171 : :
172 : : class PngCompression::PngReplacement : public BitmapReplacement
173 : : {
174 : : public:
175 : : void* mpData;
176 : : sal_Int32 mnDataSize;
177 : : Size maImageSize;
178 : 0 : PngReplacement (void)
179 : : : mpData(NULL),
180 : : mnDataSize(0),
181 : 0 : maImageSize(0,0)
182 : 0 : {}
183 : 0 : virtual ~PngReplacement (void)
184 : 0 : {
185 [ # # ]: 0 : delete [] (char*)mpData;
186 [ # # ]: 0 : }
187 : 0 : virtual sal_Int32 GetMemorySize (void) const
188 : : {
189 : 0 : return mnDataSize;
190 : : }
191 : : };
192 : :
193 : :
194 : :
195 : :
196 : 0 : ::boost::shared_ptr<BitmapReplacement> PngCompression::Compress (const Bitmap& rBitmap) const
197 : : {
198 [ # # ][ # # ]: 0 : ::vcl::PNGWriter aWriter (rBitmap);
[ # # ]
199 [ # # ]: 0 : SvMemoryStream aStream (32768, 32768);
200 [ # # ]: 0 : aWriter.Write(aStream);
201 : :
202 [ # # ]: 0 : PngReplacement* pResult = new PngReplacement();
203 [ # # ]: 0 : pResult->maImageSize = rBitmap.GetSizePixel();
204 : 0 : pResult->mnDataSize = aStream.Tell();
205 [ # # ]: 0 : pResult->mpData = new char[pResult->mnDataSize];
206 [ # # ]: 0 : memcpy(pResult->mpData, aStream.GetData(), pResult->mnDataSize);
207 : :
208 [ # # ][ # # ]: 0 : return ::boost::shared_ptr<BitmapReplacement>(pResult);
[ # # ]
209 : : }
210 : :
211 : :
212 : :
213 : :
214 : 0 : Bitmap PngCompression::Decompress (
215 : : const BitmapReplacement& rBitmapData) const
216 : : {
217 : 0 : Bitmap aResult;
218 [ # # ]: 0 : const PngReplacement* pData (dynamic_cast<const PngReplacement*>(&rBitmapData));
219 [ # # ]: 0 : if (pData != NULL)
220 : : {
221 [ # # ]: 0 : SvMemoryStream aStream (pData->mpData, pData->mnDataSize, STREAM_READ);
222 [ # # ]: 0 : ::vcl::PNGReader aReader (aStream);
223 [ # # ][ # # ]: 0 : aResult = aReader.Read().GetBitmap();
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ]
224 : : }
225 : :
226 : 0 : return aResult;
227 : : }
228 : :
229 : :
230 : :
231 : :
232 : 0 : bool PngCompression::IsLossless (void) const
233 : : {
234 : 0 : return true;
235 : : }
236 : :
237 : :
238 : :
239 : :
240 : : } } } // end of namespace ::sd::slidesorter::cache
241 : :
242 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|